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, |
| 57 | ExpectedObjectiveCInterface |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
| 61 | // Helper functions |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 64 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 65 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 66 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 67 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 68 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 69 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 70 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 71 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 72 | Ty = decl->getUnderlyingType(); |
| 73 | else |
| 74 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 76 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 77 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 78 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 79 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 80 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 81 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 84 | // FIXME: We should provide an abstraction around a method or function |
| 85 | // to provide the following bits of information. |
| 86 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 87 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 88 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 89 | static bool isFunction(const Decl *D) { |
| 90 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 94 | /// type (function or function-typed variable) or an Objective-C |
| 95 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 96 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 97 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 100 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 101 | /// type (function or function-typed variable) or an Objective-C |
| 102 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 103 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 104 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 105 | return true; |
| 106 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 107 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 108 | QualType Ty = V->getType(); |
| 109 | return Ty->isBlockPointerType(); |
| 110 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 111 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 112 | } |
| 113 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 114 | /// Return true if the given decl has a declarator that should have |
| 115 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 116 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 117 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 118 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 119 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 122 | /// hasFunctionProto - Return true if the given decl has a argument |
| 123 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 124 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 125 | static bool hasFunctionProto(const Decl *D) { |
| 126 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 127 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 128 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 129 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 135 | /// arguments. It is an error to call this on a K&R function (use |
| 136 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 138 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 139 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 140 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 141 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 142 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 145 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 146 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 147 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 148 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 149 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 150 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 151 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 154 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 155 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 156 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 157 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 160 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 161 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 162 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 163 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 164 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 165 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 166 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 167 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 171 | static bool isInstanceMethod(const Decl *D) { |
| 172 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 173 | return MethodDecl->isInstance(); |
| 174 | return false; |
| 175 | } |
| 176 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 177 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 178 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 179 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 180 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 181 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 182 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 183 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 184 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 185 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 186 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 188 | // FIXME: Should we walk the chain of classes? |
| 189 | return ClsName == &Ctx.Idents.get("NSString") || |
| 190 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 191 | } |
| 192 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 193 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 194 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 195 | if (!PT) |
| 196 | return false; |
| 197 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 198 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 199 | if (!RT) |
| 200 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 201 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 202 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 203 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 204 | return false; |
| 205 | |
| 206 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 207 | } |
| 208 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 209 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 210 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 211 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 212 | unsigned int Num) { |
| 213 | if (Attr.getNumArgs() != Num) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 214 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 215 | << Attr.getName() << Num; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 216 | return false; |
| 217 | } |
| 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 222 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 223 | /// \brief Check if the attribute has at least as many args as Num. May |
| 224 | /// output an error. |
| 225 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 226 | unsigned int Num) { |
| 227 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 228 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 229 | return false; |
| 230 | } |
| 231 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 235 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 236 | /// instance method D. May output an error. |
| 237 | /// |
| 238 | /// \returns true if IdxExpr is a valid index. |
| 239 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 240 | StringRef AttrName, |
| 241 | SourceLocation AttrLoc, |
| 242 | unsigned AttrArgNum, |
| 243 | const Expr *IdxExpr, |
| 244 | uint64_t &Idx) |
| 245 | { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 246 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 247 | |
| 248 | // In C++ the implicit 'this' function parameter also counts. |
| 249 | // Parameters are counted from one. |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 250 | bool HP = hasFunctionProto(D); |
| 251 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 252 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 253 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 254 | HasImplicitThisParam; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 255 | |
| 256 | llvm::APSInt IdxInt; |
| 257 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 258 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 259 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 260 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 261 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | |
| 265 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 266 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 267 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 268 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 269 | return false; |
| 270 | } |
| 271 | Idx--; // Convert to zero-based. |
| 272 | if (HasImplicitThisParam) { |
| 273 | if (Idx == 0) { |
| 274 | S.Diag(AttrLoc, |
| 275 | diag::err_attribute_invalid_implicit_this_argument) |
| 276 | << AttrName << IdxExpr->getSourceRange(); |
| 277 | return false; |
| 278 | } |
| 279 | --Idx; |
| 280 | } |
| 281 | |
| 282 | return true; |
| 283 | } |
| 284 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 285 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 286 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 287 | /// \return true if the Decl is a field or potentially shared global variable |
| 288 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 289 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 290 | if (isa<FieldDecl>(D)) |
| 291 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 292 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 293 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 294 | |
| 295 | return false; |
| 296 | } |
| 297 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 298 | /// \brief Check if the passed-in expression is of type int or bool. |
| 299 | static bool isIntOrBool(Expr *Exp) { |
| 300 | QualType QT = Exp->getType(); |
| 301 | return QT->isBooleanType() || QT->isIntegerType(); |
| 302 | } |
| 303 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 304 | |
| 305 | // Check to see if the type is a smart pointer of some kind. We assume |
| 306 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 307 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 308 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 309 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 310 | if (Res1.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 311 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 312 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 313 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 314 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 315 | if (Res2.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 316 | return false; |
| 317 | |
| 318 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 321 | /// \brief Check if passed in Decl is a pointer type. |
| 322 | /// Note that this function may produce an error message. |
| 323 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 324 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 325 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 326 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 327 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 328 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 329 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 330 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 331 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 332 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 333 | // (We don't want to force template instantiation if we can avoid it, |
| 334 | // since that would alter the order in which templates are instantiated.) |
| 335 | if (RT->isIncompleteType()) |
| 336 | return true; |
| 337 | |
| 338 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 339 | return true; |
| 340 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 341 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 342 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 343 | << Attr.getName()->getName() << QT; |
| 344 | } else { |
| 345 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 346 | << Attr.getName(); |
| 347 | } |
| 348 | return false; |
| 349 | } |
| 350 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 351 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 352 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 353 | static const RecordType *getRecordType(QualType QT) { |
| 354 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 355 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 356 | |
| 357 | // Now check if we point to record type. |
| 358 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 359 | return PT->getPointeeType()->getAs<RecordType>(); |
| 360 | |
| 361 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 362 | } |
| 363 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 364 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 365 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 366 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 367 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 368 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 369 | return true; |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 374 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 375 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 376 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 377 | QualType Ty) { |
| 378 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 379 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 380 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 381 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 382 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 383 | << Attr.getName() << Ty.getAsString(); |
| 384 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 385 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 386 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 387 | // 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] | 388 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 389 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 390 | |
| 391 | // Allow smart pointers to be used as lockable objects. |
| 392 | // FIXME -- Check the type that the smart pointer points to. |
| 393 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 394 | return; |
| 395 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 396 | // Check if the type is lockable. |
| 397 | RecordDecl *RD = RT->getDecl(); |
| 398 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 399 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 400 | |
| 401 | // Else check if any base classes are lockable. |
| 402 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 403 | CXXBasePaths BPaths(false, false); |
| 404 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 405 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 406 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 407 | |
| 408 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 409 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 412 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 413 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 414 | /// \param Sidx The attribute argument index to start checking with. |
| 415 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 416 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 417 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 418 | const AttributeList &Attr, |
| 419 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 420 | int Sidx = 0, |
| 421 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 422 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 423 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 424 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 425 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 426 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 427 | Args.push_back(ArgExp); |
| 428 | continue; |
| 429 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 430 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 431 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 432 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | f37e4f2 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 433 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 434 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 435 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 436 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 437 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 438 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 439 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 440 | // We allow constant strings to be used as a placeholder for expressions |
| 441 | // that are not valid C++ syntax, but warn that they are ignored. |
| 442 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 443 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 444 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 445 | continue; |
| 446 | } |
| 447 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 448 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 449 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 450 | // A pointer to member expression of the form &MyClass::mu is treated |
| 451 | // specially -- we need to look at the type of the member. |
| 452 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 453 | if (UOp->getOpcode() == UO_AddrOf) |
| 454 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 455 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 456 | ArgTy = DRE->getDecl()->getType(); |
| 457 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 458 | // First see if we can just cast to record type, or point to record type. |
| 459 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 460 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 461 | // Now check if we index into a record type function param. |
| 462 | if(!RT && ParamIdxOk) { |
| 463 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 464 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 465 | if(FD && IL) { |
| 466 | unsigned int NumParams = FD->getNumParams(); |
| 467 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 468 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 469 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 470 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 471 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 472 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 473 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 474 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 475 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 479 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 480 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 481 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 482 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 485 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 486 | // Attribute Implementations |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 489 | // FIXME: All this manual attribute parsing code is gross. At the |
| 490 | // least add some helper functions to check most argument patterns (# |
| 491 | // and types of args). |
| 492 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 493 | enum ThreadAttributeDeclKind { |
| 494 | ThreadExpectedFieldOrGlobalVar, |
| 495 | ThreadExpectedFunctionOrMethod, |
| 496 | ThreadExpectedClassOrStruct |
| 497 | }; |
| 498 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 499 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 500 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 501 | // D must be either a member field or global (potentially shared) variable. |
| 502 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 503 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 504 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 505 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 508 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 511 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 512 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 513 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 514 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 515 | D->addAttr(::new (S.Context) |
| 516 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 517 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 520 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 521 | const AttributeList &Attr) { |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 522 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 523 | return; |
| 524 | |
| 525 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 526 | return; |
| 527 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 528 | D->addAttr(::new (S.Context) |
| 529 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 530 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 533 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 534 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 535 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 536 | // D must be either a member field or global (potentially shared) variable. |
| 537 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 538 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 539 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 540 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 541 | } |
| 542 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 543 | SmallVector<Expr*, 1> Args; |
| 544 | // check that all arguments are lockable objects |
| 545 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 546 | unsigned Size = Args.size(); |
| 547 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 548 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 549 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 550 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 551 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 552 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 555 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 556 | Expr *Arg = 0; |
| 557 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 558 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 559 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 560 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 561 | } |
| 562 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 563 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 564 | const AttributeList &Attr) { |
| 565 | Expr *Arg = 0; |
| 566 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 567 | return; |
| 568 | |
| 569 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 570 | return; |
| 571 | |
| 572 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 573 | S.Context, Arg)); |
| 574 | } |
| 575 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 576 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 577 | const AttributeList &Attr) { |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 578 | // FIXME: Lockable structs for C code. |
David Blaikie | 88c4b5e | 2013-07-29 18:24:03 +0000 | [diff] [blame] | 579 | if (!isa<RecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 580 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 581 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 582 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 585 | return true; |
| 586 | } |
| 587 | |
| 588 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 589 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 590 | return; |
| 591 | |
| 592 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 593 | } |
| 594 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 595 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 596 | const AttributeList &Attr) { |
| 597 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 598 | return; |
| 599 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 600 | D->addAttr(::new (S.Context) |
| 601 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 602 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 605 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 606 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 607 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 608 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 609 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 610 | return; |
| 611 | } |
| 612 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 613 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 614 | S.Context)); |
| 615 | } |
| 616 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 617 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 618 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 619 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 620 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 621 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 622 | return; |
| 623 | } |
| 624 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 625 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 626 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 627 | Attr.getAttributeSpellingListIndex())); |
| 628 | } |
| 629 | |
| 630 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 631 | const AttributeList &Attr) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 632 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 633 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 634 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 639 | S.Context)); |
| 640 | } |
| 641 | |
| 642 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 643 | const AttributeList &Attr) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 644 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 645 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 646 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 651 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 654 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 655 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 656 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 657 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 658 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 659 | |
| 660 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 661 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 662 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 663 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 664 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 665 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 666 | } |
| 667 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 668 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 669 | QualType QT = VD->getType(); |
| 670 | if (!QT->isDependentType()) { |
| 671 | const RecordType *RT = getRecordType(QT); |
| 672 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 673 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 674 | << Attr.getName(); |
| 675 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 679 | // Check that all arguments are lockable objects. |
| 680 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 681 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 682 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 683 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 684 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 687 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 688 | const AttributeList &Attr) { |
| 689 | SmallVector<Expr*, 1> Args; |
| 690 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 691 | return; |
| 692 | |
| 693 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 694 | D->addAttr(::new (S.Context) |
| 695 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 696 | StartArg, Args.size(), |
| 697 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 700 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 701 | const AttributeList &Attr) { |
| 702 | SmallVector<Expr*, 1> Args; |
| 703 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 704 | return; |
| 705 | |
| 706 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 707 | D->addAttr(::new (S.Context) |
| 708 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 709 | StartArg, Args.size(), |
| 710 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 713 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 714 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 715 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 716 | // zero or more arguments ok |
| 717 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 718 | // check that the attribute is applied to a function |
| 719 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 720 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 721 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 722 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 725 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 726 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 727 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 728 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 731 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 732 | const AttributeList &Attr) { |
| 733 | SmallVector<Expr*, 1> Args; |
| 734 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 735 | return; |
| 736 | |
| 737 | unsigned Size = Args.size(); |
| 738 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 739 | D->addAttr(::new (S.Context) |
| 740 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 741 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 744 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 745 | const AttributeList &Attr) { |
| 746 | SmallVector<Expr*, 1> Args; |
| 747 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 748 | return; |
| 749 | |
| 750 | unsigned Size = Args.size(); |
| 751 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 752 | D->addAttr(::new (S.Context) |
| 753 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 754 | StartArg, Size, |
| 755 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 756 | } |
| 757 | |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 758 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 759 | const AttributeList &Attr) { |
| 760 | SmallVector<Expr*, 1> Args; |
| 761 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 762 | return; |
| 763 | |
| 764 | unsigned Size = Args.size(); |
| 765 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 766 | D->addAttr(::new (S.Context) |
| 767 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 768 | Attr.getAttributeSpellingListIndex())); |
| 769 | } |
| 770 | |
| 771 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 772 | const AttributeList &Attr) { |
| 773 | SmallVector<Expr*, 1> Args; |
| 774 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 775 | return; |
| 776 | |
| 777 | unsigned Size = Args.size(); |
| 778 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 779 | D->addAttr(::new (S.Context) |
| 780 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 781 | StartArg, Size, |
| 782 | Attr.getAttributeSpellingListIndex())); |
| 783 | } |
| 784 | |
| 785 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 786 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 787 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 788 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 789 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 790 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 791 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 792 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 793 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 794 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 795 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 798 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 799 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 800 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 801 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 805 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 806 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 807 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 810 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 811 | const AttributeList &Attr) { |
| 812 | SmallVector<Expr*, 2> Args; |
| 813 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 814 | return; |
| 815 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 816 | D->addAttr(::new (S.Context) |
| 817 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 818 | Attr.getArgAsExpr(0), |
| 819 | Args.data(), Args.size(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 820 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 823 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 824 | const AttributeList &Attr) { |
| 825 | SmallVector<Expr*, 2> Args; |
| 826 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 827 | return; |
| 828 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 829 | D->addAttr(::new (S.Context) |
| 830 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 831 | Attr.getArgAsExpr(0), |
| 832 | Args.data(), Args.size(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 833 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 836 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 837 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 838 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 839 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 840 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 841 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 842 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 843 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 844 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 845 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 848 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 849 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 850 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 851 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 852 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 853 | return true; |
| 854 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 855 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 856 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 857 | const AttributeList &Attr) { |
| 858 | SmallVector<Expr*, 1> Args; |
| 859 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 860 | return; |
| 861 | |
| 862 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 863 | D->addAttr(::new (S.Context) |
| 864 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 865 | StartArg, Args.size(), |
| 866 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 869 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 870 | const AttributeList &Attr) { |
| 871 | SmallVector<Expr*, 1> Args; |
| 872 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 873 | return; |
| 874 | |
| 875 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 876 | D->addAttr(::new (S.Context) |
| 877 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 878 | StartArg, Args.size(), |
| 879 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 882 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 883 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 884 | // zero or more arguments ok |
| 885 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 886 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 887 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 888 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 892 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 893 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 894 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 895 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 896 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 897 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 898 | D->addAttr(::new (S.Context) |
| 899 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 900 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 904 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 905 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 906 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 907 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 908 | return; |
| 909 | } |
| 910 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 911 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 912 | SmallVector<Expr*, 1> Args; |
| 913 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 914 | unsigned Size = Args.size(); |
| 915 | if (Size == 0) |
| 916 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 917 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 918 | D->addAttr(::new (S.Context) |
| 919 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 920 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 924 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 925 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 926 | return; |
| 927 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 928 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 929 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 930 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 931 | return; |
| 932 | } |
| 933 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 934 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 935 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 936 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 937 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 938 | if (Size == 0) |
| 939 | return; |
| 940 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 941 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 942 | D->addAttr(::new (S.Context) |
| 943 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 944 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 945 | } |
| 946 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 947 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 948 | ConsumableAttr::ConsumedState DefaultState; |
| 949 | |
| 950 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 951 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 952 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 953 | DefaultState)) { |
| 954 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 955 | << Attr.getName() << IL->Ident; |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 956 | return; |
| 957 | } |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 958 | } else { |
| 959 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 960 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 961 | return; |
| 962 | } |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 963 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 964 | if (!isa<CXXRecordDecl>(D)) { |
| 965 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 966 | Attr.getName() << ExpectedClass; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 967 | return; |
| 968 | } |
| 969 | |
| 970 | D->addAttr(::new (S.Context) |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 971 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 972 | Attr.getAttributeSpellingListIndex())); |
| 973 | } |
| 974 | |
| 975 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 976 | const AttributeList &Attr) { |
| 977 | ASTContext &CurrContext = S.getASTContext(); |
| 978 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 979 | |
| 980 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 981 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 982 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 983 | RD->getNameAsString(); |
| 984 | |
| 985 | return false; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | return true; |
| 990 | } |
| 991 | |
| 992 | static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 993 | if (!isa<CXXMethodDecl>(D)) { |
| 994 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 995 | Attr.getName() << ExpectedMethod; |
| 996 | return; |
| 997 | } |
| 998 | |
| 999 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1000 | return; |
| 1001 | |
| 1002 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1003 | ConsumesAttr(Attr.getRange(), S.Context, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1004 | Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D, |
| 1008 | const AttributeList &Attr) { |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1009 | if (!isa<CXXMethodDecl>(D)) { |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1010 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1011 | Attr.getName() << ExpectedMethod; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1012 | return; |
| 1013 | } |
| 1014 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1015 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1016 | return; |
| 1017 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1018 | D->addAttr(::new (S.Context) |
| 1019 | CallableWhenUnconsumedAttr(Attr.getRange(), S.Context, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1020 | Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | static void handleTestsConsumedAttr(Sema &S, Decl *D, |
| 1024 | const AttributeList &Attr) { |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1025 | if (!isa<CXXMethodDecl>(D)) { |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1026 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1027 | Attr.getName() << ExpectedMethod; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1028 | return; |
| 1029 | } |
| 1030 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1031 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1032 | return; |
| 1033 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1034 | D->addAttr(::new (S.Context) |
| 1035 | TestsConsumedAttr(Attr.getRange(), S.Context, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1036 | Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | static void handleTestsUnconsumedAttr(Sema &S, Decl *D, |
| 1040 | const AttributeList &Attr) { |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1041 | if (!isa<CXXMethodDecl>(D)) { |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1042 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1043 | Attr.getName() << ExpectedMethod; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1044 | return; |
| 1045 | } |
| 1046 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1047 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1048 | return; |
| 1049 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1050 | D->addAttr(::new (S.Context) |
| 1051 | TestsUnconsumedAttr(Attr.getRange(), S.Context, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1052 | Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1053 | } |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 1054 | |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1055 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 1056 | const AttributeList &Attr) { |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1057 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1058 | |
| 1059 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1060 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1061 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1062 | ReturnState)) { |
| 1063 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1064 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1065 | return; |
| 1066 | } |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1067 | } else { |
| 1068 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1069 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | if (!isa<FunctionDecl>(D)) { |
| 1074 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1075 | Attr.getName() << ExpectedFunction; |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | // FIXME: This check is currently being done in the analysis. It can be |
| 1080 | // enabled here only after the parser propagates attributes at |
| 1081 | // template specialization definition, not declaration. |
| 1082 | //QualType ReturnType; |
| 1083 | // |
| 1084 | //if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 1085 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1086 | // |
| 1087 | //} else { |
| 1088 | // |
| 1089 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1090 | //} |
| 1091 | // |
| 1092 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1093 | // |
| 1094 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1095 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1096 | // ReturnType.getAsString(); |
| 1097 | // return; |
| 1098 | //} |
| 1099 | |
| 1100 | D->addAttr(::new (S.Context) |
| 1101 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1102 | Attr.getAttributeSpellingListIndex())); |
| 1103 | } |
| 1104 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1105 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1106 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1107 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 1108 | if (TD == 0) { |
| 1109 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 1110 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1111 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1112 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1113 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1114 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1115 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1116 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1119 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1120 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1121 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1122 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1123 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1124 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1125 | if (!FD->getType()->isDependentType() && |
| 1126 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1127 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1128 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1129 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1130 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1131 | FD->addAttr(::new (S.Context) |
| 1132 | PackedAttr(Attr.getRange(), S.Context, |
| 1133 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1134 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1135 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1138 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1139 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1140 | RD->addAttr(::new (S.Context) |
| 1141 | MsStructAttr(Attr.getRange(), S.Context, |
| 1142 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1143 | else |
| 1144 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1145 | } |
| 1146 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1147 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1148 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1149 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1150 | if (MD->isInstanceMethod()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1151 | D->addAttr(::new (S.Context) |
| 1152 | IBActionAttr(Attr.getRange(), S.Context, |
| 1153 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1154 | return; |
| 1155 | } |
| 1156 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1157 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1160 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1161 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1162 | // variables or properties of Objective-C classes. The outlet must also |
| 1163 | // have an object reference type. |
| 1164 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1165 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1166 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1167 | << Attr.getName() << VD->getType() << 0; |
| 1168 | return false; |
| 1169 | } |
| 1170 | } |
| 1171 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1172 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1173 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1174 | << Attr.getName() << PD->getType() << 1; |
| 1175 | return false; |
| 1176 | } |
| 1177 | } |
| 1178 | else { |
| 1179 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1180 | return false; |
| 1181 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1182 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1183 | return true; |
| 1184 | } |
| 1185 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1186 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1187 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1188 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1189 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1190 | D->addAttr(::new (S.Context) |
| 1191 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1192 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1195 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1196 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1197 | |
| 1198 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1199 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1200 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1201 | << Attr.getName() << 1; |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1202 | return; |
| 1203 | } |
| 1204 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1205 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1206 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1207 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1208 | IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
| 1209 | IdentifierInfo *II; |
| 1210 | SourceLocation ILS; |
| 1211 | if (IL) { |
| 1212 | II = IL->Ident; |
| 1213 | ILS = IL->Loc; |
| 1214 | } else { |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1215 | II = &S.Context.Idents.get("NSObject"); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1216 | } |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1217 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1218 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1219 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1220 | if (!TypeRep) { |
| 1221 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1222 | return; |
| 1223 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1224 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1225 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1226 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1227 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1228 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1229 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1230 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1231 | return; |
| 1232 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1233 | D->addAttr(::new (S.Context) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1234 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1235 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1238 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1239 | if (const RecordType *UT = T->getAsUnionType()) |
| 1240 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1241 | RecordDecl *UD = UT->getDecl(); |
| 1242 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1243 | itend = UD->field_end(); it != itend; ++it) { |
| 1244 | QualType QT = it->getType(); |
| 1245 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1246 | T = QT; |
| 1247 | return; |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1253 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1254 | if (!isFunctionOrMethod(D)) { |
| 1255 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1256 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1257 | return; |
| 1258 | } |
| 1259 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1260 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1261 | return; |
| 1262 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1263 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1264 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1265 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1266 | uint64_t Idx; |
| 1267 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1268 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1269 | return; |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1270 | |
| 1271 | // check if the function argument is of an integer type |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1272 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1273 | if (!T->isIntegerType()) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 1274 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1275 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1276 | << Ex->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1277 | return; |
| 1278 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1279 | SizeArgs.push_back(Idx); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | // check if the function returns a pointer |
| 1283 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1284 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1285 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1288 | D->addAttr(::new (S.Context) |
| 1289 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1290 | SizeArgs.data(), SizeArgs.size(), |
| 1291 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1294 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1295 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1296 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1297 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1298 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1299 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1300 | return; |
| 1301 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1302 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1303 | SmallVector<unsigned, 8> NonNullArgs; |
| 1304 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1305 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1306 | uint64_t Idx; |
| 1307 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1308 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1309 | return; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1312 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1313 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1314 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1315 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1316 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1317 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1318 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1319 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1320 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1321 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1322 | NonNullArgs.push_back(Idx); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1323 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1324 | |
| 1325 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1326 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1327 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1328 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1329 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1330 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1331 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1332 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1333 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1334 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1335 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1336 | if (NonNullArgs.empty()) { |
| 1337 | // Warn the trivial case only if attribute is not coming from a |
| 1338 | // macro instantiation. |
| 1339 | if (Attr.getLoc().isFileID()) |
| 1340 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1341 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1342 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1343 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1344 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1345 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1346 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1347 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1348 | D->addAttr(::new (S.Context) |
| 1349 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1350 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1353 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1354 | // This attribute must be applied to a function declaration. |
| 1355 | // The first argument to the attribute must be a string, |
| 1356 | // the name of the resource, for example "malloc". |
| 1357 | // The following arguments must be argument indexes, the arguments must be |
| 1358 | // of integer type for Returns, otherwise of pointer type. |
| 1359 | // The difference between Holds and Takes is that a pointer may still be used |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1360 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1361 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1362 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1363 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1364 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1365 | << AL.getName()->getName() << 1 << AANT_ArgumentString; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1366 | return; |
| 1367 | } |
| 1368 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1369 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1370 | switch (AL.getKind()) { |
| 1371 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1372 | K = OwnershipAttr::Takes; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1373 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1374 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1375 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1376 | return; |
| 1377 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1380 | K = OwnershipAttr::Holds; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1381 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1382 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1383 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1384 | return; |
| 1385 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1386 | break; |
| 1387 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1388 | K = OwnershipAttr::Returns; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1389 | if (AL.getNumArgs() > 2) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1390 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1391 | << AL.getName() << AL.getNumArgs() + 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1392 | return; |
| 1393 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1394 | break; |
| 1395 | default: |
| 1396 | // This should never happen given how we are called. |
| 1397 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1400 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1401 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1402 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1403 | return; |
| 1404 | } |
| 1405 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1406 | StringRef Module = AL.getArgAsIdent(0)->Ident->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Normalize the argument, __foo__ becomes foo. |
| 1409 | if (Module.startswith("__") && Module.endswith("__")) |
| 1410 | Module = Module.substr(2, Module.size() - 4); |
| 1411 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1412 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1413 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1414 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1415 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1416 | uint64_t Idx; |
| 1417 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1418 | AL.getLoc(), i, Ex, Idx)) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1419 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1420 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1421 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1422 | case OwnershipAttr::Takes: |
| 1423 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1424 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1425 | QualType T = getFunctionOrMethodArgType(D, Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1426 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1427 | // FIXME: Should also highlight argument in decl. |
| 1428 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1429 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1430 | << "pointer" |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1431 | << Ex->getSourceRange(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1432 | continue; |
| 1433 | } |
| 1434 | break; |
| 1435 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1436 | case OwnershipAttr::Returns: { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1437 | if (AL.getNumArgs() > 2) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1438 | // Is the function argument an integer type? |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1439 | Expr *IdxExpr = AL.getArgAsExpr(1); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1440 | llvm::APSInt ArgNum(32); |
| 1441 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1442 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1443 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1444 | << "ownership_returns" << "integer" |
| 1445 | << IdxExpr->getSourceRange(); |
| 1446 | return; |
| 1447 | } |
| 1448 | } |
| 1449 | break; |
| 1450 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1451 | } // switch |
| 1452 | |
| 1453 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1454 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1455 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1456 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1457 | i != e; ++i) { |
| 1458 | if ((*i)->getOwnKind() != K) { |
| 1459 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1460 | I!=E; ++I) { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1461 | if (Idx == *I) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1462 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1463 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1468 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | unsigned* start = OwnershipArgs.data(); |
| 1472 | unsigned size = OwnershipArgs.size(); |
| 1473 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1474 | |
| 1475 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1476 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1477 | << AL.getName() << 2; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1478 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1479 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1480 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1481 | D->addAttr(::new (S.Context) |
| 1482 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1483 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1486 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1487 | // Check the attribute arguments. |
| 1488 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1489 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1490 | << Attr.getName() << 1; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1491 | return; |
| 1492 | } |
| 1493 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1494 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1495 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1496 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1497 | return; |
| 1498 | } |
| 1499 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1500 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1501 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1502 | // gcc rejects |
| 1503 | // class c { |
| 1504 | // static int a __attribute__((weakref ("v2"))); |
| 1505 | // static int b() __attribute__((weakref ("f3"))); |
| 1506 | // }; |
| 1507 | // and ignores the attributes of |
| 1508 | // void f(void) { |
| 1509 | // static int a __attribute__((weakref ("v2"))); |
| 1510 | // } |
| 1511 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1512 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1513 | if (!Ctx->isFileContext()) { |
| 1514 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1515 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1516 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | // The GCC manual says |
| 1520 | // |
| 1521 | // At present, a declaration to which `weakref' is attached can only |
| 1522 | // be `static'. |
| 1523 | // |
| 1524 | // It also says |
| 1525 | // |
| 1526 | // Without a TARGET, |
| 1527 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1528 | // equivalent to `weak'. |
| 1529 | // |
| 1530 | // gcc 4.4.1 will accept |
| 1531 | // int a7 __attribute__((weakref)); |
| 1532 | // as |
| 1533 | // int a7 __attribute__((weak)); |
| 1534 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1535 | // it if this behaviour is actually used. |
| 1536 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1537 | // GCC rejects |
| 1538 | // static ((alias ("y"), weakref)). |
| 1539 | // Should we? How to check that weakref is before or after alias? |
| 1540 | |
Aaron Ballman | be116e2 | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1541 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1542 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1543 | // StringRef parameter it was given anyway. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1544 | if (Attr.isArgExpr(0)) { |
| 1545 | Expr *Arg = Attr.getArgAsExpr(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1546 | Arg = Arg->IgnoreParenCasts(); |
| 1547 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1548 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1549 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1550 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1551 | << Attr.getName() << 1 << AANT_ArgumentString; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1552 | return; |
| 1553 | } |
| 1554 | // GCC will accept anything as the argument of weakref. Should we |
| 1555 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1556 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1557 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1560 | D->addAttr(::new (S.Context) |
| 1561 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1562 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1565 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 1566 | /// If not emit an error and return false. If the argument is an identifier it |
| 1567 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 1568 | /// literal. |
| 1569 | static bool checkStringLiteralArgument(Sema &S, StringRef &Str, |
| 1570 | const AttributeList &Attr, |
| 1571 | unsigned ArgNum, |
| 1572 | SourceLocation *ArgLocation = 0) { |
| 1573 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 1574 | if (Attr.isArgIdent(ArgNum)) { |
| 1575 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
| 1576 | S.Diag(Loc->Loc, diag::err_attribute_argument_type) |
| 1577 | << Attr.getName() << AANT_ArgumentString |
| 1578 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
| 1579 | << FixItHint::CreateInsertion(S.PP.getLocForEndOfToken(Loc->Loc), "\""); |
| 1580 | Str = Loc->Ident->getName(); |
| 1581 | if (ArgLocation) |
| 1582 | *ArgLocation = Loc->Loc; |
| 1583 | return true; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1584 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1585 | |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1586 | // Now check for an actual string literal. |
| 1587 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 1588 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 1589 | if (ArgLocation) |
| 1590 | *ArgLocation = ArgExpr->getLocStart(); |
| 1591 | |
| 1592 | if (!Literal || !Literal->isAscii()) { |
| 1593 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
| 1594 | << Attr.getName() << AANT_ArgumentString; |
| 1595 | return false; |
| 1596 | } |
| 1597 | |
| 1598 | Str = Literal->getString(); |
| 1599 | return true; |
| 1600 | } |
| 1601 | |
| 1602 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1603 | StringRef Str; |
| 1604 | if (!checkStringLiteralArgument(S, Str, Attr, 0)) |
| 1605 | return; |
| 1606 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1607 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1608 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1609 | return; |
| 1610 | } |
| 1611 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1612 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1613 | |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1614 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1615 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1618 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1619 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1620 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1621 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1622 | return; |
| 1623 | } |
| 1624 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1625 | D->addAttr(::new (S.Context) |
| 1626 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1627 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1630 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1631 | if (!isa<FunctionDecl>(D)) { |
| 1632 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1633 | << Attr.getName() << ExpectedFunction; |
| 1634 | return; |
| 1635 | } |
| 1636 | |
| 1637 | if (D->hasAttr<HotAttr>()) { |
| 1638 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1639 | << Attr.getName() << "hot"; |
| 1640 | return; |
| 1641 | } |
| 1642 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1643 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1644 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1648 | if (!isa<FunctionDecl>(D)) { |
| 1649 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1650 | << Attr.getName() << ExpectedFunction; |
| 1651 | return; |
| 1652 | } |
| 1653 | |
| 1654 | if (D->hasAttr<ColdAttr>()) { |
| 1655 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1656 | << Attr.getName() << "cold"; |
| 1657 | return; |
| 1658 | } |
| 1659 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1660 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1661 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1664 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1665 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1666 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1667 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1668 | return; |
| 1669 | } |
| 1670 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1671 | D->addAttr(::new (S.Context) |
| 1672 | NakedAttr(Attr.getRange(), S.Context, |
| 1673 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1676 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1677 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1678 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1679 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1680 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1681 | return; |
| 1682 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1683 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1684 | D->addAttr(::new (S.Context) |
| 1685 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1686 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1689 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1690 | const AttributeList &Attr) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1691 | StringRef Model; |
| 1692 | SourceLocation LiteralLoc; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1693 | // Check that it is a string. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1694 | if (!checkStringLiteralArgument(S, Model, Attr, 0, &LiteralLoc)) |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1695 | return; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1696 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1697 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1698 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1699 | << Attr.getName() << ExpectedTLSVar; |
| 1700 | return; |
| 1701 | } |
| 1702 | |
| 1703 | // Check that the value. |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1704 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1705 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1706 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1707 | return; |
| 1708 | } |
| 1709 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1710 | D->addAttr(::new (S.Context) |
| 1711 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1712 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1715 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1716 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1718 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1719 | D->addAttr(::new (S.Context) |
| 1720 | MallocAttr(Attr.getRange(), S.Context, |
| 1721 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1722 | return; |
| 1723 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1726 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1729 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1730 | D->addAttr(::new (S.Context) |
| 1731 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1732 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1735 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1736 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1737 | D->addAttr(::new (S.Context) |
| 1738 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1739 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1740 | else |
| 1741 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1742 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1745 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 3e1aca2 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1746 | if (S.LangOpts.CPlusPlus) { |
| 1747 | S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus); |
| 1748 | return; |
| 1749 | } |
| 1750 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1751 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1752 | D->addAttr(::new (S.Context) |
| 1753 | CommonAttr(Attr.getRange(), S.Context, |
| 1754 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1755 | else |
| 1756 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1757 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1760 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1761 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1762 | |
| 1763 | if (S.CheckNoReturnAttr(attr)) return; |
| 1764 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1765 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +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() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1768 | return; |
| 1769 | } |
| 1770 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1771 | D->addAttr(::new (S.Context) |
| 1772 | NoReturnAttr(attr.getRange(), S.Context, |
| 1773 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1777 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1778 | attr.setInvalid(); |
| 1779 | return true; |
| 1780 | } |
| 1781 | |
| 1782 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1785 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1786 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1787 | |
| 1788 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1789 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1790 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1791 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1792 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1793 | && !VD->getType()->isFunctionPointerType())) { |
| 1794 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1795 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1796 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1797 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1798 | return; |
| 1799 | } |
| 1800 | } |
| 1801 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1802 | D->addAttr(::new (S.Context) |
| 1803 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1804 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1807 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1808 | const AttributeList &Attr) { |
| 1809 | // C++11 [dcl.attr.noreturn]p1: |
| 1810 | // The attribute may be applied to the declarator-id in a function |
| 1811 | // declaration. |
| 1812 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1813 | if (!FD) { |
| 1814 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1815 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1816 | return; |
| 1817 | } |
| 1818 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1819 | D->addAttr(::new (S.Context) |
| 1820 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1821 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1824 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1825 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1826 | /* |
| 1827 | Returning a Vector Class in Registers |
| 1828 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1829 | According to the PPU ABI specifications, a class with a single member of |
| 1830 | vector type is returned in memory when used as the return value of a function. |
| 1831 | This results in inefficient code when implementing vector classes. To return |
| 1832 | the value in a single vector register, add the vecreturn attribute to the |
| 1833 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1834 | |
| 1835 | Example: |
| 1836 | |
| 1837 | struct Vector |
| 1838 | { |
| 1839 | __vector float xyzw; |
| 1840 | } __attribute__((vecreturn)); |
| 1841 | |
| 1842 | Vector Add(Vector lhs, Vector rhs) |
| 1843 | { |
| 1844 | Vector result; |
| 1845 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1846 | return result; // This will be returned in a register |
| 1847 | } |
| 1848 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1849 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1850 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1851 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1852 | return; |
| 1853 | } |
| 1854 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1855 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1856 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1857 | return; |
| 1858 | } |
| 1859 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1860 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1861 | int count = 0; |
| 1862 | |
| 1863 | if (!isa<CXXRecordDecl>(record)) { |
| 1864 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1865 | return; |
| 1866 | } |
| 1867 | |
| 1868 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1869 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1870 | return; |
| 1871 | } |
| 1872 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1873 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1874 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1875 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1876 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1877 | return; |
| 1878 | } |
| 1879 | count++; |
| 1880 | } |
| 1881 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1882 | D->addAttr(::new (S.Context) |
| 1883 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1884 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1887 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1888 | const AttributeList &Attr) { |
| 1889 | if (isa<ParmVarDecl>(D)) { |
| 1890 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1891 | // parameter of a function declaration or lambda. |
| 1892 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1893 | S.Diag(Attr.getLoc(), |
| 1894 | diag::err_carries_dependency_param_not_function_decl); |
| 1895 | return; |
| 1896 | } |
| 1897 | } else if (!isa<FunctionDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1898 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1899 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1900 | return; |
| 1901 | } |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1902 | |
| 1903 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1904 | Attr.getRange(), S.Context, |
| 1905 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1908 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1909 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1910 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1911 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1912 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1913 | return; |
| 1914 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1915 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1916 | D->addAttr(::new (S.Context) |
| 1917 | UnusedAttr(Attr.getRange(), S.Context, |
| 1918 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1921 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1922 | const AttributeList &Attr) { |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1923 | if (!isa<FunctionDecl>(D)) { |
| 1924 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1925 | << Attr.getName() << ExpectedFunction; |
| 1926 | return; |
| 1927 | } |
| 1928 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1929 | D->addAttr(::new (S.Context) |
| 1930 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1931 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1934 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1935 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 29535ba | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1936 | if (VD->hasLocalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1937 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1938 | return; |
| 1939 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1940 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1941 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1942 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1943 | return; |
| 1944 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1945 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1946 | D->addAttr(::new (S.Context) |
| 1947 | UsedAttr(Attr.getRange(), S.Context, |
| 1948 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1951 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1952 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1953 | if (Attr.getNumArgs() > 1) { |
| 1954 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1955 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1956 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1957 | |
| 1958 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1959 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1960 | Expr *E = Attr.getArgAsExpr(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1961 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1962 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1963 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1964 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1965 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1966 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1967 | return; |
| 1968 | } |
| 1969 | priority = Idx.getZExtValue(); |
| 1970 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1971 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1972 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1973 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1974 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1975 | return; |
| 1976 | } |
| 1977 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1978 | D->addAttr(::new (S.Context) |
| 1979 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1980 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1983 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1984 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1985 | if (Attr.getNumArgs() > 1) { |
| 1986 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1987 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1988 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1989 | |
| 1990 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1991 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1992 | Expr *E = Attr.getArgAsExpr(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1993 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1994 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1995 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1996 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1997 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1998 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1999 | return; |
| 2000 | } |
| 2001 | priority = Idx.getZExtValue(); |
| 2002 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2003 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2004 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2005 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2006 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2007 | return; |
| 2008 | } |
| 2009 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2010 | D->addAttr(::new (S.Context) |
| 2011 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2012 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2015 | template <typename AttrTy> |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2016 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2017 | const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2018 | unsigned NumArgs = Attr.getNumArgs(); |
| 2019 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2020 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2021 | return; |
| 2022 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2023 | |
| 2024 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2025 | StringRef Str; |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2026 | if (NumArgs == 1 && !checkStringLiteralArgument(S, Str, Attr, 0)) |
| 2027 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2028 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2029 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2030 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2033 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 2034 | const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2035 | D->addAttr(::new (S.Context) |
| 2036 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2037 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2040 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2041 | const AttributeList &Attr) { |
| 2042 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2043 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2044 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2045 | return; |
| 2046 | } |
| 2047 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2048 | D->addAttr(::new (S.Context) |
| 2049 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2050 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2053 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2054 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2055 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2056 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2057 | return; |
| 2058 | } |
| 2059 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2060 | D->addAttr(::new (S.Context) |
| 2061 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2062 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2065 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2066 | IdentifierInfo *Platform, |
| 2067 | VersionTuple Introduced, |
| 2068 | VersionTuple Deprecated, |
| 2069 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2070 | StringRef PlatformName |
| 2071 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2072 | if (PlatformName.empty()) |
| 2073 | PlatformName = Platform->getName(); |
| 2074 | |
| 2075 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2076 | // of these steps are needed). |
| 2077 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2078 | !(Introduced <= Deprecated)) { |
| 2079 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2080 | << 1 << PlatformName << Deprecated.getAsString() |
| 2081 | << 0 << Introduced.getAsString(); |
| 2082 | return true; |
| 2083 | } |
| 2084 | |
| 2085 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2086 | !(Introduced <= Obsoleted)) { |
| 2087 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2088 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2089 | << 0 << Introduced.getAsString(); |
| 2090 | return true; |
| 2091 | } |
| 2092 | |
| 2093 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2094 | !(Deprecated <= Obsoleted)) { |
| 2095 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2096 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2097 | << 1 << Deprecated.getAsString(); |
| 2098 | return true; |
| 2099 | } |
| 2100 | |
| 2101 | return false; |
| 2102 | } |
| 2103 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2104 | /// \brief Check whether the two versions match. |
| 2105 | /// |
| 2106 | /// If either version tuple is empty, then they are assumed to match. If |
| 2107 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2108 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2109 | bool BeforeIsOkay) { |
| 2110 | if (X.empty() || Y.empty()) |
| 2111 | return true; |
| 2112 | |
| 2113 | if (X == Y) |
| 2114 | return true; |
| 2115 | |
| 2116 | if (BeforeIsOkay && X < Y) |
| 2117 | return true; |
| 2118 | |
| 2119 | return false; |
| 2120 | } |
| 2121 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2122 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2123 | IdentifierInfo *Platform, |
| 2124 | VersionTuple Introduced, |
| 2125 | VersionTuple Deprecated, |
| 2126 | VersionTuple Obsoleted, |
| 2127 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2128 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2129 | bool Override, |
| 2130 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2131 | VersionTuple MergedIntroduced = Introduced; |
| 2132 | VersionTuple MergedDeprecated = Deprecated; |
| 2133 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2134 | bool FoundAny = false; |
| 2135 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2136 | if (D->hasAttrs()) { |
| 2137 | AttrVec &Attrs = D->getAttrs(); |
| 2138 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2139 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2140 | if (!OldAA) { |
| 2141 | ++i; |
| 2142 | continue; |
| 2143 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2144 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2145 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2146 | if (OldPlatform != Platform) { |
| 2147 | ++i; |
| 2148 | continue; |
| 2149 | } |
| 2150 | |
| 2151 | FoundAny = true; |
| 2152 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2153 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2154 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2155 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2156 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2157 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2158 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2159 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2160 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2161 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2162 | if (Override) { |
| 2163 | int Which = -1; |
| 2164 | VersionTuple FirstVersion; |
| 2165 | VersionTuple SecondVersion; |
| 2166 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2167 | Which = 0; |
| 2168 | FirstVersion = OldIntroduced; |
| 2169 | SecondVersion = Introduced; |
| 2170 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2171 | Which = 1; |
| 2172 | FirstVersion = Deprecated; |
| 2173 | SecondVersion = OldDeprecated; |
| 2174 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2175 | Which = 2; |
| 2176 | FirstVersion = Obsoleted; |
| 2177 | SecondVersion = OldObsoleted; |
| 2178 | } |
| 2179 | |
| 2180 | if (Which == -1) { |
| 2181 | Diag(OldAA->getLocation(), |
| 2182 | diag::warn_mismatched_availability_override_unavail) |
| 2183 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2184 | } else { |
| 2185 | Diag(OldAA->getLocation(), |
| 2186 | diag::warn_mismatched_availability_override) |
| 2187 | << Which |
| 2188 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2189 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2190 | } |
| 2191 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2192 | } else { |
| 2193 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2194 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2195 | } |
| 2196 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2197 | Attrs.erase(Attrs.begin() + i); |
| 2198 | --e; |
| 2199 | continue; |
| 2200 | } |
| 2201 | |
| 2202 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2203 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2204 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2205 | |
| 2206 | if (MergedIntroduced2.empty()) |
| 2207 | MergedIntroduced2 = OldIntroduced; |
| 2208 | if (MergedDeprecated2.empty()) |
| 2209 | MergedDeprecated2 = OldDeprecated; |
| 2210 | if (MergedObsoleted2.empty()) |
| 2211 | MergedObsoleted2 = OldObsoleted; |
| 2212 | |
| 2213 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2214 | MergedIntroduced2, MergedDeprecated2, |
| 2215 | MergedObsoleted2)) { |
| 2216 | Attrs.erase(Attrs.begin() + i); |
| 2217 | --e; |
| 2218 | continue; |
| 2219 | } |
| 2220 | |
| 2221 | MergedIntroduced = MergedIntroduced2; |
| 2222 | MergedDeprecated = MergedDeprecated2; |
| 2223 | MergedObsoleted = MergedObsoleted2; |
| 2224 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2225 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | if (FoundAny && |
| 2229 | MergedIntroduced == Introduced && |
| 2230 | MergedDeprecated == Deprecated && |
| 2231 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2232 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2233 | |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2234 | // Only create a new attribute if !Override, but we want to do |
| 2235 | // the checking. |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2236 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2237 | MergedDeprecated, MergedObsoleted) && |
| 2238 | !Override) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2239 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2240 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2241 | Obsoleted, IsUnavailable, Message, |
| 2242 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2243 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2244 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2247 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2248 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2249 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2250 | return; |
| 2251 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2252 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2253 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2254 | IdentifierInfo *II = Platform->Ident; |
| 2255 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2256 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2257 | << Platform->Ident; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2258 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2259 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2260 | if (!ND) { |
| 2261 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2262 | return; |
| 2263 | } |
| 2264 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2265 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2266 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2267 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2268 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2269 | StringRef Str; |
Benjamin Kramer | c561714 | 2013-09-13 17:31:48 +0000 | [diff] [blame^] | 2270 | if (const StringLiteral *SE = |
| 2271 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2272 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2273 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2274 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2275 | Introduced.Version, |
| 2276 | Deprecated.Version, |
| 2277 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2278 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2279 | /*Override=*/false, |
| 2280 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2281 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2282 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2285 | template <class T> |
| 2286 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2287 | typename T::VisibilityType value, |
| 2288 | unsigned attrSpellingListIndex) { |
| 2289 | T *existingAttr = D->getAttr<T>(); |
| 2290 | if (existingAttr) { |
| 2291 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2292 | if (existingValue == value) |
| 2293 | return NULL; |
| 2294 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2295 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2296 | D->dropAttr<T>(); |
| 2297 | } |
| 2298 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2299 | } |
| 2300 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2301 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2302 | VisibilityAttr::VisibilityType Vis, |
| 2303 | unsigned AttrSpellingListIndex) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2304 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2305 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2308 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2309 | TypeVisibilityAttr::VisibilityType Vis, |
| 2310 | unsigned AttrSpellingListIndex) { |
| 2311 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2312 | AttrSpellingListIndex); |
| 2313 | } |
| 2314 | |
| 2315 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2316 | bool isTypeVisibility) { |
| 2317 | // Visibility attributes don't mean anything on a typedef. |
| 2318 | if (isa<TypedefNameDecl>(D)) { |
| 2319 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2320 | << Attr.getName(); |
| 2321 | return; |
| 2322 | } |
| 2323 | |
| 2324 | // 'type_visibility' can only go on a type or namespace. |
| 2325 | if (isTypeVisibility && |
| 2326 | !(isa<TagDecl>(D) || |
| 2327 | isa<ObjCInterfaceDecl>(D) || |
| 2328 | isa<NamespaceDecl>(D))) { |
| 2329 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2330 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2331 | return; |
| 2332 | } |
| 2333 | |
Benjamin Kramer | ae3a83f | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2334 | // Check that the argument is a string literal. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2335 | StringRef TypeStr; |
| 2336 | SourceLocation LiteralLoc; |
| 2337 | if (!checkStringLiteralArgument(S, TypeStr, Attr, 0, &LiteralLoc)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2338 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2339 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2340 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2341 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2342 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2343 | << Attr.getName() << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2344 | return; |
| 2345 | } |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2346 | |
| 2347 | // Complain about attempts to use protected visibility on targets |
| 2348 | // (like Darwin) that don't support it. |
| 2349 | if (type == VisibilityAttr::Protected && |
| 2350 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2351 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2352 | type = VisibilityAttr::Default; |
| 2353 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2354 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2355 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2356 | clang::Attr *newAttr; |
| 2357 | if (isTypeVisibility) { |
| 2358 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2359 | (TypeVisibilityAttr::VisibilityType) type, |
| 2360 | Index); |
| 2361 | } else { |
| 2362 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2363 | } |
| 2364 | if (newAttr) |
| 2365 | D->addAttr(newAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2368 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2369 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2370 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2371 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2372 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2373 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2374 | return; |
| 2375 | } |
| 2376 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2377 | if (!Attr.isArgIdent(0)) { |
| 2378 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2379 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2380 | return; |
| 2381 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2382 | |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2383 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2384 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2385 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2386 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2387 | << IL->Ident; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2388 | return; |
| 2389 | } |
| 2390 | |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2391 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2392 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2393 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2394 | << method->getResultType(); |
| 2395 | // Ignore the attribute. |
| 2396 | return; |
| 2397 | } |
| 2398 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2399 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2400 | S.Context, F)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2403 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2404 | const AttributeList &Attr) { |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2405 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2406 | if (OCI == 0) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2407 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2408 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2409 | return; |
| 2410 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2411 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2412 | D->addAttr(::new (S.Context) |
| 2413 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2414 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2417 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2418 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2419 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2420 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2421 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2422 | return; |
| 2423 | } |
| 2424 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2425 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2426 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2427 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2428 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2429 | return; |
| 2430 | } |
| 2431 | } |
| 2432 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2433 | // It is okay to include this attribute on properties, e.g.: |
| 2434 | // |
| 2435 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2436 | // |
| 2437 | // In this case it follows tradition and suppresses an error in the above |
| 2438 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2439 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2440 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2441 | D->addAttr(::new (S.Context) |
| 2442 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2443 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2446 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2447 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2448 | if (!isa<FunctionDecl>(D)) { |
| 2449 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2450 | return; |
| 2451 | } |
| 2452 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2453 | D->addAttr(::new (S.Context) |
| 2454 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2455 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2458 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2459 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2460 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2461 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2462 | return; |
| 2463 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2464 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2465 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2466 | BlocksAttr::BlockType type; |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2467 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2468 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2469 | << Attr.getName() << II; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2470 | return; |
| 2471 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2472 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2473 | D->addAttr(::new (S.Context) |
| 2474 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2475 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2478 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2479 | // check the attribute arguments. |
| 2480 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2481 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2482 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2485 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2486 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2487 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2488 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2489 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2490 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2491 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2492 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2493 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2494 | return; |
| 2495 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2496 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2497 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2498 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2499 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2500 | return; |
| 2501 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2502 | |
| 2503 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2506 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2507 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2508 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2509 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2510 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2511 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2512 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2513 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2514 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2515 | return; |
| 2516 | } |
| 2517 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2518 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2519 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2520 | // FIXME: This error message could be improved, it would be nice |
| 2521 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2522 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2523 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2524 | return; |
| 2525 | } |
| 2526 | } |
| 2527 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2528 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2529 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2530 | if (isa<FunctionNoProtoType>(FT)) { |
| 2531 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2532 | return; |
| 2533 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2534 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2535 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2536 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2537 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2538 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2539 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2540 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2541 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2542 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2543 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2544 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2545 | if (!BD->isVariadic()) { |
| 2546 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2547 | return; |
| 2548 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2549 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2550 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2551 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2552 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2553 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2554 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2555 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2556 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2557 | return; |
| 2558 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2559 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2560 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2561 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2562 | return; |
| 2563 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2564 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2565 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2566 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2567 | return; |
| 2568 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2569 | D->addAttr(::new (S.Context) |
| 2570 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2571 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2574 | static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2575 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 2576 | RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); |
| 2577 | else |
| 2578 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2579 | } |
| 2580 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2581 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2582 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2583 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2584 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2585 | return; |
| 2586 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2587 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2588 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2589 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2590 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2591 | return; |
| 2592 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2593 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2594 | if (MD->getResultType()->isVoidType()) { |
| 2595 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2596 | << Attr.getName() << 1; |
| 2597 | return; |
| 2598 | } |
| 2599 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2600 | D->addAttr(::new (S.Context) |
| 2601 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2602 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2605 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2606 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2607 | if (isa<CXXRecordDecl>(D)) { |
| 2608 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2609 | return; |
| 2610 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2611 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2612 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2613 | return; |
| 2614 | } |
| 2615 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2616 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2617 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2618 | nd->addAttr(::new (S.Context) |
| 2619 | WeakAttr(Attr.getRange(), S.Context, |
| 2620 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2623 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2624 | // weak_import only applies to variable & function declarations. |
| 2625 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2626 | if (!D->canBeWeakImported(isDef)) { |
| 2627 | if (isDef) |
Reid Kleckner | beba3e8 | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2628 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2629 | << "weak_import"; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2630 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2631 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2632 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2633 | // Nothing to warn about here. |
| 2634 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2635 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2636 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2637 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2638 | return; |
| 2639 | } |
| 2640 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2641 | D->addAttr(::new (S.Context) |
| 2642 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2643 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2646 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2647 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2648 | const AttributeList &Attr) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2649 | unsigned WGSize[3]; |
| 2650 | for (unsigned i = 0; i < 3; ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2651 | Expr *E = Attr.getArgAsExpr(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2652 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2653 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2654 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 2655 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2656 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 2657 | << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2658 | return; |
| 2659 | } |
| 2660 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2661 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2662 | |
| 2663 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2664 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2665 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2666 | if (!(A->getXDim() == WGSize[0] && |
| 2667 | A->getYDim() == WGSize[1] && |
| 2668 | A->getZDim() == WGSize[2])) { |
| 2669 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2670 | Attr.getName(); |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2675 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2676 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2677 | if (!(A->getXDim() == WGSize[0] && |
| 2678 | A->getYDim() == WGSize[1] && |
| 2679 | A->getZDim() == WGSize[2])) { |
| 2680 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2681 | Attr.getName(); |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2686 | D->addAttr(::new (S.Context) |
| 2687 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2688 | WGSize[0], WGSize[1], WGSize[2], |
| 2689 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2690 | else |
| 2691 | D->addAttr(::new (S.Context) |
| 2692 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2693 | WGSize[0], WGSize[1], WGSize[2], |
| 2694 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2697 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2698 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2699 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2700 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2701 | return; |
| 2702 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2703 | if (!Attr.hasParsedType()) { |
| 2704 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2705 | << Attr.getName() << 1; |
| 2706 | return; |
| 2707 | } |
| 2708 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2709 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg()); |
| 2710 | |
| 2711 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2712 | (ParmType->isBooleanType() || |
| 2713 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2714 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2715 | << ParmType; |
| 2716 | return; |
| 2717 | } |
| 2718 | |
| 2719 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2720 | D->hasAttr<VecTypeHintAttr>()) { |
| 2721 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
| 2722 | if (A->getTypeHint() != ParmType) { |
| 2723 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2724 | return; |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
| 2729 | ParmType, Attr.getLoc())); |
| 2730 | } |
| 2731 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2732 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2733 | StringRef Name, |
| 2734 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2735 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2736 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2737 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2738 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2739 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2740 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2741 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2742 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2743 | AttrSpellingListIndex); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2746 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2747 | // Make sure that there is a string literal as the sections's single |
| 2748 | // argument. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2749 | StringRef Str; |
| 2750 | SourceLocation LiteralLoc; |
| 2751 | if (!checkStringLiteralArgument(S, Str, Attr, 0, &LiteralLoc)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2752 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2753 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2754 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2755 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2756 | if (!Error.empty()) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2757 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2758 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2759 | return; |
| 2760 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2762 | // This attribute cannot be applied to local variables. |
| 2763 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2764 | S.Diag(LiteralLoc, diag::err_attribute_section_local_variable); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2765 | return; |
| 2766 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2767 | |
| 2768 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2769 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2770 | if (NewAttr) |
| 2771 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2774 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2775 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2776 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2777 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2778 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2779 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2780 | D->addAttr(::new (S.Context) |
| 2781 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2782 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2783 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2786 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2787 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2788 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2789 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2790 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2791 | D->addAttr(::new (S.Context) |
| 2792 | ConstAttr(Attr.getRange(), S.Context, |
| 2793 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2794 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2797 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2798 | D->addAttr(::new (S.Context) |
| 2799 | PureAttr(Attr.getRange(), S.Context, |
| 2800 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2803 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2804 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2805 | if (!VD || !VD->hasLocalStorage()) { |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2806 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2807 | return; |
| 2808 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2809 | |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2810 | Expr *E = Attr.getArgAsExpr(0); |
| 2811 | SourceLocation Loc = E->getExprLoc(); |
| 2812 | FunctionDecl *FD = 0; |
| 2813 | DeclarationNameInfo NI; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2814 | |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2815 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2816 | // will warn the user. |
| 2817 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2818 | if (DRE->hasQualifier()) |
| 2819 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2820 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2821 | NI = DRE->getNameInfo(); |
| 2822 | if (!FD) { |
| 2823 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2824 | << NI.getName(); |
| 2825 | return; |
| 2826 | } |
| 2827 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2828 | if (ULE->hasExplicitTemplateArgs()) |
| 2829 | S.Diag(Loc, diag::warn_cleanup_ext); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2830 | |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2831 | // This will diagnose the case where the function cannot be found. |
| 2832 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2833 | NI = ULE->getNameInfo(); |
| 2834 | } else { |
| 2835 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2836 | return; |
| 2837 | } |
| 2838 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2839 | if (FD->getNumParams() != 1) { |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2840 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2841 | << NI.getName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2842 | return; |
| 2843 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2844 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2845 | // We're currently more strict than GCC about what function types we accept. |
| 2846 | // If this ever proves to be a problem it should be easy to fix. |
| 2847 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2848 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2849 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2850 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2851 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2852 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2853 | return; |
| 2854 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2855 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2856 | D->addAttr(::new (S.Context) |
| 2857 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2858 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2861 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2862 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2863 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2864 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2865 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2866 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2867 | return; |
| 2868 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2869 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2870 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2871 | uint64_t ArgIdx; |
| 2872 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2873 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2874 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2875 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2876 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2877 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2878 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2879 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2880 | if (not_nsstring_type && |
| 2881 | !isCFStringType(Ty, S.Context) && |
| 2882 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2883 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2884 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2885 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2886 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2887 | << IdxExpr->getSourceRange(); |
| 2888 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2889 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2890 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2891 | if (!isNSStringType(Ty, S.Context) && |
| 2892 | !isCFStringType(Ty, S.Context) && |
| 2893 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2894 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2895 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2896 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2897 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2898 | << IdxExpr->getSourceRange(); |
| 2899 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2902 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2903 | // because that has corrected for the implicit this parameter, and is zero- |
| 2904 | // based. The attribute expects what the user wrote explicitly. |
| 2905 | llvm::APSInt Val; |
| 2906 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2907 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2908 | D->addAttr(::new (S.Context) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2909 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2910 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2913 | enum FormatAttrKind { |
| 2914 | CFStringFormat, |
| 2915 | NSStringFormat, |
| 2916 | StrftimeFormat, |
| 2917 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2918 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2919 | InvalidFormat |
| 2920 | }; |
| 2921 | |
| 2922 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2923 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2924 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2925 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2926 | // Check for formats that get handled specially. |
| 2927 | .Case("NSString", NSStringFormat) |
| 2928 | .Case("CFString", CFStringFormat) |
| 2929 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2930 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2931 | // Otherwise, check for supported formats. |
| 2932 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2933 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2934 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2935 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2936 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2937 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2940 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2941 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2942 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2943 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2944 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2945 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2946 | return; |
| 2947 | } |
| 2948 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2949 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2950 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2951 | Attr.setInvalid(); |
| 2952 | return; |
| 2953 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2954 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2955 | if (S.Context.getAsArrayType(T)) |
| 2956 | T = S.Context.getBaseElementType(T); |
| 2957 | if (!T->getAs<RecordType>()) { |
| 2958 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2959 | Attr.setInvalid(); |
| 2960 | return; |
| 2961 | } |
| 2962 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2963 | Expr *priorityExpr = Attr.getArgAsExpr(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2964 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2965 | llvm::APSInt priority(32); |
| 2966 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 2967 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 2968 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2969 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 2970 | << priorityExpr->getSourceRange(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2971 | Attr.setInvalid(); |
| 2972 | return; |
| 2973 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 2974 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2975 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2976 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 2977 | << priorityExpr->getSourceRange(); |
| 2978 | Attr.setInvalid(); |
| 2979 | return; |
| 2980 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2981 | D->addAttr(::new (S.Context) |
| 2982 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2983 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2986 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2987 | IdentifierInfo *Format, int FormatIdx, |
| 2988 | int FirstArg, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2989 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2990 | // Check whether we already have an equivalent format attribute. |
| 2991 | for (specific_attr_iterator<FormatAttr> |
| 2992 | i = D->specific_attr_begin<FormatAttr>(), |
| 2993 | e = D->specific_attr_end<FormatAttr>(); |
| 2994 | i != e ; ++i) { |
| 2995 | FormatAttr *f = *i; |
| 2996 | if (f->getType() == Format && |
| 2997 | f->getFormatIdx() == FormatIdx && |
| 2998 | f->getFirstArg() == FirstArg) { |
| 2999 | // If we don't have a valid location for this attribute, adopt the |
| 3000 | // location. |
| 3001 | if (f->getLocation().isInvalid()) |
| 3002 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3003 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3004 | } |
| 3005 | } |
| 3006 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3007 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3008 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3011 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3012 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3013 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3014 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3015 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3016 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3017 | return; |
| 3018 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3019 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3020 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3021 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3022 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3023 | return; |
| 3024 | } |
| 3025 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3026 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3027 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3028 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3029 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3030 | unsigned FirstIdx = 1; |
| 3031 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3032 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3033 | StringRef Format = II->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3034 | |
| 3035 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3036 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3037 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3038 | // If we've modified the string name, we need a new identifier for it. |
| 3039 | II = &S.Context.Idents.get(Format); |
| 3040 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3041 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3042 | // Check for supported formats. |
| 3043 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3044 | |
| 3045 | if (Kind == IgnoredFormat) |
| 3046 | return; |
| 3047 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3048 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3049 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3050 | << "format" << II->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3051 | return; |
| 3052 | } |
| 3053 | |
| 3054 | // checks for the 2nd argument |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3055 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3056 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3057 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3058 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3059 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3060 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3061 | << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3062 | return; |
| 3063 | } |
| 3064 | |
| 3065 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3066 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3067 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3068 | return; |
| 3069 | } |
| 3070 | |
| 3071 | // FIXME: Do we need to bounds check? |
| 3072 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3073 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3074 | if (HasImplicitThisParam) { |
| 3075 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3076 | S.Diag(Attr.getLoc(), |
| 3077 | diag::err_format_attribute_implicit_this_format_string) |
| 3078 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3079 | return; |
| 3080 | } |
| 3081 | ArgIdx--; |
| 3082 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3084 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3085 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3086 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3087 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3088 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3089 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3090 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3091 | return; |
| 3092 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3093 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3094 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3095 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3096 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3097 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3098 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3099 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3100 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3101 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3102 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3103 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3104 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3105 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3106 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3107 | return; |
| 3108 | } |
| 3109 | |
| 3110 | // check the 3rd argument |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3111 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3112 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3113 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3114 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3115 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3116 | << Attr.getName() << 3 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3117 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3118 | return; |
| 3119 | } |
| 3120 | |
| 3121 | // check if the function is variadic if the 3rd argument non-zero |
| 3122 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3123 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3124 | ++NumArgs; // +1 for ... |
| 3125 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3126 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3127 | return; |
| 3128 | } |
| 3129 | } |
| 3130 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3131 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3132 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3133 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3134 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3135 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3136 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3137 | return; |
| 3138 | } |
| 3139 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3140 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3141 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3142 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3143 | return; |
| 3144 | } |
| 3145 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3146 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3147 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3148 | FirstArg.getZExtValue(), |
| 3149 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3150 | if (NewAttr) |
| 3151 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3154 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3155 | const AttributeList &Attr) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3156 | // Try to find the underlying union declaration. |
| 3157 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3158 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3159 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3160 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3161 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3162 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3163 | |
| 3164 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3165 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3166 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3167 | return; |
| 3168 | } |
| 3169 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3170 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3171 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3172 | diag::warn_transparent_union_attribute_not_definition); |
| 3173 | return; |
| 3174 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3175 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3176 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3177 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3178 | if (Field == FieldEnd) { |
| 3179 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3180 | return; |
| 3181 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3182 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3183 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3184 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3185 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3186 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3187 | diag::warn_transparent_union_attribute_floating) |
| 3188 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3189 | return; |
| 3190 | } |
| 3191 | |
| 3192 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3193 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3194 | for (; Field != FieldEnd; ++Field) { |
| 3195 | QualType FieldType = Field->getType(); |
| 3196 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3197 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3198 | // Warn if we drop the attribute. |
| 3199 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3200 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3201 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3202 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3203 | diag::warn_transparent_union_attribute_field_size_align) |
| 3204 | << isSize << Field->getDeclName() << FieldBits; |
| 3205 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3206 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3207 | diag::note_transparent_union_first_field_size_align) |
| 3208 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3209 | return; |
| 3210 | } |
| 3211 | } |
| 3212 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3213 | RD->addAttr(::new (S.Context) |
| 3214 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3215 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3218 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3219 | // Make sure that there is a string literal as the annotation's single |
| 3220 | // argument. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3221 | StringRef Str; |
| 3222 | if (!checkStringLiteralArgument(S, Str, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3223 | return; |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3224 | |
| 3225 | // Don't duplicate annotations that are already set. |
| 3226 | for (specific_attr_iterator<AnnotateAttr> |
| 3227 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3228 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3229 | if ((*i)->getAnnotation() == Str) |
| 3230 | return; |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3231 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3232 | |
| 3233 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3234 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3235 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3238 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3239 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3240 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3241 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3242 | << Attr.getName() << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3243 | return; |
| 3244 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3245 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3246 | if (Attr.getNumArgs() == 0) { |
| 3247 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3248 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3249 | return; |
| 3250 | } |
| 3251 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3252 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3253 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3254 | S.Diag(Attr.getEllipsisLoc(), |
| 3255 | diag::err_pack_expansion_without_parameter_packs); |
| 3256 | return; |
| 3257 | } |
| 3258 | |
| 3259 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3260 | return; |
| 3261 | |
| 3262 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3263 | Attr.isPackExpansion()); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3267 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3268 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3269 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3270 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3271 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3272 | if (TmpAttr.isAlignas()) { |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3273 | // C++11 [dcl.align]p1: |
| 3274 | // An alignment-specifier may be applied to a variable or to a class |
| 3275 | // data member, but it shall not be applied to a bit-field, a function |
| 3276 | // parameter, the formal parameter of a catch clause, or a variable |
| 3277 | // declared with the register storage class specifier. An |
| 3278 | // alignment-specifier may also be applied to the declaration of a class |
| 3279 | // or enumeration type. |
| 3280 | // C11 6.7.5/2: |
| 3281 | // An alignment attribute shall not be specified in a declaration of |
| 3282 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3283 | // object declared with the register storage-class specifier. |
| 3284 | int DiagKind = -1; |
| 3285 | if (isa<ParmVarDecl>(D)) { |
| 3286 | DiagKind = 0; |
| 3287 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3288 | if (VD->getStorageClass() == SC_Register) |
| 3289 | DiagKind = 1; |
| 3290 | if (VD->isExceptionVariable()) |
| 3291 | DiagKind = 2; |
| 3292 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3293 | if (FD->isBitField()) |
| 3294 | DiagKind = 3; |
| 3295 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3296 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3297 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3298 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3299 | : ExpectedVariableFieldOrTag); |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3300 | return; |
| 3301 | } |
| 3302 | if (DiagKind != -1) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3303 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3304 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3305 | return; |
| 3306 | } |
| 3307 | } |
| 3308 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3309 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3310 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3311 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3312 | AA->setPackExpansion(IsPackExpansion); |
| 3313 | D->addAttr(AA); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3314 | return; |
| 3315 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3316 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3317 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3318 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3319 | ExprResult ICE |
| 3320 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3321 | diag::err_aligned_attribute_argument_not_int, |
| 3322 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3323 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3324 | return; |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3325 | |
| 3326 | // C++11 [dcl.align]p2: |
| 3327 | // -- if the constant expression evaluates to zero, the alignment |
| 3328 | // specifier shall have no effect |
| 3329 | // C11 6.7.5p6: |
| 3330 | // An alignment specification of zero has no effect. |
| 3331 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3332 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3333 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3334 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3335 | return; |
| 3336 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3337 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3338 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3339 | // We've already verified it's a power of 2, now let's make sure it's |
| 3340 | // 8192 or less. |
| 3341 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3342 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3343 | << E->getSourceRange(); |
| 3344 | return; |
| 3345 | } |
| 3346 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3347 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3348 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3349 | ICE.take(), SpellingListIndex); |
| 3350 | AA->setPackExpansion(IsPackExpansion); |
| 3351 | D->addAttr(AA); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3354 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3355 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3356 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3357 | // FIXME: Perform checking of type validity |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3358 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3359 | SpellingListIndex); |
| 3360 | AA->setPackExpansion(IsPackExpansion); |
| 3361 | D->addAttr(AA); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3362 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3363 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3364 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3365 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3366 | |
| 3367 | QualType Ty; |
| 3368 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3369 | Ty = VD->getType(); |
| 3370 | else |
| 3371 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 4da0903 | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3372 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3373 | return; |
| 3374 | |
| 3375 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3376 | // The combined effect of all alignment attributes in a declaration shall |
| 3377 | // not specify an alignment that is less strict than the alignment that |
| 3378 | // would otherwise be required for the entity being declared. |
| 3379 | AlignedAttr *AlignasAttr = 0; |
| 3380 | unsigned Align = 0; |
| 3381 | for (specific_attr_iterator<AlignedAttr> |
| 3382 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3383 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3384 | if (I->isAlignmentDependent()) |
| 3385 | return; |
| 3386 | if (I->isAlignas()) |
| 3387 | AlignasAttr = *I; |
| 3388 | Align = std::max(Align, I->getAlignment(Context)); |
| 3389 | } |
| 3390 | |
| 3391 | if (AlignasAttr && Align) { |
| 3392 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3393 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3394 | if (NaturalAlign > RequestedAlign) |
| 3395 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3396 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3397 | } |
| 3398 | } |
| 3399 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3400 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3401 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3402 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3403 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3404 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3405 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3406 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3407 | // This attribute isn't documented, but glibc uses it. It changes |
| 3408 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3409 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 750f73a | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3410 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3411 | << AANT_ArgumentIdentifier; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3412 | return; |
| 3413 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3414 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3415 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3416 | StringRef Str = Name->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3417 | |
| 3418 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3419 | if (Str.startswith("__") && Str.endswith("__")) |
| 3420 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3421 | |
| 3422 | unsigned DestWidth = 0; |
| 3423 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3424 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3425 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3426 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3427 | switch (Str[0]) { |
| 3428 | case 'Q': DestWidth = 8; break; |
| 3429 | case 'H': DestWidth = 16; break; |
| 3430 | case 'S': DestWidth = 32; break; |
| 3431 | case 'D': DestWidth = 64; break; |
| 3432 | case 'X': DestWidth = 96; break; |
| 3433 | case 'T': DestWidth = 128; break; |
| 3434 | } |
| 3435 | if (Str[1] == 'F') { |
| 3436 | IntegerMode = false; |
| 3437 | } else if (Str[1] == 'C') { |
| 3438 | IntegerMode = false; |
| 3439 | ComplexMode = true; |
| 3440 | } else if (Str[1] != 'I') { |
| 3441 | DestWidth = 0; |
| 3442 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3443 | break; |
| 3444 | case 4: |
| 3445 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3446 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3447 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3448 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3449 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3450 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3451 | break; |
| 3452 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3453 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3454 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3455 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3456 | case 11: |
| 3457 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3458 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3459 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
| 3462 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3463 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3464 | OldTy = TD->getUnderlyingType(); |
| 3465 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3466 | OldTy = VD->getType(); |
| 3467 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3468 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3469 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3470 | return; |
| 3471 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3472 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3473 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3474 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3475 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3476 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3477 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3478 | } else if (ComplexMode) { |
| 3479 | if (!OldTy->isComplexType()) |
| 3480 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3481 | } else { |
| 3482 | if (!OldTy->isFloatingType()) |
| 3483 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3484 | } |
| 3485 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3486 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3487 | // and friends, at least with glibc. |
Stepan Dyatkovskiy | 8e366f0 | 2013-09-10 08:37:22 +0000 | [diff] [blame] | 3488 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3489 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3490 | // FIXME: Make sure floating-point mappings are accurate |
| 3491 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | 8e366f0 | 2013-09-10 08:37:22 +0000 | [diff] [blame] | 3492 | QualType NewTy; |
| 3493 | switch (DestWidth) { |
| 3494 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3495 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3496 | return; |
Stepan Dyatkovskiy | 8e366f0 | 2013-09-10 08:37:22 +0000 | [diff] [blame] | 3497 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3498 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3499 | return; |
Stepan Dyatkovskiy | 8e366f0 | 2013-09-10 08:37:22 +0000 | [diff] [blame] | 3500 | case 8: |
| 3501 | if (!IntegerMode) { |
| 3502 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3503 | return; |
| 3504 | } |
| 3505 | if (OldTy->isSignedIntegerType()) |
| 3506 | NewTy = S.Context.SignedCharTy; |
| 3507 | else |
| 3508 | NewTy = S.Context.UnsignedCharTy; |
| 3509 | break; |
| 3510 | case 16: |
| 3511 | if (!IntegerMode) { |
| 3512 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3513 | return; |
| 3514 | } |
| 3515 | if (OldTy->isSignedIntegerType()) |
| 3516 | NewTy = S.Context.ShortTy; |
| 3517 | else |
| 3518 | NewTy = S.Context.UnsignedShortTy; |
| 3519 | break; |
| 3520 | case 32: |
| 3521 | if (!IntegerMode) |
| 3522 | NewTy = S.Context.FloatTy; |
| 3523 | else if (OldTy->isSignedIntegerType()) |
| 3524 | NewTy = S.Context.IntTy; |
| 3525 | else |
| 3526 | NewTy = S.Context.UnsignedIntTy; |
| 3527 | break; |
| 3528 | case 64: |
| 3529 | if (!IntegerMode) |
| 3530 | NewTy = S.Context.DoubleTy; |
| 3531 | else if (OldTy->isSignedIntegerType()) |
| 3532 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
| 3533 | NewTy = S.Context.LongTy; |
| 3534 | else |
| 3535 | NewTy = S.Context.LongLongTy; |
| 3536 | else |
| 3537 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
| 3538 | NewTy = S.Context.UnsignedLongTy; |
| 3539 | else |
| 3540 | NewTy = S.Context.UnsignedLongLongTy; |
| 3541 | break; |
| 3542 | case 96: |
| 3543 | NewTy = S.Context.LongDoubleTy; |
| 3544 | break; |
| 3545 | case 128: |
| 3546 | if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() != |
| 3547 | &llvm::APFloat::PPCDoubleDouble) { |
| 3548 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3549 | return; |
| 3550 | } |
| 3551 | if (IntegerMode) { |
| 3552 | if (OldTy->isSignedIntegerType()) |
| 3553 | NewTy = S.Context.Int128Ty; |
| 3554 | else |
| 3555 | NewTy = S.Context.UnsignedInt128Ty; |
| 3556 | } else |
| 3557 | NewTy = S.Context.LongDoubleTy; |
| 3558 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3561 | if (ComplexMode) { |
| 3562 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | // Install the new type. |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3566 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3567 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3568 | else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3569 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3570 | |
| 3571 | D->addAttr(::new (S.Context) |
| 3572 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3573 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3574 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3575 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3576 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3577 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3578 | if (!VD->hasGlobalStorage()) |
| 3579 | S.Diag(Attr.getLoc(), |
| 3580 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3581 | << Attr.getName(); |
| 3582 | } else if (!isFunctionOrMethod(D)) { |
| 3583 | S.Diag(Attr.getLoc(), |
| 3584 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3585 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3586 | return; |
| 3587 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3588 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3589 | D->addAttr(::new (S.Context) |
| 3590 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3591 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3594 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3595 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3596 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3597 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3598 | return; |
| 3599 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3600 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3601 | D->addAttr(::new (S.Context) |
| 3602 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3603 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3606 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3607 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3608 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3609 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3610 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3611 | return; |
| 3612 | } |
| 3613 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3614 | D->addAttr(::new (S.Context) |
| 3615 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3616 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3619 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3620 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3621 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3622 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3623 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3624 | return; |
| 3625 | } |
| 3626 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3627 | D->addAttr(::new (S.Context) |
| 3628 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3629 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3630 | } else { |
| 3631 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3632 | } |
| 3633 | } |
| 3634 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3635 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3636 | if (S.LangOpts.CUDA) { |
| 3637 | // check the attribute arguments. |
| 3638 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3639 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3640 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3641 | return; |
| 3642 | } |
| 3643 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3644 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3645 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3646 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3647 | return; |
| 3648 | } |
| 3649 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3650 | D->addAttr(::new (S.Context) |
| 3651 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3652 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3653 | } else { |
| 3654 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3655 | } |
| 3656 | } |
| 3657 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3658 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3659 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3660 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3661 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3662 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3663 | return; |
| 3664 | } |
| 3665 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3666 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3667 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3668 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3669 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3670 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3671 | << FD->getType() |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3672 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3673 | "void"); |
| 3674 | } else { |
| 3675 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3676 | << FD->getType(); |
| 3677 | } |
| 3678 | return; |
| 3679 | } |
| 3680 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3681 | D->addAttr(::new (S.Context) |
| 3682 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3683 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3684 | } else { |
| 3685 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3686 | } |
| 3687 | } |
| 3688 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3689 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3690 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3691 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3692 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3693 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3694 | return; |
| 3695 | } |
| 3696 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3697 | D->addAttr(::new (S.Context) |
| 3698 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3699 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3700 | } else { |
| 3701 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3702 | } |
| 3703 | } |
| 3704 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3705 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3706 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3707 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3708 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3709 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3710 | return; |
| 3711 | } |
| 3712 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3713 | D->addAttr(::new (S.Context) |
| 3714 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3715 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3716 | } else { |
| 3717 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3718 | } |
| 3719 | } |
| 3720 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3721 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3722 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3723 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3724 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3725 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3726 | return; |
| 3727 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3728 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3729 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3730 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3731 | return; |
| 3732 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3733 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3734 | D->addAttr(::new (S.Context) |
| 3735 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3736 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3739 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3740 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3741 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3742 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3743 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3744 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3745 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3746 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3747 | return; |
| 3748 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3749 | if (!isa<ObjCMethodDecl>(D)) { |
| 3750 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3751 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3752 | return; |
| 3753 | } |
| 3754 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3755 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3756 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3757 | D->addAttr(::new (S.Context) |
| 3758 | FastCallAttr(Attr.getRange(), S.Context, |
| 3759 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3760 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3761 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3762 | D->addAttr(::new (S.Context) |
| 3763 | StdCallAttr(Attr.getRange(), S.Context, |
| 3764 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3765 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3766 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3767 | D->addAttr(::new (S.Context) |
| 3768 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3769 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3770 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3771 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3772 | D->addAttr(::new (S.Context) |
| 3773 | CDeclAttr(Attr.getRange(), S.Context, |
| 3774 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3775 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3776 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3777 | D->addAttr(::new (S.Context) |
| 3778 | PascalAttr(Attr.getRange(), S.Context, |
| 3779 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3780 | return; |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3781 | case AttributeList::AT_MSABI: |
| 3782 | D->addAttr(::new (S.Context) |
| 3783 | MSABIAttr(Attr.getRange(), S.Context, |
| 3784 | Attr.getAttributeSpellingListIndex())); |
| 3785 | return; |
| 3786 | case AttributeList::AT_SysVABI: |
| 3787 | D->addAttr(::new (S.Context) |
| 3788 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3789 | Attr.getAttributeSpellingListIndex())); |
| 3790 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3791 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3792 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3793 | switch (CC) { |
| 3794 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3795 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3796 | break; |
| 3797 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3798 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3799 | break; |
| 3800 | default: |
| 3801 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3804 | D->addAttr(::new (S.Context) |
| 3805 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3806 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3807 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3808 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3809 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3810 | D->addAttr(::new (S.Context) |
| 3811 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3812 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3813 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3814 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3815 | D->addAttr(::new (S.Context) |
| 3816 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3817 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3818 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3819 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3820 | default: |
| 3821 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3822 | } |
| 3823 | } |
| 3824 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3825 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3826 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3829 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3830 | Expr *E = Attr.getArgAsExpr(0); |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3831 | llvm::APSInt ArgNum(32); |
| 3832 | if (E->isTypeDependent() || E->isValueDependent() || |
| 3833 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3834 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3835 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3836 | << E->getSourceRange(); |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3837 | return; |
| 3838 | } |
| 3839 | |
| 3840 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 3841 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 3842 | } |
| 3843 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3844 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3845 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3846 | if (attr.isInvalid()) |
| 3847 | return true; |
| 3848 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3849 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3850 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3851 | attr.setInvalid(); |
| 3852 | return true; |
| 3853 | } |
| 3854 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3855 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3856 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3857 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3858 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3859 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3860 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3861 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3862 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3863 | case AttributeList::AT_MSABI: |
| 3864 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3865 | CC_X86_64Win64; |
| 3866 | break; |
| 3867 | case AttributeList::AT_SysVABI: |
| 3868 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3869 | CC_C; |
| 3870 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3871 | case AttributeList::AT_Pcs: { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3872 | StringLiteral *Str = 0; |
| 3873 | if (attr.isArgExpr(0)) |
| 3874 | Str = dyn_cast<StringLiteral>(attr.getArgAsExpr(0)); |
| 3875 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3876 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3877 | Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName() |
| 3878 | << AANT_ArgumentString; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3879 | attr.setInvalid(); |
| 3880 | return true; |
| 3881 | } |
| 3882 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3883 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3884 | if (StrRef == "aapcs") { |
| 3885 | CC = CC_AAPCS; |
| 3886 | break; |
| 3887 | } else if (StrRef == "aapcs-vfp") { |
| 3888 | CC = CC_AAPCS_VFP; |
| 3889 | break; |
| 3890 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3891 | |
| 3892 | attr.setInvalid(); |
| 3893 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3894 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3895 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3896 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3897 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3898 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3901 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3902 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3903 | if (A == TargetInfo::CCCR_Warning) { |
| 3904 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3905 | |
| 3906 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3907 | if (FD) |
| 3908 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3909 | TargetInfo::CCMT_NonMember; |
| 3910 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3913 | return false; |
| 3914 | } |
| 3915 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3916 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3917 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3918 | |
| 3919 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3920 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3921 | return; |
| 3922 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3923 | if (!isa<ObjCMethodDecl>(D)) { |
| 3924 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3925 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3926 | return; |
| 3927 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3928 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3929 | D->addAttr(::new (S.Context) |
| 3930 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 3931 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
| 3934 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3935 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3936 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3937 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3938 | return true; |
| 3939 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3940 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3941 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3942 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3943 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3944 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3945 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3946 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3947 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3948 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3949 | Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3950 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3951 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3952 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3953 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3956 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3957 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3958 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3959 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3960 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3963 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3964 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3965 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3966 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3967 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3968 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3971 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3974 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3975 | if (S.LangOpts.CUDA) { |
| 3976 | // check the attribute arguments. |
| 3977 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 3978 | // FIXME: 0 is not okay. |
| 3979 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3980 | return; |
| 3981 | } |
| 3982 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3983 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3984 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3985 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3986 | return; |
| 3987 | } |
| 3988 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3989 | Expr *MaxThreadsExpr = Attr.getArgAsExpr(0); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3990 | llvm::APSInt MaxThreads(32); |
| 3991 | if (MaxThreadsExpr->isTypeDependent() || |
| 3992 | MaxThreadsExpr->isValueDependent() || |
| 3993 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3994 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3995 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3996 | << MaxThreadsExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3997 | return; |
| 3998 | } |
| 3999 | |
| 4000 | llvm::APSInt MinBlocks(32); |
| 4001 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4002 | Expr *MinBlocksExpr = Attr.getArgAsExpr(1); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4003 | if (MinBlocksExpr->isTypeDependent() || |
| 4004 | MinBlocksExpr->isValueDependent() || |
| 4005 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4006 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4007 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4008 | << MinBlocksExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4009 | return; |
| 4010 | } |
| 4011 | } |
| 4012 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4013 | D->addAttr(::new (S.Context) |
| 4014 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4015 | MaxThreads.getZExtValue(), |
| 4016 | MinBlocks.getZExtValue(), |
| 4017 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4018 | } else { |
| 4019 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4020 | } |
| 4021 | } |
| 4022 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4023 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4024 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4025 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4026 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4027 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4028 | return; |
| 4029 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4030 | |
| 4031 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4032 | return; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4033 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4034 | StringRef AttrName = Attr.getName()->getName(); |
| 4035 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4036 | |
| 4037 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4038 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4039 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4040 | return; |
| 4041 | } |
| 4042 | |
| 4043 | uint64_t ArgumentIdx; |
| 4044 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4045 | Attr.getLoc(), 2, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4046 | Attr.getArgAsExpr(1), ArgumentIdx)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4047 | return; |
| 4048 | |
| 4049 | uint64_t TypeTagIdx; |
| 4050 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4051 | Attr.getLoc(), 3, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4052 | Attr.getArgAsExpr(2), TypeTagIdx)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4053 | return; |
| 4054 | |
| 4055 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4056 | if (IsPointer) { |
| 4057 | // Ensure that buffer has a pointer type. |
| 4058 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4059 | if (!BufferTy->isPointerType()) { |
| 4060 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4061 | << Attr.getName(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4062 | } |
| 4063 | } |
| 4064 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4065 | D->addAttr(::new (S.Context) |
| 4066 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4067 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4068 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4072 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4073 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4074 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4075 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4076 | return; |
| 4077 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4078 | |
| 4079 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4080 | return; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4081 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4082 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4083 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4084 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4085 | D->addAttr(::new (S.Context) |
| 4086 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4087 | MatchingCType, |
| 4088 | Attr.getLayoutCompatible(), |
| 4089 | Attr.getMustBeNull(), |
| 4090 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4093 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4094 | // Checker-specific attribute handlers. |
| 4095 | //===----------------------------------------------------------------------===// |
| 4096 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4097 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4098 | return type->isDependentType() || |
| 4099 | type->isObjCObjectPointerType() || |
| 4100 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4101 | } |
| 4102 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4103 | return type->isDependentType() || |
| 4104 | type->isPointerType() || |
| 4105 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4108 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4109 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4110 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4111 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4112 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4113 | return; |
| 4114 | } |
| 4115 | |
| 4116 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4117 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4118 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4119 | cf = false; |
| 4120 | } else { |
| 4121 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4122 | cf = true; |
| 4123 | } |
| 4124 | |
| 4125 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4126 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4127 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4128 | return; |
| 4129 | } |
| 4130 | |
| 4131 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4132 | param->addAttr(::new (S.Context) |
| 4133 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4134 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4135 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4136 | param->addAttr(::new (S.Context) |
| 4137 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4138 | Attr.getAttributeSpellingListIndex())); |
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 handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4142 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4143 | if (!isa<ObjCMethodDecl>(D)) { |
| 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() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4146 | return; |
| 4147 | } |
| 4148 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4149 | D->addAttr(::new (S.Context) |
| 4150 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4151 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4154 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4155 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4156 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4157 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4158 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4159 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4160 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4161 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4162 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4163 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4164 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4165 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4166 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4167 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4168 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4169 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4170 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4171 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4172 | return; |
| 4173 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4174 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4175 | bool typeOK; |
| 4176 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4177 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4178 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4179 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4180 | case AttributeList::AT_NSReturnsRetained: |
| 4181 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4182 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4183 | cf = false; |
| 4184 | break; |
| 4185 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4186 | case AttributeList::AT_CFReturnsRetained: |
| 4187 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4188 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4189 | cf = true; |
| 4190 | break; |
| 4191 | } |
| 4192 | |
| 4193 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4194 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4195 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4196 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4197 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4198 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4199 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4200 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4201 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4202 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4203 | D->addAttr(::new (S.Context) |
| 4204 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4205 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4206 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4207 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4208 | D->addAttr(::new (S.Context) |
| 4209 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4210 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4211 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4212 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4213 | D->addAttr(::new (S.Context) |
| 4214 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4215 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4216 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4217 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4218 | D->addAttr(::new (S.Context) |
| 4219 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4220 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4221 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4222 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4223 | D->addAttr(::new (S.Context) |
| 4224 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4225 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4226 | return; |
| 4227 | }; |
| 4228 | } |
| 4229 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4230 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4231 | const AttributeList &attr) { |
| 4232 | SourceLocation loc = attr.getLoc(); |
| 4233 | |
| 4234 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4235 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4236 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4237 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4238 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4239 | return; |
| 4240 | } |
| 4241 | |
| 4242 | // Check that the method returns a normal pointer. |
| 4243 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4244 | |
| 4245 | if (!resultType->isReferenceType() && |
| 4246 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4247 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4248 | << SourceRange(loc) |
| 4249 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4250 | |
| 4251 | // Drop the attribute. |
| 4252 | return; |
| 4253 | } |
| 4254 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4255 | method->addAttr(::new (S.Context) |
| 4256 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4257 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4260 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4261 | const AttributeList &attr) { |
| 4262 | SourceLocation loc = attr.getLoc(); |
| 4263 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4264 | |
| 4265 | if (!method) { |
| 4266 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4267 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4268 | return; |
| 4269 | } |
| 4270 | DeclContext *DC = method->getDeclContext(); |
| 4271 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4272 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4273 | << attr.getName() << 0; |
| 4274 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4275 | return; |
| 4276 | } |
| 4277 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4278 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4279 | << attr.getName() << 1; |
| 4280 | return; |
| 4281 | } |
| 4282 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4283 | method->addAttr(::new (S.Context) |
| 4284 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4285 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4288 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4289 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4290 | if (!isa<FunctionDecl>(D)) { |
| 4291 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4292 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4293 | return; |
| 4294 | } |
| 4295 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4296 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4297 | |
| 4298 | // Check whether there's a conflicting attribute already present. |
| 4299 | Attr *Existing; |
| 4300 | if (IsAudited) { |
| 4301 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4302 | } else { |
| 4303 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4304 | } |
| 4305 | if (Existing) { |
| 4306 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4307 | << A.getName() |
| 4308 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4309 | << A.getRange() << Existing->getRange(); |
| 4310 | return; |
| 4311 | } |
| 4312 | |
| 4313 | // All clear; add the attribute. |
| 4314 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4315 | D->addAttr(::new (S.Context) |
| 4316 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4317 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4318 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4319 | D->addAttr(::new (S.Context) |
| 4320 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4321 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4322 | } |
| 4323 | } |
| 4324 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4325 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4326 | const AttributeList &Attr) { |
| 4327 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4328 | if (!RD || RD->isUnion()) { |
| 4329 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4330 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4333 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4334 | |
| 4335 | // In Objective-C, verify that the type names an Objective-C type. |
| 4336 | // We don't want to check this outside of ObjC because people sometimes |
| 4337 | // do crazy C declarations of Objective-C types. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4338 | if (Parm && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4339 | // Check for an existing type with this name. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4340 | LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc, |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4341 | Sema::LookupOrdinaryName); |
| 4342 | if (S.LookupName(R, Sc)) { |
| 4343 | NamedDecl *Target = R.getFoundDecl(); |
| 4344 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4345 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4346 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4347 | } |
| 4348 | } |
| 4349 | } |
| 4350 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4351 | D->addAttr(::new (S.Context) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4352 | NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4353 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4356 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4357 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4358 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4359 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4360 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4361 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4364 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4365 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4366 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4367 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4368 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4369 | return; |
| 4370 | } |
| 4371 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4372 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4373 | QualType type = vd->getType(); |
| 4374 | |
| 4375 | if (!type->isDependentType() && |
| 4376 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4377 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4378 | << type; |
| 4379 | return; |
| 4380 | } |
| 4381 | |
| 4382 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4383 | |
| 4384 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4385 | // going to infer. |
| 4386 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4387 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4388 | |
| 4389 | switch (lifetime) { |
| 4390 | case Qualifiers::OCL_None: |
| 4391 | assert(type->isDependentType() && |
| 4392 | "didn't infer lifetime for non-dependent type?"); |
| 4393 | break; |
| 4394 | |
| 4395 | case Qualifiers::OCL_Weak: // meaningful |
| 4396 | case Qualifiers::OCL_Strong: // meaningful |
| 4397 | break; |
| 4398 | |
| 4399 | case Qualifiers::OCL_ExplicitNone: |
| 4400 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4401 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4402 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4403 | break; |
| 4404 | } |
| 4405 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4406 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4407 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4408 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4411 | //===----------------------------------------------------------------------===// |
| 4412 | // Microsoft specific attribute handlers. |
| 4413 | //===----------------------------------------------------------------------===// |
| 4414 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4415 | // Check if MS extensions or some other language extensions are enabled. If |
| 4416 | // not, issue a diagnostic that the given attribute is unused. |
| 4417 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4418 | bool OtherExtension = false) { |
| 4419 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4420 | return true; |
| 4421 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4422 | return false; |
| 4423 | } |
| 4424 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4425 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4426 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4427 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4428 | |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4429 | StringRef StrRef; |
| 4430 | SourceLocation LiteralLoc; |
| 4431 | if (!checkStringLiteralArgument(S, StrRef, Attr, 0, &LiteralLoc)) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4432 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4433 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4434 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4435 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4436 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4437 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4438 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4439 | // Validate GUID length. |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4440 | if (StrRef.size() != 36) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4441 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4442 | return; |
| 4443 | } |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4444 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4445 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4446 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4447 | if (StrRef[i] != '-') { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4448 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4449 | return; |
| 4450 | } |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4451 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4452 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4453 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4454 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4455 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4456 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4457 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4458 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4461 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4462 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4463 | return; |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4464 | |
| 4465 | AttributeList::Kind Kind = Attr.getKind(); |
| 4466 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4467 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4468 | ::new (S.Context) |
| 4469 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4470 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4471 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4472 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4473 | ::new (S.Context) |
| 4474 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4475 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4476 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4477 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4478 | ::new (S.Context) |
| 4479 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4480 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4484 | if (!checkMicrosoftExt(S, Attr)) |
| 4485 | return; |
| 4486 | |
| 4487 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4488 | if (Kind == AttributeList::AT_Win64) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4489 | D->addAttr( |
| 4490 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4491 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4494 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4495 | if (!checkMicrosoftExt(S, Attr)) |
| 4496 | return; |
| 4497 | D->addAttr(::new (S.Context) |
| 4498 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4499 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4502 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4503 | if (!checkMicrosoftExt(S, Attr)) |
| 4504 | return; |
| 4505 | // Check linkage after possibly merging declaratinos. See |
| 4506 | // checkAttributesAfterMerging(). |
| 4507 | D->addAttr(::new (S.Context) |
| 4508 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4509 | Attr.getAttributeSpellingListIndex())); |
| 4510 | } |
| 4511 | |
Aaron Ballman | bbb3b32 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4512 | /// Handles semantic checking for features that are common to all attributes, |
| 4513 | /// such as checking whether a parameter was properly specified, or the correct |
| 4514 | /// number of arguments were passed, etc. |
| 4515 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4516 | const AttributeList &Attr) { |
| 4517 | // Several attributes carry different semantics than the parsing requires, so |
| 4518 | // those are opted out of the common handling. |
| 4519 | // |
| 4520 | // We also bail on unknown and ignored attributes because those are handled |
| 4521 | // as part of the target-specific handling logic. |
| 4522 | if (Attr.hasCustomParsing() || |
| 4523 | Attr.getKind() == AttributeList::UnknownAttribute || |
| 4524 | Attr.getKind() == AttributeList::IgnoredAttribute) |
| 4525 | return false; |
| 4526 | |
| 4527 | // If there are no optional arguments, then checking for the argument count |
| 4528 | // is trivial. |
| 4529 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 4530 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4531 | return true; |
| 4532 | return false; |
| 4533 | } |
| 4534 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4535 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4536 | // Top Level Sema Entry Points |
| 4537 | //===----------------------------------------------------------------------===// |
| 4538 | |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4539 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4540 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4541 | /// silently ignore it if a GNU attribute. |
| 4542 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4543 | const AttributeList &Attr, |
| 4544 | bool IncludeCXX11Attributes) { |
| 4545 | if (Attr.isInvalid()) |
| 4546 | return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4547 | |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4548 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4549 | // instead. |
| 4550 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4551 | return; |
| 4552 | |
Aaron Ballman | bbb3b32 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4553 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4554 | return; |
| 4555 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4556 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4557 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4558 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4559 | case AttributeList::AT_IBOutletCollection: |
| 4560 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4561 | case AttributeList::AT_AddressSpace: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4562 | case AttributeList::AT_ObjCGC: |
| 4563 | case AttributeList::AT_VectorSize: |
| 4564 | case AttributeList::AT_NeonVectorType: |
| 4565 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4566 | case AttributeList::AT_Ptr32: |
| 4567 | case AttributeList::AT_Ptr64: |
| 4568 | case AttributeList::AT_SPtr: |
| 4569 | case AttributeList::AT_UPtr: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4570 | // Ignore these, these are type attributes, handled by |
| 4571 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4572 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4573 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4574 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4575 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4576 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4577 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4578 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4579 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4580 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4581 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4582 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4583 | case AttributeList::AT_CarriesDependency: |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4584 | handleDependencyAttr(S, scope, D, Attr); |
| 4585 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4586 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4587 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4588 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4589 | case AttributeList::AT_CXX11NoReturn: |
| 4590 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4591 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4592 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4593 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4594 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4595 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4596 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4597 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4598 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4599 | case AttributeList::AT_MinSize: |
| 4600 | handleMinSizeAttr(S, D, Attr); |
| 4601 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4602 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4603 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4604 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | 1951323 | 2013-08-28 23:13:26 +0000 | [diff] [blame] | 4605 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4606 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4607 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4608 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4609 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4610 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4611 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4612 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4613 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4614 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4615 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4616 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4617 | case AttributeList::AT_ownership_returns: |
| 4618 | case AttributeList::AT_ownership_takes: |
| 4619 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4620 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4621 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4622 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4623 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4624 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4625 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4626 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4627 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4628 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4629 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4630 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4631 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4632 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4633 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4634 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4635 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4636 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4637 | case AttributeList::AT_ObjCRequiresSuper: |
| 4638 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4639 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4640 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4641 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4642 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4643 | case AttributeList::AT_CFAuditedTransfer: |
| 4644 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4645 | handleCFTransferAttr(S, D, Attr); break; |
| 4646 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4647 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4648 | case AttributeList::AT_CFConsumed: |
| 4649 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4650 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4651 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4652 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4653 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4654 | case AttributeList::AT_NSReturnsNotRetained: |
| 4655 | case AttributeList::AT_CFReturnsNotRetained: |
| 4656 | case AttributeList::AT_NSReturnsRetained: |
| 4657 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4658 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4659 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4660 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4661 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4662 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4663 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4664 | case AttributeList::AT_VecTypeHint: |
| 4665 | handleVecTypeHint(S, D, Attr); break; |
| 4666 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4667 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4668 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4669 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4670 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4671 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4672 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4673 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4674 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4675 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4676 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4677 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4678 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4679 | handleObjCRootClassAttr(S, D, Attr); |
| 4680 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4681 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4682 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4683 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4684 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4685 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4686 | handleReturnsTwiceAttr(S, D, Attr); |
| 4687 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4688 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4689 | case AttributeList::AT_Visibility: |
| 4690 | handleVisibilityAttr(S, D, Attr, false); |
| 4691 | break; |
| 4692 | case AttributeList::AT_TypeVisibility: |
| 4693 | handleVisibilityAttr(S, D, Attr, true); |
| 4694 | break; |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4695 | case AttributeList::AT_WarnUnused: |
| 4696 | handleWarnUnusedAttr(S, D, Attr); |
| 4697 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4698 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4699 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4700 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4701 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4702 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4703 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4704 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4705 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4706 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4707 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4708 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4709 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4710 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4711 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4712 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4713 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4714 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4715 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4716 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4717 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4718 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4719 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4720 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4721 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4722 | // Just ignore |
| 4723 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4724 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4725 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4726 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4727 | case AttributeList::AT_StdCall: |
| 4728 | case AttributeList::AT_CDecl: |
| 4729 | case AttributeList::AT_FastCall: |
| 4730 | case AttributeList::AT_ThisCall: |
| 4731 | case AttributeList::AT_Pascal: |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4732 | case AttributeList::AT_MSABI: |
| 4733 | case AttributeList::AT_SysVABI: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4734 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4735 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4736 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4737 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4738 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4739 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4740 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4741 | break; |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4742 | case AttributeList::AT_OpenCLImageAccess: |
| 4743 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4744 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4745 | |
| 4746 | // Microsoft attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4747 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4748 | handleMsStructAttr(S, D, Attr); |
| 4749 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4750 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4751 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4752 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4753 | case AttributeList::AT_SingleInheritance: |
| 4754 | case AttributeList::AT_MultipleInheritance: |
| 4755 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4756 | handleInheritanceAttr(S, D, Attr); |
| 4757 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4758 | case AttributeList::AT_Win64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4759 | handlePortabilityAttr(S, D, Attr); |
| 4760 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4761 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4762 | handleForceInlineAttr(S, D, Attr); |
| 4763 | break; |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4764 | case AttributeList::AT_SelectAny: |
| 4765 | handleSelectAnyAttr(S, D, Attr); |
| 4766 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4767 | |
| 4768 | // Thread safety attributes: |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4769 | case AttributeList::AT_AssertExclusiveLock: |
| 4770 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4771 | break; |
| 4772 | case AttributeList::AT_AssertSharedLock: |
| 4773 | handleAssertSharedLockAttr(S, D, Attr); |
| 4774 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4775 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4776 | handleGuardedVarAttr(S, D, Attr); |
| 4777 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4778 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4779 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4780 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4781 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4782 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4783 | break; |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4784 | case AttributeList::AT_NoSanitizeAddress: |
| 4785 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4786 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4787 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4788 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4789 | break; |
| 4790 | case AttributeList::AT_NoSanitizeThread: |
| 4791 | handleNoSanitizeThread(S, D, Attr); |
| 4792 | break; |
| 4793 | case AttributeList::AT_NoSanitizeMemory: |
| 4794 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4795 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4796 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4797 | handleLockableAttr(S, D, Attr); |
| 4798 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4799 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4800 | handleGuardedByAttr(S, D, Attr); |
| 4801 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4802 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4803 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4804 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4805 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4806 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4807 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4808 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4809 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4810 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4811 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4812 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4813 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4814 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4815 | handleLockReturnedAttr(S, D, Attr); |
| 4816 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4817 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4818 | handleLocksExcludedAttr(S, D, Attr); |
| 4819 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4820 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4821 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4822 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4823 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4824 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4825 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4826 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4827 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4828 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4829 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4830 | handleUnlockFunAttr(S, D, Attr); |
| 4831 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4832 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4833 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4834 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4835 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4836 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4837 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4838 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4839 | // Uniqueness analysis attributes. |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4840 | case AttributeList::AT_Consumable: |
| 4841 | handleConsumableAttr(S, D, Attr); |
| 4842 | break; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4843 | case AttributeList::AT_Consumes: |
| 4844 | handleConsumesAttr(S, D, Attr); |
| 4845 | break; |
| 4846 | case AttributeList::AT_CallableWhenUnconsumed: |
| 4847 | handleCallableWhenUnconsumedAttr(S, D, Attr); |
| 4848 | break; |
| 4849 | case AttributeList::AT_TestsConsumed: |
| 4850 | handleTestsConsumedAttr(S, D, Attr); |
| 4851 | break; |
| 4852 | case AttributeList::AT_TestsUnconsumed: |
| 4853 | handleTestsUnconsumedAttr(S, D, Attr); |
| 4854 | break; |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4855 | case AttributeList::AT_ReturnTypestate: |
| 4856 | handleReturnTypestateAttr(S, D, Attr); |
| 4857 | break; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4858 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4859 | // Type safety attributes. |
| 4860 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4861 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4862 | break; |
| 4863 | case AttributeList::AT_TypeTagForDatatype: |
| 4864 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4865 | break; |
| 4866 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4867 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4868 | // Ask target about the attribute. |
| 4869 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4870 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4871 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4872 | diag::warn_unhandled_ms_attribute_ignored : |
| 4873 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4874 | break; |
| 4875 | } |
| 4876 | } |
| 4877 | |
| 4878 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4879 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4880 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4881 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4882 | bool IncludeCXX11Attributes) { |
| 4883 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4884 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4885 | |
| 4886 | // GCC accepts |
| 4887 | // static int a9 __attribute__((weakref)); |
| 4888 | // but that looks really pointless. We reject it. |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4889 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4890 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4891 | cast<NamedDecl>(D)->getNameAsString(); |
| 4892 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4893 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4894 | } |
| 4895 | } |
| 4896 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4897 | // Annotation attributes are the only attributes allowed after an access |
| 4898 | // specifier. |
| 4899 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4900 | const AttributeList *AttrList) { |
| 4901 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4902 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4903 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4904 | } else { |
| 4905 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4906 | return true; |
| 4907 | } |
| 4908 | } |
| 4909 | |
| 4910 | return false; |
| 4911 | } |
| 4912 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4913 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4914 | /// contains any decl attributes that we should warn about. |
| 4915 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4916 | for ( ; A; A = A->getNext()) { |
| 4917 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4918 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4919 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4920 | |
| 4921 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4922 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4923 | << A->getName() << A->getRange(); |
| 4924 | } else { |
| 4925 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4926 | << A->getName() << A->getRange(); |
| 4927 | } |
| 4928 | } |
| 4929 | } |
| 4930 | |
| 4931 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4932 | /// used to build a declaration, complain about any decl attributes |
| 4933 | /// which might be lying around on it. |
| 4934 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4935 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4936 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4937 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4938 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4939 | } |
| 4940 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4941 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4942 | /// \#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] | 4943 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4944 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4945 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4946 | NamedDecl *NewD = 0; |
| 4947 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4948 | FunctionDecl *NewFD; |
| 4949 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4950 | // FIXME: Mangling? |
| 4951 | // FIXME: Is the qualifier info correct? |
| 4952 | // FIXME: Is the DeclContext correct? |
| 4953 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4954 | Loc, Loc, DeclarationName(II), |
| 4955 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4956 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4957 | FD->hasPrototype(), |
| 4958 | false/*isConstexprSpecified*/); |
| 4959 | NewD = NewFD; |
| 4960 | |
| 4961 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4962 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4963 | |
| 4964 | // Fake up parameter variables; they are declared as if this were |
| 4965 | // a typedef. |
| 4966 | QualType FDTy = FD->getType(); |
| 4967 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4968 | SmallVector<ParmVarDecl*, 16> Params; |
| 4969 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4970 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4971 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4972 | Param->setScopeInfo(0, Params.size()); |
| 4973 | Params.push_back(Param); |
| 4974 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4975 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4976 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4977 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4978 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4979 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4980 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4981 | VD->getStorageClass()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4982 | if (VD->getQualifier()) { |
| 4983 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4984 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4985 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4986 | } |
| 4987 | return NewD; |
| 4988 | } |
| 4989 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4990 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4991 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4992 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4993 | if (W.getUsed()) return; // only do this once |
| 4994 | W.setUsed(true); |
| 4995 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4996 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4997 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4998 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4999 | NDId->getName())); |
| 5000 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5001 | WeakTopLevelDecl.push_back(NewD); |
| 5002 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5003 | // to insert Decl at TU scope, sorry. |
| 5004 | DeclContext *SavedContext = CurContext; |
| 5005 | CurContext = Context.getTranslationUnitDecl(); |
| 5006 | PushOnScopeChains(NewD, S); |
| 5007 | CurContext = SavedContext; |
| 5008 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5009 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5010 | } |
| 5011 | } |
| 5012 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5013 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5014 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5015 | // have to do this. |
| 5016 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5017 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5018 | NamedDecl *ND = NULL; |
| 5019 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5020 | if (VD->isExternC()) |
| 5021 | ND = VD; |
| 5022 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5023 | if (FD->isExternC()) |
| 5024 | ND = FD; |
| 5025 | if (ND) { |
| 5026 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5027 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5028 | = WeakUndeclaredIdentifiers.find(Id); |
| 5029 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5030 | WeakInfo W = I->second; |
| 5031 | DeclApplyPragmaWeak(S, ND, W); |
| 5032 | WeakUndeclaredIdentifiers[Id] = W; |
| 5033 | } |
| 5034 | } |
| 5035 | } |
| 5036 | } |
| 5037 | } |
| 5038 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5039 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5040 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5041 | /// 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] | 5042 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5043 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5044 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5045 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5046 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5047 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5048 | // position to the decl itself. This handles cases like: |
| 5049 | // int *__attr__(x)** D; |
| 5050 | // when X is a decl attribute. |
| 5051 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5052 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5053 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5054 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5055 | // Finally, apply any attributes on the decl itself. |
| 5056 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5057 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5058 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5059 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5060 | /// Is the given declaration allowed to use a forbidden type? |
| 5061 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5062 | // Private ivars are always okay. Unfortunately, people don't |
| 5063 | // always properly make their ivars private, even in system headers. |
| 5064 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5065 | // Function declarations in sys headers will be marked unavailable. |
| 5066 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5067 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5068 | return false; |
| 5069 | |
| 5070 | // Require it to be declared in a system header. |
| 5071 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5072 | } |
| 5073 | |
| 5074 | /// Handle a delayed forbidden-type diagnostic. |
| 5075 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5076 | Decl *decl) { |
| 5077 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5078 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5079 | "this system declaration uses an unsupported type")); |
| 5080 | return; |
| 5081 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5082 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5083 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5084 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5085 | // kind of forbidden type messages on unavailable functions. |
| 5086 | if (FD->hasAttr<UnavailableAttr>() && |
| 5087 | diag.getForbiddenTypeDiagnostic() == |
| 5088 | diag::err_arc_array_param_no_ownership) { |
| 5089 | diag.Triggered = true; |
| 5090 | return; |
| 5091 | } |
| 5092 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5093 | |
| 5094 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5095 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5096 | diag.Triggered = true; |
| 5097 | } |
| 5098 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5099 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5100 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5101 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5102 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5103 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5104 | // When delaying diagnostics to run in the context of a parsed |
| 5105 | // declaration, we only want to actually emit anything if parsing |
| 5106 | // succeeds. |
| 5107 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5108 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5109 | // We emit all the active diagnostics in this pool or any of its |
| 5110 | // parents. In general, we'll get one pool for the decl spec |
| 5111 | // and a child pool for each declarator; in a decl group like: |
| 5112 | // deprecated_typedef foo, *bar, baz(); |
| 5113 | // only the declarator pops will be passed decls. This is correct; |
| 5114 | // we really do need to consider delayed diagnostics from the decl spec |
| 5115 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5116 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5117 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5118 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5119 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5120 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5121 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5122 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5123 | continue; |
| 5124 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5125 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5126 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5127 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5128 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5129 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5130 | break; |
| 5131 | |
| 5132 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5133 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5134 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5135 | |
| 5136 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5137 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5138 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5139 | } |
| 5140 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5141 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5144 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5145 | /// been delayed in the current context instead of in the given pool. |
| 5146 | /// Essentially, this just moves them to the current pool. |
| 5147 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5148 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5149 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5150 | curPool->steal(pool); |
| 5151 | } |
| 5152 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5153 | static bool isDeclDeprecated(Decl *D) { |
| 5154 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5155 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5156 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5157 | // A category implicitly has the availability of the interface. |
| 5158 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5159 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5160 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5161 | return false; |
| 5162 | } |
| 5163 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5164 | static void |
| 5165 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5166 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5167 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5168 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5169 | DeclarationName Name = D->getDeclName(); |
| 5170 | if (!Message.empty()) { |
| 5171 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5172 | S.Diag(D->getLocation(), |
| 5173 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5174 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5175 | if (ObjCPropery) |
| 5176 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5177 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5178 | } else if (!UnknownObjCClass) { |
| 5179 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5180 | S.Diag(D->getLocation(), |
| 5181 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5182 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5183 | if (ObjCPropery) |
| 5184 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5185 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5186 | } else { |
| 5187 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5188 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5189 | } |
| 5190 | } |
| 5191 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5192 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5193 | Decl *Ctx) { |
| 5194 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5195 | return; |
| 5196 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5197 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5198 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5199 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5200 | DD.getUnknownObjCClass(), |
| 5201 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5202 | } |
| 5203 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5204 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5205 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5206 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5207 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5208 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5209 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5210 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5211 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5212 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5213 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5214 | return; |
| 5215 | } |
| 5216 | |
| 5217 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5218 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5219 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5220 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5221 | } |