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, |
| 46 | ExpectedStruct |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
| 50 | // Helper functions |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 53 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 54 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 55 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 56 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 57 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 58 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 59 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 60 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 61 | Ty = decl->getUnderlyingType(); |
| 62 | else |
| 63 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 65 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 66 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 67 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 68 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 69 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 70 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 73 | // FIXME: We should provide an abstraction around a method or function |
| 74 | // to provide the following bits of information. |
| 75 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 76 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 77 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 78 | static bool isFunction(const Decl *D) { |
| 79 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 83 | /// type (function or function-typed variable) or an Objective-C |
| 84 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 85 | static bool isFunctionOrMethod(const Decl *D) { |
| 86 | return isFunction(D)|| isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 89 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 90 | /// type (function or function-typed variable) or an Objective-C |
| 91 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 92 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 93 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 94 | return true; |
| 95 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 96 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 97 | QualType Ty = V->getType(); |
| 98 | return Ty->isBlockPointerType(); |
| 99 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 100 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 101 | } |
| 102 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 103 | /// Return true if the given decl has a declarator that should have |
| 104 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 105 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 106 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 107 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 108 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 111 | /// hasFunctionProto - Return true if the given decl has a argument |
| 112 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 113 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 114 | static bool hasFunctionProto(const Decl *D) { |
| 115 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 116 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 117 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 118 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 124 | /// arguments. It is an error to call this on a K&R function (use |
| 125 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 126 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 127 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 128 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 129 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 130 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 131 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 134 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 135 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 136 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 138 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 139 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 140 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 143 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 144 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 145 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 146 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 149 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 150 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 151 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 152 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 153 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 154 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 155 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 156 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 160 | static bool isInstanceMethod(const Decl *D) { |
| 161 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 162 | return MethodDecl->isInstance(); |
| 163 | return false; |
| 164 | } |
| 165 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 166 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 167 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 168 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 169 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 170 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 171 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 172 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 173 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 174 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 175 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 177 | // FIXME: Should we walk the chain of classes? |
| 178 | return ClsName == &Ctx.Idents.get("NSString") || |
| 179 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 180 | } |
| 181 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 182 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 183 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 184 | if (!PT) |
| 185 | return false; |
| 186 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 187 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 188 | if (!RT) |
| 189 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 190 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 191 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 192 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 193 | return false; |
| 194 | |
| 195 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 196 | } |
| 197 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 198 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 199 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 200 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 201 | unsigned int Num) { |
| 202 | if (Attr.getNumArgs() != Num) { |
| 203 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | return true; |
| 208 | } |
| 209 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 210 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 211 | /// \brief Check if the attribute has at least as many args as Num. May |
| 212 | /// output an error. |
| 213 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 214 | unsigned int Num) { |
| 215 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 216 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 217 | return false; |
| 218 | } |
| 219 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 220 | return true; |
| 221 | } |
| 222 | |
| 223 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 224 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 225 | /// \return true if the Decl is a field or potentially shared global variable |
| 226 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 227 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 228 | if (isa<FieldDecl>(D)) |
| 229 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 230 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 231 | return (vd->hasGlobalStorage() && !(vd->isThreadSpecified())); |
| 232 | |
| 233 | return false; |
| 234 | } |
| 235 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 236 | /// \brief Check if the passed-in expression is of type int or bool. |
| 237 | static bool isIntOrBool(Expr *Exp) { |
| 238 | QualType QT = Exp->getType(); |
| 239 | return QT->isBooleanType() || QT->isIntegerType(); |
| 240 | } |
| 241 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 242 | |
| 243 | // Check to see if the type is a smart pointer of some kind. We assume |
| 244 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 245 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 246 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 247 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
| 248 | if (Res1.first == Res1.second) |
| 249 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 250 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 251 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 252 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
| 253 | if (Res2.first == Res2.second) |
| 254 | return false; |
| 255 | |
| 256 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 259 | /// \brief Check if passed in Decl is a pointer type. |
| 260 | /// Note that this function may produce an error message. |
| 261 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 262 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 263 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 264 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 265 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 266 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 267 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 268 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 269 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 270 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 271 | // (We don't want to force template instantiation if we can avoid it, |
| 272 | // since that would alter the order in which templates are instantiated.) |
| 273 | if (RT->isIncompleteType()) |
| 274 | return true; |
| 275 | |
| 276 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 277 | return true; |
| 278 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 279 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 280 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 281 | << Attr.getName()->getName() << QT; |
| 282 | } else { |
| 283 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 284 | << Attr.getName(); |
| 285 | } |
| 286 | return false; |
| 287 | } |
| 288 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 289 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 290 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 291 | static const RecordType *getRecordType(QualType QT) { |
| 292 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 293 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 294 | |
| 295 | // Now check if we point to record type. |
| 296 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 297 | return PT->getPointeeType()->getAs<RecordType>(); |
| 298 | |
| 299 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 300 | } |
| 301 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 302 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 303 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 304 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 305 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 306 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 307 | return true; |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 312 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 313 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 314 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 315 | QualType Ty) { |
| 316 | const RecordType *RT = getRecordType(Ty); |
| 317 | |
| 318 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 319 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 320 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 321 | << Attr.getName() << Ty.getAsString(); |
| 322 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 323 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 324 | |
DeLesley Hutchins | 634b293 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 325 | // Don't check for lockable if the class hasn't been defined yet. |
| 326 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 327 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 328 | |
| 329 | // Allow smart pointers to be used as lockable objects. |
| 330 | // FIXME -- Check the type that the smart pointer points to. |
| 331 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 332 | return; |
| 333 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 334 | // Check if the type is lockable. |
| 335 | RecordDecl *RD = RT->getDecl(); |
| 336 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 337 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 338 | |
| 339 | // Else check if any base classes are lockable. |
| 340 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 341 | CXXBasePaths BPaths(false, false); |
| 342 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 343 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 344 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 345 | |
| 346 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 347 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 350 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 351 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 352 | /// \param Sidx The attribute argument index to start checking with. |
| 353 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 354 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 355 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 356 | const AttributeList &Attr, |
| 357 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 358 | int Sidx = 0, |
| 359 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 360 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 361 | Expr *ArgExp = Attr.getArg(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 362 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 363 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 364 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 365 | Args.push_back(ArgExp); |
| 366 | continue; |
| 367 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 368 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 369 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
| 370 | // Ignore empty strings without warnings |
| 371 | if (StrLit->getLength() == 0) |
| 372 | continue; |
| 373 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 374 | // We allow constant strings to be used as a placeholder for expressions |
| 375 | // that are not valid C++ syntax, but warn that they are ignored. |
| 376 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 377 | Attr.getName(); |
| 378 | continue; |
| 379 | } |
| 380 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 381 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 382 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 383 | // A pointer to member expression of the form &MyClass::mu is treated |
| 384 | // specially -- we need to look at the type of the member. |
| 385 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 386 | if (UOp->getOpcode() == UO_AddrOf) |
| 387 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 388 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 389 | ArgTy = DRE->getDecl()->getType(); |
| 390 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 391 | // First see if we can just cast to record type, or point to record type. |
| 392 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 393 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 394 | // Now check if we index into a record type function param. |
| 395 | if(!RT && ParamIdxOk) { |
| 396 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 397 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 398 | if(FD && IL) { |
| 399 | unsigned int NumParams = FD->getNumParams(); |
| 400 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 401 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 402 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 403 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 404 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 405 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 406 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 407 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 408 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 412 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 413 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 414 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 415 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 418 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 419 | // Attribute Implementations |
| 420 | //===----------------------------------------------------------------------===// |
| 421 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 422 | // FIXME: All this manual attribute parsing code is gross. At the |
| 423 | // least add some helper functions to check most argument patterns (# |
| 424 | // and types of args). |
| 425 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 426 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 427 | bool pointer = false) { |
| 428 | assert(!Attr.isInvalid()); |
| 429 | |
| 430 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 431 | return; |
| 432 | |
| 433 | // D must be either a member field or global (potentially shared) variable. |
| 434 | if (!mayBeSharedVariable(D)) { |
| 435 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 436 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 440 | if (pointer && !threadSafetyCheckIsPointer(S, D, Attr)) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 441 | return; |
| 442 | |
| 443 | if (pointer) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 444 | D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context)); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 445 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 446 | D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context)); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 449 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 450 | bool pointer = false) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 451 | assert(!Attr.isInvalid()); |
| 452 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 453 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 454 | return; |
| 455 | |
| 456 | // D must be either a member field or global (potentially shared) variable. |
| 457 | if (!mayBeSharedVariable(D)) { |
| 458 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 459 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 463 | if (pointer && !threadSafetyCheckIsPointer(S, D, Attr)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 464 | return; |
| 465 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 466 | SmallVector<Expr*, 1> Args; |
| 467 | // check that all arguments are lockable objects |
| 468 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 469 | unsigned Size = Args.size(); |
| 470 | if (Size != 1) |
| 471 | return; |
| 472 | Expr *Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 473 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 474 | if (pointer) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 475 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 476 | S.Context, Arg)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 477 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 478 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 482 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 483 | bool scoped = false) { |
| 484 | assert(!Attr.isInvalid()); |
| 485 | |
| 486 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 487 | return; |
| 488 | |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 489 | // FIXME: Lockable structs for C code. |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 490 | if (!isa<CXXRecordDecl>(D)) { |
| 491 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 492 | << Attr.getName() << ExpectedClass; |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | if (scoped) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 497 | D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context)); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 498 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 499 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static void handleNoThreadSafetyAttr(Sema &S, Decl *D, |
| 503 | const AttributeList &Attr) { |
| 504 | assert(!Attr.isInvalid()); |
| 505 | |
| 506 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 507 | return; |
| 508 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 509 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 510 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 511 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 512 | return; |
| 513 | } |
| 514 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 515 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 516 | S.Context)); |
| 517 | } |
| 518 | |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 519 | static void handleNoAddressSafetyAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 520 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 521 | assert(!Attr.isInvalid()); |
| 522 | |
| 523 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 524 | return; |
| 525 | |
| 526 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 527 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 528 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(), |
| 533 | S.Context)); |
| 534 | } |
| 535 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 536 | static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 537 | bool before) { |
| 538 | assert(!Attr.isInvalid()); |
| 539 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 540 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 541 | return; |
| 542 | |
| 543 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 544 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 545 | if (!VD || !mayBeSharedVariable(D)) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 546 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 547 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 548 | return; |
| 549 | } |
| 550 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 551 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 552 | QualType QT = VD->getType(); |
| 553 | if (!QT->isDependentType()) { |
| 554 | const RecordType *RT = getRecordType(QT); |
| 555 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 556 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 557 | << Attr.getName(); |
| 558 | return; |
| 559 | } |
| 560 | } |
| 561 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 562 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 563 | // Check that all arguments are lockable objects. |
| 564 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 565 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 566 | if (Size == 0) |
| 567 | return; |
| 568 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 569 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 570 | if (before) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 571 | D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 572 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 573 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 574 | D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 575 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 579 | bool exclusive = false) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 580 | assert(!Attr.isInvalid()); |
| 581 | |
| 582 | // zero or more arguments ok |
| 583 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 584 | // check that the attribute is applied to a function |
| 585 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 586 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 587 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 588 | return; |
| 589 | } |
| 590 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 591 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 592 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 593 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 594 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 595 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 596 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 597 | if (exclusive) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 598 | D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(), |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 599 | S.Context, StartArg, |
| 600 | Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 601 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 602 | D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(), |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 603 | S.Context, StartArg, |
| 604 | Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 608 | bool exclusive = false) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 609 | assert(!Attr.isInvalid()); |
| 610 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 611 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 612 | return; |
| 613 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 614 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 615 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 616 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 617 | return; |
| 618 | } |
| 619 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 620 | if (!isIntOrBool(Attr.getArg(0))) { |
| 621 | S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool) |
| 622 | << Attr.getName(); |
| 623 | return; |
| 624 | } |
| 625 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 626 | SmallVector<Expr*, 2> Args; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 627 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 628 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 629 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 630 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 631 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 632 | if (exclusive) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 633 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(), |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 634 | S.Context, |
Caitlin Sadowski | 69f5d14 | 2011-09-15 17:50:19 +0000 | [diff] [blame] | 635 | Attr.getArg(0), |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 636 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 637 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 638 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(), |
Caitlin Sadowski | 69f5d14 | 2011-09-15 17:50:19 +0000 | [diff] [blame] | 639 | S.Context, |
| 640 | Attr.getArg(0), |
| 641 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 645 | bool exclusive = false) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 646 | assert(!Attr.isInvalid()); |
| 647 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 648 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 649 | return; |
| 650 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 651 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 652 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 653 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 654 | return; |
| 655 | } |
| 656 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 657 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 658 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 659 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 660 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 661 | if (Size == 0) |
| 662 | return; |
| 663 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 664 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 665 | if (exclusive) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 666 | D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(), |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 667 | S.Context, StartArg, |
| 668 | Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 669 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 670 | D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(), |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 671 | S.Context, StartArg, |
| 672 | Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 675 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 676 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 677 | assert(!Attr.isInvalid()); |
| 678 | |
| 679 | // zero or more arguments ok |
| 680 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 681 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 682 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 683 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 684 | return; |
| 685 | } |
| 686 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 687 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 688 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 689 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 690 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 691 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 692 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 693 | D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 694 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 698 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 699 | assert(!Attr.isInvalid()); |
| 700 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 701 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 702 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 703 | Expr *Arg = Attr.getArg(0); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 704 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 705 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 706 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 707 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 708 | return; |
| 709 | } |
| 710 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 711 | if (Arg->isTypeDependent()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 712 | return; |
| 713 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 714 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 715 | SmallVector<Expr*, 1> Args; |
| 716 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 717 | unsigned Size = Args.size(); |
| 718 | if (Size == 0) |
| 719 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 720 | |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 721 | D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, |
| 722 | Args[0])); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 726 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 727 | assert(!Attr.isInvalid()); |
| 728 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 729 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 730 | return; |
| 731 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 732 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 733 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 734 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 735 | return; |
| 736 | } |
| 737 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 738 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 739 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 740 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 741 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 742 | if (Size == 0) |
| 743 | return; |
| 744 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 745 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 746 | D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 747 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 751 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 752 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 753 | TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 754 | if (tDecl == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 755 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 756 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 757 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 758 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 759 | QualType curType = tDecl->getUnderlyingType(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 760 | |
| 761 | Expr *sizeExpr; |
| 762 | |
| 763 | // Special case where the argument is a template id. |
| 764 | if (Attr.getParameterName()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 765 | CXXScopeSpec SS; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 766 | SourceLocation TemplateKWLoc; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 767 | UnqualifiedId id; |
| 768 | id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 769 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 770 | ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id, |
| 771 | false, false); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 772 | if (Size.isInvalid()) |
| 773 | return; |
| 774 | |
| 775 | sizeExpr = Size.get(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 776 | } else { |
| 777 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 778 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 779 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 780 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 781 | sizeExpr = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 782 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 783 | |
| 784 | // Instantiate/Install the vector type, and let Sema build the type for us. |
| 785 | // This will run the reguired checks. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 786 | QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 787 | if (!T.isNull()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 788 | // FIXME: preserve the old source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 789 | tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 791 | // Remember this typedef decl, we will need it later for diagnostics. |
| 792 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 793 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 796 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 797 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 798 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 799 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 800 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 801 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 802 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 803 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 804 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 805 | // has no effect. |
| 806 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 807 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 808 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 809 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 810 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 811 | FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 812 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 813 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 816 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 817 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 818 | TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context)); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 819 | else |
| 820 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 821 | } |
| 822 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 823 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 824 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 825 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 826 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 827 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 828 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 829 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 830 | if (MD->isInstanceMethod()) { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 831 | D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 835 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 838 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 839 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 840 | // variables or properties of Objective-C classes. The outlet must also |
| 841 | // have an object reference type. |
| 842 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 843 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 844 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 845 | << Attr.getName() << VD->getType() << 0; |
| 846 | return false; |
| 847 | } |
| 848 | } |
| 849 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 850 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 851 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 852 | << Attr.getName() << PD->getType() << 1; |
| 853 | return false; |
| 854 | } |
| 855 | } |
| 856 | else { |
| 857 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 858 | return false; |
| 859 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 860 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 861 | return true; |
| 862 | } |
| 863 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 864 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 865 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 866 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 867 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 868 | |
| 869 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 870 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 871 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 872 | D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 875 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 876 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 877 | |
| 878 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 879 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 880 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 881 | return; |
| 882 | } |
| 883 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 884 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 885 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 886 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 887 | IdentifierInfo *II = Attr.getParameterName(); |
| 888 | if (!II) |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 889 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 890 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 891 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 892 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 893 | if (!TypeRep) { |
| 894 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 895 | return; |
| 896 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 897 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 898 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 899 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 900 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 901 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 902 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 903 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 904 | return; |
| 905 | } |
Argyrios Kyrtzidis | f1e7af3 | 2011-09-13 18:41:59 +0000 | [diff] [blame] | 906 | D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 907 | QT, Attr.getParameterLoc())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 910 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 911 | if (const RecordType *UT = T->getAsUnionType()) |
| 912 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 913 | RecordDecl *UD = UT->getDecl(); |
| 914 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 915 | itend = UD->field_end(); it != itend; ++it) { |
| 916 | QualType QT = it->getType(); |
| 917 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 918 | T = QT; |
| 919 | return; |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 925 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 926 | if (!isFunctionOrMethod(D)) { |
| 927 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 928 | << "alloc_size" << ExpectedFunctionOrMethod; |
| 929 | return; |
| 930 | } |
| 931 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 932 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 933 | return; |
| 934 | |
| 935 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 936 | // counted from one. |
| 937 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 938 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 939 | |
| 940 | SmallVector<unsigned, 8> SizeArgs; |
| 941 | |
| 942 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 943 | E = Attr.arg_end(); I!=E; ++I) { |
| 944 | // The argument must be an integer constant expression. |
| 945 | Expr *Ex = *I; |
| 946 | llvm::APSInt ArgNum; |
| 947 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 948 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 949 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 950 | << "alloc_size" << Ex->getSourceRange(); |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | uint64_t x = ArgNum.getZExtValue(); |
| 955 | |
| 956 | if (x < 1 || x > NumArgs) { |
| 957 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 958 | << "alloc_size" << I.getArgNum() << Ex->getSourceRange(); |
| 959 | return; |
| 960 | } |
| 961 | |
| 962 | --x; |
| 963 | if (HasImplicitThisParam) { |
| 964 | if (x == 0) { |
| 965 | S.Diag(Attr.getLoc(), |
| 966 | diag::err_attribute_invalid_implicit_this_argument) |
| 967 | << "alloc_size" << Ex->getSourceRange(); |
| 968 | return; |
| 969 | } |
| 970 | --x; |
| 971 | } |
| 972 | |
| 973 | // check if the function argument is of an integer type |
| 974 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| 975 | if (!T->isIntegerType()) { |
| 976 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 977 | << "alloc_size" << Ex->getSourceRange(); |
| 978 | return; |
| 979 | } |
| 980 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 981 | SizeArgs.push_back(x); |
| 982 | } |
| 983 | |
| 984 | // check if the function returns a pointer |
| 985 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 986 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 987 | << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
| 988 | } |
| 989 | |
Nuno Lopes | 96c67d1 | 2012-06-18 16:27:56 +0000 | [diff] [blame] | 990 | D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context, |
| 991 | SizeArgs.data(), SizeArgs.size())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 994 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 995 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 996 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 997 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 998 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 999 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1000 | return; |
| 1001 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1002 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1003 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1004 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1005 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1006 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1007 | |
| 1008 | // The nonnull attribute only applies to pointers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1009 | SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1010 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1011 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 1012 | E=Attr.arg_end(); I!=E; ++I) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1013 | |
| 1014 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1015 | // The argument must be an integer constant expression. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1016 | Expr *Ex = *I; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1017 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1018 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1019 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1020 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1021 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1022 | return; |
| 1023 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1024 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1025 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1026 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1027 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1028 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 1029 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1030 | return; |
| 1031 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1032 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 1033 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1034 | if (HasImplicitThisParam) { |
| 1035 | if (x == 0) { |
| 1036 | S.Diag(Attr.getLoc(), |
| 1037 | diag::err_attribute_invalid_implicit_this_argument) |
| 1038 | << "nonnull" << Ex->getSourceRange(); |
| 1039 | return; |
| 1040 | } |
| 1041 | --x; |
| 1042 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1043 | |
| 1044 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1045 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1046 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1047 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1048 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1049 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1050 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1051 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1052 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1053 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1054 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1055 | NonNullArgs.push_back(x); |
| 1056 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1057 | |
| 1058 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1059 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1060 | if (NonNullArgs.empty()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1061 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) { |
| 1062 | QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1063 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1064 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 1065 | NonNullArgs.push_back(I); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1066 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1067 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1068 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1069 | if (NonNullArgs.empty()) { |
| 1070 | // Warn the trivial case only if attribute is not coming from a |
| 1071 | // macro instantiation. |
| 1072 | if (Attr.getLoc().isFileID()) |
| 1073 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1074 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1075 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1076 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1077 | |
| 1078 | unsigned* start = &NonNullArgs[0]; |
| 1079 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1080 | llvm::array_pod_sort(start, start + size); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1081 | D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1082 | size)); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1085 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1086 | // This attribute must be applied to a function declaration. |
| 1087 | // The first argument to the attribute must be a string, |
| 1088 | // the name of the resource, for example "malloc". |
| 1089 | // The following arguments must be argument indexes, the arguments must be |
| 1090 | // of integer type for Returns, otherwise of pointer type. |
| 1091 | // 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] | 1092 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1093 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (!AL.getParameterName()) { |
| 1096 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1097 | << AL.getName()->getName() << 1; |
| 1098 | return; |
| 1099 | } |
| 1100 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1101 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1102 | switch (AL.getKind()) { |
| 1103 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1104 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1105 | if (AL.getNumArgs() < 1) { |
| 1106 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1107 | return; |
| 1108 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1109 | break; |
| 1110 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1111 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1112 | if (AL.getNumArgs() < 1) { |
| 1113 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1114 | return; |
| 1115 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1116 | break; |
| 1117 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1118 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1119 | if (AL.getNumArgs() > 1) { |
| 1120 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1121 | << AL.getNumArgs() + 1; |
| 1122 | return; |
| 1123 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1124 | break; |
| 1125 | default: |
| 1126 | // This should never happen given how we are called. |
| 1127 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1130 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1131 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1132 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1136 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1137 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1138 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1139 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1140 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1141 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1142 | |
| 1143 | // Normalize the argument, __foo__ becomes foo. |
| 1144 | if (Module.startswith("__") && Module.endswith("__")) |
| 1145 | Module = Module.substr(2, Module.size() - 4); |
| 1146 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1147 | SmallVector<unsigned, 10> OwnershipArgs; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1148 | |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1149 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 1150 | ++I) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1151 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1152 | Expr *IdxExpr = *I; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1153 | llvm::APSInt ArgNum(32); |
| 1154 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1155 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1156 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 1157 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 1158 | continue; |
| 1159 | } |
| 1160 | |
| 1161 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 1162 | |
| 1163 | if (x > NumArgs || x < 1) { |
| 1164 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1165 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 1166 | continue; |
| 1167 | } |
| 1168 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1169 | if (HasImplicitThisParam) { |
| 1170 | if (x == 0) { |
| 1171 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1172 | << "ownership" << IdxExpr->getSourceRange(); |
| 1173 | return; |
| 1174 | } |
| 1175 | --x; |
| 1176 | } |
| 1177 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1178 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1179 | case OwnershipAttr::Takes: |
| 1180 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1181 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1182 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1183 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1184 | // FIXME: Should also highlight argument in decl. |
| 1185 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1186 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1187 | << "pointer" |
| 1188 | << IdxExpr->getSourceRange(); |
| 1189 | continue; |
| 1190 | } |
| 1191 | break; |
| 1192 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1193 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1194 | if (AL.getNumArgs() > 1) { |
| 1195 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1196 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1197 | llvm::APSInt ArgNum(32); |
| 1198 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1199 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1200 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1201 | << "ownership_returns" << "integer" |
| 1202 | << IdxExpr->getSourceRange(); |
| 1203 | return; |
| 1204 | } |
| 1205 | } |
| 1206 | break; |
| 1207 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1208 | } // switch |
| 1209 | |
| 1210 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1211 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1212 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1213 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1214 | i != e; ++i) { |
| 1215 | if ((*i)->getOwnKind() != K) { |
| 1216 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1217 | I!=E; ++I) { |
| 1218 | if (x == *I) { |
| 1219 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1220 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | OwnershipArgs.push_back(x); |
| 1226 | } |
| 1227 | |
| 1228 | unsigned* start = OwnershipArgs.data(); |
| 1229 | unsigned size = OwnershipArgs.size(); |
| 1230 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1231 | |
| 1232 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 1233 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1234 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1235 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1236 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1237 | D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1238 | start, size)); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1241 | /// Whether this declaration has internal linkage for the purposes of |
| 1242 | /// things that want to complain about things not have internal linkage. |
| 1243 | static bool hasEffectivelyInternalLinkage(NamedDecl *D) { |
| 1244 | switch (D->getLinkage()) { |
| 1245 | case NoLinkage: |
| 1246 | case InternalLinkage: |
| 1247 | return true; |
| 1248 | |
| 1249 | // Template instantiations that go from external to unique-external |
| 1250 | // shouldn't get diagnosed. |
| 1251 | case UniqueExternalLinkage: |
| 1252 | return true; |
| 1253 | |
| 1254 | case ExternalLinkage: |
| 1255 | return false; |
| 1256 | } |
| 1257 | llvm_unreachable("unknown linkage kind!"); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1260 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1261 | // Check the attribute arguments. |
| 1262 | if (Attr.getNumArgs() > 1) { |
| 1263 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1264 | return; |
| 1265 | } |
| 1266 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1267 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1268 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1269 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1270 | return; |
| 1271 | } |
| 1272 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1273 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1274 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1275 | // gcc rejects |
| 1276 | // class c { |
| 1277 | // static int a __attribute__((weakref ("v2"))); |
| 1278 | // static int b() __attribute__((weakref ("f3"))); |
| 1279 | // }; |
| 1280 | // and ignores the attributes of |
| 1281 | // void f(void) { |
| 1282 | // static int a __attribute__((weakref ("v2"))); |
| 1283 | // } |
| 1284 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1285 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1286 | if (!Ctx->isFileContext()) { |
| 1287 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1288 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1289 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | // The GCC manual says |
| 1293 | // |
| 1294 | // At present, a declaration to which `weakref' is attached can only |
| 1295 | // be `static'. |
| 1296 | // |
| 1297 | // It also says |
| 1298 | // |
| 1299 | // Without a TARGET, |
| 1300 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1301 | // equivalent to `weak'. |
| 1302 | // |
| 1303 | // gcc 4.4.1 will accept |
| 1304 | // int a7 __attribute__((weakref)); |
| 1305 | // as |
| 1306 | // int a7 __attribute__((weak)); |
| 1307 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1308 | // it if this behaviour is actually used. |
| 1309 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1310 | if (!hasEffectivelyInternalLinkage(nd)) { |
| 1311 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1312 | return; |
| 1313 | } |
| 1314 | |
| 1315 | // GCC rejects |
| 1316 | // static ((alias ("y"), weakref)). |
| 1317 | // Should we? How to check that weakref is before or after alias? |
| 1318 | |
| 1319 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1320 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1321 | Arg = Arg->IgnoreParenCasts(); |
| 1322 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1323 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1324 | if (!Str || !Str->isAscii()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1325 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1326 | << "weakref" << 1; |
| 1327 | return; |
| 1328 | } |
| 1329 | // GCC will accept anything as the argument of weakref. Should we |
| 1330 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1331 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1332 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1335 | D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context)); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1338 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1339 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1340 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1341 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1342 | return; |
| 1343 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1344 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1345 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1346 | Arg = Arg->IgnoreParenCasts(); |
| 1347 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1348 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1349 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1350 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1351 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1352 | return; |
| 1353 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1354 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1355 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1356 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1357 | return; |
| 1358 | } |
| 1359 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1360 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1361 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1362 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1363 | Str->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1366 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1367 | // Check the attribute arguments. |
| 1368 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1369 | return; |
| 1370 | |
| 1371 | if (!isa<FunctionDecl>(D)) { |
| 1372 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1373 | << Attr.getName() << ExpectedFunction; |
| 1374 | return; |
| 1375 | } |
| 1376 | |
| 1377 | if (D->hasAttr<HotAttr>()) { |
| 1378 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1379 | << Attr.getName() << "hot"; |
| 1380 | return; |
| 1381 | } |
| 1382 | |
| 1383 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context)); |
| 1384 | } |
| 1385 | |
| 1386 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1387 | // Check the attribute arguments. |
| 1388 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1389 | return; |
| 1390 | |
| 1391 | if (!isa<FunctionDecl>(D)) { |
| 1392 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1393 | << Attr.getName() << ExpectedFunction; |
| 1394 | return; |
| 1395 | } |
| 1396 | |
| 1397 | if (D->hasAttr<ColdAttr>()) { |
| 1398 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1399 | << Attr.getName() << "cold"; |
| 1400 | return; |
| 1401 | } |
| 1402 | |
| 1403 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context)); |
| 1404 | } |
| 1405 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1406 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1407 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1408 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1409 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1410 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1411 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1412 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1413 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1414 | return; |
| 1415 | } |
| 1416 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1417 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1420 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1421 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1422 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1423 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1424 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1425 | return; |
| 1426 | } |
| 1427 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1428 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1429 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1430 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1431 | return; |
| 1432 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1433 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1434 | D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1437 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1438 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1439 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1440 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1441 | return; |
| 1442 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1444 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1446 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1447 | D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1448 | return; |
| 1449 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1452 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1455 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1456 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1457 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1458 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1459 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1460 | D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context)); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1463 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1464 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1465 | if (isa<VarDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1466 | D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context)); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1467 | else |
| 1468 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1469 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1472 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1473 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1474 | if (isa<VarDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1475 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context)); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1476 | else |
| 1477 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1478 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1481 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1482 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1483 | |
| 1484 | if (S.CheckNoReturnAttr(attr)) return; |
| 1485 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1486 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1487 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1488 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1489 | return; |
| 1490 | } |
| 1491 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1492 | D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1496 | if (attr.hasParameterOrArguments()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1497 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1498 | attr.setInvalid(); |
| 1499 | return true; |
| 1500 | } |
| 1501 | |
| 1502 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1505 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1506 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1507 | |
| 1508 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1509 | // because 'analyzer_noreturn' does not impact the type. |
| 1510 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1511 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1512 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1513 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1514 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1515 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1516 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1517 | && !VD->getType()->isFunctionPointerType())) { |
| 1518 | S.Diag(Attr.getLoc(), |
| 1519 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 1520 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1521 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1522 | return; |
| 1523 | } |
| 1524 | } |
| 1525 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1526 | D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1529 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1530 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1531 | /* |
| 1532 | Returning a Vector Class in Registers |
| 1533 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1534 | According to the PPU ABI specifications, a class with a single member of |
| 1535 | vector type is returned in memory when used as the return value of a function. |
| 1536 | This results in inefficient code when implementing vector classes. To return |
| 1537 | the value in a single vector register, add the vecreturn attribute to the |
| 1538 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1539 | |
| 1540 | Example: |
| 1541 | |
| 1542 | struct Vector |
| 1543 | { |
| 1544 | __vector float xyzw; |
| 1545 | } __attribute__((vecreturn)); |
| 1546 | |
| 1547 | Vector Add(Vector lhs, Vector rhs) |
| 1548 | { |
| 1549 | Vector result; |
| 1550 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1551 | return result; // This will be returned in a register |
| 1552 | } |
| 1553 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1554 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1555 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1556 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1557 | return; |
| 1558 | } |
| 1559 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1560 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1561 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1562 | return; |
| 1563 | } |
| 1564 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1565 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1566 | int count = 0; |
| 1567 | |
| 1568 | if (!isa<CXXRecordDecl>(record)) { |
| 1569 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1570 | return; |
| 1571 | } |
| 1572 | |
| 1573 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1574 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1575 | return; |
| 1576 | } |
| 1577 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1578 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1579 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1580 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1581 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1582 | return; |
| 1583 | } |
| 1584 | count++; |
| 1585 | } |
| 1586 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1587 | D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context)); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1590 | static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1591 | if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1592 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1593 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1594 | return; |
| 1595 | } |
| 1596 | // FIXME: Actually store the attribute on the declaration |
| 1597 | } |
| 1598 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1599 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1600 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1601 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1602 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1603 | return; |
| 1604 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1605 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1606 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1607 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1608 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1609 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1610 | return; |
| 1611 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1612 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1613 | D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1616 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1617 | const AttributeList &Attr) { |
| 1618 | // check the attribute arguments. |
| 1619 | if (Attr.hasParameterOrArguments()) { |
| 1620 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1621 | return; |
| 1622 | } |
| 1623 | |
| 1624 | if (!isa<FunctionDecl>(D)) { |
| 1625 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1626 | << Attr.getName() << ExpectedFunction; |
| 1627 | return; |
| 1628 | } |
| 1629 | |
| 1630 | D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context)); |
| 1631 | } |
| 1632 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1633 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1634 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1635 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1636 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1637 | return; |
| 1638 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1639 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1640 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1641 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1642 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1643 | return; |
| 1644 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1645 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1646 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1647 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1648 | return; |
| 1649 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1650 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1651 | D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1654 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1655 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1656 | if (Attr.getNumArgs() > 1) { |
| 1657 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1658 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1659 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1660 | |
| 1661 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1662 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1663 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1664 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1665 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1666 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1667 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1668 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1669 | return; |
| 1670 | } |
| 1671 | priority = Idx.getZExtValue(); |
| 1672 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1673 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1674 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1675 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1676 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1677 | return; |
| 1678 | } |
| 1679 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1680 | D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1681 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1684 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1685 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1686 | if (Attr.getNumArgs() > 1) { |
| 1687 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1688 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1689 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1690 | |
| 1691 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1692 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1693 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1694 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1695 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1696 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1697 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1698 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1699 | return; |
| 1700 | } |
| 1701 | priority = Idx.getZExtValue(); |
| 1702 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1703 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1704 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1705 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1706 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1707 | return; |
| 1708 | } |
| 1709 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1710 | D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1711 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1714 | template <typename AttrTy> |
| 1715 | static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr, |
| 1716 | const char *Name) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1717 | unsigned NumArgs = Attr.getNumArgs(); |
| 1718 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1719 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1720 | return; |
| 1721 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1722 | |
| 1723 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1724 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1725 | if (NumArgs == 1) { |
| 1726 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1727 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1728 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1729 | << Name; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1730 | return; |
| 1731 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1732 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1733 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1734 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1735 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str)); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1738 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1739 | const AttributeList &Attr) { |
| 1740 | unsigned NumArgs = Attr.getNumArgs(); |
| 1741 | if (NumArgs > 0) { |
| 1742 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1743 | return; |
| 1744 | } |
| 1745 | |
| 1746 | D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr( |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1747 | Attr.getRange(), S.Context)); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1750 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 1751 | const AttributeList &Attr) { |
| 1752 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1753 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1754 | return; |
| 1755 | } |
| 1756 | |
| 1757 | unsigned NumArgs = Attr.getNumArgs(); |
| 1758 | if (NumArgs > 0) { |
| 1759 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1760 | return; |
| 1761 | } |
| 1762 | |
| 1763 | D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context)); |
| 1764 | } |
| 1765 | |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1766 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1767 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 1768 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1769 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 1770 | return; |
| 1771 | } |
| 1772 | |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1773 | unsigned NumArgs = Attr.getNumArgs(); |
| 1774 | if (NumArgs > 0) { |
| 1775 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1776 | return; |
| 1777 | } |
| 1778 | |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1779 | D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr( |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1780 | Attr.getRange(), S.Context)); |
| 1781 | } |
| 1782 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1783 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1784 | IdentifierInfo *Platform, |
| 1785 | VersionTuple Introduced, |
| 1786 | VersionTuple Deprecated, |
| 1787 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1788 | StringRef PlatformName |
| 1789 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1790 | if (PlatformName.empty()) |
| 1791 | PlatformName = Platform->getName(); |
| 1792 | |
| 1793 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1794 | // of these steps are needed). |
| 1795 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1796 | !(Introduced <= Deprecated)) { |
| 1797 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1798 | << 1 << PlatformName << Deprecated.getAsString() |
| 1799 | << 0 << Introduced.getAsString(); |
| 1800 | return true; |
| 1801 | } |
| 1802 | |
| 1803 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1804 | !(Introduced <= Obsoleted)) { |
| 1805 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1806 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1807 | << 0 << Introduced.getAsString(); |
| 1808 | return true; |
| 1809 | } |
| 1810 | |
| 1811 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1812 | !(Deprecated <= Obsoleted)) { |
| 1813 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1814 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1815 | << 1 << Deprecated.getAsString(); |
| 1816 | return true; |
| 1817 | } |
| 1818 | |
| 1819 | return false; |
| 1820 | } |
| 1821 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1822 | AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range, |
| 1823 | IdentifierInfo *Platform, |
| 1824 | VersionTuple Introduced, |
| 1825 | VersionTuple Deprecated, |
| 1826 | VersionTuple Obsoleted, |
| 1827 | bool IsUnavailable, |
| 1828 | StringRef Message) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1829 | VersionTuple MergedIntroduced = Introduced; |
| 1830 | VersionTuple MergedDeprecated = Deprecated; |
| 1831 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1832 | bool FoundAny = false; |
| 1833 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1834 | if (D->hasAttrs()) { |
| 1835 | AttrVec &Attrs = D->getAttrs(); |
| 1836 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1837 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1838 | if (!OldAA) { |
| 1839 | ++i; |
| 1840 | continue; |
| 1841 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1842 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1843 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1844 | if (OldPlatform != Platform) { |
| 1845 | ++i; |
| 1846 | continue; |
| 1847 | } |
| 1848 | |
| 1849 | FoundAny = true; |
| 1850 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1851 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1852 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1853 | bool OldIsUnavailable = OldAA->getUnavailable(); |
| 1854 | StringRef OldMessage = OldAA->getMessage(); |
| 1855 | |
| 1856 | if ((!OldIntroduced.empty() && !Introduced.empty() && |
| 1857 | OldIntroduced != Introduced) || |
| 1858 | (!OldDeprecated.empty() && !Deprecated.empty() && |
| 1859 | OldDeprecated != Deprecated) || |
| 1860 | (!OldObsoleted.empty() && !Obsoleted.empty() && |
| 1861 | OldObsoleted != Obsoleted) || |
| 1862 | (OldIsUnavailable != IsUnavailable) || |
| 1863 | (OldMessage != Message)) { |
| 1864 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1865 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1866 | Attrs.erase(Attrs.begin() + i); |
| 1867 | --e; |
| 1868 | continue; |
| 1869 | } |
| 1870 | |
| 1871 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1872 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1873 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1874 | |
| 1875 | if (MergedIntroduced2.empty()) |
| 1876 | MergedIntroduced2 = OldIntroduced; |
| 1877 | if (MergedDeprecated2.empty()) |
| 1878 | MergedDeprecated2 = OldDeprecated; |
| 1879 | if (MergedObsoleted2.empty()) |
| 1880 | MergedObsoleted2 = OldObsoleted; |
| 1881 | |
| 1882 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1883 | MergedIntroduced2, MergedDeprecated2, |
| 1884 | MergedObsoleted2)) { |
| 1885 | Attrs.erase(Attrs.begin() + i); |
| 1886 | --e; |
| 1887 | continue; |
| 1888 | } |
| 1889 | |
| 1890 | MergedIntroduced = MergedIntroduced2; |
| 1891 | MergedDeprecated = MergedDeprecated2; |
| 1892 | MergedObsoleted = MergedObsoleted2; |
| 1893 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1894 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | if (FoundAny && |
| 1898 | MergedIntroduced == Introduced && |
| 1899 | MergedDeprecated == Deprecated && |
| 1900 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1901 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1902 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1903 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1904 | MergedDeprecated, MergedObsoleted)) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1905 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1906 | Introduced, Deprecated, |
| 1907 | Obsoleted, IsUnavailable, Message); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1908 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1909 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1912 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1913 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1914 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 1915 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
| 1916 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1917 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1918 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 1919 | << Platform; |
| 1920 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1921 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1922 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1923 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1924 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1925 | StringRef Str; |
| 1926 | const StringLiteral *SE = |
| 1927 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 1928 | if (SE) |
| 1929 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1930 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1931 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(), |
| 1932 | Platform, |
| 1933 | Introduced.Version, |
| 1934 | Deprecated.Version, |
| 1935 | Obsoleted.Version, |
| 1936 | IsUnavailable, Str); |
| 1937 | if (NewAttr) |
| 1938 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1941 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
| 1942 | VisibilityAttr::VisibilityType Vis) { |
Rafael Espindola | dd44f34 | 2012-05-10 03:01:34 +0000 | [diff] [blame] | 1943 | if (isa<TypedefNameDecl>(D)) { |
| 1944 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility"; |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1945 | return NULL; |
Rafael Espindola | dd44f34 | 2012-05-10 03:01:34 +0000 | [diff] [blame] | 1946 | } |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1947 | VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>(); |
| 1948 | if (ExistingAttr) { |
| 1949 | VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility(); |
| 1950 | if (ExistingVis == Vis) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1951 | return NULL; |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1952 | Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility); |
| 1953 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1954 | D->dropAttr<VisibilityAttr>(); |
| 1955 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1956 | return ::new (Context) VisibilityAttr(Range, Context, Vis); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1959 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1960 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1961 | if(!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1962 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1963 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1964 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1965 | Arg = Arg->IgnoreParenCasts(); |
| 1966 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1967 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1968 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1969 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1970 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1971 | return; |
| 1972 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1973 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1974 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1975 | VisibilityAttr::VisibilityType type; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1976 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1977 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1978 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1979 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1980 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1981 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1982 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 1983 | else if (TypeStr == "protected") { |
| 1984 | // Complain about attempts to use protected visibility on targets |
| 1985 | // (like Darwin) that don't support it. |
| 1986 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 1987 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 1988 | type = VisibilityAttr::Default; |
| 1989 | } else { |
| 1990 | type = VisibilityAttr::Protected; |
| 1991 | } |
| 1992 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1993 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1994 | return; |
| 1995 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1996 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1997 | VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type); |
| 1998 | if (NewAttr) |
| 1999 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2002 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2003 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2004 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2005 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2006 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2007 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2008 | return; |
| 2009 | } |
| 2010 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2011 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2012 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 2013 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2014 | << "objc_method_family" << 1; |
| 2015 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2016 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2017 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2018 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2022 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2023 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2024 | if (param == "none") |
| 2025 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2026 | else if (param == "alloc") |
| 2027 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2028 | else if (param == "copy") |
| 2029 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2030 | else if (param == "init") |
| 2031 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2032 | else if (param == "mutableCopy") |
| 2033 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2034 | else if (param == "new") |
| 2035 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2036 | else { |
| 2037 | // Just warn and ignore it. This is future-proof against new |
| 2038 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2039 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2040 | return; |
| 2041 | } |
| 2042 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2043 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2044 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2045 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2046 | << method->getResultType(); |
| 2047 | // Ignore the attribute. |
| 2048 | return; |
| 2049 | } |
| 2050 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2051 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2052 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2055 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2056 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2057 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2058 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2059 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2060 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2061 | if (OCI == 0) { |
| 2062 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 2063 | return; |
| 2064 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2065 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2066 | D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2069 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2070 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2071 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2072 | return; |
| 2073 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2074 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2075 | QualType T = TD->getUnderlyingType(); |
| 2076 | if (!T->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2077 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2078 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2079 | return; |
| 2080 | } |
| 2081 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2082 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2083 | QualType T = PD->getType(); |
| 2084 | if (!T->isPointerType() || |
| 2085 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
| 2086 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2087 | return; |
| 2088 | } |
| 2089 | } |
| 2090 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2091 | // It is okay to include this attribute on properties, e.g.: |
| 2092 | // |
| 2093 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2094 | // |
| 2095 | // In this case it follows tradition and suppresses an error in the above |
| 2096 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2097 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2098 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2099 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context)); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2102 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2103 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2104 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2105 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2106 | return; |
| 2107 | } |
| 2108 | |
| 2109 | if (!isa<FunctionDecl>(D)) { |
| 2110 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2111 | return; |
| 2112 | } |
| 2113 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2114 | D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2117 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2118 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2119 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2120 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2121 | return; |
| 2122 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2123 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2124 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2125 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2126 | return; |
| 2127 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2128 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2129 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2130 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2131 | type = BlocksAttr::ByRef; |
| 2132 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2133 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2134 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2135 | return; |
| 2136 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2137 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2138 | D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type)); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2141 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2142 | // check the attribute arguments. |
| 2143 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2144 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2145 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2148 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2149 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2150 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2151 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2152 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2153 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2154 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2155 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2156 | return; |
| 2157 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2158 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2159 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2160 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2161 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2162 | return; |
| 2163 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2164 | |
| 2165 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2168 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2169 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2170 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2171 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2172 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2173 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2174 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2175 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2176 | return; |
| 2177 | } |
| 2178 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2179 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2180 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2181 | // FIXME: This error message could be improved, it would be nice |
| 2182 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2183 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2184 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2185 | return; |
| 2186 | } |
| 2187 | } |
| 2188 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2189 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2190 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2191 | if (isa<FunctionNoProtoType>(FT)) { |
| 2192 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2193 | return; |
| 2194 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2195 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2196 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2197 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2198 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2199 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2200 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2201 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2202 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2203 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2204 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2205 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2206 | if (!BD->isVariadic()) { |
| 2207 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2208 | return; |
| 2209 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2210 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2211 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2212 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2213 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2214 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2215 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2216 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2217 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2218 | return; |
| 2219 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2220 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2221 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2222 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2223 | return; |
| 2224 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2225 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2226 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2227 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2228 | return; |
| 2229 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2230 | D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2231 | nullPos)); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2234 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2235 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2236 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2237 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2238 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2239 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2240 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2241 | << Attr.getName() << ExpectedFunctionOrMethod; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2242 | return; |
| 2243 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2244 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2245 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2246 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2247 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2248 | return; |
| 2249 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2250 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2251 | if (MD->getResultType()->isVoidType()) { |
| 2252 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2253 | << Attr.getName() << 1; |
| 2254 | return; |
| 2255 | } |
| 2256 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2257 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2260 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2261 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2262 | if (Attr.hasParameterOrArguments()) { |
| 2263 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2264 | return; |
| 2265 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2266 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2267 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2268 | if (isa<CXXRecordDecl>(D)) { |
| 2269 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2270 | return; |
| 2271 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2272 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2273 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2274 | return; |
| 2275 | } |
| 2276 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2277 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2278 | |
| 2279 | // 'weak' only applies to declarations with external linkage. |
| 2280 | if (hasEffectivelyInternalLinkage(nd)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2281 | S.Diag(Attr.getLoc(), diag::err_attribute_weak_static); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2282 | return; |
| 2283 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2284 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2285 | nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2288 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2289 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2290 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2291 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2292 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2293 | |
| 2294 | // weak_import only applies to variable & function declarations. |
| 2295 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2296 | if (!D->canBeWeakImported(isDef)) { |
| 2297 | if (isDef) |
| 2298 | S.Diag(Attr.getLoc(), |
| 2299 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 2300 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2301 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2302 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2303 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2304 | // Nothing to warn about here. |
| 2305 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2306 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2307 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2308 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2309 | return; |
| 2310 | } |
| 2311 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2312 | D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2315 | static void handleReqdWorkGroupSize(Sema &S, Decl *D, |
| 2316 | const AttributeList &Attr) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2317 | // Attribute has 3 arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2318 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2319 | return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2320 | |
| 2321 | unsigned WGSize[3]; |
| 2322 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2323 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2324 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2325 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2326 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2327 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2328 | << "reqd_work_group_size" << E->getSourceRange(); |
| 2329 | return; |
| 2330 | } |
| 2331 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2332 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2333 | D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2334 | WGSize[0], WGSize[1], |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2335 | WGSize[2])); |
| 2336 | } |
| 2337 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2338 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
| 2339 | StringRef Name) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2340 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2341 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2342 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2343 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2344 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2345 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2346 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2347 | return ::new (Context) SectionAttr(Range, Context, Name); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2350 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2351 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2352 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2353 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2354 | |
| 2355 | // Make sure that there is a string literal as the sections's single |
| 2356 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2357 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2358 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2359 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2360 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2361 | return; |
| 2362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2364 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2365 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2366 | if (!Error.empty()) { |
| 2367 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2368 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2369 | return; |
| 2370 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2372 | // This attribute cannot be applied to local variables. |
| 2373 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2374 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2375 | return; |
| 2376 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2377 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
| 2378 | SE->getString()); |
| 2379 | if (NewAttr) |
| 2380 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2383 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2384 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2385 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2386 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2387 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2388 | return; |
| 2389 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2390 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2391 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2392 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2393 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2394 | } else { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2395 | D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2396 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2399 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2400 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2401 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2402 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2403 | return; |
| 2404 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2405 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2406 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2407 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2408 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2409 | } else { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2410 | D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2411 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2414 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2415 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2416 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2417 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2418 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2419 | D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2422 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2423 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2424 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2425 | return; |
| 2426 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2427 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2428 | if (Attr.getNumArgs() != 0) { |
| 2429 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2430 | return; |
| 2431 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2432 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2433 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2434 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2435 | if (!VD || !VD->hasLocalStorage()) { |
| 2436 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2437 | return; |
| 2438 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2439 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2440 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2441 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2442 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2443 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2444 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2445 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2446 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2447 | Attr.getParameterName(); |
| 2448 | return; |
| 2449 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2450 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2451 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2452 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2453 | S.Diag(Attr.getParameterLoc(), |
| 2454 | diag::err_attribute_cleanup_arg_not_function) |
| 2455 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2456 | return; |
| 2457 | } |
| 2458 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2459 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2460 | S.Diag(Attr.getParameterLoc(), |
| 2461 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2462 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2463 | return; |
| 2464 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2465 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2466 | // We're currently more strict than GCC about what function types we accept. |
| 2467 | // If this ever proves to be a problem it should be easy to fix. |
| 2468 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2469 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2470 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2471 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2472 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2473 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2474 | Attr.getParameterName() << ParamTy << Ty; |
| 2475 | return; |
| 2476 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2477 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2478 | D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD)); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2479 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2482 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 2483 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2484 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2485 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2486 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2487 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2488 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2489 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2490 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2491 | return; |
| 2492 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2493 | |
| 2494 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2495 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2496 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2497 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2498 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2499 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2500 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2501 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2502 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2503 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2504 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2505 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2506 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2507 | return; |
| 2508 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2509 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2510 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 2511 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 2512 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2513 | return; |
| 2514 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2515 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2516 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2517 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2518 | if (HasImplicitThisParam) { |
| 2519 | if (ArgIdx == 0) { |
| 2520 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 2521 | << "format_arg" << IdxExpr->getSourceRange(); |
| 2522 | return; |
| 2523 | } |
| 2524 | ArgIdx--; |
| 2525 | } |
| 2526 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2527 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2528 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2529 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2530 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2531 | if (not_nsstring_type && |
| 2532 | !isCFStringType(Ty, S.Context) && |
| 2533 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2534 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2535 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2536 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2537 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2538 | << IdxExpr->getSourceRange(); |
| 2539 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2540 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2541 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2542 | if (!isNSStringType(Ty, S.Context) && |
| 2543 | !isCFStringType(Ty, S.Context) && |
| 2544 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2545 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2546 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2547 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2548 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2549 | << IdxExpr->getSourceRange(); |
| 2550 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2553 | D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context, |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2554 | Idx.getZExtValue())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2557 | enum FormatAttrKind { |
| 2558 | CFStringFormat, |
| 2559 | NSStringFormat, |
| 2560 | StrftimeFormat, |
| 2561 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2562 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2563 | InvalidFormat |
| 2564 | }; |
| 2565 | |
| 2566 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2567 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2568 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2569 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2570 | // Check for formats that get handled specially. |
| 2571 | .Case("NSString", NSStringFormat) |
| 2572 | .Case("CFString", CFStringFormat) |
| 2573 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2574 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2575 | // Otherwise, check for supported formats. |
| 2576 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2577 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2578 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2579 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2580 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2581 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2584 | /// Handle __attribute__((init_priority(priority))) attributes based on |
| 2585 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2586 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2587 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2588 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2589 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2590 | return; |
| 2591 | } |
| 2592 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2593 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2594 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2595 | Attr.setInvalid(); |
| 2596 | return; |
| 2597 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2598 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2599 | if (S.Context.getAsArrayType(T)) |
| 2600 | T = S.Context.getBaseElementType(T); |
| 2601 | if (!T->getAs<RecordType>()) { |
| 2602 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2603 | Attr.setInvalid(); |
| 2604 | return; |
| 2605 | } |
| 2606 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2607 | if (Attr.getNumArgs() != 1) { |
| 2608 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2609 | Attr.setInvalid(); |
| 2610 | return; |
| 2611 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2612 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2613 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2614 | llvm::APSInt priority(32); |
| 2615 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 2616 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 2617 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2618 | << "init_priority" << priorityExpr->getSourceRange(); |
| 2619 | Attr.setInvalid(); |
| 2620 | return; |
| 2621 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 2622 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2623 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2624 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 2625 | << priorityExpr->getSourceRange(); |
| 2626 | Attr.setInvalid(); |
| 2627 | return; |
| 2628 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2629 | D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2630 | prioritynum)); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2633 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
| 2634 | int FormatIdx, int FirstArg) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2635 | // Check whether we already have an equivalent format attribute. |
| 2636 | for (specific_attr_iterator<FormatAttr> |
| 2637 | i = D->specific_attr_begin<FormatAttr>(), |
| 2638 | e = D->specific_attr_end<FormatAttr>(); |
| 2639 | i != e ; ++i) { |
| 2640 | FormatAttr *f = *i; |
| 2641 | if (f->getType() == Format && |
| 2642 | f->getFormatIdx() == FormatIdx && |
| 2643 | f->getFirstArg() == FirstArg) { |
| 2644 | // If we don't have a valid location for this attribute, adopt the |
| 2645 | // location. |
| 2646 | if (f->getLocation().isInvalid()) |
| 2647 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2648 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2649 | } |
| 2650 | } |
| 2651 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2652 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2653 | FirstArg); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2656 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 2657 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2658 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2659 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2660 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2661 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2662 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2663 | return; |
| 2664 | } |
| 2665 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2666 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2667 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2668 | return; |
| 2669 | } |
| 2670 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2671 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2672 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2673 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2674 | return; |
| 2675 | } |
| 2676 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2677 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2678 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2679 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2680 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2681 | unsigned FirstIdx = 1; |
| 2682 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2683 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2684 | |
| 2685 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2686 | if (Format.startswith("__") && Format.endswith("__")) |
| 2687 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2688 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2689 | // Check for supported formats. |
| 2690 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2691 | |
| 2692 | if (Kind == IgnoredFormat) |
| 2693 | return; |
| 2694 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2695 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2696 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2697 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2698 | return; |
| 2699 | } |
| 2700 | |
| 2701 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2702 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2703 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2704 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2705 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2706 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2707 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2708 | return; |
| 2709 | } |
| 2710 | |
| 2711 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2712 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2713 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2714 | return; |
| 2715 | } |
| 2716 | |
| 2717 | // FIXME: Do we need to bounds check? |
| 2718 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2719 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2720 | if (HasImplicitThisParam) { |
| 2721 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2722 | S.Diag(Attr.getLoc(), |
| 2723 | diag::err_format_attribute_implicit_this_format_string) |
| 2724 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2725 | return; |
| 2726 | } |
| 2727 | ArgIdx--; |
| 2728 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2730 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2731 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2732 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2733 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2734 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2735 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2736 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2737 | return; |
| 2738 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2739 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2740 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2741 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2742 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2743 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2744 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2745 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2746 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2747 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2748 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2749 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2750 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2751 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2752 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2753 | return; |
| 2754 | } |
| 2755 | |
| 2756 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2757 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2758 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2759 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 2760 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2761 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2762 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2763 | return; |
| 2764 | } |
| 2765 | |
| 2766 | // check if the function is variadic if the 3rd argument non-zero |
| 2767 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2768 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2769 | ++NumArgs; // +1 for ... |
| 2770 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2771 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2772 | return; |
| 2773 | } |
| 2774 | } |
| 2775 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2776 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2777 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2778 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2779 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2780 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2781 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2782 | return; |
| 2783 | } |
| 2784 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2785 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2786 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2787 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2788 | return; |
| 2789 | } |
| 2790 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2791 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 2792 | Idx.getZExtValue(), |
| 2793 | FirstArg.getZExtValue()); |
| 2794 | if (NewAttr) |
| 2795 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2798 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2799 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2800 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2801 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2802 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2803 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2804 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2805 | // Try to find the underlying union declaration. |
| 2806 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2807 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2808 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2809 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2810 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2811 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2812 | |
| 2813 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2814 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2815 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2816 | return; |
| 2817 | } |
| 2818 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2819 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2820 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2821 | diag::warn_transparent_union_attribute_not_definition); |
| 2822 | return; |
| 2823 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2824 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2825 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2826 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2827 | if (Field == FieldEnd) { |
| 2828 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2829 | return; |
| 2830 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2831 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2832 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2833 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2834 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2835 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2836 | diag::warn_transparent_union_attribute_floating) |
| 2837 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2838 | return; |
| 2839 | } |
| 2840 | |
| 2841 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2842 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2843 | for (; Field != FieldEnd; ++Field) { |
| 2844 | QualType FieldType = Field->getType(); |
| 2845 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 2846 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 2847 | // Warn if we drop the attribute. |
| 2848 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2849 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2850 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2851 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2852 | diag::warn_transparent_union_attribute_field_size_align) |
| 2853 | << isSize << Field->getDeclName() << FieldBits; |
| 2854 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2855 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2856 | diag::note_transparent_union_first_field_size_align) |
| 2857 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2858 | return; |
| 2859 | } |
| 2860 | } |
| 2861 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2862 | RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2865 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2866 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2867 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2868 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2869 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2870 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2871 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2872 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2873 | // Make sure that there is a string literal as the annotation's single |
| 2874 | // argument. |
| 2875 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2876 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2877 | return; |
| 2878 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2879 | |
| 2880 | // Don't duplicate annotations that are already set. |
| 2881 | for (specific_attr_iterator<AnnotateAttr> |
| 2882 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 2883 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 2884 | if ((*i)->getAnnotation() == SE->getString()) |
| 2885 | return; |
| 2886 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2887 | D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2888 | SE->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2891 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2892 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2893 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2894 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2895 | return; |
| 2896 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2897 | |
| 2898 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 2899 | // than GNU's, and should error out when it is used to specify a |
| 2900 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2901 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2902 | if (Attr.getNumArgs() == 0) { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2903 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2904 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2905 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2906 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2907 | S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2910 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) { |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2911 | // FIXME: Handle pack-expansions here. |
| 2912 | if (DiagnoseUnexpandedParameterPack(E)) |
| 2913 | return; |
| 2914 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2915 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2916 | // Save dependent expressions in the AST to be instantiated. |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2917 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2918 | return; |
| 2919 | } |
| 2920 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2921 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2922 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2923 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2924 | ExprResult ICE |
| 2925 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2926 | diag::err_aligned_attribute_argument_not_int, |
| 2927 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2928 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2929 | return; |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2930 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2931 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2932 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2933 | return; |
| 2934 | } |
| 2935 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2936 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take())); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2939 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2940 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2941 | // FIXME: Perform checking of type validity |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2942 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS)); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2943 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2944 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2945 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2946 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2947 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2948 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2949 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2950 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2951 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2952 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2953 | // This attribute isn't documented, but glibc uses it. It changes |
| 2954 | // the width of an int or unsigned int to the specified size. |
| 2955 | |
| 2956 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2957 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2958 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2959 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2960 | |
| 2961 | IdentifierInfo *Name = Attr.getParameterName(); |
| 2962 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2963 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2964 | return; |
| 2965 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2966 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2967 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2968 | |
| 2969 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2970 | if (Str.startswith("__") && Str.endswith("__")) |
| 2971 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2972 | |
| 2973 | unsigned DestWidth = 0; |
| 2974 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2975 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2976 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2977 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2978 | switch (Str[0]) { |
| 2979 | case 'Q': DestWidth = 8; break; |
| 2980 | case 'H': DestWidth = 16; break; |
| 2981 | case 'S': DestWidth = 32; break; |
| 2982 | case 'D': DestWidth = 64; break; |
| 2983 | case 'X': DestWidth = 96; break; |
| 2984 | case 'T': DestWidth = 128; break; |
| 2985 | } |
| 2986 | if (Str[1] == 'F') { |
| 2987 | IntegerMode = false; |
| 2988 | } else if (Str[1] == 'C') { |
| 2989 | IntegerMode = false; |
| 2990 | ComplexMode = true; |
| 2991 | } else if (Str[1] != 'I') { |
| 2992 | DestWidth = 0; |
| 2993 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2994 | break; |
| 2995 | case 4: |
| 2996 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2997 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2998 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2999 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3000 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3001 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3002 | break; |
| 3003 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3004 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3005 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3006 | break; |
| 3007 | } |
| 3008 | |
| 3009 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3010 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3011 | OldTy = TD->getUnderlyingType(); |
| 3012 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3013 | OldTy = VD->getType(); |
| 3014 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3015 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3016 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3017 | return; |
| 3018 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3019 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3020 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3021 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3022 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3023 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3024 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3025 | } else if (ComplexMode) { |
| 3026 | if (!OldTy->isComplexType()) |
| 3027 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3028 | } else { |
| 3029 | if (!OldTy->isFloatingType()) |
| 3030 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3031 | } |
| 3032 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3033 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3034 | // and friends, at least with glibc. |
| 3035 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3036 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3037 | // FIXME: Make sure floating-point mappings are accurate |
| 3038 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3039 | QualType NewTy; |
| 3040 | switch (DestWidth) { |
| 3041 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3042 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3043 | return; |
| 3044 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3045 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3046 | return; |
| 3047 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3048 | if (!IntegerMode) { |
| 3049 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3050 | return; |
| 3051 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3052 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3053 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3054 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3055 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3056 | break; |
| 3057 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3058 | if (!IntegerMode) { |
| 3059 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3060 | return; |
| 3061 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3062 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3063 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3064 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3065 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3066 | break; |
| 3067 | case 32: |
| 3068 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3069 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3070 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3071 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3072 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3073 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3074 | break; |
| 3075 | case 64: |
| 3076 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3077 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3078 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3079 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3080 | NewTy = S.Context.LongTy; |
| 3081 | else |
| 3082 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3083 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3084 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3085 | NewTy = S.Context.UnsignedLongTy; |
| 3086 | else |
| 3087 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3088 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3089 | case 96: |
| 3090 | NewTy = S.Context.LongDoubleTy; |
| 3091 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3092 | case 128: |
| 3093 | if (!IntegerMode) { |
| 3094 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3095 | return; |
| 3096 | } |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 3097 | if (OldTy->isSignedIntegerType()) |
| 3098 | NewTy = S.Context.Int128Ty; |
| 3099 | else |
| 3100 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3101 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3104 | if (ComplexMode) { |
| 3105 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | // Install the new type. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3109 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3110 | // FIXME: preserve existing source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3111 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3112 | } else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3113 | cast<ValueDecl>(D)->setType(NewTy); |
| 3114 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3115 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3116 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3117 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3118 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3119 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3120 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3121 | if (!isFunctionOrMethod(D)) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3122 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3123 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3124 | return; |
| 3125 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3126 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3127 | D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3130 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3131 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3132 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3133 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3134 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3135 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3136 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3137 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3138 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3139 | return; |
| 3140 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3141 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3142 | D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3145 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3146 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3147 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3148 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3149 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3150 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3151 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3152 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3153 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3154 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3155 | return; |
| 3156 | } |
| 3157 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3158 | D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3159 | S.Context)); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3162 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3163 | if (S.LangOpts.CUDA) { |
| 3164 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3165 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3166 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3167 | return; |
| 3168 | } |
| 3169 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3170 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3171 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3172 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3173 | return; |
| 3174 | } |
| 3175 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3176 | D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3177 | } else { |
| 3178 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3179 | } |
| 3180 | } |
| 3181 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3182 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3183 | if (S.LangOpts.CUDA) { |
| 3184 | // check the attribute arguments. |
| 3185 | if (Attr.getNumArgs() != 0) { |
| 3186 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3187 | return; |
| 3188 | } |
| 3189 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3190 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3191 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3192 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3193 | return; |
| 3194 | } |
| 3195 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3196 | D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3197 | } else { |
| 3198 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3199 | } |
| 3200 | } |
| 3201 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3202 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3203 | if (S.LangOpts.CUDA) { |
| 3204 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3205 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3206 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3207 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3208 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3209 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3210 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3211 | return; |
| 3212 | } |
| 3213 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3214 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3215 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3216 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3217 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 3218 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3219 | << FD->getType() |
| 3220 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 3221 | "void"); |
| 3222 | } else { |
| 3223 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3224 | << FD->getType(); |
| 3225 | } |
| 3226 | return; |
| 3227 | } |
| 3228 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3229 | D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3230 | } else { |
| 3231 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3232 | } |
| 3233 | } |
| 3234 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3235 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3236 | if (S.LangOpts.CUDA) { |
| 3237 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3238 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3239 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3240 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3241 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3242 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3243 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3244 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3245 | return; |
| 3246 | } |
| 3247 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3248 | D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3249 | } else { |
| 3250 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3251 | } |
| 3252 | } |
| 3253 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3254 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3255 | if (S.LangOpts.CUDA) { |
| 3256 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3257 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3258 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3259 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3260 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3261 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3262 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3263 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3264 | return; |
| 3265 | } |
| 3266 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3267 | D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3268 | } else { |
| 3269 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3270 | } |
| 3271 | } |
| 3272 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3273 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3274 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3275 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3276 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3277 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3278 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3279 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3280 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3281 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3282 | return; |
| 3283 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3284 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3285 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3286 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3287 | return; |
| 3288 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3289 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3290 | D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3293 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3294 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3295 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3296 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3297 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3298 | CallingConv CC; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3299 | if (S.CheckCallingConvAttr(Attr, CC)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3300 | return; |
| 3301 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3302 | if (!isa<ObjCMethodDecl>(D)) { |
| 3303 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3304 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3305 | return; |
| 3306 | } |
| 3307 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3308 | switch (Attr.getKind()) { |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3309 | case AttributeList::AT_FastCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3310 | D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3311 | return; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3312 | case AttributeList::AT_StdCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3313 | D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3314 | return; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3315 | case AttributeList::AT_ThisCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3316 | D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3317 | return; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3318 | case AttributeList::AT_CDecl: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3319 | D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3320 | return; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3321 | case AttributeList::AT_Pascal: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3322 | D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context)); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3323 | return; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3324 | case AttributeList::AT_Pcs: { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3325 | Expr *Arg = Attr.getArg(0); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3326 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3327 | if (!Str || !Str->isAscii()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3328 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3329 | << "pcs" << 1; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3330 | Attr.setInvalid(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3331 | return; |
| 3332 | } |
| 3333 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3334 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3335 | PcsAttr::PCSType PCS; |
| 3336 | if (StrRef == "aapcs") |
| 3337 | PCS = PcsAttr::AAPCS; |
| 3338 | else if (StrRef == "aapcs-vfp") |
| 3339 | PCS = PcsAttr::AAPCS_VFP; |
| 3340 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3341 | S.Diag(Attr.getLoc(), diag::err_invalid_pcs); |
| 3342 | Attr.setInvalid(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3343 | return; |
| 3344 | } |
| 3345 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3346 | D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS)); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3347 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3348 | default: |
| 3349 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3350 | } |
| 3351 | } |
| 3352 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3353 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 3354 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3355 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3358 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { |
| 3359 | if (attr.isInvalid()) |
| 3360 | return true; |
| 3361 | |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3362 | if ((attr.getNumArgs() != 0 && |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3363 | !(attr.getKind() == AttributeList::AT_Pcs && attr.getNumArgs() == 1)) || |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3364 | attr.getParameterName()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3365 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3366 | attr.setInvalid(); |
| 3367 | return true; |
| 3368 | } |
| 3369 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3370 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3371 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3372 | switch (attr.getKind()) { |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3373 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3374 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3375 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3376 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3377 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 3378 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3379 | Expr *Arg = attr.getArg(0); |
| 3380 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3381 | if (!Str || !Str->isAscii()) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3382 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3383 | << "pcs" << 1; |
| 3384 | attr.setInvalid(); |
| 3385 | return true; |
| 3386 | } |
| 3387 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3388 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3389 | if (StrRef == "aapcs") { |
| 3390 | CC = CC_AAPCS; |
| 3391 | break; |
| 3392 | } else if (StrRef == "aapcs-vfp") { |
| 3393 | CC = CC_AAPCS_VFP; |
| 3394 | break; |
| 3395 | } |
| 3396 | // FALLS THROUGH |
| 3397 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3398 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | return false; |
| 3402 | } |
| 3403 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3404 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3405 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3406 | |
| 3407 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3408 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3409 | return; |
| 3410 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3411 | if (!isa<ObjCMethodDecl>(D)) { |
| 3412 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3413 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3414 | return; |
| 3415 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3416 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3417 | D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3421 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3422 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3423 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3424 | return true; |
| 3425 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3426 | if (Attr.getNumArgs() != 1) { |
| 3427 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3428 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3429 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3430 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3431 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3432 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3433 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3434 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3435 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3436 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3437 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3438 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3439 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3442 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3443 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3444 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3445 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3446 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3449 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3450 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3451 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3452 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3453 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3454 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3457 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3460 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3461 | if (S.LangOpts.CUDA) { |
| 3462 | // check the attribute arguments. |
| 3463 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 3464 | // FIXME: 0 is not okay. |
| 3465 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3466 | return; |
| 3467 | } |
| 3468 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3469 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3470 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3471 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3472 | return; |
| 3473 | } |
| 3474 | |
| 3475 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 3476 | llvm::APSInt MaxThreads(32); |
| 3477 | if (MaxThreadsExpr->isTypeDependent() || |
| 3478 | MaxThreadsExpr->isValueDependent() || |
| 3479 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 3480 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3481 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 3482 | return; |
| 3483 | } |
| 3484 | |
| 3485 | llvm::APSInt MinBlocks(32); |
| 3486 | if (Attr.getNumArgs() > 1) { |
| 3487 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 3488 | if (MinBlocksExpr->isTypeDependent() || |
| 3489 | MinBlocksExpr->isValueDependent() || |
| 3490 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 3491 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3492 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 3493 | return; |
| 3494 | } |
| 3495 | } |
| 3496 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3497 | D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3498 | MaxThreads.getZExtValue(), |
| 3499 | MinBlocks.getZExtValue())); |
| 3500 | } else { |
| 3501 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 3502 | } |
| 3503 | } |
| 3504 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3505 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3506 | // Checker-specific attribute handlers. |
| 3507 | //===----------------------------------------------------------------------===// |
| 3508 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3509 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3510 | return type->isDependentType() || |
| 3511 | type->isObjCObjectPointerType() || |
| 3512 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3513 | } |
| 3514 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3515 | return type->isDependentType() || |
| 3516 | type->isPointerType() || |
| 3517 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3520 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3521 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3522 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3523 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3524 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3525 | return; |
| 3526 | } |
| 3527 | |
| 3528 | bool typeOK, cf; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3529 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3530 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3531 | cf = false; |
| 3532 | } else { |
| 3533 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3534 | cf = true; |
| 3535 | } |
| 3536 | |
| 3537 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3538 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3539 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3540 | return; |
| 3541 | } |
| 3542 | |
| 3543 | if (cf) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3544 | param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3545 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3546 | param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3549 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 3550 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3551 | if (!isa<ObjCMethodDecl>(D)) { |
| 3552 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3553 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3554 | return; |
| 3555 | } |
| 3556 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3557 | D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3560 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3561 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3562 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3563 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3564 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3565 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3566 | returnType = MD->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3567 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 3568 | returnType = PD->getType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3569 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3570 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3571 | return; // ignore: was handled as a type attribute |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3572 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3573 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3574 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3575 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3576 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3577 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3578 | return; |
| 3579 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3580 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3581 | bool typeOK; |
| 3582 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3583 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3584 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3585 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3586 | case AttributeList::AT_NSReturnsRetained: |
| 3587 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3588 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3589 | cf = false; |
| 3590 | break; |
| 3591 | |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3592 | case AttributeList::AT_CFReturnsRetained: |
| 3593 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3594 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3595 | cf = true; |
| 3596 | break; |
| 3597 | } |
| 3598 | |
| 3599 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3600 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3601 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3602 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3603 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3604 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3605 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3606 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3607 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3608 | case AttributeList::AT_NSReturnsAutoreleased: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3609 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(), |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3610 | S.Context)); |
| 3611 | return; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3612 | case AttributeList::AT_CFReturnsNotRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3613 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3614 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3615 | return; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3616 | case AttributeList::AT_NSReturnsNotRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3617 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3618 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3619 | return; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3620 | case AttributeList::AT_CFReturnsRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3621 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3622 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3623 | return; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3624 | case AttributeList::AT_NSReturnsRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3625 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3626 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3627 | return; |
| 3628 | }; |
| 3629 | } |
| 3630 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3631 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3632 | const AttributeList &attr) { |
| 3633 | SourceLocation loc = attr.getLoc(); |
| 3634 | |
| 3635 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 3636 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 3637 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 3638 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3639 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3640 | return; |
| 3641 | } |
| 3642 | |
| 3643 | // Check that the method returns a normal pointer. |
| 3644 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3645 | |
| 3646 | if (!resultType->isReferenceType() && |
| 3647 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3648 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 3649 | << SourceRange(loc) |
| 3650 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 3651 | |
| 3652 | // Drop the attribute. |
| 3653 | return; |
| 3654 | } |
| 3655 | |
| 3656 | method->addAttr( |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3657 | ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context)); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3660 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 3661 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 3662 | if (!isa<FunctionDecl>(D)) { |
| 3663 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3664 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3665 | return; |
| 3666 | } |
| 3667 | |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3668 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3669 | |
| 3670 | // Check whether there's a conflicting attribute already present. |
| 3671 | Attr *Existing; |
| 3672 | if (IsAudited) { |
| 3673 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 3674 | } else { |
| 3675 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 3676 | } |
| 3677 | if (Existing) { |
| 3678 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 3679 | << A.getName() |
| 3680 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 3681 | << A.getRange() << Existing->getRange(); |
| 3682 | return; |
| 3683 | } |
| 3684 | |
| 3685 | // All clear; add the attribute. |
| 3686 | if (IsAudited) { |
| 3687 | D->addAttr( |
| 3688 | ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context)); |
| 3689 | } else { |
| 3690 | D->addAttr( |
| 3691 | ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context)); |
| 3692 | } |
| 3693 | } |
| 3694 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3695 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3696 | const AttributeList &Attr) { |
| 3697 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 3698 | if (!RD || RD->isUnion()) { |
| 3699 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3700 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 3704 | |
| 3705 | // In Objective-C, verify that the type names an Objective-C type. |
| 3706 | // We don't want to check this outside of ObjC because people sometimes |
| 3707 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3708 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3709 | // Check for an existing type with this name. |
| 3710 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 3711 | Sema::LookupOrdinaryName); |
| 3712 | if (S.LookupName(R, Sc)) { |
| 3713 | NamedDecl *Target = R.getFoundDecl(); |
| 3714 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 3715 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 3716 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 3717 | } |
| 3718 | } |
| 3719 | } |
| 3720 | |
| 3721 | D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context, |
| 3722 | ParmName)); |
| 3723 | } |
| 3724 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3725 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3726 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3727 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3728 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3729 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3730 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3733 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3734 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3735 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3736 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3737 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3738 | return; |
| 3739 | } |
| 3740 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3741 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3742 | QualType type = vd->getType(); |
| 3743 | |
| 3744 | if (!type->isDependentType() && |
| 3745 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3746 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3747 | << type; |
| 3748 | return; |
| 3749 | } |
| 3750 | |
| 3751 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3752 | |
| 3753 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3754 | // going to infer. |
| 3755 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3756 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3757 | |
| 3758 | switch (lifetime) { |
| 3759 | case Qualifiers::OCL_None: |
| 3760 | assert(type->isDependentType() && |
| 3761 | "didn't infer lifetime for non-dependent type?"); |
| 3762 | break; |
| 3763 | |
| 3764 | case Qualifiers::OCL_Weak: // meaningful |
| 3765 | case Qualifiers::OCL_Strong: // meaningful |
| 3766 | break; |
| 3767 | |
| 3768 | case Qualifiers::OCL_ExplicitNone: |
| 3769 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3770 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3771 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3772 | break; |
| 3773 | } |
| 3774 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3775 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3776 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3779 | static bool isKnownDeclSpecAttr(const AttributeList &Attr) { |
Aaron Ballman | 9428772 | 2012-02-23 22:46:33 +0000 | [diff] [blame] | 3780 | switch (Attr.getKind()) { |
| 3781 | default: |
| 3782 | return false; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3783 | case AttributeList::AT_DLLImport: |
| 3784 | case AttributeList::AT_DLLExport: |
| 3785 | case AttributeList::AT_Uuid: |
| 3786 | case AttributeList::AT_Deprecated: |
| 3787 | case AttributeList::AT_NoReturn: |
| 3788 | case AttributeList::AT_NoThrow: |
| 3789 | case AttributeList::AT_Naked: |
| 3790 | case AttributeList::AT_NoInline: |
Aaron Ballman | 9428772 | 2012-02-23 22:46:33 +0000 | [diff] [blame] | 3791 | return true; |
| 3792 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | //===----------------------------------------------------------------------===// |
| 3796 | // Microsoft specific attribute handlers. |
| 3797 | //===----------------------------------------------------------------------===// |
| 3798 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3799 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3800 | if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) { |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3801 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3802 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3803 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3804 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3805 | Expr *Arg = Attr.getArg(0); |
| 3806 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3807 | if (!Str || !Str->isAscii()) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3808 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3809 | << "uuid" << 1; |
| 3810 | return; |
| 3811 | } |
| 3812 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3813 | StringRef StrRef = Str->getString(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3814 | |
| 3815 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 3816 | StrRef.back() == '}'; |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3817 | |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3818 | // Validate GUID length. |
| 3819 | if (IsCurly && StrRef.size() != 38) { |
| 3820 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3821 | return; |
| 3822 | } |
| 3823 | if (!IsCurly && StrRef.size() != 36) { |
| 3824 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3825 | return; |
| 3826 | } |
| 3827 | |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3828 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3829 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3830 | StringRef::iterator I = StrRef.begin(); |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3831 | if (IsCurly) // Skip the optional '{' |
| 3832 | ++I; |
| 3833 | |
| 3834 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3835 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 3836 | if (*I != '-') { |
| 3837 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3838 | return; |
| 3839 | } |
| 3840 | } else if (!isxdigit(*I)) { |
| 3841 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3842 | return; |
| 3843 | } |
| 3844 | I++; |
| 3845 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3846 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3847 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3848 | Str->getString())); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3849 | } else |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3850 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 3853 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3854 | if (S.LangOpts.MicrosoftExt) { |
| 3855 | AttributeList::Kind Kind = Attr.getKind(); |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3856 | if (Kind == AttributeList::AT_SingleInheritance) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 3857 | D->addAttr( |
| 3858 | ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context)); |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3859 | else if (Kind == AttributeList::AT_MultipleInheritance) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 3860 | D->addAttr( |
| 3861 | ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context)); |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3862 | else if (Kind == AttributeList::AT_VirtualInheritance) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 3863 | D->addAttr( |
| 3864 | ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context)); |
| 3865 | } else |
| 3866 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3867 | } |
| 3868 | |
| 3869 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3870 | if (S.LangOpts.MicrosoftExt) { |
| 3871 | AttributeList::Kind Kind = Attr.getKind(); |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3872 | if (Kind == AttributeList::AT_Ptr32) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 3873 | D->addAttr( |
| 3874 | ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context)); |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3875 | else if (Kind == AttributeList::AT_Ptr64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 3876 | D->addAttr( |
| 3877 | ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context)); |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3878 | else if (Kind == AttributeList::AT_Win64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 3879 | D->addAttr( |
| 3880 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context)); |
| 3881 | } else |
| 3882 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3883 | } |
| 3884 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3885 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3886 | if (S.LangOpts.MicrosoftExt) |
| 3887 | D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context)); |
| 3888 | else |
| 3889 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3890 | } |
| 3891 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3892 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3893 | // Top Level Sema Entry Points |
| 3894 | //===----------------------------------------------------------------------===// |
| 3895 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3896 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 3897 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3898 | switch (Attr.getKind()) { |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3899 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 3900 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 3901 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3902 | default: |
| 3903 | break; |
| 3904 | } |
| 3905 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3906 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3907 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 3908 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3909 | switch (Attr.getKind()) { |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3910 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 3911 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 3912 | case AttributeList::AT_IBOutletCollection: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3913 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3914 | case AttributeList::AT_AddressSpace: |
| 3915 | case AttributeList::AT_OpenCLImageAccess: |
| 3916 | case AttributeList::AT_ObjCGC: |
| 3917 | case AttributeList::AT_VectorSize: |
| 3918 | case AttributeList::AT_NeonVectorType: |
| 3919 | case AttributeList::AT_NeonPolyVectorType: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3920 | // Ignore these, these are type attributes, handled by |
| 3921 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3922 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3923 | case AttributeList::AT_CUDADevice: |
| 3924 | case AttributeList::AT_CUDAHost: |
| 3925 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3926 | // Ignore, this is a non-inheritable attribute, handled |
| 3927 | // by ProcessNonInheritableDeclAttr. |
| 3928 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3929 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 3930 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 3931 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 3932 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3933 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3934 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3935 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3936 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 3937 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 3938 | case AttributeList::AT_CarriesDependency: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3939 | handleDependencyAttr (S, D, Attr); break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3940 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 3941 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 3942 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
| 3943 | case AttributeList::AT_Deprecated: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 3944 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated"); |
| 3945 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3946 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 3947 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3948 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3949 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3950 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 3951 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 3952 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 3953 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 3954 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3955 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3956 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3957 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 3958 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 3959 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 3960 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 3961 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 3962 | case AttributeList::AT_ownership_returns: |
| 3963 | case AttributeList::AT_ownership_takes: |
| 3964 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3965 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3966 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 3967 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 3968 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 3969 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 3970 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 3971 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 3972 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3973 | |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3974 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3975 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3976 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3977 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3978 | |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3979 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3980 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 3981 | |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3982 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3983 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 3984 | |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3985 | case AttributeList::AT_CFAuditedTransfer: |
| 3986 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3987 | handleCFTransferAttr(S, D, Attr); break; |
| 3988 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3989 | // Checker-specific. |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3990 | case AttributeList::AT_CFConsumed: |
| 3991 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 3992 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3993 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3994 | |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 3995 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3996 | case AttributeList::AT_NSReturnsNotRetained: |
| 3997 | case AttributeList::AT_CFReturnsNotRetained: |
| 3998 | case AttributeList::AT_NSReturnsRetained: |
| 3999 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4000 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4001 | |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4002 | case AttributeList::AT_ReqdWorkGroupSize: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4003 | handleReqdWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4004 | |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4005 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4006 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4007 | |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4008 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4009 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4010 | case AttributeList::AT_Unavailable: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4011 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable"); |
| 4012 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4013 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4014 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4015 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4016 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4017 | handleObjCRootClassAttr(S, D, Attr); |
| 4018 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4019 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4020 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4021 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4022 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4023 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4024 | handleReturnsTwiceAttr(S, D, Attr); |
| 4025 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4026 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
| 4027 | case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break; |
| 4028 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4029 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4030 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4031 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4032 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4033 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4034 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4035 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4036 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4037 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4038 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4039 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4040 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4041 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4042 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4043 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4044 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4045 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4046 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4047 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4048 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4049 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4050 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4051 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4052 | // Just ignore |
| 4053 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4054 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4055 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4056 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4057 | case AttributeList::AT_StdCall: |
| 4058 | case AttributeList::AT_CDecl: |
| 4059 | case AttributeList::AT_FastCall: |
| 4060 | case AttributeList::AT_ThisCall: |
| 4061 | case AttributeList::AT_Pascal: |
| 4062 | case AttributeList::AT_Pcs: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4063 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4064 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4065 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4066 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4067 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4068 | |
| 4069 | // Microsoft attributes: |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4070 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4071 | handleMsStructAttr(S, D, Attr); |
| 4072 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4073 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4074 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4075 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4076 | case AttributeList::AT_SingleInheritance: |
| 4077 | case AttributeList::AT_MultipleInheritance: |
| 4078 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4079 | handleInheritanceAttr(S, D, Attr); |
| 4080 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4081 | case AttributeList::AT_Win64: |
| 4082 | case AttributeList::AT_Ptr32: |
| 4083 | case AttributeList::AT_Ptr64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4084 | handlePortabilityAttr(S, D, Attr); |
| 4085 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4086 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4087 | handleForceInlineAttr(S, D, Attr); |
| 4088 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4089 | |
| 4090 | // Thread safety attributes: |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4091 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4092 | handleGuardedVarAttr(S, D, Attr); |
| 4093 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4094 | case AttributeList::AT_PtGuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4095 | handleGuardedVarAttr(S, D, Attr, /*pointer = */true); |
| 4096 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4097 | case AttributeList::AT_ScopedLockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4098 | handleLockableAttr(S, D, Attr, /*scoped = */true); |
| 4099 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4100 | case AttributeList::AT_NoAddressSafetyAnalysis: |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4101 | handleNoAddressSafetyAttr(S, D, Attr); |
| 4102 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4103 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4104 | handleNoThreadSafetyAttr(S, D, Attr); |
| 4105 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4106 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4107 | handleLockableAttr(S, D, Attr); |
| 4108 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4109 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4110 | handleGuardedByAttr(S, D, Attr); |
| 4111 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4112 | case AttributeList::AT_PtGuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4113 | handleGuardedByAttr(S, D, Attr, /*pointer = */true); |
| 4114 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4115 | case AttributeList::AT_ExclusiveLockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4116 | handleLockFunAttr(S, D, Attr, /*exclusive = */true); |
| 4117 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4118 | case AttributeList::AT_ExclusiveLocksRequired: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4119 | handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true); |
| 4120 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4121 | case AttributeList::AT_ExclusiveTrylockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4122 | handleTrylockFunAttr(S, D, Attr, /*exclusive = */true); |
| 4123 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4124 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4125 | handleLockReturnedAttr(S, D, Attr); |
| 4126 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4127 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4128 | handleLocksExcludedAttr(S, D, Attr); |
| 4129 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4130 | case AttributeList::AT_SharedLockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4131 | handleLockFunAttr(S, D, Attr); |
| 4132 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4133 | case AttributeList::AT_SharedLocksRequired: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4134 | handleLocksRequiredAttr(S, D, Attr); |
| 4135 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4136 | case AttributeList::AT_SharedTrylockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4137 | handleTrylockFunAttr(S, D, Attr); |
| 4138 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4139 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4140 | handleUnlockFunAttr(S, D, Attr); |
| 4141 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4142 | case AttributeList::AT_AcquiredBefore: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4143 | handleAcquireOrderAttr(S, D, Attr, /*before = */true); |
| 4144 | break; |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4145 | case AttributeList::AT_AcquiredAfter: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4146 | handleAcquireOrderAttr(S, D, Attr, /*before = */false); |
| 4147 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4148 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4149 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4150 | // Ask target about the attribute. |
| 4151 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4152 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Chandler Carruth | 7d5c45e | 2010-07-08 09:42:26 +0000 | [diff] [blame] | 4153 | S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) |
| 4154 | << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4155 | break; |
| 4156 | } |
| 4157 | } |
| 4158 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4159 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4160 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4161 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 4162 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4163 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4164 | const AttributeList &Attr, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4165 | bool NonInheritable, bool Inheritable) { |
| 4166 | if (Attr.isInvalid()) |
| 4167 | return; |
| 4168 | |
| 4169 | if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) |
| 4170 | // FIXME: Try to deal with other __declspec attributes! |
| 4171 | return; |
| 4172 | |
| 4173 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4174 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4175 | |
| 4176 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4177 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4180 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4181 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4182 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4183 | const AttributeList *AttrList, |
| 4184 | bool NonInheritable, bool Inheritable) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4185 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 4186 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable); |
Rafael Espindola | 9b79fc9 | 2012-05-07 23:58:18 +0000 | [diff] [blame] | 4187 | } |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4188 | |
| 4189 | // GCC accepts |
| 4190 | // static int a9 __attribute__((weakref)); |
| 4191 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4192 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4193 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4194 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4195 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4196 | } |
| 4197 | } |
| 4198 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4199 | // Annotation attributes are the only attributes allowed after an access |
| 4200 | // specifier. |
| 4201 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4202 | const AttributeList *AttrList) { |
| 4203 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | bfcb037 | 2012-06-19 03:39:03 +0000 | [diff] [blame] | 4204 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4205 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4206 | } else { |
| 4207 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4208 | return true; |
| 4209 | } |
| 4210 | } |
| 4211 | |
| 4212 | return false; |
| 4213 | } |
| 4214 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4215 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4216 | /// contains any decl attributes that we should warn about. |
| 4217 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4218 | for ( ; A; A = A->getNext()) { |
| 4219 | // Only warn if the attribute is an unignored, non-type attribute. |
| 4220 | if (A->isUsedAsTypeAttr()) continue; |
| 4221 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4222 | |
| 4223 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4224 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4225 | << A->getName() << A->getRange(); |
| 4226 | } else { |
| 4227 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4228 | << A->getName() << A->getRange(); |
| 4229 | } |
| 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4234 | /// used to build a declaration, complain about any decl attributes |
| 4235 | /// which might be lying around on it. |
| 4236 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4237 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4238 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4239 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4240 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4241 | } |
| 4242 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4243 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4244 | /// \#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] | 4245 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4246 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4247 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4248 | NamedDecl *NewD = 0; |
| 4249 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4250 | FunctionDecl *NewFD; |
| 4251 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4252 | // FIXME: Mangling? |
| 4253 | // FIXME: Is the qualifier info correct? |
| 4254 | // FIXME: Is the DeclContext correct? |
| 4255 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4256 | Loc, Loc, DeclarationName(II), |
| 4257 | FD->getType(), FD->getTypeSourceInfo(), |
| 4258 | SC_None, SC_None, |
| 4259 | false/*isInlineSpecified*/, |
| 4260 | FD->hasPrototype(), |
| 4261 | false/*isConstexprSpecified*/); |
| 4262 | NewD = NewFD; |
| 4263 | |
| 4264 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4265 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4266 | |
| 4267 | // Fake up parameter variables; they are declared as if this were |
| 4268 | // a typedef. |
| 4269 | QualType FDTy = FD->getType(); |
| 4270 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4271 | SmallVector<ParmVarDecl*, 16> Params; |
| 4272 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4273 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4274 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4275 | Param->setScopeInfo(0, Params.size()); |
| 4276 | Params.push_back(Param); |
| 4277 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4278 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4279 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4280 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4281 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4282 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4283 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4284 | VD->getStorageClass(), |
| 4285 | VD->getStorageClassAsWritten()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4286 | if (VD->getQualifier()) { |
| 4287 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4288 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4289 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4290 | } |
| 4291 | return NewD; |
| 4292 | } |
| 4293 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4294 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4295 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4296 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4297 | if (W.getUsed()) return; // only do this once |
| 4298 | W.setUsed(true); |
| 4299 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4300 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4301 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4302 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4303 | NDId->getName())); |
| 4304 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4305 | WeakTopLevelDecl.push_back(NewD); |
| 4306 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4307 | // to insert Decl at TU scope, sorry. |
| 4308 | DeclContext *SavedContext = CurContext; |
| 4309 | CurContext = Context.getTranslationUnitDecl(); |
| 4310 | PushOnScopeChains(NewD, S); |
| 4311 | CurContext = SavedContext; |
| 4312 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4313 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4314 | } |
| 4315 | } |
| 4316 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4317 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4318 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4319 | /// 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] | 4320 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 4321 | bool NonInheritable, bool Inheritable) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 4322 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4323 | // have to do this. |
Douglas Gregor | 31e37b2 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 4324 | if (Inheritable) { |
| 4325 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4326 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4327 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 4328 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4329 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4330 | = WeakUndeclaredIdentifiers.find(Id); |
| 4331 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 4332 | WeakInfo W = I->second; |
| 4333 | DeclApplyPragmaWeak(S, ND, W); |
| 4334 | WeakUndeclaredIdentifiers[Id] = W; |
| 4335 | } |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 4336 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4337 | } |
| 4338 | } |
| 4339 | } |
| 4340 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4341 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4342 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4343 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4344 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4345 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4346 | // position to the decl itself. This handles cases like: |
| 4347 | // int *__attr__(x)** D; |
| 4348 | // when X is a decl attribute. |
| 4349 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4350 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4351 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4352 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4353 | // Finally, apply any attributes on the decl itself. |
| 4354 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4355 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4356 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4357 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4358 | /// Is the given declaration allowed to use a forbidden type? |
| 4359 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4360 | // Private ivars are always okay. Unfortunately, people don't |
| 4361 | // always properly make their ivars private, even in system headers. |
| 4362 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4363 | // Function declarations in sys headers will be marked unavailable. |
| 4364 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4365 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4366 | return false; |
| 4367 | |
| 4368 | // Require it to be declared in a system header. |
| 4369 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4370 | } |
| 4371 | |
| 4372 | /// Handle a delayed forbidden-type diagnostic. |
| 4373 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4374 | Decl *decl) { |
| 4375 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 4376 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 4377 | "this system declaration uses an unsupported type")); |
| 4378 | return; |
| 4379 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4380 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4381 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4382 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4383 | // kind of forbidden type messages on unavailable functions. |
| 4384 | if (FD->hasAttr<UnavailableAttr>() && |
| 4385 | diag.getForbiddenTypeDiagnostic() == |
| 4386 | diag::err_arc_array_param_no_ownership) { |
| 4387 | diag.Triggered = true; |
| 4388 | return; |
| 4389 | } |
| 4390 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4391 | |
| 4392 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4393 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4394 | diag.Triggered = true; |
| 4395 | } |
| 4396 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4397 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4398 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4399 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4400 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4401 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4402 | // When delaying diagnostics to run in the context of a parsed |
| 4403 | // declaration, we only want to actually emit anything if parsing |
| 4404 | // succeeds. |
| 4405 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4406 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4407 | // We emit all the active diagnostics in this pool or any of its |
| 4408 | // parents. In general, we'll get one pool for the decl spec |
| 4409 | // and a child pool for each declarator; in a decl group like: |
| 4410 | // deprecated_typedef foo, *bar, baz(); |
| 4411 | // only the declarator pops will be passed decls. This is correct; |
| 4412 | // we really do need to consider delayed diagnostics from the decl spec |
| 4413 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4414 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4415 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4416 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4417 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4418 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4419 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4420 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4421 | continue; |
| 4422 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4423 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4424 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4425 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 4426 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4427 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4428 | break; |
| 4429 | |
| 4430 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4431 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4432 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4433 | |
| 4434 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4435 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4436 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4437 | } |
| 4438 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4439 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4440 | } |
| 4441 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4442 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4443 | /// been delayed in the current context instead of in the given pool. |
| 4444 | /// Essentially, this just moves them to the current pool. |
| 4445 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4446 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4447 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4448 | curPool->steal(pool); |
| 4449 | } |
| 4450 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4451 | static bool isDeclDeprecated(Decl *D) { |
| 4452 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4453 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4454 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4455 | // A category implicitly has the availability of the interface. |
| 4456 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4457 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4458 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4459 | return false; |
| 4460 | } |
| 4461 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 4462 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4463 | Decl *Ctx) { |
| 4464 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4465 | return; |
| 4466 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4467 | DD.Triggered = true; |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 4468 | if (!DD.getDeprecationMessage().empty()) |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 4469 | Diag(DD.Loc, diag::warn_deprecated_message) |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 4470 | << DD.getDeprecationDecl()->getDeclName() |
| 4471 | << DD.getDeprecationMessage(); |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4472 | else if (DD.getUnknownObjCClass()) { |
| 4473 | Diag(DD.Loc, diag::warn_deprecated_fwdclass_message) |
| 4474 | << DD.getDeprecationDecl()->getDeclName(); |
| 4475 | Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class); |
| 4476 | } |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 4477 | else |
| 4478 | Diag(DD.Loc, diag::warn_deprecated) |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 4479 | << DD.getDeprecationDecl()->getDeclName(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4482 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4483 | SourceLocation Loc, |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 4484 | const ObjCInterfaceDecl *UnknownObjCClass) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4485 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4486 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4487 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 4488 | UnknownObjCClass, |
| 4489 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4490 | return; |
| 4491 | } |
| 4492 | |
| 4493 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 4494 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4495 | return; |
Fariborz Jahanian | d672436 | 2012-04-23 20:30:52 +0000 | [diff] [blame] | 4496 | if (!Message.empty()) { |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 4497 | Diag(Loc, diag::warn_deprecated_message) << D->getDeclName() |
| 4498 | << Message; |
Fariborz Jahanian | d672436 | 2012-04-23 20:30:52 +0000 | [diff] [blame] | 4499 | Diag(D->getLocation(), |
| 4500 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4501 | : diag::note_previous_decl) << D->getDeclName(); |
| 4502 | } |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4503 | else { |
Fariborz Jahanian | 350e956 | 2012-05-27 16:59:48 +0000 | [diff] [blame] | 4504 | if (!UnknownObjCClass) { |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4505 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
Fariborz Jahanian | 350e956 | 2012-05-27 16:59:48 +0000 | [diff] [blame] | 4506 | Diag(D->getLocation(), |
| 4507 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4508 | : diag::note_previous_decl) << D->getDeclName(); |
| 4509 | } |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 4510 | else { |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4511 | Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName(); |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 4512 | Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4513 | } |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4514 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4515 | } |