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" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
| 21 | #include "clang/AST/Expr.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 24 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 25 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Lookup.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 28 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 29 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 30 | |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 31 | /// These constants match the enumerated choices of |
| 32 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 33 | enum AttributeDeclKind { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 34 | ExpectedFunction, |
| 35 | ExpectedUnion, |
| 36 | ExpectedVariableOrFunction, |
| 37 | ExpectedFunctionOrMethod, |
| 38 | ExpectedParameter, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 39 | ExpectedFunctionMethodOrBlock, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 40 | ExpectedFunctionMethodOrParameter, |
| 41 | ExpectedClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 42 | ExpectedVariable, |
| 43 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 44 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 45 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 46 | ExpectedStruct, |
| 47 | ExpectedTLSVar |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | // Helper functions |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 54 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 55 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 56 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 57 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 58 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 59 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 60 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 61 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 62 | Ty = decl->getUnderlyingType(); |
| 63 | else |
| 64 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 66 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 67 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 68 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 69 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 70 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 71 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 74 | // FIXME: We should provide an abstraction around a method or function |
| 75 | // to provide the following bits of information. |
| 76 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 77 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 78 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 79 | static bool isFunction(const Decl *D) { |
| 80 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 84 | /// type (function or function-typed variable) or an Objective-C |
| 85 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 86 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 87 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 90 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 91 | /// type (function or function-typed variable) or an Objective-C |
| 92 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 93 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 94 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 95 | return true; |
| 96 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 97 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 98 | QualType Ty = V->getType(); |
| 99 | return Ty->isBlockPointerType(); |
| 100 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 101 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 102 | } |
| 103 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 104 | /// Return true if the given decl has a declarator that should have |
| 105 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 106 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 107 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 108 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 109 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 112 | /// hasFunctionProto - Return true if the given decl has a argument |
| 113 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 114 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | static bool hasFunctionProto(const Decl *D) { |
| 116 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 117 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 118 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 119 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 125 | /// arguments. It is an error to call this on a K&R function (use |
| 126 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 127 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 128 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 129 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 130 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 131 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 132 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 135 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 136 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 137 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 138 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 139 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 140 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 144 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 145 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 146 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 147 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 150 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 151 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 152 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 153 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 154 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 155 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 156 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 157 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 161 | static bool isInstanceMethod(const Decl *D) { |
| 162 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 163 | return MethodDecl->isInstance(); |
| 164 | return false; |
| 165 | } |
| 166 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 167 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 168 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 169 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 170 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 171 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 172 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 173 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 174 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 175 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 176 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 178 | // FIXME: Should we walk the chain of classes? |
| 179 | return ClsName == &Ctx.Idents.get("NSString") || |
| 180 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 181 | } |
| 182 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 183 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 184 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 185 | if (!PT) |
| 186 | return false; |
| 187 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 188 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 189 | if (!RT) |
| 190 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 191 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 192 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 193 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 194 | return false; |
| 195 | |
| 196 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 197 | } |
| 198 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 199 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 200 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 201 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 202 | unsigned int Num) { |
| 203 | if (Attr.getNumArgs() != Num) { |
| 204 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | return true; |
| 209 | } |
| 210 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 211 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 212 | /// \brief Check if the attribute has at least as many args as Num. May |
| 213 | /// output an error. |
| 214 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 215 | unsigned int Num) { |
| 216 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 217 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 218 | return false; |
| 219 | } |
| 220 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 224 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 225 | /// instance method D. May output an error. |
| 226 | /// |
| 227 | /// \returns true if IdxExpr is a valid index. |
| 228 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 229 | StringRef AttrName, |
| 230 | SourceLocation AttrLoc, |
| 231 | unsigned AttrArgNum, |
| 232 | const Expr *IdxExpr, |
| 233 | uint64_t &Idx) |
| 234 | { |
| 235 | assert(isFunctionOrMethod(D) && hasFunctionProto(D)); |
| 236 | |
| 237 | // In C++ the implicit 'this' function parameter also counts. |
| 238 | // Parameters are counted from one. |
| 239 | const bool HasImplicitThisParam = isInstanceMethod(D); |
| 240 | const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 241 | const unsigned FirstIdx = 1; |
| 242 | |
| 243 | llvm::APSInt IdxInt; |
| 244 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 245 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
| 246 | S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int) |
| 247 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | Idx = IdxInt.getLimitedValue(); |
| 252 | if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) { |
| 253 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 254 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 255 | return false; |
| 256 | } |
| 257 | Idx--; // Convert to zero-based. |
| 258 | if (HasImplicitThisParam) { |
| 259 | if (Idx == 0) { |
| 260 | S.Diag(AttrLoc, |
| 261 | diag::err_attribute_invalid_implicit_this_argument) |
| 262 | << AttrName << IdxExpr->getSourceRange(); |
| 263 | return false; |
| 264 | } |
| 265 | --Idx; |
| 266 | } |
| 267 | |
| 268 | return true; |
| 269 | } |
| 270 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 271 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 272 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 273 | /// \return true if the Decl is a field or potentially shared global variable |
| 274 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 275 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 276 | if (isa<FieldDecl>(D)) |
| 277 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 278 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 279 | return (vd->hasGlobalStorage() && !(vd->isThreadSpecified())); |
| 280 | |
| 281 | return false; |
| 282 | } |
| 283 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 284 | /// \brief Check if the passed-in expression is of type int or bool. |
| 285 | static bool isIntOrBool(Expr *Exp) { |
| 286 | QualType QT = Exp->getType(); |
| 287 | return QT->isBooleanType() || QT->isIntegerType(); |
| 288 | } |
| 289 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 290 | |
| 291 | // Check to see if the type is a smart pointer of some kind. We assume |
| 292 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 293 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 294 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 295 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
| 296 | if (Res1.first == Res1.second) |
| 297 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 298 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 299 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 300 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
| 301 | if (Res2.first == Res2.second) |
| 302 | return false; |
| 303 | |
| 304 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 307 | /// \brief Check if passed in Decl is a pointer type. |
| 308 | /// Note that this function may produce an error message. |
| 309 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 310 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 311 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 312 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 313 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 314 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 315 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 316 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 317 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 318 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 319 | // (We don't want to force template instantiation if we can avoid it, |
| 320 | // since that would alter the order in which templates are instantiated.) |
| 321 | if (RT->isIncompleteType()) |
| 322 | return true; |
| 323 | |
| 324 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 325 | return true; |
| 326 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 327 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 328 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 329 | << Attr.getName()->getName() << QT; |
| 330 | } else { |
| 331 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 332 | << Attr.getName(); |
| 333 | } |
| 334 | return false; |
| 335 | } |
| 336 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 337 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 338 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 339 | static const RecordType *getRecordType(QualType QT) { |
| 340 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 341 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 342 | |
| 343 | // Now check if we point to record type. |
| 344 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 345 | return PT->getPointeeType()->getAs<RecordType>(); |
| 346 | |
| 347 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 348 | } |
| 349 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 350 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 351 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 352 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 353 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 354 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 355 | return true; |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 360 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 361 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 362 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 363 | QualType Ty) { |
| 364 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 365 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 366 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 367 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 368 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 369 | << Attr.getName() << Ty.getAsString(); |
| 370 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 371 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 372 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 373 | // 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] | 374 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 375 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 376 | |
| 377 | // Allow smart pointers to be used as lockable objects. |
| 378 | // FIXME -- Check the type that the smart pointer points to. |
| 379 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 380 | return; |
| 381 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 382 | // Check if the type is lockable. |
| 383 | RecordDecl *RD = RT->getDecl(); |
| 384 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 385 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 386 | |
| 387 | // Else check if any base classes are lockable. |
| 388 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 389 | CXXBasePaths BPaths(false, false); |
| 390 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 391 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 392 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 393 | |
| 394 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 395 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 398 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 399 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 400 | /// \param Sidx The attribute argument index to start checking with. |
| 401 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 402 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 403 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 404 | const AttributeList &Attr, |
| 405 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 406 | int Sidx = 0, |
| 407 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 408 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 409 | Expr *ArgExp = Attr.getArg(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 410 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 411 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 412 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 413 | Args.push_back(ArgExp); |
| 414 | continue; |
| 415 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 416 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 417 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 418 | if (StrLit->getLength() == 0 || |
| 419 | StrLit->getString() == StringRef("*")) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 420 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 421 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 422 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 423 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 424 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 425 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 426 | // We allow constant strings to be used as a placeholder for expressions |
| 427 | // that are not valid C++ syntax, but warn that they are ignored. |
| 428 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 429 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 430 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 431 | continue; |
| 432 | } |
| 433 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 434 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 435 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 436 | // A pointer to member expression of the form &MyClass::mu is treated |
| 437 | // specially -- we need to look at the type of the member. |
| 438 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 439 | if (UOp->getOpcode() == UO_AddrOf) |
| 440 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 441 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 442 | ArgTy = DRE->getDecl()->getType(); |
| 443 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 444 | // First see if we can just cast to record type, or point to record type. |
| 445 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 446 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 447 | // Now check if we index into a record type function param. |
| 448 | if(!RT && ParamIdxOk) { |
| 449 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 450 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 451 | if(FD && IL) { |
| 452 | unsigned int NumParams = FD->getNumParams(); |
| 453 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 454 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 455 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 456 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 457 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 458 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 459 | continue; |
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 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 465 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 466 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 467 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 468 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 471 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 472 | // Attribute Implementations |
| 473 | //===----------------------------------------------------------------------===// |
| 474 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 475 | // FIXME: All this manual attribute parsing code is gross. At the |
| 476 | // least add some helper functions to check most argument patterns (# |
| 477 | // and types of args). |
| 478 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 479 | enum ThreadAttributeDeclKind { |
| 480 | ThreadExpectedFieldOrGlobalVar, |
| 481 | ThreadExpectedFunctionOrMethod, |
| 482 | ThreadExpectedClassOrStruct |
| 483 | }; |
| 484 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 485 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 486 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 487 | assert(!Attr.isInvalid()); |
| 488 | |
| 489 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 490 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 491 | |
| 492 | // D must be either a member field or global (potentially shared) variable. |
| 493 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 494 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 495 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 496 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 499 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 502 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 503 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 504 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 505 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 506 | D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context)); |
| 507 | } |
| 508 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 509 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 510 | const AttributeList &Attr) { |
| 511 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 512 | return; |
| 513 | |
| 514 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 515 | return; |
| 516 | |
| 517 | D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context)); |
| 518 | } |
| 519 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 520 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 521 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 522 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 523 | assert(!Attr.isInvalid()); |
| 524 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 525 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 526 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 527 | |
| 528 | // D must be either a member field or global (potentially shared) variable. |
| 529 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 530 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 531 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 532 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 533 | } |
| 534 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 535 | SmallVector<Expr*, 1> Args; |
| 536 | // check that all arguments are lockable objects |
| 537 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 538 | unsigned Size = Args.size(); |
| 539 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 540 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 541 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 542 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 543 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 544 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 547 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 548 | Expr *Arg = 0; |
| 549 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 550 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 551 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 552 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 553 | } |
| 554 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 555 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 556 | const AttributeList &Attr) { |
| 557 | Expr *Arg = 0; |
| 558 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 559 | return; |
| 560 | |
| 561 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 562 | return; |
| 563 | |
| 564 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 565 | S.Context, Arg)); |
| 566 | } |
| 567 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 568 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 569 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 570 | assert(!Attr.isInvalid()); |
| 571 | |
| 572 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 573 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 574 | |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 575 | // FIXME: Lockable structs for C code. |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 576 | if (!isa<CXXRecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 577 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 578 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 579 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 582 | return true; |
| 583 | } |
| 584 | |
| 585 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 586 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 587 | return; |
| 588 | |
| 589 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 590 | } |
| 591 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 592 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 593 | const AttributeList &Attr) { |
| 594 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 595 | return; |
| 596 | |
| 597 | D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context)); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void handleNoThreadSafetyAttr(Sema &S, Decl *D, |
| 601 | const AttributeList &Attr) { |
| 602 | assert(!Attr.isInvalid()); |
| 603 | |
| 604 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 605 | return; |
| 606 | |
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 | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 617 | static void handleNoAddressSafetyAttr(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 | assert(!Attr.isInvalid()); |
| 620 | |
| 621 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 622 | return; |
| 623 | |
| 624 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 625 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 626 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(), |
Nick Lewycky | f50b6fe | 2012-07-24 01:37:23 +0000 | [diff] [blame] | 631 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 634 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 635 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 636 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 637 | assert(!Attr.isInvalid()); |
| 638 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 639 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 640 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 641 | |
| 642 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 643 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 644 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 645 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 646 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 647 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 648 | } |
| 649 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 650 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 651 | QualType QT = VD->getType(); |
| 652 | if (!QT->isDependentType()) { |
| 653 | const RecordType *RT = getRecordType(QT); |
| 654 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 655 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 656 | << Attr.getName(); |
| 657 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 661 | // Check that all arguments are lockable objects. |
| 662 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 663 | if (Args.size() == 0) |
| 664 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 665 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 666 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 669 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 670 | const AttributeList &Attr) { |
| 671 | SmallVector<Expr*, 1> Args; |
| 672 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 673 | return; |
| 674 | |
| 675 | Expr **StartArg = &Args[0]; |
| 676 | D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 677 | StartArg, Args.size())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 680 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 681 | const AttributeList &Attr) { |
| 682 | SmallVector<Expr*, 1> Args; |
| 683 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 684 | return; |
| 685 | |
| 686 | Expr **StartArg = &Args[0]; |
| 687 | D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 688 | StartArg, Args.size())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 691 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 692 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 693 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 694 | assert(!Attr.isInvalid()); |
| 695 | |
| 696 | // zero or more arguments ok |
| 697 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 698 | // check that the attribute is applied to a function |
| 699 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 700 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 701 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 702 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 705 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 706 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 707 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 708 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 711 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 712 | const AttributeList &Attr) { |
| 713 | SmallVector<Expr*, 1> Args; |
| 714 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 715 | return; |
| 716 | |
| 717 | unsigned Size = Args.size(); |
| 718 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 719 | D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 720 | S.Context, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 721 | StartArg, Size)); |
| 722 | } |
| 723 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 724 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 725 | const AttributeList &Attr) { |
| 726 | SmallVector<Expr*, 1> Args; |
| 727 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 728 | return; |
| 729 | |
| 730 | unsigned Size = Args.size(); |
| 731 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 732 | D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 733 | S.Context, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 734 | StartArg, Size)); |
| 735 | } |
| 736 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 737 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 738 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 739 | SmallVector<Expr*, 2> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 740 | assert(!Attr.isInvalid()); |
| 741 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 742 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 743 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 744 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 745 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 746 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 747 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 748 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 751 | if (!isIntOrBool(Attr.getArg(0))) { |
| 752 | S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 753 | << Attr.getName(); |
| 754 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 758 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 759 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 760 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 763 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 764 | const AttributeList &Attr) { |
| 765 | SmallVector<Expr*, 2> Args; |
| 766 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 767 | return; |
| 768 | |
| 769 | unsigned Size = Args.size(); |
| 770 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 771 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 772 | S.Context, |
| 773 | Attr.getArg(0), |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 774 | StartArg, Size)); |
| 775 | } |
| 776 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 777 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 778 | const AttributeList &Attr) { |
| 779 | SmallVector<Expr*, 2> Args; |
| 780 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 781 | return; |
| 782 | |
| 783 | unsigned Size = Args.size(); |
| 784 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 785 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 786 | S.Context, |
| 787 | Attr.getArg(0), |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 788 | StartArg, Size)); |
| 789 | } |
| 790 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 791 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 792 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 793 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 794 | assert(!Attr.isInvalid()); |
| 795 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 796 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 797 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 798 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 799 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 800 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 801 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 802 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 805 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 806 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 807 | if (Args.size() == 0) |
| 808 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 809 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 810 | return true; |
| 811 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 812 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 813 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 814 | const AttributeList &Attr) { |
| 815 | SmallVector<Expr*, 1> Args; |
| 816 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 817 | return; |
| 818 | |
| 819 | Expr **StartArg = &Args[0]; |
| 820 | D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 821 | S.Context, |
| 822 | StartArg, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 823 | Args.size())); |
| 824 | } |
| 825 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 826 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 827 | const AttributeList &Attr) { |
| 828 | SmallVector<Expr*, 1> Args; |
| 829 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 830 | return; |
| 831 | |
| 832 | Expr **StartArg = &Args[0]; |
| 833 | D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 834 | S.Context, |
| 835 | StartArg, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 836 | Args.size())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 839 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 840 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 841 | assert(!Attr.isInvalid()); |
| 842 | |
| 843 | // zero or more arguments ok |
| 844 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 845 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 846 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 847 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 848 | return; |
| 849 | } |
| 850 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 851 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 852 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 853 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 854 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 855 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 856 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 857 | D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 858 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 862 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 863 | assert(!Attr.isInvalid()); |
| 864 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 865 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 866 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 867 | Expr *Arg = Attr.getArg(0); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 868 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 869 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 870 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 871 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 872 | return; |
| 873 | } |
| 874 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 875 | if (Arg->isTypeDependent()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 876 | return; |
| 877 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 878 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 879 | SmallVector<Expr*, 1> Args; |
| 880 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 881 | unsigned Size = Args.size(); |
| 882 | if (Size == 0) |
| 883 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 884 | |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 885 | D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, |
| 886 | Args[0])); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 890 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 891 | assert(!Attr.isInvalid()); |
| 892 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 893 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 894 | return; |
| 895 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 896 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 897 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 898 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 899 | return; |
| 900 | } |
| 901 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 902 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 903 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 904 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 905 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 906 | if (Size == 0) |
| 907 | return; |
| 908 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 909 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 910 | D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 911 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 915 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 916 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 917 | TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 918 | if (tDecl == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 919 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 920 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 921 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 922 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 923 | QualType curType = tDecl->getUnderlyingType(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 924 | |
| 925 | Expr *sizeExpr; |
| 926 | |
| 927 | // Special case where the argument is a template id. |
| 928 | if (Attr.getParameterName()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 929 | CXXScopeSpec SS; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 930 | SourceLocation TemplateKWLoc; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 931 | UnqualifiedId id; |
| 932 | id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 933 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 934 | ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id, |
| 935 | false, false); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 936 | if (Size.isInvalid()) |
| 937 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 938 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 939 | sizeExpr = Size.get(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 940 | } else { |
| 941 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 942 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 943 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 944 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 945 | sizeExpr = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 946 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 947 | |
| 948 | // Instantiate/Install the vector type, and let Sema build the type for us. |
| 949 | // This will run the reguired checks. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 950 | QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 951 | if (!T.isNull()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 952 | // FIXME: preserve the old source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 953 | tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 954 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 955 | // Remember this typedef decl, we will need it later for diagnostics. |
| 956 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 957 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 960 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 961 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 962 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 963 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 964 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 965 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 966 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 967 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 968 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 969 | // has no effect. |
| 970 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 971 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 972 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 973 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 974 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 975 | FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 976 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 977 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 980 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 981 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 982 | TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context)); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 983 | else |
| 984 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 985 | } |
| 986 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 987 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 988 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 989 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 990 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 991 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 992 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 993 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 994 | if (MD->isInstanceMethod()) { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 995 | D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 996 | return; |
| 997 | } |
| 998 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 999 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1002 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1003 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1004 | // variables or properties of Objective-C classes. The outlet must also |
| 1005 | // have an object reference type. |
| 1006 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1007 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1008 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1009 | << Attr.getName() << VD->getType() << 0; |
| 1010 | return false; |
| 1011 | } |
| 1012 | } |
| 1013 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1014 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1015 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1016 | << Attr.getName() << PD->getType() << 1; |
| 1017 | return false; |
| 1018 | } |
| 1019 | } |
| 1020 | else { |
| 1021 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1022 | return false; |
| 1023 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1024 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1025 | return true; |
| 1026 | } |
| 1027 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1028 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1029 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1030 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1031 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1032 | |
| 1033 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1034 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1035 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1036 | D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1039 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1040 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1041 | |
| 1042 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1043 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1044 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1045 | return; |
| 1046 | } |
| 1047 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1048 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1049 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1050 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1051 | IdentifierInfo *II = Attr.getParameterName(); |
| 1052 | if (!II) |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1053 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1054 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1055 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1056 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1057 | if (!TypeRep) { |
| 1058 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1059 | return; |
| 1060 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1061 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1062 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1063 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1064 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1065 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1066 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1067 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1068 | return; |
| 1069 | } |
Argyrios Kyrtzidis | f1e7af3 | 2011-09-13 18:41:59 +0000 | [diff] [blame] | 1070 | D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 1071 | QT, Attr.getParameterLoc())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1074 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1075 | if (const RecordType *UT = T->getAsUnionType()) |
| 1076 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1077 | RecordDecl *UD = UT->getDecl(); |
| 1078 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1079 | itend = UD->field_end(); it != itend; ++it) { |
| 1080 | QualType QT = it->getType(); |
| 1081 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1082 | T = QT; |
| 1083 | return; |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1089 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1090 | if (!isFunctionOrMethod(D)) { |
| 1091 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1092 | << "alloc_size" << ExpectedFunctionOrMethod; |
| 1093 | return; |
| 1094 | } |
| 1095 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1096 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1097 | return; |
| 1098 | |
| 1099 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1100 | // counted from one. |
| 1101 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1102 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 1103 | |
| 1104 | SmallVector<unsigned, 8> SizeArgs; |
| 1105 | |
| 1106 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1107 | E = Attr.arg_end(); I!=E; ++I) { |
| 1108 | // The argument must be an integer constant expression. |
| 1109 | Expr *Ex = *I; |
| 1110 | llvm::APSInt ArgNum; |
| 1111 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1112 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1113 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1114 | << "alloc_size" << Ex->getSourceRange(); |
| 1115 | return; |
| 1116 | } |
| 1117 | |
| 1118 | uint64_t x = ArgNum.getZExtValue(); |
| 1119 | |
| 1120 | if (x < 1 || x > NumArgs) { |
| 1121 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1122 | << "alloc_size" << I.getArgNum() << Ex->getSourceRange(); |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | --x; |
| 1127 | if (HasImplicitThisParam) { |
| 1128 | if (x == 0) { |
| 1129 | S.Diag(Attr.getLoc(), |
| 1130 | diag::err_attribute_invalid_implicit_this_argument) |
| 1131 | << "alloc_size" << Ex->getSourceRange(); |
| 1132 | return; |
| 1133 | } |
| 1134 | --x; |
| 1135 | } |
| 1136 | |
| 1137 | // check if the function argument is of an integer type |
| 1138 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| 1139 | if (!T->isIntegerType()) { |
| 1140 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1141 | << "alloc_size" << Ex->getSourceRange(); |
| 1142 | return; |
| 1143 | } |
| 1144 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1145 | SizeArgs.push_back(x); |
| 1146 | } |
| 1147 | |
| 1148 | // check if the function returns a pointer |
| 1149 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1150 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 1151 | << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
| 1152 | } |
| 1153 | |
Nuno Lopes | 96c67d1 | 2012-06-18 16:27:56 +0000 | [diff] [blame] | 1154 | D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context, |
| 1155 | SizeArgs.data(), SizeArgs.size())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1158 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1159 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1160 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1161 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1162 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1163 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1164 | return; |
| 1165 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1166 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1167 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1168 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1169 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1170 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1171 | |
| 1172 | // The nonnull attribute only applies to pointers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1173 | SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1174 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1175 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 1176 | E=Attr.arg_end(); I!=E; ++I) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1177 | |
| 1178 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1179 | // The argument must be an integer constant expression. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1180 | Expr *Ex = *I; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1181 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1182 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1183 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1184 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1185 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1186 | return; |
| 1187 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1188 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1189 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1190 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1191 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1192 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 1193 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1194 | return; |
| 1195 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1196 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 1197 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1198 | if (HasImplicitThisParam) { |
| 1199 | if (x == 0) { |
| 1200 | S.Diag(Attr.getLoc(), |
| 1201 | diag::err_attribute_invalid_implicit_this_argument) |
| 1202 | << "nonnull" << Ex->getSourceRange(); |
| 1203 | return; |
| 1204 | } |
| 1205 | --x; |
| 1206 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1207 | |
| 1208 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1209 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1210 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1211 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1212 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1213 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1214 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1215 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1216 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1217 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1218 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1219 | NonNullArgs.push_back(x); |
| 1220 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1221 | |
| 1222 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1223 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1224 | if (NonNullArgs.empty()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1225 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) { |
| 1226 | QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1227 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1228 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 1229 | NonNullArgs.push_back(I); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1230 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1231 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1232 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1233 | if (NonNullArgs.empty()) { |
| 1234 | // Warn the trivial case only if attribute is not coming from a |
| 1235 | // macro instantiation. |
| 1236 | if (Attr.getLoc().isFileID()) |
| 1237 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1238 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1239 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1240 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1241 | |
| 1242 | unsigned* start = &NonNullArgs[0]; |
| 1243 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1244 | llvm::array_pod_sort(start, start + size); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1245 | D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1246 | size)); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1249 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1250 | // This attribute must be applied to a function declaration. |
| 1251 | // The first argument to the attribute must be a string, |
| 1252 | // the name of the resource, for example "malloc". |
| 1253 | // The following arguments must be argument indexes, the arguments must be |
| 1254 | // of integer type for Returns, otherwise of pointer type. |
| 1255 | // 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] | 1256 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1257 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (!AL.getParameterName()) { |
| 1260 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1261 | << AL.getName()->getName() << 1; |
| 1262 | return; |
| 1263 | } |
| 1264 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1265 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1266 | switch (AL.getKind()) { |
| 1267 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1268 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1269 | if (AL.getNumArgs() < 1) { |
| 1270 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1271 | return; |
| 1272 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1273 | break; |
| 1274 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1275 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1276 | if (AL.getNumArgs() < 1) { |
| 1277 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1278 | return; |
| 1279 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1280 | break; |
| 1281 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1282 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1283 | if (AL.getNumArgs() > 1) { |
| 1284 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1285 | << AL.getNumArgs() + 1; |
| 1286 | return; |
| 1287 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1288 | break; |
| 1289 | default: |
| 1290 | // This should never happen given how we are called. |
| 1291 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1294 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1295 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1296 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1297 | return; |
| 1298 | } |
| 1299 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1300 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1301 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1302 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1303 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1305 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1306 | |
| 1307 | // Normalize the argument, __foo__ becomes foo. |
| 1308 | if (Module.startswith("__") && Module.endswith("__")) |
| 1309 | Module = Module.substr(2, Module.size() - 4); |
| 1310 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1311 | SmallVector<unsigned, 10> OwnershipArgs; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1312 | |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1313 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 1314 | ++I) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1315 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1316 | Expr *IdxExpr = *I; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1317 | llvm::APSInt ArgNum(32); |
| 1318 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1319 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1320 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 1321 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 1322 | continue; |
| 1323 | } |
| 1324 | |
| 1325 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 1326 | |
| 1327 | if (x > NumArgs || x < 1) { |
| 1328 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1329 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 1330 | continue; |
| 1331 | } |
| 1332 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1333 | if (HasImplicitThisParam) { |
| 1334 | if (x == 0) { |
| 1335 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1336 | << "ownership" << IdxExpr->getSourceRange(); |
| 1337 | return; |
| 1338 | } |
| 1339 | --x; |
| 1340 | } |
| 1341 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1342 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1343 | case OwnershipAttr::Takes: |
| 1344 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1345 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1346 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1347 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1348 | // FIXME: Should also highlight argument in decl. |
| 1349 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1350 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1351 | << "pointer" |
| 1352 | << IdxExpr->getSourceRange(); |
| 1353 | continue; |
| 1354 | } |
| 1355 | break; |
| 1356 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1357 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1358 | if (AL.getNumArgs() > 1) { |
| 1359 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1360 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1361 | llvm::APSInt ArgNum(32); |
| 1362 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1363 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1364 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1365 | << "ownership_returns" << "integer" |
| 1366 | << IdxExpr->getSourceRange(); |
| 1367 | return; |
| 1368 | } |
| 1369 | } |
| 1370 | break; |
| 1371 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1372 | } // switch |
| 1373 | |
| 1374 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1375 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1376 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1377 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1378 | i != e; ++i) { |
| 1379 | if ((*i)->getOwnKind() != K) { |
| 1380 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1381 | I!=E; ++I) { |
| 1382 | if (x == *I) { |
| 1383 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1384 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | OwnershipArgs.push_back(x); |
| 1390 | } |
| 1391 | |
| 1392 | unsigned* start = OwnershipArgs.data(); |
| 1393 | unsigned size = OwnershipArgs.size(); |
| 1394 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1395 | |
| 1396 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 1397 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1398 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1399 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1400 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1401 | D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1402 | start, size)); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1405 | /// Whether this declaration has internal linkage for the purposes of |
| 1406 | /// things that want to complain about things not have internal linkage. |
| 1407 | static bool hasEffectivelyInternalLinkage(NamedDecl *D) { |
| 1408 | switch (D->getLinkage()) { |
| 1409 | case NoLinkage: |
| 1410 | case InternalLinkage: |
| 1411 | return true; |
| 1412 | |
| 1413 | // Template instantiations that go from external to unique-external |
| 1414 | // shouldn't get diagnosed. |
| 1415 | case UniqueExternalLinkage: |
| 1416 | return true; |
| 1417 | |
| 1418 | case ExternalLinkage: |
| 1419 | return false; |
| 1420 | } |
| 1421 | llvm_unreachable("unknown linkage kind!"); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1424 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1425 | // Check the attribute arguments. |
| 1426 | if (Attr.getNumArgs() > 1) { |
| 1427 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1428 | return; |
| 1429 | } |
| 1430 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1431 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1432 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1433 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1434 | return; |
| 1435 | } |
| 1436 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1437 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1438 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1439 | // gcc rejects |
| 1440 | // class c { |
| 1441 | // static int a __attribute__((weakref ("v2"))); |
| 1442 | // static int b() __attribute__((weakref ("f3"))); |
| 1443 | // }; |
| 1444 | // and ignores the attributes of |
| 1445 | // void f(void) { |
| 1446 | // static int a __attribute__((weakref ("v2"))); |
| 1447 | // } |
| 1448 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1449 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1450 | if (!Ctx->isFileContext()) { |
| 1451 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1452 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1453 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | // The GCC manual says |
| 1457 | // |
| 1458 | // At present, a declaration to which `weakref' is attached can only |
| 1459 | // be `static'. |
| 1460 | // |
| 1461 | // It also says |
| 1462 | // |
| 1463 | // Without a TARGET, |
| 1464 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1465 | // equivalent to `weak'. |
| 1466 | // |
| 1467 | // gcc 4.4.1 will accept |
| 1468 | // int a7 __attribute__((weakref)); |
| 1469 | // as |
| 1470 | // int a7 __attribute__((weak)); |
| 1471 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1472 | // it if this behaviour is actually used. |
| 1473 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1474 | if (!hasEffectivelyInternalLinkage(nd)) { |
| 1475 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1476 | return; |
| 1477 | } |
| 1478 | |
| 1479 | // GCC rejects |
| 1480 | // static ((alias ("y"), weakref)). |
| 1481 | // Should we? How to check that weakref is before or after alias? |
| 1482 | |
| 1483 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1484 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1485 | Arg = Arg->IgnoreParenCasts(); |
| 1486 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1487 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1488 | if (!Str || !Str->isAscii()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1489 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1490 | << "weakref" << 1; |
| 1491 | return; |
| 1492 | } |
| 1493 | // GCC will accept anything as the argument of weakref. Should we |
| 1494 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1495 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1496 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1499 | D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context)); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1502 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1503 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1504 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1505 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1506 | return; |
| 1507 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1508 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1509 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1510 | Arg = Arg->IgnoreParenCasts(); |
| 1511 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1512 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1513 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1514 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1515 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1516 | return; |
| 1517 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1518 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1519 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1520 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1521 | return; |
| 1522 | } |
| 1523 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1524 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1525 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1526 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1527 | Str->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1530 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1531 | // Check the attribute arguments. |
| 1532 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1533 | return; |
| 1534 | |
| 1535 | if (!isa<FunctionDecl>(D)) { |
| 1536 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1537 | << Attr.getName() << ExpectedFunction; |
| 1538 | return; |
| 1539 | } |
| 1540 | |
| 1541 | if (D->hasAttr<HotAttr>()) { |
| 1542 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1543 | << Attr.getName() << "hot"; |
| 1544 | return; |
| 1545 | } |
| 1546 | |
| 1547 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context)); |
| 1548 | } |
| 1549 | |
| 1550 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1551 | // Check the attribute arguments. |
| 1552 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1553 | return; |
| 1554 | |
| 1555 | if (!isa<FunctionDecl>(D)) { |
| 1556 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1557 | << Attr.getName() << ExpectedFunction; |
| 1558 | return; |
| 1559 | } |
| 1560 | |
| 1561 | if (D->hasAttr<ColdAttr>()) { |
| 1562 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1563 | << Attr.getName() << "cold"; |
| 1564 | return; |
| 1565 | } |
| 1566 | |
| 1567 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context)); |
| 1568 | } |
| 1569 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1570 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1571 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1572 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1573 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1574 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1575 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1576 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1577 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1578 | return; |
| 1579 | } |
| 1580 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1581 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1584 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1585 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1586 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1587 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1588 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1589 | return; |
| 1590 | } |
| 1591 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1592 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1593 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1594 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1595 | return; |
| 1596 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1597 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1598 | D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1601 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1602 | const AttributeList &Attr) { |
| 1603 | // Check the attribute arguments. |
| 1604 | if (Attr.getNumArgs() != 1) { |
| 1605 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1606 | return; |
| 1607 | } |
| 1608 | |
| 1609 | Expr *Arg = Attr.getArg(0); |
| 1610 | Arg = Arg->IgnoreParenCasts(); |
| 1611 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1612 | |
| 1613 | // Check that it is a string. |
| 1614 | if (!Str) { |
| 1615 | S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model"; |
| 1616 | return; |
| 1617 | } |
| 1618 | |
| 1619 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) { |
| 1620 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1621 | << Attr.getName() << ExpectedTLSVar; |
| 1622 | return; |
| 1623 | } |
| 1624 | |
| 1625 | // Check that the value. |
| 1626 | StringRef Model = Str->getString(); |
| 1627 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1628 | && Model != "initial-exec" && Model != "local-exec") { |
| 1629 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1630 | return; |
| 1631 | } |
| 1632 | |
| 1633 | D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context, |
| 1634 | Model)); |
| 1635 | } |
| 1636 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1637 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1638 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1639 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1640 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1641 | return; |
| 1642 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1644 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1646 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1647 | D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1648 | return; |
| 1649 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1652 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1655 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1656 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1657 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1658 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1659 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1660 | D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context)); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1663 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1664 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1665 | if (isa<VarDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1666 | D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context)); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1667 | else |
| 1668 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1669 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1672 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1673 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1674 | if (isa<VarDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1675 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context)); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1676 | else |
| 1677 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1678 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1681 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1682 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1683 | |
| 1684 | if (S.CheckNoReturnAttr(attr)) return; |
| 1685 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1686 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1687 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1688 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1689 | return; |
| 1690 | } |
| 1691 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1692 | D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1696 | if (attr.hasParameterOrArguments()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1697 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1698 | attr.setInvalid(); |
| 1699 | return true; |
| 1700 | } |
| 1701 | |
| 1702 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1705 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1706 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1707 | |
| 1708 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1709 | // because 'analyzer_noreturn' does not impact the type. |
| 1710 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1711 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1712 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1713 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1714 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1715 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1716 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1717 | && !VD->getType()->isFunctionPointerType())) { |
| 1718 | S.Diag(Attr.getLoc(), |
| 1719 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 1720 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1721 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1722 | return; |
| 1723 | } |
| 1724 | } |
| 1725 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1726 | D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1729 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1730 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1731 | /* |
| 1732 | Returning a Vector Class in Registers |
| 1733 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1734 | According to the PPU ABI specifications, a class with a single member of |
| 1735 | vector type is returned in memory when used as the return value of a function. |
| 1736 | This results in inefficient code when implementing vector classes. To return |
| 1737 | the value in a single vector register, add the vecreturn attribute to the |
| 1738 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1739 | |
| 1740 | Example: |
| 1741 | |
| 1742 | struct Vector |
| 1743 | { |
| 1744 | __vector float xyzw; |
| 1745 | } __attribute__((vecreturn)); |
| 1746 | |
| 1747 | Vector Add(Vector lhs, Vector rhs) |
| 1748 | { |
| 1749 | Vector result; |
| 1750 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1751 | return result; // This will be returned in a register |
| 1752 | } |
| 1753 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1754 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1755 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1756 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1757 | return; |
| 1758 | } |
| 1759 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1760 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1761 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1762 | return; |
| 1763 | } |
| 1764 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1765 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1766 | int count = 0; |
| 1767 | |
| 1768 | if (!isa<CXXRecordDecl>(record)) { |
| 1769 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1770 | return; |
| 1771 | } |
| 1772 | |
| 1773 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1774 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1775 | return; |
| 1776 | } |
| 1777 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1778 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1779 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1780 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1781 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1782 | return; |
| 1783 | } |
| 1784 | count++; |
| 1785 | } |
| 1786 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1787 | D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context)); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1790 | static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1791 | if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1792 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1793 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1794 | return; |
| 1795 | } |
| 1796 | // FIXME: Actually store the attribute on the declaration |
| 1797 | } |
| 1798 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1799 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1800 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1801 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1802 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1803 | return; |
| 1804 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1805 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1806 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1807 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1808 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1809 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1810 | return; |
| 1811 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1812 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1813 | D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1816 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1817 | const AttributeList &Attr) { |
| 1818 | // check the attribute arguments. |
| 1819 | if (Attr.hasParameterOrArguments()) { |
| 1820 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1821 | return; |
| 1822 | } |
| 1823 | |
| 1824 | if (!isa<FunctionDecl>(D)) { |
| 1825 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1826 | << Attr.getName() << ExpectedFunction; |
| 1827 | return; |
| 1828 | } |
| 1829 | |
| 1830 | D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context)); |
| 1831 | } |
| 1832 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1833 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1834 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1835 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1836 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1837 | return; |
| 1838 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1839 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1840 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1841 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1842 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1843 | return; |
| 1844 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1845 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1846 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1847 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1848 | return; |
| 1849 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1850 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1851 | D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1854 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1855 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1856 | if (Attr.getNumArgs() > 1) { |
| 1857 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1858 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1859 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1860 | |
| 1861 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1862 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1863 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1864 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1865 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1866 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1867 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1868 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1869 | return; |
| 1870 | } |
| 1871 | priority = Idx.getZExtValue(); |
| 1872 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1873 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1874 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1875 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1876 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1877 | return; |
| 1878 | } |
| 1879 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1880 | D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1881 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1884 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1885 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1886 | if (Attr.getNumArgs() > 1) { |
| 1887 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1888 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1889 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1890 | |
| 1891 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1892 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1893 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1894 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1895 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1896 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1897 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1898 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1899 | return; |
| 1900 | } |
| 1901 | priority = Idx.getZExtValue(); |
| 1902 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1903 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1904 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1905 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1906 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1907 | return; |
| 1908 | } |
| 1909 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1910 | D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1911 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1914 | template <typename AttrTy> |
| 1915 | static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr, |
| 1916 | const char *Name) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1917 | unsigned NumArgs = Attr.getNumArgs(); |
| 1918 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1919 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1920 | return; |
| 1921 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1922 | |
| 1923 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1924 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1925 | if (NumArgs == 1) { |
| 1926 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1927 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1928 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1929 | << Name; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1930 | return; |
| 1931 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1932 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1933 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1934 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1935 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str)); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1938 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1939 | const AttributeList &Attr) { |
| 1940 | unsigned NumArgs = Attr.getNumArgs(); |
| 1941 | if (NumArgs > 0) { |
| 1942 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1943 | return; |
| 1944 | } |
| 1945 | |
| 1946 | D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr( |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1947 | Attr.getRange(), S.Context)); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1950 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 1951 | const AttributeList &Attr) { |
| 1952 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1953 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1954 | return; |
| 1955 | } |
| 1956 | |
| 1957 | unsigned NumArgs = Attr.getNumArgs(); |
| 1958 | if (NumArgs > 0) { |
| 1959 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1960 | return; |
| 1961 | } |
| 1962 | |
| 1963 | D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context)); |
| 1964 | } |
| 1965 | |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1966 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1967 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 1968 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1969 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 1970 | return; |
| 1971 | } |
| 1972 | |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1973 | unsigned NumArgs = Attr.getNumArgs(); |
| 1974 | if (NumArgs > 0) { |
| 1975 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1976 | return; |
| 1977 | } |
| 1978 | |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1979 | D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr( |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1980 | Attr.getRange(), S.Context)); |
| 1981 | } |
| 1982 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1983 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1984 | IdentifierInfo *Platform, |
| 1985 | VersionTuple Introduced, |
| 1986 | VersionTuple Deprecated, |
| 1987 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1988 | StringRef PlatformName |
| 1989 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1990 | if (PlatformName.empty()) |
| 1991 | PlatformName = Platform->getName(); |
| 1992 | |
| 1993 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1994 | // of these steps are needed). |
| 1995 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1996 | !(Introduced <= Deprecated)) { |
| 1997 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1998 | << 1 << PlatformName << Deprecated.getAsString() |
| 1999 | << 0 << Introduced.getAsString(); |
| 2000 | return true; |
| 2001 | } |
| 2002 | |
| 2003 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2004 | !(Introduced <= Obsoleted)) { |
| 2005 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2006 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2007 | << 0 << Introduced.getAsString(); |
| 2008 | return true; |
| 2009 | } |
| 2010 | |
| 2011 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2012 | !(Deprecated <= Obsoleted)) { |
| 2013 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2014 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2015 | << 1 << Deprecated.getAsString(); |
| 2016 | return true; |
| 2017 | } |
| 2018 | |
| 2019 | return false; |
| 2020 | } |
| 2021 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2022 | AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range, |
| 2023 | IdentifierInfo *Platform, |
| 2024 | VersionTuple Introduced, |
| 2025 | VersionTuple Deprecated, |
| 2026 | VersionTuple Obsoleted, |
| 2027 | bool IsUnavailable, |
| 2028 | StringRef Message) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2029 | VersionTuple MergedIntroduced = Introduced; |
| 2030 | VersionTuple MergedDeprecated = Deprecated; |
| 2031 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2032 | bool FoundAny = false; |
| 2033 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2034 | if (D->hasAttrs()) { |
| 2035 | AttrVec &Attrs = D->getAttrs(); |
| 2036 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2037 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2038 | if (!OldAA) { |
| 2039 | ++i; |
| 2040 | continue; |
| 2041 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2042 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2043 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2044 | if (OldPlatform != Platform) { |
| 2045 | ++i; |
| 2046 | continue; |
| 2047 | } |
| 2048 | |
| 2049 | FoundAny = true; |
| 2050 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2051 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2052 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2053 | bool OldIsUnavailable = OldAA->getUnavailable(); |
| 2054 | StringRef OldMessage = OldAA->getMessage(); |
| 2055 | |
| 2056 | if ((!OldIntroduced.empty() && !Introduced.empty() && |
| 2057 | OldIntroduced != Introduced) || |
| 2058 | (!OldDeprecated.empty() && !Deprecated.empty() && |
| 2059 | OldDeprecated != Deprecated) || |
| 2060 | (!OldObsoleted.empty() && !Obsoleted.empty() && |
| 2061 | OldObsoleted != Obsoleted) || |
| 2062 | (OldIsUnavailable != IsUnavailable) || |
| 2063 | (OldMessage != Message)) { |
| 2064 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2065 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2066 | Attrs.erase(Attrs.begin() + i); |
| 2067 | --e; |
| 2068 | continue; |
| 2069 | } |
| 2070 | |
| 2071 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2072 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2073 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2074 | |
| 2075 | if (MergedIntroduced2.empty()) |
| 2076 | MergedIntroduced2 = OldIntroduced; |
| 2077 | if (MergedDeprecated2.empty()) |
| 2078 | MergedDeprecated2 = OldDeprecated; |
| 2079 | if (MergedObsoleted2.empty()) |
| 2080 | MergedObsoleted2 = OldObsoleted; |
| 2081 | |
| 2082 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2083 | MergedIntroduced2, MergedDeprecated2, |
| 2084 | MergedObsoleted2)) { |
| 2085 | Attrs.erase(Attrs.begin() + i); |
| 2086 | --e; |
| 2087 | continue; |
| 2088 | } |
| 2089 | |
| 2090 | MergedIntroduced = MergedIntroduced2; |
| 2091 | MergedDeprecated = MergedDeprecated2; |
| 2092 | MergedObsoleted = MergedObsoleted2; |
| 2093 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2094 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | if (FoundAny && |
| 2098 | MergedIntroduced == Introduced && |
| 2099 | MergedDeprecated == Deprecated && |
| 2100 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2101 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2102 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2103 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2104 | MergedDeprecated, MergedObsoleted)) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2105 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2106 | Introduced, Deprecated, |
| 2107 | Obsoleted, IsUnavailable, Message); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2108 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2109 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2112 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2113 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2114 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2115 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
| 2116 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2117 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2118 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2119 | << Platform; |
| 2120 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2121 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2122 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2123 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2124 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2125 | StringRef Str; |
| 2126 | const StringLiteral *SE = |
| 2127 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2128 | if (SE) |
| 2129 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2130 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2131 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(), |
| 2132 | Platform, |
| 2133 | Introduced.Version, |
| 2134 | Deprecated.Version, |
| 2135 | Obsoleted.Version, |
| 2136 | IsUnavailable, Str); |
| 2137 | if (NewAttr) |
| 2138 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2141 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
| 2142 | VisibilityAttr::VisibilityType Vis) { |
Rafael Espindola | dd44f34 | 2012-05-10 03:01:34 +0000 | [diff] [blame] | 2143 | if (isa<TypedefNameDecl>(D)) { |
| 2144 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility"; |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2145 | return NULL; |
Rafael Espindola | dd44f34 | 2012-05-10 03:01:34 +0000 | [diff] [blame] | 2146 | } |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2147 | VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>(); |
| 2148 | if (ExistingAttr) { |
| 2149 | VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility(); |
| 2150 | if (ExistingVis == Vis) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2151 | return NULL; |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2152 | Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2153 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2154 | D->dropAttr<VisibilityAttr>(); |
| 2155 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2156 | return ::new (Context) VisibilityAttr(Range, Context, Vis); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2159 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2160 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2161 | if(!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2162 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2163 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2164 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2165 | Arg = Arg->IgnoreParenCasts(); |
| 2166 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2167 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2168 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2169 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2170 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2171 | return; |
| 2172 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2173 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2174 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2175 | VisibilityAttr::VisibilityType type; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2176 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2177 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2178 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2179 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2180 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2181 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2182 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2183 | else if (TypeStr == "protected") { |
| 2184 | // Complain about attempts to use protected visibility on targets |
| 2185 | // (like Darwin) that don't support it. |
| 2186 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2187 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2188 | type = VisibilityAttr::Default; |
| 2189 | } else { |
| 2190 | type = VisibilityAttr::Protected; |
| 2191 | } |
| 2192 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2193 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2194 | return; |
| 2195 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2196 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2197 | VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type); |
| 2198 | if (NewAttr) |
| 2199 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2202 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2203 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2204 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2205 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2206 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2207 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2208 | return; |
| 2209 | } |
| 2210 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2211 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2212 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 2213 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2214 | << "objc_method_family" << 1; |
| 2215 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2216 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2217 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2218 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2219 | return; |
| 2220 | } |
| 2221 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2222 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2223 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2224 | if (param == "none") |
| 2225 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2226 | else if (param == "alloc") |
| 2227 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2228 | else if (param == "copy") |
| 2229 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2230 | else if (param == "init") |
| 2231 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2232 | else if (param == "mutableCopy") |
| 2233 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2234 | else if (param == "new") |
| 2235 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2236 | else { |
| 2237 | // Just warn and ignore it. This is future-proof against new |
| 2238 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2239 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2240 | return; |
| 2241 | } |
| 2242 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2243 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2244 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2245 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2246 | << method->getResultType(); |
| 2247 | // Ignore the attribute. |
| 2248 | return; |
| 2249 | } |
| 2250 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2251 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2252 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2255 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2256 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2257 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2258 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2259 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2260 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2261 | if (OCI == 0) { |
| 2262 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 2263 | return; |
| 2264 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2265 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2266 | D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2269 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2270 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2271 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2272 | return; |
| 2273 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2274 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2275 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2276 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2277 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2278 | return; |
| 2279 | } |
| 2280 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2281 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2282 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2283 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2284 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2285 | return; |
| 2286 | } |
| 2287 | } |
| 2288 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2289 | // It is okay to include this attribute on properties, e.g.: |
| 2290 | // |
| 2291 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2292 | // |
| 2293 | // In this case it follows tradition and suppresses an error in the above |
| 2294 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2295 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2296 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2297 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context)); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2300 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2301 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2302 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2303 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2304 | return; |
| 2305 | } |
| 2306 | |
| 2307 | if (!isa<FunctionDecl>(D)) { |
| 2308 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2309 | return; |
| 2310 | } |
| 2311 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2312 | D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2315 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2316 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2317 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2318 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2319 | return; |
| 2320 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2321 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2322 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2323 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2324 | return; |
| 2325 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2326 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2327 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2328 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2329 | type = BlocksAttr::ByRef; |
| 2330 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2331 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2332 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2333 | return; |
| 2334 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2335 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2336 | D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type)); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2339 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2340 | // check the attribute arguments. |
| 2341 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2342 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2343 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2346 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2347 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2348 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2349 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2350 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2351 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2352 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2353 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2354 | return; |
| 2355 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2356 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2357 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2358 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2359 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2360 | return; |
| 2361 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2362 | |
| 2363 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2366 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2367 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2368 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2369 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2370 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2371 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2372 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2373 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2374 | return; |
| 2375 | } |
| 2376 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2377 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2378 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2379 | // FIXME: This error message could be improved, it would be nice |
| 2380 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2381 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2382 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2383 | return; |
| 2384 | } |
| 2385 | } |
| 2386 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2387 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2388 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2389 | if (isa<FunctionNoProtoType>(FT)) { |
| 2390 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2391 | return; |
| 2392 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2393 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2394 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2395 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2396 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2397 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2398 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2399 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2400 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2401 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2402 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2403 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2404 | if (!BD->isVariadic()) { |
| 2405 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2406 | return; |
| 2407 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2408 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2409 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2410 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2411 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2412 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2413 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2414 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2415 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2416 | return; |
| 2417 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2418 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2419 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2420 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2421 | return; |
| 2422 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2423 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2424 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2425 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2426 | return; |
| 2427 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2428 | D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2429 | nullPos)); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2432 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2433 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2434 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2435 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2436 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2437 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2438 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2439 | << Attr.getName() << ExpectedFunctionOrMethod; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2440 | return; |
| 2441 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2442 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2443 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2444 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2445 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2446 | return; |
| 2447 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2448 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2449 | if (MD->getResultType()->isVoidType()) { |
| 2450 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2451 | << Attr.getName() << 1; |
| 2452 | return; |
| 2453 | } |
| 2454 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2455 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2458 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2459 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2460 | if (Attr.hasParameterOrArguments()) { |
| 2461 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2462 | return; |
| 2463 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2464 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2465 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2466 | if (isa<CXXRecordDecl>(D)) { |
| 2467 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2468 | return; |
| 2469 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2470 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2471 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2472 | return; |
| 2473 | } |
| 2474 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2475 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2476 | |
| 2477 | // 'weak' only applies to declarations with external linkage. |
| 2478 | if (hasEffectivelyInternalLinkage(nd)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2479 | S.Diag(Attr.getLoc(), diag::err_attribute_weak_static); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2480 | return; |
| 2481 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2482 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2483 | nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2486 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2487 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2488 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2489 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2490 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2491 | |
| 2492 | // weak_import only applies to variable & function declarations. |
| 2493 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2494 | if (!D->canBeWeakImported(isDef)) { |
| 2495 | if (isDef) |
| 2496 | S.Diag(Attr.getLoc(), |
| 2497 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 2498 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2499 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2500 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2501 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2502 | // Nothing to warn about here. |
| 2503 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2504 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2505 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2506 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2507 | return; |
| 2508 | } |
| 2509 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2510 | D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2513 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2514 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2515 | const AttributeList &Attr) { |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2516 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2517 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2518 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2519 | // Attribute has 3 arguments. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2520 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2521 | |
| 2522 | unsigned WGSize[3]; |
| 2523 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2524 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2525 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2526 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2527 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2528 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2529 | << Attr.getName()->getName() << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2530 | return; |
| 2531 | } |
| 2532 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2533 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2534 | |
| 2535 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2536 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2537 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2538 | if (!(A->getXDim() == WGSize[0] && |
| 2539 | A->getYDim() == WGSize[1] && |
| 2540 | A->getZDim() == WGSize[2])) { |
| 2541 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2542 | Attr.getName(); |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2547 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2548 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2549 | if (!(A->getXDim() == WGSize[0] && |
| 2550 | A->getYDim() == WGSize[1] && |
| 2551 | A->getZDim() == WGSize[2])) { |
| 2552 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2553 | Attr.getName(); |
| 2554 | } |
| 2555 | } |
| 2556 | |
| 2557 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2558 | D->addAttr(::new (S.Context) |
| 2559 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
| 2560 | WGSize[0], WGSize[1], WGSize[2])); |
| 2561 | else |
| 2562 | D->addAttr(::new (S.Context) |
| 2563 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
| 2564 | WGSize[0], WGSize[1], WGSize[2])); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2567 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
| 2568 | StringRef Name) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2569 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2570 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2571 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2572 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2573 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2574 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2575 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2576 | return ::new (Context) SectionAttr(Range, Context, Name); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2579 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2580 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2581 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2582 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2583 | |
| 2584 | // Make sure that there is a string literal as the sections's single |
| 2585 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2586 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2587 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2588 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2589 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2590 | return; |
| 2591 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2593 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2594 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2595 | if (!Error.empty()) { |
| 2596 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2597 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2598 | return; |
| 2599 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2601 | // This attribute cannot be applied to local variables. |
| 2602 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2603 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2604 | return; |
| 2605 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2606 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
| 2607 | SE->getString()); |
| 2608 | if (NewAttr) |
| 2609 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2612 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2613 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2614 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2615 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2616 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2617 | return; |
| 2618 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2619 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2620 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2621 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2622 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2623 | } else { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2624 | D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2625 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2628 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2629 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2630 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2631 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2632 | return; |
| 2633 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2634 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2635 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2636 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2637 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2638 | } else { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2639 | D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2640 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2643 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2644 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2645 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2646 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2647 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2648 | D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2651 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2652 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2653 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2654 | return; |
| 2655 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2656 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2657 | if (Attr.getNumArgs() != 0) { |
| 2658 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2659 | return; |
| 2660 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2661 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2662 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2663 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2664 | if (!VD || !VD->hasLocalStorage()) { |
| 2665 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2666 | return; |
| 2667 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2668 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2669 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2670 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2671 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2672 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2673 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2674 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2675 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2676 | Attr.getParameterName(); |
| 2677 | return; |
| 2678 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2679 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2680 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2681 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2682 | S.Diag(Attr.getParameterLoc(), |
| 2683 | diag::err_attribute_cleanup_arg_not_function) |
| 2684 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2685 | return; |
| 2686 | } |
| 2687 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2688 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2689 | S.Diag(Attr.getParameterLoc(), |
| 2690 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2691 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2692 | return; |
| 2693 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2694 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2695 | // We're currently more strict than GCC about what function types we accept. |
| 2696 | // If this ever proves to be a problem it should be easy to fix. |
| 2697 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2698 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2699 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2700 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2701 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2702 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2703 | Attr.getParameterName() << ParamTy << Ty; |
| 2704 | return; |
| 2705 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2706 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2707 | D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD)); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2708 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2711 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 2712 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2713 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2714 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2715 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2716 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2717 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2718 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2719 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2720 | return; |
| 2721 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2722 | |
| 2723 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2724 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2725 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2726 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2727 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2728 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2729 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2730 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2731 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2732 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2733 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2734 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2735 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2736 | return; |
| 2737 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2738 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2739 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 2740 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 2741 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2742 | return; |
| 2743 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2744 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2745 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2746 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2747 | if (HasImplicitThisParam) { |
| 2748 | if (ArgIdx == 0) { |
| 2749 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 2750 | << "format_arg" << IdxExpr->getSourceRange(); |
| 2751 | return; |
| 2752 | } |
| 2753 | ArgIdx--; |
| 2754 | } |
| 2755 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2756 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2757 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2758 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2759 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2760 | if (not_nsstring_type && |
| 2761 | !isCFStringType(Ty, S.Context) && |
| 2762 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2763 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2764 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2765 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2766 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2767 | << IdxExpr->getSourceRange(); |
| 2768 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2769 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2770 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2771 | if (!isNSStringType(Ty, S.Context) && |
| 2772 | !isCFStringType(Ty, S.Context) && |
| 2773 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2774 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2775 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2776 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2777 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2778 | << IdxExpr->getSourceRange(); |
| 2779 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2782 | D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context, |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2783 | Idx.getZExtValue())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2786 | enum FormatAttrKind { |
| 2787 | CFStringFormat, |
| 2788 | NSStringFormat, |
| 2789 | StrftimeFormat, |
| 2790 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2791 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2792 | InvalidFormat |
| 2793 | }; |
| 2794 | |
| 2795 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2796 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2797 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2798 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2799 | // Check for formats that get handled specially. |
| 2800 | .Case("NSString", NSStringFormat) |
| 2801 | .Case("CFString", CFStringFormat) |
| 2802 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2803 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2804 | // Otherwise, check for supported formats. |
| 2805 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2806 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2807 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2808 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2809 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2810 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2813 | /// Handle __attribute__((init_priority(priority))) attributes based on |
| 2814 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2815 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2816 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2817 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2818 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2819 | return; |
| 2820 | } |
| 2821 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2822 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2823 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2824 | Attr.setInvalid(); |
| 2825 | return; |
| 2826 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2827 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2828 | if (S.Context.getAsArrayType(T)) |
| 2829 | T = S.Context.getBaseElementType(T); |
| 2830 | if (!T->getAs<RecordType>()) { |
| 2831 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2832 | Attr.setInvalid(); |
| 2833 | return; |
| 2834 | } |
| 2835 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2836 | if (Attr.getNumArgs() != 1) { |
| 2837 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2838 | Attr.setInvalid(); |
| 2839 | return; |
| 2840 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2841 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2842 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2843 | llvm::APSInt priority(32); |
| 2844 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 2845 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 2846 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2847 | << "init_priority" << priorityExpr->getSourceRange(); |
| 2848 | Attr.setInvalid(); |
| 2849 | return; |
| 2850 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 2851 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2852 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2853 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 2854 | << priorityExpr->getSourceRange(); |
| 2855 | Attr.setInvalid(); |
| 2856 | return; |
| 2857 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2858 | D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2859 | prioritynum)); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2862 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
| 2863 | int FormatIdx, int FirstArg) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2864 | // Check whether we already have an equivalent format attribute. |
| 2865 | for (specific_attr_iterator<FormatAttr> |
| 2866 | i = D->specific_attr_begin<FormatAttr>(), |
| 2867 | e = D->specific_attr_end<FormatAttr>(); |
| 2868 | i != e ; ++i) { |
| 2869 | FormatAttr *f = *i; |
| 2870 | if (f->getType() == Format && |
| 2871 | f->getFormatIdx() == FormatIdx && |
| 2872 | f->getFirstArg() == FirstArg) { |
| 2873 | // If we don't have a valid location for this attribute, adopt the |
| 2874 | // location. |
| 2875 | if (f->getLocation().isInvalid()) |
| 2876 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2877 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2878 | } |
| 2879 | } |
| 2880 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2881 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2882 | FirstArg); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2885 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 2886 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2887 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2888 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2889 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2890 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2891 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2892 | return; |
| 2893 | } |
| 2894 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2895 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2896 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2897 | return; |
| 2898 | } |
| 2899 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2900 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2901 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2902 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2903 | return; |
| 2904 | } |
| 2905 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2906 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2907 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2908 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2909 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2910 | unsigned FirstIdx = 1; |
| 2911 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2912 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2913 | |
| 2914 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2915 | if (Format.startswith("__") && Format.endswith("__")) |
| 2916 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2917 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2918 | // Check for supported formats. |
| 2919 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2920 | |
| 2921 | if (Kind == IgnoredFormat) |
| 2922 | return; |
| 2923 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2924 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2925 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2926 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2927 | return; |
| 2928 | } |
| 2929 | |
| 2930 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2931 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2932 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2933 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2934 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2935 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2936 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2937 | return; |
| 2938 | } |
| 2939 | |
| 2940 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2941 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2942 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2943 | return; |
| 2944 | } |
| 2945 | |
| 2946 | // FIXME: Do we need to bounds check? |
| 2947 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2948 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2949 | if (HasImplicitThisParam) { |
| 2950 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2951 | S.Diag(Attr.getLoc(), |
| 2952 | diag::err_format_attribute_implicit_this_format_string) |
| 2953 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2954 | return; |
| 2955 | } |
| 2956 | ArgIdx--; |
| 2957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2958 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2959 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2960 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2961 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2962 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2963 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2964 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2965 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2966 | return; |
| 2967 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2968 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2969 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2970 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2971 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2972 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2973 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2974 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2975 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2976 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2977 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2978 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2979 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2980 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2981 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2982 | return; |
| 2983 | } |
| 2984 | |
| 2985 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2986 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2987 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2988 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 2989 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2990 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2991 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2992 | return; |
| 2993 | } |
| 2994 | |
| 2995 | // check if the function is variadic if the 3rd argument non-zero |
| 2996 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2997 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2998 | ++NumArgs; // +1 for ... |
| 2999 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3000 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3001 | return; |
| 3002 | } |
| 3003 | } |
| 3004 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3005 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3006 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3007 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3008 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3009 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3010 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3011 | return; |
| 3012 | } |
| 3013 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3014 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3015 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3016 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3017 | return; |
| 3018 | } |
| 3019 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3020 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 3021 | Idx.getZExtValue(), |
| 3022 | FirstArg.getZExtValue()); |
| 3023 | if (NewAttr) |
| 3024 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3027 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3028 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3029 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3030 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3031 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3032 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3033 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3034 | // Try to find the underlying union declaration. |
| 3035 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3036 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3037 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3038 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3039 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3040 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3041 | |
| 3042 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3043 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3044 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3045 | return; |
| 3046 | } |
| 3047 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3048 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3049 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3050 | diag::warn_transparent_union_attribute_not_definition); |
| 3051 | return; |
| 3052 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3053 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3054 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3055 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3056 | if (Field == FieldEnd) { |
| 3057 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3058 | return; |
| 3059 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3060 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3061 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3062 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3063 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3064 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3065 | diag::warn_transparent_union_attribute_floating) |
| 3066 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3067 | return; |
| 3068 | } |
| 3069 | |
| 3070 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3071 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3072 | for (; Field != FieldEnd; ++Field) { |
| 3073 | QualType FieldType = Field->getType(); |
| 3074 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3075 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3076 | // Warn if we drop the attribute. |
| 3077 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3078 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3079 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3080 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3081 | diag::warn_transparent_union_attribute_field_size_align) |
| 3082 | << isSize << Field->getDeclName() << FieldBits; |
| 3083 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3084 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3085 | diag::note_transparent_union_first_field_size_align) |
| 3086 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3087 | return; |
| 3088 | } |
| 3089 | } |
| 3090 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3091 | RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3094 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3095 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3096 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3097 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3098 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3099 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3100 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
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 | // Make sure that there is a string literal as the annotation's single |
| 3103 | // argument. |
| 3104 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3105 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3106 | return; |
| 3107 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3108 | |
| 3109 | // Don't duplicate annotations that are already set. |
| 3110 | for (specific_attr_iterator<AnnotateAttr> |
| 3111 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3112 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3113 | if ((*i)->getAnnotation() == SE->getString()) |
| 3114 | return; |
| 3115 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3116 | D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3117 | SE->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3120 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3121 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3122 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3123 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3124 | return; |
| 3125 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3126 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3127 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 3128 | // than GNU's, and should error out when it is used to specify a |
| 3129 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3130 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3131 | if (Attr.getNumArgs() == 0) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3132 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3133 | true, 0, Attr.isDeclspecAttribute())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3134 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3135 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3136 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3137 | S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0), |
| 3138 | Attr.isDeclspecAttribute()); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3141 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3142 | bool isDeclSpec) { |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 3143 | // FIXME: Handle pack-expansions here. |
| 3144 | if (DiagnoseUnexpandedParameterPack(E)) |
| 3145 | return; |
| 3146 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3147 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3148 | // Save dependent expressions in the AST to be instantiated. |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3149 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E, |
| 3150 | isDeclSpec)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3151 | return; |
| 3152 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3153 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3154 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3155 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3156 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3157 | ExprResult ICE |
| 3158 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3159 | diag::err_aligned_attribute_argument_not_int, |
| 3160 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3161 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3162 | return; |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3163 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3164 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3165 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3166 | return; |
| 3167 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3168 | if (isDeclSpec) { |
| 3169 | // We've already verified it's a power of 2, now let's make sure it's |
| 3170 | // 8192 or less. |
| 3171 | if (Alignment.getZExtValue() > 8192) { |
| 3172 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
| 3173 | << E->getSourceRange(); |
| 3174 | return; |
| 3175 | } |
| 3176 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3177 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3178 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(), |
| 3179 | isDeclSpec)); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3182 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
| 3183 | bool isDeclSpec) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3184 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3185 | // FIXME: Perform checking of type validity |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3186 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3187 | isDeclSpec)); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3188 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3189 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3190 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3191 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3192 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3193 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3194 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3195 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3196 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3197 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3198 | // This attribute isn't documented, but glibc uses it. It changes |
| 3199 | // the width of an int or unsigned int to the specified size. |
| 3200 | |
| 3201 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3202 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3203 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3204 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3205 | |
| 3206 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3207 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3208 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3209 | return; |
| 3210 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3211 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3212 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3213 | |
| 3214 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3215 | if (Str.startswith("__") && Str.endswith("__")) |
| 3216 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3217 | |
| 3218 | unsigned DestWidth = 0; |
| 3219 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3220 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3221 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3222 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3223 | switch (Str[0]) { |
| 3224 | case 'Q': DestWidth = 8; break; |
| 3225 | case 'H': DestWidth = 16; break; |
| 3226 | case 'S': DestWidth = 32; break; |
| 3227 | case 'D': DestWidth = 64; break; |
| 3228 | case 'X': DestWidth = 96; break; |
| 3229 | case 'T': DestWidth = 128; break; |
| 3230 | } |
| 3231 | if (Str[1] == 'F') { |
| 3232 | IntegerMode = false; |
| 3233 | } else if (Str[1] == 'C') { |
| 3234 | IntegerMode = false; |
| 3235 | ComplexMode = true; |
| 3236 | } else if (Str[1] != 'I') { |
| 3237 | DestWidth = 0; |
| 3238 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3239 | break; |
| 3240 | case 4: |
| 3241 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3242 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3243 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3244 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3245 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3246 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3247 | break; |
| 3248 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3249 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3250 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3251 | break; |
| 3252 | } |
| 3253 | |
| 3254 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3255 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3256 | OldTy = TD->getUnderlyingType(); |
| 3257 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3258 | OldTy = VD->getType(); |
| 3259 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3260 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3261 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3262 | return; |
| 3263 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3264 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3265 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3266 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3267 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3268 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3269 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3270 | } else if (ComplexMode) { |
| 3271 | if (!OldTy->isComplexType()) |
| 3272 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3273 | } else { |
| 3274 | if (!OldTy->isFloatingType()) |
| 3275 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3276 | } |
| 3277 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3278 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3279 | // and friends, at least with glibc. |
| 3280 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3281 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3282 | // FIXME: Make sure floating-point mappings are accurate |
| 3283 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3284 | QualType NewTy; |
| 3285 | switch (DestWidth) { |
| 3286 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3287 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3288 | return; |
| 3289 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3290 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3291 | return; |
| 3292 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3293 | if (!IntegerMode) { |
| 3294 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3295 | return; |
| 3296 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3297 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3298 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3299 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3300 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3301 | break; |
| 3302 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3303 | if (!IntegerMode) { |
| 3304 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3305 | return; |
| 3306 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3307 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3308 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3309 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3310 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3311 | break; |
| 3312 | case 32: |
| 3313 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3314 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3315 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3316 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3317 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3318 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3319 | break; |
| 3320 | case 64: |
| 3321 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3322 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3323 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3324 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3325 | NewTy = S.Context.LongTy; |
| 3326 | else |
| 3327 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3328 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3329 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3330 | NewTy = S.Context.UnsignedLongTy; |
| 3331 | else |
| 3332 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3333 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3334 | case 96: |
| 3335 | NewTy = S.Context.LongDoubleTy; |
| 3336 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3337 | case 128: |
| 3338 | if (!IntegerMode) { |
| 3339 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3340 | return; |
| 3341 | } |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 3342 | if (OldTy->isSignedIntegerType()) |
| 3343 | NewTy = S.Context.Int128Ty; |
| 3344 | else |
| 3345 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3346 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3349 | if (ComplexMode) { |
| 3350 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | // Install the new type. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3354 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3355 | // FIXME: preserve existing source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3356 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3357 | } else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3358 | cast<ValueDecl>(D)->setType(NewTy); |
| 3359 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3360 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3361 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3362 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3363 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3364 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3365 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3366 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3367 | if (!VD->hasGlobalStorage()) |
| 3368 | S.Diag(Attr.getLoc(), |
| 3369 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3370 | << Attr.getName(); |
| 3371 | } else if (!isFunctionOrMethod(D)) { |
| 3372 | S.Diag(Attr.getLoc(), |
| 3373 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3374 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3375 | return; |
| 3376 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3377 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3378 | D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3381 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3382 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3383 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3384 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3385 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3386 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3387 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3388 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3389 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3390 | return; |
| 3391 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3392 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3393 | D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3396 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3397 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3398 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3399 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3400 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3401 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3402 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3403 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3404 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3405 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3406 | return; |
| 3407 | } |
| 3408 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3409 | D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3410 | S.Context)); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3413 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3414 | if (S.LangOpts.CUDA) { |
| 3415 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3416 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3417 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3418 | return; |
| 3419 | } |
| 3420 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3421 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3422 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3423 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3424 | return; |
| 3425 | } |
| 3426 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3427 | D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3428 | } else { |
| 3429 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3430 | } |
| 3431 | } |
| 3432 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3433 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3434 | if (S.LangOpts.CUDA) { |
| 3435 | // check the attribute arguments. |
| 3436 | if (Attr.getNumArgs() != 0) { |
| 3437 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3438 | return; |
| 3439 | } |
| 3440 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3441 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3442 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3443 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3444 | return; |
| 3445 | } |
| 3446 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3447 | D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3448 | } else { |
| 3449 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3450 | } |
| 3451 | } |
| 3452 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3453 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3454 | if (S.LangOpts.CUDA) { |
| 3455 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3456 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3457 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3458 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3459 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3460 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3461 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3462 | return; |
| 3463 | } |
| 3464 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3465 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3466 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3467 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3468 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 3469 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3470 | << FD->getType() |
| 3471 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 3472 | "void"); |
| 3473 | } else { |
| 3474 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3475 | << FD->getType(); |
| 3476 | } |
| 3477 | return; |
| 3478 | } |
| 3479 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3480 | D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3481 | } else { |
| 3482 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3483 | } |
| 3484 | } |
| 3485 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3486 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3487 | if (S.LangOpts.CUDA) { |
| 3488 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3489 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3490 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3491 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3492 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3493 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3494 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3495 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3496 | return; |
| 3497 | } |
| 3498 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3499 | D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3500 | } else { |
| 3501 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3502 | } |
| 3503 | } |
| 3504 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3505 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3506 | if (S.LangOpts.CUDA) { |
| 3507 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3508 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3509 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3510 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3511 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3512 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3513 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3514 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3515 | return; |
| 3516 | } |
| 3517 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3518 | D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3519 | } else { |
| 3520 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3521 | } |
| 3522 | } |
| 3523 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3524 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3525 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3526 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3527 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3528 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3529 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3530 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3531 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3532 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3533 | return; |
| 3534 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3535 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3536 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3537 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3538 | return; |
| 3539 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3540 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3541 | D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3544 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3545 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3546 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3547 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3548 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3549 | CallingConv CC; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3550 | if (S.CheckCallingConvAttr(Attr, CC)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3551 | return; |
| 3552 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3553 | if (!isa<ObjCMethodDecl>(D)) { |
| 3554 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3555 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3556 | return; |
| 3557 | } |
| 3558 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3559 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3560 | case AttributeList::AT_FastCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3561 | D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3562 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3563 | case AttributeList::AT_StdCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3564 | D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3565 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3566 | case AttributeList::AT_ThisCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3567 | D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3568 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3569 | case AttributeList::AT_CDecl: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3570 | D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3571 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3572 | case AttributeList::AT_Pascal: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3573 | D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context)); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3574 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3575 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3576 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3577 | switch (CC) { |
| 3578 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3579 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3580 | break; |
| 3581 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3582 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3583 | break; |
| 3584 | default: |
| 3585 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3588 | D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS)); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3589 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3590 | default: |
| 3591 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3592 | } |
| 3593 | } |
| 3594 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3595 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 3596 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3597 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3600 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { |
| 3601 | if (attr.isInvalid()) |
| 3602 | return true; |
| 3603 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3604 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 3605 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
| 3606 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3607 | attr.setInvalid(); |
| 3608 | return true; |
| 3609 | } |
| 3610 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3611 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3612 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3613 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3614 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3615 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3616 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3617 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3618 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 3619 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3620 | Expr *Arg = attr.getArg(0); |
| 3621 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3622 | if (!Str || !Str->isAscii()) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3623 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3624 | << "pcs" << 1; |
| 3625 | attr.setInvalid(); |
| 3626 | return true; |
| 3627 | } |
| 3628 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3629 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3630 | if (StrRef == "aapcs") { |
| 3631 | CC = CC_AAPCS; |
| 3632 | break; |
| 3633 | } else if (StrRef == "aapcs-vfp") { |
| 3634 | CC = CC_AAPCS_VFP; |
| 3635 | break; |
| 3636 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3637 | |
| 3638 | attr.setInvalid(); |
| 3639 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3640 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3641 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3642 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | return false; |
| 3646 | } |
| 3647 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3648 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3649 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3650 | |
| 3651 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3652 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3653 | return; |
| 3654 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3655 | if (!isa<ObjCMethodDecl>(D)) { |
| 3656 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3657 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3658 | return; |
| 3659 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3660 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3661 | D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3665 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3666 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3667 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3668 | return true; |
| 3669 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3670 | if (Attr.getNumArgs() != 1) { |
| 3671 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3672 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3673 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3674 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3675 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3676 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3677 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3678 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3679 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3680 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3681 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3682 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3683 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3686 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3687 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3688 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3689 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3690 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3693 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3694 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3695 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3696 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3697 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3698 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3701 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3704 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3705 | if (S.LangOpts.CUDA) { |
| 3706 | // check the attribute arguments. |
| 3707 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 3708 | // FIXME: 0 is not okay. |
| 3709 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3710 | return; |
| 3711 | } |
| 3712 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3713 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3714 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3715 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3716 | return; |
| 3717 | } |
| 3718 | |
| 3719 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 3720 | llvm::APSInt MaxThreads(32); |
| 3721 | if (MaxThreadsExpr->isTypeDependent() || |
| 3722 | MaxThreadsExpr->isValueDependent() || |
| 3723 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 3724 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3725 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 3726 | return; |
| 3727 | } |
| 3728 | |
| 3729 | llvm::APSInt MinBlocks(32); |
| 3730 | if (Attr.getNumArgs() > 1) { |
| 3731 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 3732 | if (MinBlocksExpr->isTypeDependent() || |
| 3733 | MinBlocksExpr->isValueDependent() || |
| 3734 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 3735 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3736 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 3737 | return; |
| 3738 | } |
| 3739 | } |
| 3740 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3741 | D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3742 | MaxThreads.getZExtValue(), |
| 3743 | MinBlocks.getZExtValue())); |
| 3744 | } else { |
| 3745 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 3746 | } |
| 3747 | } |
| 3748 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3749 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3750 | const AttributeList &Attr) { |
| 3751 | StringRef AttrName = Attr.getName()->getName(); |
| 3752 | if (!Attr.getParameterName()) { |
| 3753 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 3754 | << Attr.getName() << /* arg num = */ 1; |
| 3755 | return; |
| 3756 | } |
| 3757 | |
| 3758 | if (Attr.getNumArgs() != 2) { |
| 3759 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3760 | << /* required args = */ 3; |
| 3761 | return; |
| 3762 | } |
| 3763 | |
| 3764 | IdentifierInfo *ArgumentKind = Attr.getParameterName(); |
| 3765 | |
| 3766 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3767 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3768 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3769 | return; |
| 3770 | } |
| 3771 | |
| 3772 | uint64_t ArgumentIdx; |
| 3773 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3774 | Attr.getLoc(), 2, |
| 3775 | Attr.getArg(0), ArgumentIdx)) |
| 3776 | return; |
| 3777 | |
| 3778 | uint64_t TypeTagIdx; |
| 3779 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3780 | Attr.getLoc(), 3, |
| 3781 | Attr.getArg(1), TypeTagIdx)) |
| 3782 | return; |
| 3783 | |
| 3784 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 3785 | if (IsPointer) { |
| 3786 | // Ensure that buffer has a pointer type. |
| 3787 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 3788 | if (!BufferTy->isPointerType()) { |
| 3789 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
| 3790 | << AttrName; |
| 3791 | } |
| 3792 | } |
| 3793 | |
| 3794 | D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(), |
| 3795 | S.Context, |
| 3796 | ArgumentKind, |
| 3797 | ArgumentIdx, |
| 3798 | TypeTagIdx, |
| 3799 | IsPointer)); |
| 3800 | } |
| 3801 | |
| 3802 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3803 | const AttributeList &Attr) { |
| 3804 | IdentifierInfo *PointerKind = Attr.getParameterName(); |
| 3805 | if (!PointerKind) { |
| 3806 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 3807 | << "type_tag_for_datatype" << 1; |
| 3808 | return; |
| 3809 | } |
| 3810 | |
| 3811 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 3812 | |
| 3813 | D->addAttr(::new (S.Context) TypeTagForDatatypeAttr( |
| 3814 | Attr.getRange(), |
| 3815 | S.Context, |
| 3816 | PointerKind, |
| 3817 | MatchingCType, |
| 3818 | Attr.getLayoutCompatible(), |
| 3819 | Attr.getMustBeNull())); |
| 3820 | } |
| 3821 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3822 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3823 | // Checker-specific attribute handlers. |
| 3824 | //===----------------------------------------------------------------------===// |
| 3825 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3826 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3827 | return type->isDependentType() || |
| 3828 | type->isObjCObjectPointerType() || |
| 3829 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3830 | } |
| 3831 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3832 | return type->isDependentType() || |
| 3833 | type->isPointerType() || |
| 3834 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3837 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3838 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3839 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3840 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3841 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3842 | return; |
| 3843 | } |
| 3844 | |
| 3845 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3846 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3847 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3848 | cf = false; |
| 3849 | } else { |
| 3850 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3851 | cf = true; |
| 3852 | } |
| 3853 | |
| 3854 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3855 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3856 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3857 | return; |
| 3858 | } |
| 3859 | |
| 3860 | if (cf) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3861 | param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3862 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3863 | param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3864 | } |
| 3865 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3866 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 3867 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3868 | if (!isa<ObjCMethodDecl>(D)) { |
| 3869 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3870 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3871 | return; |
| 3872 | } |
| 3873 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3874 | D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3877 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3878 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3879 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3880 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3881 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3882 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3883 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3884 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3885 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3886 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3887 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3888 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3889 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3890 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3891 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3892 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3893 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3894 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3895 | return; |
| 3896 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3897 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3898 | bool typeOK; |
| 3899 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3900 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3901 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3902 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3903 | case AttributeList::AT_NSReturnsRetained: |
| 3904 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3905 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3906 | cf = false; |
| 3907 | break; |
| 3908 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3909 | case AttributeList::AT_CFReturnsRetained: |
| 3910 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3911 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3912 | cf = true; |
| 3913 | break; |
| 3914 | } |
| 3915 | |
| 3916 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3917 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3918 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3919 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3920 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3921 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3922 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3923 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3924 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3925 | case AttributeList::AT_NSReturnsAutoreleased: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3926 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(), |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3927 | S.Context)); |
| 3928 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3929 | case AttributeList::AT_CFReturnsNotRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3930 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3931 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3932 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3933 | case AttributeList::AT_NSReturnsNotRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3934 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3935 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3936 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3937 | case AttributeList::AT_CFReturnsRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3938 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3939 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3940 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3941 | case AttributeList::AT_NSReturnsRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3942 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3943 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3944 | return; |
| 3945 | }; |
| 3946 | } |
| 3947 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3948 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3949 | const AttributeList &attr) { |
| 3950 | SourceLocation loc = attr.getLoc(); |
| 3951 | |
| 3952 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 3953 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 3954 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 3955 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3956 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3957 | return; |
| 3958 | } |
| 3959 | |
| 3960 | // Check that the method returns a normal pointer. |
| 3961 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3962 | |
| 3963 | if (!resultType->isReferenceType() && |
| 3964 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3965 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 3966 | << SourceRange(loc) |
| 3967 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 3968 | |
| 3969 | // Drop the attribute. |
| 3970 | return; |
| 3971 | } |
| 3972 | |
| 3973 | method->addAttr( |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3974 | ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context)); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame^] | 3977 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3978 | const AttributeList &attr) { |
| 3979 | SourceLocation loc = attr.getLoc(); |
| 3980 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 3981 | |
| 3982 | if (!method) { |
| 3983 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 3984 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 3985 | return; |
| 3986 | } |
| 3987 | DeclContext *DC = method->getDeclContext(); |
| 3988 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3989 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3990 | << attr.getName() << 0; |
| 3991 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3992 | return; |
| 3993 | } |
| 3994 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3995 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3996 | << attr.getName() << 1; |
| 3997 | return; |
| 3998 | } |
| 3999 | |
| 4000 | method->addAttr( |
| 4001 | ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context)); |
| 4002 | } |
| 4003 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4004 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4005 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4006 | if (!isa<FunctionDecl>(D)) { |
| 4007 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4008 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4009 | return; |
| 4010 | } |
| 4011 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4012 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4013 | |
| 4014 | // Check whether there's a conflicting attribute already present. |
| 4015 | Attr *Existing; |
| 4016 | if (IsAudited) { |
| 4017 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4018 | } else { |
| 4019 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4020 | } |
| 4021 | if (Existing) { |
| 4022 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4023 | << A.getName() |
| 4024 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4025 | << A.getRange() << Existing->getRange(); |
| 4026 | return; |
| 4027 | } |
| 4028 | |
| 4029 | // All clear; add the attribute. |
| 4030 | if (IsAudited) { |
| 4031 | D->addAttr( |
| 4032 | ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context)); |
| 4033 | } else { |
| 4034 | D->addAttr( |
| 4035 | ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context)); |
| 4036 | } |
| 4037 | } |
| 4038 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4039 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4040 | const AttributeList &Attr) { |
| 4041 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4042 | if (!RD || RD->isUnion()) { |
| 4043 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4044 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 4048 | |
| 4049 | // In Objective-C, verify that the type names an Objective-C type. |
| 4050 | // We don't want to check this outside of ObjC because people sometimes |
| 4051 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4052 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4053 | // Check for an existing type with this name. |
| 4054 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 4055 | Sema::LookupOrdinaryName); |
| 4056 | if (S.LookupName(R, Sc)) { |
| 4057 | NamedDecl *Target = R.getFoundDecl(); |
| 4058 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4059 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4060 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4061 | } |
| 4062 | } |
| 4063 | } |
| 4064 | |
| 4065 | D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context, |
| 4066 | ParmName)); |
| 4067 | } |
| 4068 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4069 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4070 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4071 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4072 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4073 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4074 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4077 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4078 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4079 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4080 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4081 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4082 | return; |
| 4083 | } |
| 4084 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4085 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4086 | QualType type = vd->getType(); |
| 4087 | |
| 4088 | if (!type->isDependentType() && |
| 4089 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4090 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4091 | << type; |
| 4092 | return; |
| 4093 | } |
| 4094 | |
| 4095 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4096 | |
| 4097 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4098 | // going to infer. |
| 4099 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4100 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4101 | |
| 4102 | switch (lifetime) { |
| 4103 | case Qualifiers::OCL_None: |
| 4104 | assert(type->isDependentType() && |
| 4105 | "didn't infer lifetime for non-dependent type?"); |
| 4106 | break; |
| 4107 | |
| 4108 | case Qualifiers::OCL_Weak: // meaningful |
| 4109 | case Qualifiers::OCL_Strong: // meaningful |
| 4110 | break; |
| 4111 | |
| 4112 | case Qualifiers::OCL_ExplicitNone: |
| 4113 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4114 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4115 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4116 | break; |
| 4117 | } |
| 4118 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4119 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4120 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4123 | //===----------------------------------------------------------------------===// |
| 4124 | // Microsoft specific attribute handlers. |
| 4125 | //===----------------------------------------------------------------------===// |
| 4126 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4127 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4128 | if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) { |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4129 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4130 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4131 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4132 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4133 | Expr *Arg = Attr.getArg(0); |
| 4134 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4135 | if (!Str || !Str->isAscii()) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4136 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 4137 | << "uuid" << 1; |
| 4138 | return; |
| 4139 | } |
| 4140 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4141 | StringRef StrRef = Str->getString(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4142 | |
| 4143 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 4144 | StrRef.back() == '}'; |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4145 | |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4146 | // Validate GUID length. |
| 4147 | if (IsCurly && StrRef.size() != 38) { |
| 4148 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4149 | return; |
| 4150 | } |
| 4151 | if (!IsCurly && StrRef.size() != 36) { |
| 4152 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4153 | return; |
| 4154 | } |
| 4155 | |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4156 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4157 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4158 | StringRef::iterator I = StrRef.begin(); |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4159 | if (IsCurly) // Skip the optional '{' |
| 4160 | ++I; |
| 4161 | |
| 4162 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4163 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 4164 | if (*I != '-') { |
| 4165 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4166 | return; |
| 4167 | } |
| 4168 | } else if (!isxdigit(*I)) { |
| 4169 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4170 | return; |
| 4171 | } |
| 4172 | I++; |
| 4173 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4174 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4175 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4176 | Str->getString())); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4177 | } else |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4178 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4181 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Joao Matos | 679fc93 | 2012-09-04 17:18:12 +0000 | [diff] [blame] | 4182 | if (S.LangOpts.MicrosoftExt) { |
| 4183 | AttributeList::Kind Kind = Attr.getKind(); |
| 4184 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4185 | D->addAttr( |
| 4186 | ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context)); |
| 4187 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4188 | D->addAttr( |
| 4189 | ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context)); |
| 4190 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4191 | D->addAttr( |
| 4192 | ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context)); |
| 4193 | } else |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4194 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4195 | } |
| 4196 | |
| 4197 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4198 | if (S.LangOpts.MicrosoftExt) { |
| 4199 | AttributeList::Kind Kind = Attr.getKind(); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4200 | if (Kind == AttributeList::AT_Ptr32) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4201 | D->addAttr( |
| 4202 | ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context)); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4203 | else if (Kind == AttributeList::AT_Ptr64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4204 | D->addAttr( |
| 4205 | ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context)); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4206 | else if (Kind == AttributeList::AT_Win64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4207 | D->addAttr( |
| 4208 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context)); |
| 4209 | } else |
| 4210 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4211 | } |
| 4212 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4213 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4214 | if (S.LangOpts.MicrosoftExt) |
| 4215 | D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context)); |
| 4216 | else |
| 4217 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4218 | } |
| 4219 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4220 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4221 | // Top Level Sema Entry Points |
| 4222 | //===----------------------------------------------------------------------===// |
| 4223 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4224 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4225 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4226 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4227 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4228 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4229 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4230 | default: |
| 4231 | break; |
| 4232 | } |
| 4233 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4234 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4235 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4236 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4237 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4238 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4239 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4240 | case AttributeList::AT_IBOutletCollection: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4241 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4242 | case AttributeList::AT_AddressSpace: |
| 4243 | case AttributeList::AT_OpenCLImageAccess: |
| 4244 | case AttributeList::AT_ObjCGC: |
| 4245 | case AttributeList::AT_VectorSize: |
| 4246 | case AttributeList::AT_NeonVectorType: |
| 4247 | case AttributeList::AT_NeonPolyVectorType: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4248 | // Ignore these, these are type attributes, handled by |
| 4249 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4250 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4251 | case AttributeList::AT_CUDADevice: |
| 4252 | case AttributeList::AT_CUDAHost: |
| 4253 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4254 | // Ignore, this is a non-inheritable attribute, handled |
| 4255 | // by ProcessNonInheritableDeclAttr. |
| 4256 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4257 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4258 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4259 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4260 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4261 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4262 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4263 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4264 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4265 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4266 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4267 | case AttributeList::AT_CarriesDependency: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4268 | handleDependencyAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4269 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4270 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4271 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
| 4272 | case AttributeList::AT_Deprecated: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4273 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated"); |
| 4274 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4275 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4276 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4277 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4278 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4279 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4280 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4281 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4282 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4283 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4284 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4285 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4286 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4287 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4288 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4289 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4290 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4291 | case AttributeList::AT_ownership_returns: |
| 4292 | case AttributeList::AT_ownership_takes: |
| 4293 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4294 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4295 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4296 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4297 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4298 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4299 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4300 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4301 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4302 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4303 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4304 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4305 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4306 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4307 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4308 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4309 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4310 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame^] | 4311 | case AttributeList::AT_ObjCRequiresSuper: |
| 4312 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4313 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4314 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4315 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4316 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4317 | case AttributeList::AT_CFAuditedTransfer: |
| 4318 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4319 | handleCFTransferAttr(S, D, Attr); break; |
| 4320 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4321 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4322 | case AttributeList::AT_CFConsumed: |
| 4323 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4324 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4325 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4326 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4327 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4328 | case AttributeList::AT_NSReturnsNotRetained: |
| 4329 | case AttributeList::AT_CFReturnsNotRetained: |
| 4330 | case AttributeList::AT_NSReturnsRetained: |
| 4331 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4332 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4333 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4334 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4335 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4336 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4337 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4338 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4339 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4340 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4341 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4342 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4343 | case AttributeList::AT_Unavailable: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4344 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable"); |
| 4345 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4346 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4347 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4348 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4349 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4350 | handleObjCRootClassAttr(S, D, Attr); |
| 4351 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4352 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4353 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4354 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4355 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4356 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4357 | handleReturnsTwiceAttr(S, D, Attr); |
| 4358 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4359 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
| 4360 | case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break; |
| 4361 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4362 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4363 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4364 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4365 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4366 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4367 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4368 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4369 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4370 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4371 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4372 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4373 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4374 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4375 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4376 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4377 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4378 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4379 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4380 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4381 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4382 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4383 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4384 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4385 | // Just ignore |
| 4386 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4387 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4388 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4389 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4390 | case AttributeList::AT_StdCall: |
| 4391 | case AttributeList::AT_CDecl: |
| 4392 | case AttributeList::AT_FastCall: |
| 4393 | case AttributeList::AT_ThisCall: |
| 4394 | case AttributeList::AT_Pascal: |
| 4395 | case AttributeList::AT_Pcs: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4396 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4397 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4398 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4399 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4400 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4401 | |
| 4402 | // Microsoft attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4403 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4404 | handleMsStructAttr(S, D, Attr); |
| 4405 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4406 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4407 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4408 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4409 | case AttributeList::AT_SingleInheritance: |
| 4410 | case AttributeList::AT_MultipleInheritance: |
| 4411 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4412 | handleInheritanceAttr(S, D, Attr); |
| 4413 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4414 | case AttributeList::AT_Win64: |
| 4415 | case AttributeList::AT_Ptr32: |
| 4416 | case AttributeList::AT_Ptr64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4417 | handlePortabilityAttr(S, D, Attr); |
| 4418 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4419 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4420 | handleForceInlineAttr(S, D, Attr); |
| 4421 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4422 | |
| 4423 | // Thread safety attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4424 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4425 | handleGuardedVarAttr(S, D, Attr); |
| 4426 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4427 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4428 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4429 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4430 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4431 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4432 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4433 | case AttributeList::AT_NoAddressSafetyAnalysis: |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4434 | handleNoAddressSafetyAttr(S, D, Attr); |
| 4435 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4436 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4437 | handleNoThreadSafetyAttr(S, D, Attr); |
| 4438 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4439 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4440 | handleLockableAttr(S, D, Attr); |
| 4441 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4442 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4443 | handleGuardedByAttr(S, D, Attr); |
| 4444 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4445 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4446 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4447 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4448 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4449 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4450 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4451 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4452 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4453 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4454 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4455 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4456 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4457 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4458 | handleLockReturnedAttr(S, D, Attr); |
| 4459 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4460 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4461 | handleLocksExcludedAttr(S, D, Attr); |
| 4462 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4463 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4464 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4465 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4466 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4467 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4468 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4469 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4470 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4471 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4472 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4473 | handleUnlockFunAttr(S, D, Attr); |
| 4474 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4475 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4476 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4477 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4478 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4479 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4480 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4481 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4482 | // Type safety attributes. |
| 4483 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4484 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4485 | break; |
| 4486 | case AttributeList::AT_TypeTagForDatatype: |
| 4487 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4488 | break; |
| 4489 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4490 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4491 | // Ask target about the attribute. |
| 4492 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4493 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4494 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4495 | diag::warn_unhandled_ms_attribute_ignored : |
| 4496 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4497 | break; |
| 4498 | } |
| 4499 | } |
| 4500 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4501 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4502 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4503 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 4504 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4505 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4506 | const AttributeList &Attr, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4507 | bool NonInheritable, bool Inheritable) { |
| 4508 | if (Attr.isInvalid()) |
| 4509 | return; |
| 4510 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4511 | // Type attributes are still treated as declaration attributes by |
| 4512 | // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't |
| 4513 | // want to process them, however, because we will simply warn about ignoring |
| 4514 | // them. So instead, we will bail out early. |
| 4515 | if (Attr.isMSTypespecAttribute()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4516 | return; |
| 4517 | |
| 4518 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4519 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4520 | |
| 4521 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4522 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4525 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4526 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4527 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4528 | const AttributeList *AttrList, |
| 4529 | bool NonInheritable, bool Inheritable) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4530 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 4531 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable); |
Rafael Espindola | 9b79fc9 | 2012-05-07 23:58:18 +0000 | [diff] [blame] | 4532 | } |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4533 | |
| 4534 | // GCC accepts |
| 4535 | // static int a9 __attribute__((weakref)); |
| 4536 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4537 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4538 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4539 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4540 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4541 | } |
| 4542 | } |
| 4543 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4544 | // Annotation attributes are the only attributes allowed after an access |
| 4545 | // specifier. |
| 4546 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4547 | const AttributeList *AttrList) { |
| 4548 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4549 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4550 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4551 | } else { |
| 4552 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4553 | return true; |
| 4554 | } |
| 4555 | } |
| 4556 | |
| 4557 | return false; |
| 4558 | } |
| 4559 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4560 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4561 | /// contains any decl attributes that we should warn about. |
| 4562 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4563 | for ( ; A; A = A->getNext()) { |
| 4564 | // Only warn if the attribute is an unignored, non-type attribute. |
| 4565 | if (A->isUsedAsTypeAttr()) continue; |
| 4566 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4567 | |
| 4568 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4569 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4570 | << A->getName() << A->getRange(); |
| 4571 | } else { |
| 4572 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4573 | << A->getName() << A->getRange(); |
| 4574 | } |
| 4575 | } |
| 4576 | } |
| 4577 | |
| 4578 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4579 | /// used to build a declaration, complain about any decl attributes |
| 4580 | /// which might be lying around on it. |
| 4581 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4582 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4583 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4584 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4585 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4586 | } |
| 4587 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4588 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4589 | /// \#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] | 4590 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4591 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4592 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4593 | NamedDecl *NewD = 0; |
| 4594 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4595 | FunctionDecl *NewFD; |
| 4596 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4597 | // FIXME: Mangling? |
| 4598 | // FIXME: Is the qualifier info correct? |
| 4599 | // FIXME: Is the DeclContext correct? |
| 4600 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4601 | Loc, Loc, DeclarationName(II), |
| 4602 | FD->getType(), FD->getTypeSourceInfo(), |
| 4603 | SC_None, SC_None, |
| 4604 | false/*isInlineSpecified*/, |
| 4605 | FD->hasPrototype(), |
| 4606 | false/*isConstexprSpecified*/); |
| 4607 | NewD = NewFD; |
| 4608 | |
| 4609 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4610 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4611 | |
| 4612 | // Fake up parameter variables; they are declared as if this were |
| 4613 | // a typedef. |
| 4614 | QualType FDTy = FD->getType(); |
| 4615 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4616 | SmallVector<ParmVarDecl*, 16> Params; |
| 4617 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4618 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4619 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4620 | Param->setScopeInfo(0, Params.size()); |
| 4621 | Params.push_back(Param); |
| 4622 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4623 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4624 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4625 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4626 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4627 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4628 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4629 | VD->getStorageClass(), |
| 4630 | VD->getStorageClassAsWritten()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4631 | if (VD->getQualifier()) { |
| 4632 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4633 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4634 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4635 | } |
| 4636 | return NewD; |
| 4637 | } |
| 4638 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4639 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4640 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4641 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4642 | if (W.getUsed()) return; // only do this once |
| 4643 | W.setUsed(true); |
| 4644 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4645 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4646 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4647 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4648 | NDId->getName())); |
| 4649 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4650 | WeakTopLevelDecl.push_back(NewD); |
| 4651 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4652 | // to insert Decl at TU scope, sorry. |
| 4653 | DeclContext *SavedContext = CurContext; |
| 4654 | CurContext = Context.getTranslationUnitDecl(); |
| 4655 | PushOnScopeChains(NewD, S); |
| 4656 | CurContext = SavedContext; |
| 4657 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4658 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4659 | } |
| 4660 | } |
| 4661 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4662 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4663 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4664 | /// specified in many different places, and we need to find and apply them all. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4665 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 4666 | bool NonInheritable, bool Inheritable) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 4667 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4668 | // have to do this. |
Douglas Gregor | 31e37b2 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 4669 | if (Inheritable) { |
| 4670 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4671 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4672 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 4673 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4674 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4675 | = WeakUndeclaredIdentifiers.find(Id); |
| 4676 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 4677 | WeakInfo W = I->second; |
| 4678 | DeclApplyPragmaWeak(S, ND, W); |
| 4679 | WeakUndeclaredIdentifiers[Id] = W; |
| 4680 | } |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 4681 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4682 | } |
| 4683 | } |
| 4684 | } |
| 4685 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4686 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4687 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4688 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4689 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4690 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4691 | // position to the decl itself. This handles cases like: |
| 4692 | // int *__attr__(x)** D; |
| 4693 | // when X is a decl attribute. |
| 4694 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4695 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4696 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4697 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4698 | // Finally, apply any attributes on the decl itself. |
| 4699 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4700 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4701 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4702 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4703 | /// Is the given declaration allowed to use a forbidden type? |
| 4704 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4705 | // Private ivars are always okay. Unfortunately, people don't |
| 4706 | // always properly make their ivars private, even in system headers. |
| 4707 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4708 | // Function declarations in sys headers will be marked unavailable. |
| 4709 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4710 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4711 | return false; |
| 4712 | |
| 4713 | // Require it to be declared in a system header. |
| 4714 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4715 | } |
| 4716 | |
| 4717 | /// Handle a delayed forbidden-type diagnostic. |
| 4718 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4719 | Decl *decl) { |
| 4720 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 4721 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 4722 | "this system declaration uses an unsupported type")); |
| 4723 | return; |
| 4724 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4725 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4726 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4727 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4728 | // kind of forbidden type messages on unavailable functions. |
| 4729 | if (FD->hasAttr<UnavailableAttr>() && |
| 4730 | diag.getForbiddenTypeDiagnostic() == |
| 4731 | diag::err_arc_array_param_no_ownership) { |
| 4732 | diag.Triggered = true; |
| 4733 | return; |
| 4734 | } |
| 4735 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4736 | |
| 4737 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4738 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4739 | diag.Triggered = true; |
| 4740 | } |
| 4741 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4742 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4743 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4744 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4745 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4746 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4747 | // When delaying diagnostics to run in the context of a parsed |
| 4748 | // declaration, we only want to actually emit anything if parsing |
| 4749 | // succeeds. |
| 4750 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4751 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4752 | // We emit all the active diagnostics in this pool or any of its |
| 4753 | // parents. In general, we'll get one pool for the decl spec |
| 4754 | // and a child pool for each declarator; in a decl group like: |
| 4755 | // deprecated_typedef foo, *bar, baz(); |
| 4756 | // only the declarator pops will be passed decls. This is correct; |
| 4757 | // we really do need to consider delayed diagnostics from the decl spec |
| 4758 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4759 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4760 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4761 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4762 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4763 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4764 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4765 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4766 | continue; |
| 4767 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4768 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4769 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4770 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 4771 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4772 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4773 | break; |
| 4774 | |
| 4775 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4776 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4777 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4778 | |
| 4779 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4780 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4781 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4782 | } |
| 4783 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4784 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4785 | } |
| 4786 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4787 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4788 | /// been delayed in the current context instead of in the given pool. |
| 4789 | /// Essentially, this just moves them to the current pool. |
| 4790 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4791 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4792 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4793 | curPool->steal(pool); |
| 4794 | } |
| 4795 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4796 | static bool isDeclDeprecated(Decl *D) { |
| 4797 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4798 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4799 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4800 | // A category implicitly has the availability of the interface. |
| 4801 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4802 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4803 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4804 | return false; |
| 4805 | } |
| 4806 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4807 | static void |
| 4808 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 4809 | SourceLocation Loc, |
| 4810 | const ObjCInterfaceDecl *UnknownObjCClass) { |
| 4811 | DeclarationName Name = D->getDeclName(); |
| 4812 | if (!Message.empty()) { |
| 4813 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 4814 | S.Diag(D->getLocation(), |
| 4815 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4816 | : diag::note_previous_decl) << Name; |
| 4817 | } else if (!UnknownObjCClass) { |
| 4818 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 4819 | S.Diag(D->getLocation(), |
| 4820 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4821 | : diag::note_previous_decl) << Name; |
| 4822 | } else { |
| 4823 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 4824 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4825 | } |
| 4826 | } |
| 4827 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 4828 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4829 | Decl *Ctx) { |
| 4830 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4831 | return; |
| 4832 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4833 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4834 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 4835 | DD.getDeprecationMessage(), DD.Loc, |
| 4836 | DD.getUnknownObjCClass()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4837 | } |
| 4838 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4839 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4840 | SourceLocation Loc, |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 4841 | const ObjCInterfaceDecl *UnknownObjCClass) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4842 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4843 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4844 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 4845 | UnknownObjCClass, |
| 4846 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4847 | return; |
| 4848 | } |
| 4849 | |
| 4850 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 4851 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4852 | return; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4853 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4854 | } |