Chris Lattner | 2c6fcf5 | 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 | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/Expr.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 23 | #include "clang/Sema/DelayedDiagnostic.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 25 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 26 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 27 | |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 28 | /// These constants match the enumerated choices of |
| 29 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
| 30 | enum { |
| 31 | ExpectedFunction, |
| 32 | ExpectedUnion, |
| 33 | ExpectedVariableOrFunction, |
| 34 | ExpectedFunctionOrMethod, |
| 35 | ExpectedParameter, |
| 36 | ExpectedParameterOrMethod, |
| 37 | ExpectedFunctionMethodOrBlock, |
| 38 | ExpectedClassOrVirtualMethod, |
| 39 | ExpectedFunctionMethodOrParameter, |
| 40 | ExpectedClass, |
| 41 | ExpectedVirtualMethod, |
| 42 | ExpectedClassMember, |
| 43 | ExpectedVariable, |
| 44 | ExpectedMethod, |
| 45 | ExpectedVariableFunctionOrLabel |
| 46 | }; |
| 47 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
| 49 | // Helper functions |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 52 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 53 | bool blocksToo = true) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 54 | QualType Ty; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 55 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 56 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 57 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 58 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 59 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 60 | Ty = decl->getUnderlyingType(); |
| 61 | else |
| 62 | return 0; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 64 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 65 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 28c433d | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 66 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 67 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 68 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 69 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 72 | // FIXME: We should provide an abstraction around a method or function |
| 73 | // to provide the following bits of information. |
| 74 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 75 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 76 | /// type (function or function-typed variable). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 77 | static bool isFunction(const Decl *D) { |
| 78 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 82 | /// type (function or function-typed variable) or an Objective-C |
| 83 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 84 | static bool isFunctionOrMethod(const Decl *D) { |
| 85 | return isFunction(D)|| isa<ObjCMethodDecl>(D); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 88 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 89 | /// type (function or function-typed variable) or an Objective-C |
| 90 | /// method or a block. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 91 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 92 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 93 | return true; |
| 94 | // check for block is more involved. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 95 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 96 | QualType Ty = V->getType(); |
| 97 | return Ty->isBlockPointerType(); |
| 98 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 99 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 100 | } |
| 101 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 102 | /// Return true if the given decl has a declarator that should have |
| 103 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 104 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 105 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 106 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 107 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 110 | /// hasFunctionProto - Return true if the given decl has a argument |
| 111 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 112 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 113 | static bool hasFunctionProto(const Decl *D) { |
| 114 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 115 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 116 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 117 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 123 | /// arguments. It is an error to call this on a K&R function (use |
| 124 | /// hasFunctionProto first). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 125 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 126 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 127 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 128 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 129 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 130 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 133 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 134 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 135 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 136 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 137 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 138 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 139 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 142 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 143 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 144 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 145 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 148 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 149 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 150 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 151 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 152 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 153 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 154 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 155 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 159 | static bool isInstanceMethod(const Decl *D) { |
| 160 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 161 | return MethodDecl->isInstance(); |
| 162 | return false; |
| 163 | } |
| 164 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 165 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 166 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 167 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 168 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 169 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 170 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 171 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 172 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 173 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 174 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 176 | // FIXME: Should we walk the chain of classes? |
| 177 | return ClsName == &Ctx.Idents.get("NSString") || |
| 178 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 179 | } |
| 180 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 181 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 182 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 183 | if (!PT) |
| 184 | return false; |
| 185 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 186 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 187 | if (!RT) |
| 188 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 189 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 190 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 191 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 192 | return false; |
| 193 | |
| 194 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 195 | } |
| 196 | |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 197 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 198 | unsigned int Num) { |
| 199 | if (Attr.getNumArgs() != Num) { |
| 200 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | return true; |
| 205 | } |
| 206 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 207 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 208 | // Attribute Implementations |
| 209 | //===----------------------------------------------------------------------===// |
| 210 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 211 | // FIXME: All this manual attribute parsing code is gross. At the |
| 212 | // least add some helper functions to check most argument patterns (# |
| 213 | // and types of args). |
| 214 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 215 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 216 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 217 | TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D); |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 218 | if (tDecl == 0) { |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 219 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 220 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 221 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 223 | QualType curType = tDecl->getUnderlyingType(); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 224 | |
| 225 | Expr *sizeExpr; |
| 226 | |
| 227 | // Special case where the argument is a template id. |
| 228 | if (Attr.getParameterName()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 229 | CXXScopeSpec SS; |
| 230 | UnqualifiedId id; |
| 231 | id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 232 | |
| 233 | ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false); |
| 234 | if (Size.isInvalid()) |
| 235 | return; |
| 236 | |
| 237 | sizeExpr = Size.get(); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 238 | } else { |
| 239 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 240 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 241 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 242 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 243 | sizeExpr = Attr.getArg(0); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 244 | } |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 245 | |
| 246 | // Instantiate/Install the vector type, and let Sema build the type for us. |
| 247 | // This will run the reguired checks. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 248 | QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 249 | if (!T.isNull()) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 250 | // FIXME: preserve the old source info. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 251 | tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 253 | // Remember this typedef decl, we will need it later for diagnostics. |
| 254 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 255 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 258 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 259 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 260 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 261 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 262 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 263 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 264 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 265 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 266 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 267 | // has no effect. |
| 268 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 269 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 270 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 271 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 272 | else |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 273 | FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 274 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 275 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 278 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 279 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 280 | TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context)); |
| 281 | else |
| 282 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 283 | } |
| 284 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 285 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 286 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 287 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 288 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 289 | |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 290 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 291 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 292 | if (MD->isInstanceMethod()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 293 | D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 297 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 300 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 301 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 302 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 303 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 304 | |
| 305 | // The IBOutlet attributes only apply to instance variables of |
Ted Kremenek | 06be968 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 306 | // Objective-C classes. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 307 | if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) { |
| 308 | D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 309 | return; |
Ted Kremenek | 06be968 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 310 | } |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 311 | |
Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 312 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 315 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 316 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 317 | |
| 318 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 319 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 320 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | // The IBOutletCollection attributes only apply to instance variables of |
| 325 | // Objective-C classes. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 326 | if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 327 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 328 | return; |
| 329 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 330 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
Fariborz Jahanian | 798f832 | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 331 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
| 332 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type) |
| 333 | << VD->getType() << 0; |
| 334 | return; |
| 335 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 336 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
Fariborz Jahanian | 798f832 | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 337 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
| 338 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type) |
| 339 | << PD->getType() << 1; |
| 340 | return; |
| 341 | } |
| 342 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 343 | IdentifierInfo *II = Attr.getParameterName(); |
| 344 | if (!II) |
| 345 | II = &S.Context.Idents.get("id"); |
Fariborz Jahanian | 798f832 | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 346 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 347 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 348 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 349 | if (!TypeRep) { |
| 350 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 351 | return; |
| 352 | } |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 353 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 354 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 355 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 356 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 357 | // treated as __attribute__((iboutletcollection())). |
| 358 | if (!QT->isObjCIdType() && !QT->isObjCClassType() && |
| 359 | !QT->isObjCObjectType()) { |
| 360 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 361 | return; |
| 362 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 363 | D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context, |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 364 | QT)); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 367 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 368 | if (const RecordType *UT = T->getAsUnionType()) |
| 369 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 370 | RecordDecl *UD = UT->getDecl(); |
| 371 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 372 | itend = UD->field_end(); it != itend; ++it) { |
| 373 | QualType QT = it->getType(); |
| 374 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 375 | T = QT; |
| 376 | return; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 382 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 383 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 384 | // ignore it as well |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 385 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 386 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 387 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 388 | return; |
| 389 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 390 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 391 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 392 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 393 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 394 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 395 | |
| 396 | // The nonnull attribute only applies to pointers. |
| 397 | llvm::SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 399 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 400 | E=Attr.arg_end(); I!=E; ++I) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 401 | |
| 402 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 403 | // The argument must be an integer constant expression. |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 404 | Expr *Ex = *I; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 405 | llvm::APSInt ArgNum(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 406 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 407 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 408 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 409 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 410 | return; |
| 411 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 412 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 413 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 414 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 415 | if (x < 1 || x > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 416 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 91aea71 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 417 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 418 | return; |
| 419 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 420 | |
Ted Kremenek | 5224e6a | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 421 | --x; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 422 | if (HasImplicitThisParam) { |
| 423 | if (x == 0) { |
| 424 | S.Diag(Attr.getLoc(), |
| 425 | diag::err_attribute_invalid_implicit_this_argument) |
| 426 | << "nonnull" << Ex->getSourceRange(); |
| 427 | return; |
| 428 | } |
| 429 | --x; |
| 430 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 431 | |
| 432 | // Is the function argument a pointer type? |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 433 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 434 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 435 | |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 436 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 437 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | 62157e5 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 438 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 439 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 440 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 441 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 442 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 443 | NonNullArgs.push_back(x); |
| 444 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 445 | |
| 446 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 447 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 448 | if (NonNullArgs.empty()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 449 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) { |
| 450 | QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 451 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 452 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 453 | NonNullArgs.push_back(I); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 454 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 455 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 456 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 457 | if (NonNullArgs.empty()) { |
| 458 | // Warn the trivial case only if attribute is not coming from a |
| 459 | // macro instantiation. |
| 460 | if (Attr.getLoc().isFileID()) |
| 461 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 462 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 463 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 464 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 465 | |
| 466 | unsigned* start = &NonNullArgs[0]; |
| 467 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 468 | llvm::array_pod_sort(start, start + size); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 469 | D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start, |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 470 | size)); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 473 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 474 | // This attribute must be applied to a function declaration. |
| 475 | // The first argument to the attribute must be a string, |
| 476 | // the name of the resource, for example "malloc". |
| 477 | // The following arguments must be argument indexes, the arguments must be |
| 478 | // of integer type for Returns, otherwise of pointer type. |
| 479 | // The difference between Holds and Takes is that a pointer may still be used |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 480 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 481 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 482 | |
| 483 | if (!AL.getParameterName()) { |
| 484 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 485 | << AL.getName()->getName() << 1; |
| 486 | return; |
| 487 | } |
| 488 | // Figure out our Kind, and check arguments while we're at it. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 489 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 490 | switch (AL.getKind()) { |
| 491 | case AttributeList::AT_ownership_takes: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 492 | K = OwnershipAttr::Takes; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 493 | if (AL.getNumArgs() < 1) { |
| 494 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 495 | return; |
| 496 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 497 | break; |
| 498 | case AttributeList::AT_ownership_holds: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 499 | K = OwnershipAttr::Holds; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 500 | if (AL.getNumArgs() < 1) { |
| 501 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 502 | return; |
| 503 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 504 | break; |
| 505 | case AttributeList::AT_ownership_returns: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 506 | K = OwnershipAttr::Returns; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 507 | if (AL.getNumArgs() > 1) { |
| 508 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 509 | << AL.getNumArgs() + 1; |
| 510 | return; |
| 511 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 512 | break; |
| 513 | default: |
| 514 | // This should never happen given how we are called. |
| 515 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 518 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 519 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 520 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 524 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 525 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 526 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 527 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 528 | |
| 529 | llvm::StringRef Module = AL.getParameterName()->getName(); |
| 530 | |
| 531 | // Normalize the argument, __foo__ becomes foo. |
| 532 | if (Module.startswith("__") && Module.endswith("__")) |
| 533 | Module = Module.substr(2, Module.size() - 4); |
| 534 | |
| 535 | llvm::SmallVector<unsigned, 10> OwnershipArgs; |
| 536 | |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 537 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 538 | ++I) { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 539 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 540 | Expr *IdxExpr = *I; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 541 | llvm::APSInt ArgNum(32); |
| 542 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 543 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 544 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 545 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 546 | continue; |
| 547 | } |
| 548 | |
| 549 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 550 | |
| 551 | if (x > NumArgs || x < 1) { |
| 552 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 553 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 554 | continue; |
| 555 | } |
| 556 | --x; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 557 | if (HasImplicitThisParam) { |
| 558 | if (x == 0) { |
| 559 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 560 | << "ownership" << IdxExpr->getSourceRange(); |
| 561 | return; |
| 562 | } |
| 563 | --x; |
| 564 | } |
| 565 | |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 566 | switch (K) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 567 | case OwnershipAttr::Takes: |
| 568 | case OwnershipAttr::Holds: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 569 | // Is the function argument a pointer type? |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 570 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 571 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 572 | // FIXME: Should also highlight argument in decl. |
| 573 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 574 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 575 | << "pointer" |
| 576 | << IdxExpr->getSourceRange(); |
| 577 | continue; |
| 578 | } |
| 579 | break; |
| 580 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 581 | case OwnershipAttr::Returns: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 582 | if (AL.getNumArgs() > 1) { |
| 583 | // Is the function argument an integer type? |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 584 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 585 | llvm::APSInt ArgNum(32); |
| 586 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 587 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 588 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 589 | << "ownership_returns" << "integer" |
| 590 | << IdxExpr->getSourceRange(); |
| 591 | return; |
| 592 | } |
| 593 | } |
| 594 | break; |
| 595 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 596 | default: |
| 597 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 598 | } // switch |
| 599 | |
| 600 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 601 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 602 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 603 | e = D->specific_attr_end<OwnershipAttr>(); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 604 | i != e; ++i) { |
| 605 | if ((*i)->getOwnKind() != K) { |
| 606 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 607 | I!=E; ++I) { |
| 608 | if (x == *I) { |
| 609 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 610 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | OwnershipArgs.push_back(x); |
| 616 | } |
| 617 | |
| 618 | unsigned* start = OwnershipArgs.data(); |
| 619 | unsigned size = OwnershipArgs.size(); |
| 620 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 621 | |
| 622 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 623 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 624 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 625 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 626 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 627 | D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 628 | start, size)); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 629 | } |
| 630 | |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 631 | /// Whether this declaration has internal linkage for the purposes of |
| 632 | /// things that want to complain about things not have internal linkage. |
| 633 | static bool hasEffectivelyInternalLinkage(NamedDecl *D) { |
| 634 | switch (D->getLinkage()) { |
| 635 | case NoLinkage: |
| 636 | case InternalLinkage: |
| 637 | return true; |
| 638 | |
| 639 | // Template instantiations that go from external to unique-external |
| 640 | // shouldn't get diagnosed. |
| 641 | case UniqueExternalLinkage: |
| 642 | return true; |
| 643 | |
| 644 | case ExternalLinkage: |
| 645 | return false; |
| 646 | } |
| 647 | llvm_unreachable("unknown linkage kind!"); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 648 | return false; |
| 649 | } |
| 650 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 651 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 652 | // Check the attribute arguments. |
| 653 | if (Attr.getNumArgs() > 1) { |
| 654 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 655 | return; |
| 656 | } |
| 657 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 658 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 659 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 660 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 661 | return; |
| 662 | } |
| 663 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 664 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 665 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 666 | // gcc rejects |
| 667 | // class c { |
| 668 | // static int a __attribute__((weakref ("v2"))); |
| 669 | // static int b() __attribute__((weakref ("f3"))); |
| 670 | // }; |
| 671 | // and ignores the attributes of |
| 672 | // void f(void) { |
| 673 | // static int a __attribute__((weakref ("v2"))); |
| 674 | // } |
| 675 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 676 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 677 | if (!Ctx->isFileContext()) { |
| 678 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 679 | nd->getNameAsString(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 680 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | // The GCC manual says |
| 684 | // |
| 685 | // At present, a declaration to which `weakref' is attached can only |
| 686 | // be `static'. |
| 687 | // |
| 688 | // It also says |
| 689 | // |
| 690 | // Without a TARGET, |
| 691 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 692 | // equivalent to `weak'. |
| 693 | // |
| 694 | // gcc 4.4.1 will accept |
| 695 | // int a7 __attribute__((weakref)); |
| 696 | // as |
| 697 | // int a7 __attribute__((weak)); |
| 698 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 699 | // it if this behaviour is actually used. |
| 700 | |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 701 | if (!hasEffectivelyInternalLinkage(nd)) { |
| 702 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 703 | return; |
| 704 | } |
| 705 | |
| 706 | // GCC rejects |
| 707 | // static ((alias ("y"), weakref)). |
| 708 | // Should we? How to check that weakref is before or after alias? |
| 709 | |
| 710 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 711 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 712 | Arg = Arg->IgnoreParenCasts(); |
| 713 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 714 | |
| 715 | if (Str == 0 || Str->isWide()) { |
| 716 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 717 | << "weakref" << 1; |
| 718 | return; |
| 719 | } |
| 720 | // GCC will accept anything as the argument of weakref. Should we |
| 721 | // check for an existing decl? |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 722 | D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 723 | Str->getString())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 726 | D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context)); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 729 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 730 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 731 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 732 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 733 | return; |
| 734 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 735 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 736 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 737 | Arg = Arg->IgnoreParenCasts(); |
| 738 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 739 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 740 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 741 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 742 | << "alias" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 743 | return; |
| 744 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 745 | |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 746 | if (S.Context.Target.getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 747 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 748 | return; |
| 749 | } |
| 750 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 751 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 752 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 753 | D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 754 | Str->getString())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 757 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 758 | // Check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 759 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 760 | return; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 761 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 762 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 763 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 764 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 765 | return; |
| 766 | } |
| 767 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 768 | D->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 771 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 772 | const AttributeList &Attr) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 773 | // Check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 774 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 775 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 776 | return; |
| 777 | } |
| 778 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 779 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 780 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 781 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 782 | return; |
| 783 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 784 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 785 | D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 788 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 789 | // Check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 790 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 791 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 792 | return; |
| 793 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 795 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 797 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 798 | D->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 799 | return; |
| 800 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 803 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 806 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 807 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 808 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 809 | return; |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 810 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 811 | D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context)); |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 814 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 815 | assert(Attr.isInvalid() == false); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 816 | if (isa<VarDecl>(D)) |
| 817 | D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context)); |
Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 818 | else |
| 819 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 820 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 823 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 824 | assert(Attr.isInvalid() == false); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 825 | if (isa<VarDecl>(D)) |
| 826 | D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context)); |
Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 827 | else |
| 828 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 829 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 832 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 833 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 834 | |
| 835 | if (S.CheckNoReturnAttr(attr)) return; |
| 836 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 837 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 838 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 839 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 840 | return; |
| 841 | } |
| 842 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 843 | D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context)); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 847 | if (attr.hasParameterOrArguments()) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 848 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 849 | attr.setInvalid(); |
| 850 | return true; |
| 851 | } |
| 852 | |
| 853 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 856 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 857 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 858 | |
| 859 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 860 | // because 'analyzer_noreturn' does not impact the type. |
| 861 | |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 862 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 863 | return; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 864 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 865 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 866 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 867 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 868 | && !VD->getType()->isFunctionPointerType())) { |
| 869 | S.Diag(Attr.getLoc(), |
| 870 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 871 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 872 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 873 | return; |
| 874 | } |
| 875 | } |
| 876 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 877 | D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 878 | } |
| 879 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 880 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 881 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 882 | /* |
| 883 | Returning a Vector Class in Registers |
| 884 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 885 | According to the PPU ABI specifications, a class with a single member of |
| 886 | vector type is returned in memory when used as the return value of a function. |
| 887 | This results in inefficient code when implementing vector classes. To return |
| 888 | the value in a single vector register, add the vecreturn attribute to the |
| 889 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 890 | |
| 891 | Example: |
| 892 | |
| 893 | struct Vector |
| 894 | { |
| 895 | __vector float xyzw; |
| 896 | } __attribute__((vecreturn)); |
| 897 | |
| 898 | Vector Add(Vector lhs, Vector rhs) |
| 899 | { |
| 900 | Vector result; |
| 901 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 902 | return result; // This will be returned in a register |
| 903 | } |
| 904 | */ |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 905 | if (!isa<RecordDecl>(D)) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 906 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 907 | << Attr.getName() << ExpectedClass; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 908 | return; |
| 909 | } |
| 910 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 911 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 912 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 913 | return; |
| 914 | } |
| 915 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 916 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 917 | int count = 0; |
| 918 | |
| 919 | if (!isa<CXXRecordDecl>(record)) { |
| 920 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 921 | return; |
| 922 | } |
| 923 | |
| 924 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 925 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 926 | return; |
| 927 | } |
| 928 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 929 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 930 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 931 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 932 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 933 | return; |
| 934 | } |
| 935 | count++; |
| 936 | } |
| 937 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 938 | D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context)); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 941 | static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 942 | if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 943 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 944 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | // FIXME: Actually store the attribute on the declaration |
| 948 | } |
| 949 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 950 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 951 | // check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 952 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 953 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 954 | return; |
| 955 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 956 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 957 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
| 958 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 959 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 960 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 961 | return; |
| 962 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 963 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 964 | D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 967 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 968 | // check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 969 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 970 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 971 | return; |
| 972 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 973 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 974 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 311bf29 | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 975 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 976 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 977 | return; |
| 978 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 979 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 980 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 981 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 982 | return; |
| 983 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 984 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 985 | D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 988 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 989 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 990 | if (Attr.getNumArgs() > 1) { |
| 991 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 992 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 993 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 994 | |
| 995 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 996 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 997 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 998 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 999 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1000 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1001 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1002 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1003 | return; |
| 1004 | } |
| 1005 | priority = Idx.getZExtValue(); |
| 1006 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1007 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1008 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1009 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1010 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1011 | return; |
| 1012 | } |
| 1013 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1014 | D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1015 | priority)); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1018 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1019 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1020 | if (Attr.getNumArgs() > 1) { |
| 1021 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1022 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1023 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1024 | |
| 1025 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1026 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1027 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1028 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1029 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1030 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1031 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1032 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1033 | return; |
| 1034 | } |
| 1035 | priority = Idx.getZExtValue(); |
| 1036 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1037 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1038 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1039 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1040 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1044 | D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1045 | priority)); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1048 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1049 | unsigned NumArgs = Attr.getNumArgs(); |
| 1050 | if (NumArgs > 1) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1051 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1052 | return; |
| 1053 | } |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1054 | |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1055 | // Handle the case where deprecated attribute has a text message. |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1056 | llvm::StringRef Str; |
| 1057 | if (NumArgs == 1) { |
| 1058 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1059 | if (!SE) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1060 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
| 1061 | << "deprecated"; |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1062 | return; |
| 1063 | } |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1064 | Str = SE->getString(); |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1065 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1066 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1067 | D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1070 | static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1071 | unsigned NumArgs = Attr.getNumArgs(); |
| 1072 | if (NumArgs > 1) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1073 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1074 | return; |
| 1075 | } |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1076 | |
Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1077 | // Handle the case where unavailable attribute has a text message. |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1078 | llvm::StringRef Str; |
| 1079 | if (NumArgs == 1) { |
| 1080 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1081 | if (!SE) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1082 | S.Diag(Attr.getArg(0)->getLocStart(), |
Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1083 | diag::err_attribute_not_string) << "unavailable"; |
| 1084 | return; |
| 1085 | } |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1086 | Str = SE->getString(); |
Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1087 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1088 | D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str)); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1091 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1092 | const AttributeList &Attr) { |
| 1093 | unsigned NumArgs = Attr.getNumArgs(); |
| 1094 | if (NumArgs > 0) { |
| 1095 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr( |
| 1100 | Attr.getLoc(), S.Context)); |
| 1101 | } |
| 1102 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1103 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1104 | const AttributeList &Attr) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1105 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 1106 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
| 1107 | |
| 1108 | llvm::StringRef PlatformName |
| 1109 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1110 | if (PlatformName.empty()) { |
| 1111 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 1112 | << Platform; |
| 1113 | |
| 1114 | PlatformName = Platform->getName(); |
| 1115 | } |
| 1116 | |
| 1117 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1118 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1119 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1120 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Ensure that Introduced < Deprecated < Obsoleted (although not all |
| 1123 | // of these steps are needed). |
| 1124 | if (Introduced.isValid() && Deprecated.isValid() && |
| 1125 | !(Introduced.Version < Deprecated.Version)) { |
| 1126 | S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) |
| 1127 | << 1 << PlatformName << Deprecated.Version.getAsString() |
| 1128 | << 0 << Introduced.Version.getAsString(); |
| 1129 | return; |
| 1130 | } |
| 1131 | |
| 1132 | if (Introduced.isValid() && Obsoleted.isValid() && |
| 1133 | !(Introduced.Version < Obsoleted.Version)) { |
| 1134 | S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) |
| 1135 | << 2 << PlatformName << Obsoleted.Version.getAsString() |
| 1136 | << 0 << Introduced.Version.getAsString(); |
| 1137 | return; |
| 1138 | } |
| 1139 | |
| 1140 | if (Deprecated.isValid() && Obsoleted.isValid() && |
| 1141 | !(Deprecated.Version < Obsoleted.Version)) { |
| 1142 | S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering) |
| 1143 | << 2 << PlatformName << Obsoleted.Version.getAsString() |
| 1144 | << 1 << Deprecated.Version.getAsString(); |
| 1145 | return; |
| 1146 | } |
| 1147 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1148 | D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context, |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1149 | Platform, |
| 1150 | Introduced.Version, |
| 1151 | Deprecated.Version, |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1152 | Obsoleted.Version, |
| 1153 | IsUnavailable)); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1156 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1157 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1158 | if(!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1159 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1160 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1161 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1162 | Arg = Arg->IgnoreParenCasts(); |
| 1163 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1165 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1166 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1167 | << "visibility" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1168 | return; |
| 1169 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1170 | |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1171 | llvm::StringRef TypeStr = Str->getString(); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1172 | VisibilityAttr::VisibilityType type; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1173 | |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1174 | if (TypeStr == "default") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1175 | type = VisibilityAttr::Default; |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1176 | else if (TypeStr == "hidden") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1177 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1178 | else if (TypeStr == "internal") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1179 | type = VisibilityAttr::Hidden; // FIXME |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1180 | else if (TypeStr == "protected") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1181 | type = VisibilityAttr::Protected; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1182 | else { |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1183 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1186 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1187 | D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1190 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 1191 | const AttributeList &Attr) { |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1192 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 1193 | if (!method) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1194 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1195 | << ExpectedMethod; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1196 | return; |
| 1197 | } |
| 1198 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1199 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 1200 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 1201 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1202 | << "objc_method_family" << 1; |
| 1203 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1204 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1205 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1206 | Attr.setInvalid(); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1207 | return; |
| 1208 | } |
| 1209 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1210 | llvm::StringRef param = Attr.getParameterName()->getName(); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1211 | ObjCMethodFamilyAttr::FamilyKind family; |
| 1212 | if (param == "none") |
| 1213 | family = ObjCMethodFamilyAttr::OMF_None; |
| 1214 | else if (param == "alloc") |
| 1215 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 1216 | else if (param == "copy") |
| 1217 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 1218 | else if (param == "init") |
| 1219 | family = ObjCMethodFamilyAttr::OMF_init; |
| 1220 | else if (param == "mutableCopy") |
| 1221 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 1222 | else if (param == "new") |
| 1223 | family = ObjCMethodFamilyAttr::OMF_new; |
| 1224 | else { |
| 1225 | // Just warn and ignore it. This is future-proof against new |
| 1226 | // families being used in system headers. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1227 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1228 | return; |
| 1229 | } |
| 1230 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1231 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 1232 | !method->getResultType()->isObjCObjectPointerType()) { |
| 1233 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 1234 | << method->getResultType(); |
| 1235 | // Ignore the attribute. |
| 1236 | return; |
| 1237 | } |
| 1238 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1239 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1240 | S.Context, family)); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1243 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 1244 | const AttributeList &Attr) { |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1245 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1246 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1247 | |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1248 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 1249 | if (OCI == 0) { |
| 1250 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1251 | return; |
| 1252 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1253 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1254 | D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1257 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1258 | if (Attr.getNumArgs() != 0) { |
John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1259 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1260 | return; |
| 1261 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1262 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1263 | QualType T = TD->getUnderlyingType(); |
| 1264 | if (!T->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1265 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1266 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 1267 | return; |
| 1268 | } |
| 1269 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1270 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context)); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1273 | static void |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1274 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1275 | if (Attr.getNumArgs() != 0) { |
John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1276 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1277 | return; |
| 1278 | } |
| 1279 | |
| 1280 | if (!isa<FunctionDecl>(D)) { |
| 1281 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 1282 | return; |
| 1283 | } |
| 1284 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1285 | D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1288 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1289 | if (!Attr.getParameterName()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1290 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1291 | << "blocks" << 1; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1292 | return; |
| 1293 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1294 | |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1295 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1296 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1297 | return; |
| 1298 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1299 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1300 | BlocksAttr::BlockType type; |
Chris Lattner | 68e4868 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 1301 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1302 | type = BlocksAttr::ByRef; |
| 1303 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1304 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1305 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1306 | return; |
| 1307 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1308 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1309 | D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type)); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1312 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1313 | // check the attribute arguments. |
| 1314 | if (Attr.getNumArgs() > 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1315 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1316 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1319 | int sentinel = 0; |
| 1320 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1321 | Expr *E = Attr.getArg(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1322 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1323 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1324 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1325 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1326 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1327 | return; |
| 1328 | } |
| 1329 | sentinel = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1330 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1331 | if (sentinel < 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1332 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 1333 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1334 | return; |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | int nullPos = 0; |
| 1339 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1340 | Expr *E = Attr.getArg(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1341 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1342 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1343 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1344 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1345 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1346 | return; |
| 1347 | } |
| 1348 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1349 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1350 | if (nullPos > 1 || nullPos < 0) { |
| 1351 | // FIXME: This error message could be improved, it would be nice |
| 1352 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1353 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 1354 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1355 | return; |
| 1356 | } |
| 1357 | } |
| 1358 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1359 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1360 | const FunctionType *FT = FD->getType()->getAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1361 | assert(FT && "FunctionDecl has non-function type?"); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1362 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1363 | if (isa<FunctionNoProtoType>(FT)) { |
| 1364 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 1365 | return; |
| 1366 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1368 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1369 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1370 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1371 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1372 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1373 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1374 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1375 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1376 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1377 | } else if (isa<BlockDecl>(D)) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1378 | // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the |
| 1379 | // caller. |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1380 | ; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1381 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1382 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 1383 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1384 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1385 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1386 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1387 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 1388 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1389 | return; |
| 1390 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1391 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1392 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1393 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1394 | return; |
| 1395 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1396 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1397 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1398 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1399 | return; |
| 1400 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1401 | D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1402 | nullPos)); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1405 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1406 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1407 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1408 | return; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1409 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1410 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1411 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1412 | << Attr.getName() << ExpectedFunctionOrMethod; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1413 | return; |
| 1414 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1415 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1416 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 1417 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1418 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1421 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 1422 | if (MD->getResultType()->isVoidType()) { |
| 1423 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1424 | << Attr.getName() << 1; |
| 1425 | return; |
| 1426 | } |
| 1427 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1428 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1431 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1432 | // check the attribute arguments. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1433 | if (Attr.hasParameterOrArguments()) { |
| 1434 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1435 | return; |
| 1436 | } |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1437 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1438 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
| 1439 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1440 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | 41136ee | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 1441 | return; |
| 1442 | } |
| 1443 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1444 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1445 | |
| 1446 | // 'weak' only applies to declarations with external linkage. |
| 1447 | if (hasEffectivelyInternalLinkage(nd)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1448 | S.Diag(Attr.getLoc(), diag::err_attribute_weak_static); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1449 | return; |
| 1450 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1451 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1452 | nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1455 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1456 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1457 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1458 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1459 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1460 | |
| 1461 | // weak_import only applies to variable & function declarations. |
| 1462 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1463 | if (!D->canBeWeakImported(isDef)) { |
| 1464 | if (isDef) |
| 1465 | S.Diag(Attr.getLoc(), |
| 1466 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 1467 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 1468 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 1469 | (S.Context.Target.getTriple().isOSDarwin() && |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 1470 | isa<ObjCInterfaceDecl>(D))) { |
| 1471 | // Nothing to warn about here. |
| 1472 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 1473 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1474 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1475 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1476 | return; |
| 1477 | } |
| 1478 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1479 | D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1482 | static void handleReqdWorkGroupSize(Sema &S, Decl *D, |
| 1483 | const AttributeList &Attr) { |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1484 | // Attribute has 3 arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1485 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1486 | return; |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1487 | |
| 1488 | unsigned WGSize[3]; |
| 1489 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1490 | Expr *E = Attr.getArg(i); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1491 | llvm::APSInt ArgNum(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1492 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1493 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1494 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1495 | << "reqd_work_group_size" << E->getSourceRange(); |
| 1496 | return; |
| 1497 | } |
| 1498 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 1499 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1500 | D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context, |
| 1501 | WGSize[0], WGSize[1], |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1502 | WGSize[2])); |
| 1503 | } |
| 1504 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1505 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1506 | // Attribute has no arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1507 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1508 | return; |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1509 | |
| 1510 | // Make sure that there is a string literal as the sections's single |
| 1511 | // argument. |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1512 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1513 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1514 | if (!SE) { |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1515 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1516 | return; |
| 1517 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1519 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 5f08912 | 2009-11-30 17:08:26 +0000 | [diff] [blame] | 1520 | std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString()); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1521 | if (!Error.empty()) { |
| 1522 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 1523 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1524 | return; |
| 1525 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1527 | // This attribute cannot be applied to local variables. |
| 1528 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 1529 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 1530 | return; |
| 1531 | } |
| 1532 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1533 | D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, |
| 1534 | SE->getString())); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1537 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1538 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1539 | // check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1540 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1541 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1542 | return; |
| 1543 | } |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1544 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1545 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1546 | if (Existing->getLocation().isInvalid()) |
| 1547 | Existing->setLocation(Attr.getLoc()); |
| 1548 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1549 | D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1550 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1553 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1554 | // check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1555 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1556 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1557 | return; |
| 1558 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1559 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1560 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1561 | if (Existing->getLocation().isInvalid()) |
| 1562 | Existing->setLocation(Attr.getLoc()); |
| 1563 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1564 | D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1565 | } |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1568 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1569 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1570 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1571 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1572 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1573 | D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1576 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1577 | if (!Attr.getParameterName()) { |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1578 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1579 | return; |
| 1580 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1581 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1582 | if (Attr.getNumArgs() != 0) { |
| 1583 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1584 | return; |
| 1585 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1586 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1587 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1588 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1589 | if (!VD || !VD->hasLocalStorage()) { |
| 1590 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 1591 | return; |
| 1592 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1593 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1594 | // Look up the function |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1595 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1596 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1597 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 1598 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1599 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1600 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1601 | Attr.getParameterName(); |
| 1602 | return; |
| 1603 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1604 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1605 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 1606 | if (!FD) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1607 | S.Diag(Attr.getParameterLoc(), |
| 1608 | diag::err_attribute_cleanup_arg_not_function) |
| 1609 | << Attr.getParameterName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1610 | return; |
| 1611 | } |
| 1612 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1613 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1614 | S.Diag(Attr.getParameterLoc(), |
| 1615 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 1616 | << Attr.getParameterName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1617 | return; |
| 1618 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1619 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1620 | // We're currently more strict than GCC about what function types we accept. |
| 1621 | // If this ever proves to be a problem it should be easy to fix. |
| 1622 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 1623 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 1624 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 1625 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1626 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1627 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 1628 | Attr.getParameterName() << ParamTy << Ty; |
| 1629 | return; |
| 1630 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1631 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1632 | D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD)); |
Argyrios Kyrtzidis | e88168a | 2010-12-06 17:51:53 +0000 | [diff] [blame] | 1633 | S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1636 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 1637 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1638 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1639 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1640 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1641 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1642 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1643 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1644 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1645 | return; |
| 1646 | } |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1647 | |
| 1648 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1649 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1650 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1651 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1652 | unsigned FirstIdx = 1; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1653 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1654 | // checks for the 2nd argument |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1655 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1656 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1657 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1658 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1659 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 1660 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1661 | return; |
| 1662 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1663 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1664 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 1665 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1666 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1667 | return; |
| 1668 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1669 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1670 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1671 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1672 | if (HasImplicitThisParam) { |
| 1673 | if (ArgIdx == 0) { |
| 1674 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1675 | << "format_arg" << IdxExpr->getSourceRange(); |
| 1676 | return; |
| 1677 | } |
| 1678 | ArgIdx--; |
| 1679 | } |
| 1680 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1681 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1682 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1683 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1684 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 1685 | if (not_nsstring_type && |
| 1686 | !isCFStringType(Ty, S.Context) && |
| 1687 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1688 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1689 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1690 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1691 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1692 | << IdxExpr->getSourceRange(); |
| 1693 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1694 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1695 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1696 | if (!isNSStringType(Ty, S.Context) && |
| 1697 | !isCFStringType(Ty, S.Context) && |
| 1698 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1699 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1700 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1701 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1702 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1703 | << IdxExpr->getSourceRange(); |
| 1704 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1707 | D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1708 | Idx.getZExtValue())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1711 | enum FormatAttrKind { |
| 1712 | CFStringFormat, |
| 1713 | NSStringFormat, |
| 1714 | StrftimeFormat, |
| 1715 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1716 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1717 | InvalidFormat |
| 1718 | }; |
| 1719 | |
| 1720 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 1721 | /// types. |
| 1722 | static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) { |
| 1723 | // Check for formats that get handled specially. |
| 1724 | if (Format == "NSString") |
| 1725 | return NSStringFormat; |
| 1726 | if (Format == "CFString") |
| 1727 | return CFStringFormat; |
| 1728 | if (Format == "strftime") |
| 1729 | return StrftimeFormat; |
| 1730 | |
| 1731 | // Otherwise, check for supported formats. |
| 1732 | if (Format == "scanf" || Format == "printf" || Format == "printf0" || |
| 1733 | Format == "strfmon" || Format == "cmn_err" || Format == "strftime" || |
| 1734 | Format == "NSString" || Format == "CFString" || Format == "vcmn_err" || |
Chris Lattner | 0ddd0ae | 2011-02-18 17:05:55 +0000 | [diff] [blame] | 1735 | Format == "zcmn_err" || |
| 1736 | Format == "kprintf") // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1737 | return SupportedFormat; |
| 1738 | |
Duncan Sands | de4fe35 | 2010-03-23 14:44:19 +0000 | [diff] [blame] | 1739 | if (Format == "gcc_diag" || Format == "gcc_cdiag" || |
| 1740 | Format == "gcc_cxxdiag" || Format == "gcc_tdiag") |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1741 | return IgnoredFormat; |
| 1742 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1743 | return InvalidFormat; |
| 1744 | } |
| 1745 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1746 | /// Handle __attribute__((init_priority(priority))) attributes based on |
| 1747 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1748 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 1749 | const AttributeList &Attr) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1750 | if (!S.getLangOptions().CPlusPlus) { |
| 1751 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1752 | return; |
| 1753 | } |
| 1754 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1755 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1756 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 1757 | Attr.setInvalid(); |
| 1758 | return; |
| 1759 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1760 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1761 | if (S.Context.getAsArrayType(T)) |
| 1762 | T = S.Context.getBaseElementType(T); |
| 1763 | if (!T->getAs<RecordType>()) { |
| 1764 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 1765 | Attr.setInvalid(); |
| 1766 | return; |
| 1767 | } |
| 1768 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1769 | if (Attr.getNumArgs() != 1) { |
| 1770 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1771 | Attr.setInvalid(); |
| 1772 | return; |
| 1773 | } |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1774 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1775 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1776 | llvm::APSInt priority(32); |
| 1777 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 1778 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 1779 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1780 | << "init_priority" << priorityExpr->getSourceRange(); |
| 1781 | Attr.setInvalid(); |
| 1782 | return; |
| 1783 | } |
Fariborz Jahanian | 9f2a4ee | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 1784 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1785 | if (prioritynum < 101 || prioritynum > 65535) { |
| 1786 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 1787 | << priorityExpr->getSourceRange(); |
| 1788 | Attr.setInvalid(); |
| 1789 | return; |
| 1790 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1791 | D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1792 | prioritynum)); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1795 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 1796 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1797 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1798 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1799 | if (!Attr.getParameterName()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1800 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1801 | << "format" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1802 | return; |
| 1803 | } |
| 1804 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1805 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1806 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1807 | return; |
| 1808 | } |
| 1809 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1810 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1811 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1812 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1813 | return; |
| 1814 | } |
| 1815 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1816 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1817 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1818 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1819 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1820 | unsigned FirstIdx = 1; |
| 1821 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1822 | llvm::StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1823 | |
| 1824 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1825 | if (Format.startswith("__") && Format.endswith("__")) |
| 1826 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1827 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1828 | // Check for supported formats. |
| 1829 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1830 | |
| 1831 | if (Kind == IgnoredFormat) |
| 1832 | return; |
| 1833 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1834 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1835 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1836 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1837 | return; |
| 1838 | } |
| 1839 | |
| 1840 | // checks for the 2nd argument |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1841 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1842 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1843 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1844 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1845 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1846 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1847 | return; |
| 1848 | } |
| 1849 | |
| 1850 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1851 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1852 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1853 | return; |
| 1854 | } |
| 1855 | |
| 1856 | // FIXME: Do we need to bounds check? |
| 1857 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1858 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1859 | if (HasImplicitThisParam) { |
| 1860 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1861 | S.Diag(Attr.getLoc(), |
| 1862 | diag::err_format_attribute_implicit_this_format_string) |
| 1863 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1864 | return; |
| 1865 | } |
| 1866 | ArgIdx--; |
| 1867 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1869 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1870 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1871 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1872 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1873 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1874 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1875 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1876 | return; |
| 1877 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1878 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1879 | // FIXME: do we need to check if the type is NSString*? What are the |
| 1880 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1881 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1882 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1883 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1884 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1885 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1886 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1887 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1888 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1889 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1890 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1891 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1892 | return; |
| 1893 | } |
| 1894 | |
| 1895 | // check the 3rd argument |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1896 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1897 | llvm::APSInt FirstArg(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1898 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 1899 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1900 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1901 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1902 | return; |
| 1903 | } |
| 1904 | |
| 1905 | // check if the function is variadic if the 3rd argument non-zero |
| 1906 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1907 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1908 | ++NumArgs; // +1 for ... |
| 1909 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1910 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1911 | return; |
| 1912 | } |
| 1913 | } |
| 1914 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1915 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 1916 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1917 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1918 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1919 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 1920 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1921 | return; |
| 1922 | } |
| 1923 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 1924 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1925 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1926 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1927 | return; |
| 1928 | } |
| 1929 | |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1930 | // Check whether we already have an equivalent format attribute. |
| 1931 | for (specific_attr_iterator<FormatAttr> |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1932 | i = D->specific_attr_begin<FormatAttr>(), |
| 1933 | e = D->specific_attr_end<FormatAttr>(); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1934 | i != e ; ++i) { |
| 1935 | FormatAttr *f = *i; |
| 1936 | if (f->getType() == Format && |
| 1937 | f->getFormatIdx() == (int)Idx.getZExtValue() && |
| 1938 | f->getFirstArg() == (int)FirstArg.getZExtValue()) { |
| 1939 | // If we don't have a valid location for this attribute, adopt the |
| 1940 | // location. |
| 1941 | if (f->getLocation().isInvalid()) |
| 1942 | f->setLocation(Attr.getLoc()); |
| 1943 | return; |
| 1944 | } |
| 1945 | } |
| 1946 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1947 | D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format, |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1948 | Idx.getZExtValue(), |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1949 | FirstArg.getZExtValue())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1952 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 1953 | const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1954 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1955 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1956 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 1957 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1959 | // Try to find the underlying union declaration. |
| 1960 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1961 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1962 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 1963 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 1964 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1965 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1966 | |
| 1967 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1968 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1969 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1970 | return; |
| 1971 | } |
| 1972 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1973 | if (!RD->isDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1974 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1975 | diag::warn_transparent_union_attribute_not_definition); |
| 1976 | return; |
| 1977 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1978 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1979 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 1980 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1981 | if (Field == FieldEnd) { |
| 1982 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 1983 | return; |
| 1984 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1985 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1986 | FieldDecl *FirstField = *Field; |
| 1987 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 1988 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1989 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 1990 | diag::warn_transparent_union_attribute_floating) |
| 1991 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1992 | return; |
| 1993 | } |
| 1994 | |
| 1995 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 1996 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 1997 | for (; Field != FieldEnd; ++Field) { |
| 1998 | QualType FieldType = Field->getType(); |
| 1999 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 2000 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 2001 | // Warn if we drop the attribute. |
| 2002 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2003 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2004 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2005 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2006 | diag::warn_transparent_union_attribute_field_size_align) |
| 2007 | << isSize << Field->getDeclName() << FieldBits; |
| 2008 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2009 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2010 | diag::note_transparent_union_first_field_size_align) |
| 2011 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2012 | return; |
| 2013 | } |
| 2014 | } |
| 2015 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2016 | RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2019 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2020 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2021 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2022 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2023 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2024 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2025 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2026 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2027 | // Make sure that there is a string literal as the annotation's single |
| 2028 | // argument. |
| 2029 | if (!SE) { |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2030 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2031 | return; |
| 2032 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2033 | D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2034 | SE->getString())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2037 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2038 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2039 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2040 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2041 | return; |
| 2042 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2043 | |
| 2044 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 2045 | // than GNU's, and should error out when it is used to specify a |
| 2046 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2047 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2048 | if (Attr.getNumArgs() == 0) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2049 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2050 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2051 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2052 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2053 | S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0)); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) { |
| 2057 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2058 | // Save dependent expressions in the AST to be instantiated. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2059 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2060 | return; |
| 2061 | } |
| 2062 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2063 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2064 | llvm::APSInt Alignment(32); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2065 | if (!E->isIntegerConstantExpr(Alignment, Context)) { |
| 2066 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 2067 | << "aligned" << E->getSourceRange(); |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2068 | return; |
| 2069 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2070 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2071 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2072 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2073 | return; |
| 2074 | } |
| 2075 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2076 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); |
| 2077 | } |
| 2078 | |
| 2079 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) { |
| 2080 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2081 | // FIXME: Perform checking of type validity |
| 2082 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS)); |
| 2083 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2084 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2085 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2086 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2087 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2088 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2089 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2090 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2091 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2092 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2093 | // This attribute isn't documented, but glibc uses it. It changes |
| 2094 | // the width of an int or unsigned int to the specified size. |
| 2095 | |
| 2096 | // Check that there aren't any arguments |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2097 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2098 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2099 | |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2100 | |
| 2101 | IdentifierInfo *Name = Attr.getParameterName(); |
| 2102 | if (!Name) { |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2103 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2104 | return; |
| 2105 | } |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2106 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2107 | llvm::StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2108 | |
| 2109 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2110 | if (Str.startswith("__") && Str.endswith("__")) |
| 2111 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2112 | |
| 2113 | unsigned DestWidth = 0; |
| 2114 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2115 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2116 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2117 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2118 | switch (Str[0]) { |
| 2119 | case 'Q': DestWidth = 8; break; |
| 2120 | case 'H': DestWidth = 16; break; |
| 2121 | case 'S': DestWidth = 32; break; |
| 2122 | case 'D': DestWidth = 64; break; |
| 2123 | case 'X': DestWidth = 96; break; |
| 2124 | case 'T': DestWidth = 128; break; |
| 2125 | } |
| 2126 | if (Str[1] == 'F') { |
| 2127 | IntegerMode = false; |
| 2128 | } else if (Str[1] == 'C') { |
| 2129 | IntegerMode = false; |
| 2130 | ComplexMode = true; |
| 2131 | } else if (Str[1] != 'I') { |
| 2132 | DestWidth = 0; |
| 2133 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2134 | break; |
| 2135 | case 4: |
| 2136 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2137 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2138 | if (Str == "word") |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2139 | DestWidth = S.Context.Target.getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2140 | else if (Str == "byte") |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2141 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2142 | break; |
| 2143 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2144 | if (Str == "pointer") |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2145 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2146 | break; |
| 2147 | } |
| 2148 | |
| 2149 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2150 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2151 | OldTy = TD->getUnderlyingType(); |
| 2152 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2153 | OldTy = VD->getType(); |
| 2154 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2155 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 2156 | << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2157 | return; |
| 2158 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2159 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2160 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2161 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2162 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2163 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2164 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2165 | } else if (ComplexMode) { |
| 2166 | if (!OldTy->isComplexType()) |
| 2167 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2168 | } else { |
| 2169 | if (!OldTy->isFloatingType()) |
| 2170 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2171 | } |
| 2172 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2173 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2174 | // and friends, at least with glibc. |
| 2175 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 2176 | // width on unusual platforms. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2177 | // FIXME: Make sure floating-point mappings are accurate |
| 2178 | // FIXME: Support XF and TF types |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2179 | QualType NewTy; |
| 2180 | switch (DestWidth) { |
| 2181 | case 0: |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2182 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2183 | return; |
| 2184 | default: |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2185 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2186 | return; |
| 2187 | case 8: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2188 | if (!IntegerMode) { |
| 2189 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2190 | return; |
| 2191 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2192 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2193 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2194 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2195 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2196 | break; |
| 2197 | case 16: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2198 | if (!IntegerMode) { |
| 2199 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2200 | return; |
| 2201 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2202 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2203 | NewTy = S.Context.ShortTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2204 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2205 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | case 32: |
| 2208 | if (!IntegerMode) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2209 | NewTy = S.Context.FloatTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2210 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2211 | NewTy = S.Context.IntTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2212 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2213 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2214 | break; |
| 2215 | case 64: |
| 2216 | if (!IntegerMode) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2217 | NewTy = S.Context.DoubleTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2218 | else if (OldTy->isSignedIntegerType()) |
Chandler Carruth | 7234370 | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2219 | if (S.Context.Target.getLongWidth() == 64) |
| 2220 | NewTy = S.Context.LongTy; |
| 2221 | else |
| 2222 | NewTy = S.Context.LongLongTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2223 | else |
Chandler Carruth | 7234370 | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2224 | if (S.Context.Target.getLongWidth() == 64) |
| 2225 | NewTy = S.Context.UnsignedLongTy; |
| 2226 | else |
| 2227 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2228 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2229 | case 96: |
| 2230 | NewTy = S.Context.LongDoubleTy; |
| 2231 | break; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2232 | case 128: |
| 2233 | if (!IntegerMode) { |
| 2234 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2235 | return; |
| 2236 | } |
Anders Carlsson | 88ea245 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 2237 | if (OldTy->isSignedIntegerType()) |
| 2238 | NewTy = S.Context.Int128Ty; |
| 2239 | else |
| 2240 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2241 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2244 | if (ComplexMode) { |
| 2245 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | // Install the new type. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2249 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2250 | // FIXME: preserve existing source info. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2251 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2252 | } else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2253 | cast<ValueDecl>(D)->setType(NewTy); |
| 2254 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2255 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2256 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2257 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2258 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2259 | return; |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 2260 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2261 | if (!isFunctionOrMethod(D)) { |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2262 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2263 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2264 | return; |
| 2265 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2266 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2267 | D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2270 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2271 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2272 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2273 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2274 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2275 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2276 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2277 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2278 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2279 | return; |
| 2280 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2281 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2282 | D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2285 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 2286 | const AttributeList &Attr) { |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2287 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2288 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2289 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2290 | |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2291 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2292 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2293 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2294 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2295 | return; |
| 2296 | } |
| 2297 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2298 | D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2299 | S.Context)); |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2302 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2303 | if (S.LangOpts.CUDA) { |
| 2304 | // check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2305 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2306 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2307 | return; |
| 2308 | } |
| 2309 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2310 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2311 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2312 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2313 | return; |
| 2314 | } |
| 2315 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2316 | D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2317 | } else { |
| 2318 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 2319 | } |
| 2320 | } |
| 2321 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2322 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2323 | if (S.LangOpts.CUDA) { |
| 2324 | // check the attribute arguments. |
| 2325 | if (Attr.getNumArgs() != 0) { |
| 2326 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2327 | return; |
| 2328 | } |
| 2329 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2330 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2331 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2332 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2333 | return; |
| 2334 | } |
| 2335 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2336 | D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2337 | } else { |
| 2338 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 2339 | } |
| 2340 | } |
| 2341 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2342 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2343 | if (S.LangOpts.CUDA) { |
| 2344 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2345 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2346 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2347 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2348 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2349 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2350 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2351 | return; |
| 2352 | } |
| 2353 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2354 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2355 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 2356 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2357 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 2358 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2359 | << FD->getType() |
| 2360 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 2361 | "void"); |
| 2362 | } else { |
| 2363 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2364 | << FD->getType(); |
| 2365 | } |
| 2366 | return; |
| 2367 | } |
| 2368 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2369 | D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2370 | } else { |
| 2371 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 2372 | } |
| 2373 | } |
| 2374 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2375 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2376 | if (S.LangOpts.CUDA) { |
| 2377 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2378 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2379 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2380 | |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2381 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2382 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2383 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2384 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2385 | return; |
| 2386 | } |
| 2387 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2388 | D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2389 | } else { |
| 2390 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 2391 | } |
| 2392 | } |
| 2393 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2394 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2395 | if (S.LangOpts.CUDA) { |
| 2396 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2397 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2398 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2399 | |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2400 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2401 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2402 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2403 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2404 | return; |
| 2405 | } |
| 2406 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2407 | D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2408 | } else { |
| 2409 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 2410 | } |
| 2411 | } |
| 2412 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2413 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2414 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2415 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2416 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2417 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2418 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2419 | if (Fn == 0) { |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2420 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2421 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2422 | return; |
| 2423 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2424 | |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 2425 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 2426 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2427 | return; |
| 2428 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2429 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2430 | D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2433 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2434 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2435 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2436 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2437 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 2438 | CallingConv CC; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2439 | if (S.CheckCallingConvAttr(Attr, CC)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2440 | return; |
| 2441 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2442 | if (!isa<ObjCMethodDecl>(D)) { |
| 2443 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2444 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2445 | return; |
| 2446 | } |
| 2447 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2448 | switch (Attr.getKind()) { |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2449 | case AttributeList::AT_fastcall: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2450 | D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context)); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2451 | return; |
| 2452 | case AttributeList::AT_stdcall: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2453 | D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context)); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2454 | return; |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2455 | case AttributeList::AT_thiscall: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2456 | D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 2457 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2458 | case AttributeList::AT_cdecl: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2459 | D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context)); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2460 | return; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2461 | case AttributeList::AT_pascal: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2462 | D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context)); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2463 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2464 | case AttributeList::AT_pcs: { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2465 | Expr *Arg = Attr.getArg(0); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2466 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 2467 | if (Str == 0 || Str->isWide()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2468 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2469 | << "pcs" << 1; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2470 | Attr.setInvalid(); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2471 | return; |
| 2472 | } |
| 2473 | |
| 2474 | llvm::StringRef StrRef = Str->getString(); |
| 2475 | PcsAttr::PCSType PCS; |
| 2476 | if (StrRef == "aapcs") |
| 2477 | PCS = PcsAttr::AAPCS; |
| 2478 | else if (StrRef == "aapcs-vfp") |
| 2479 | PCS = PcsAttr::AAPCS_VFP; |
| 2480 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2481 | S.Diag(Attr.getLoc(), diag::err_invalid_pcs); |
| 2482 | Attr.setInvalid(); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2483 | return; |
| 2484 | } |
| 2485 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2486 | D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS)); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2487 | } |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2488 | default: |
| 2489 | llvm_unreachable("unexpected attribute kind"); |
| 2490 | return; |
| 2491 | } |
| 2492 | } |
| 2493 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2494 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2495 | assert(Attr.isInvalid() == false); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2496 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2499 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { |
| 2500 | if (attr.isInvalid()) |
| 2501 | return true; |
| 2502 | |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2503 | if ((attr.getNumArgs() != 0 && |
| 2504 | !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) || |
| 2505 | attr.getParameterName()) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2506 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2507 | attr.setInvalid(); |
| 2508 | return true; |
| 2509 | } |
| 2510 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2511 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 2512 | // move to TargetAttributesSema one day. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2513 | switch (attr.getKind()) { |
| 2514 | case AttributeList::AT_cdecl: CC = CC_C; break; |
| 2515 | case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; |
| 2516 | case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; |
| 2517 | case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break; |
| 2518 | case AttributeList::AT_pascal: CC = CC_X86Pascal; break; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2519 | case AttributeList::AT_pcs: { |
| 2520 | Expr *Arg = attr.getArg(0); |
| 2521 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 2522 | if (Str == 0 || Str->isWide()) { |
| 2523 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 2524 | << "pcs" << 1; |
| 2525 | attr.setInvalid(); |
| 2526 | return true; |
| 2527 | } |
| 2528 | |
| 2529 | llvm::StringRef StrRef = Str->getString(); |
| 2530 | if (StrRef == "aapcs") { |
| 2531 | CC = CC_AAPCS; |
| 2532 | break; |
| 2533 | } else if (StrRef == "aapcs-vfp") { |
| 2534 | CC = CC_AAPCS_VFP; |
| 2535 | break; |
| 2536 | } |
| 2537 | // FALLS THROUGH |
| 2538 | } |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2539 | default: llvm_unreachable("unexpected attribute kind"); return true; |
| 2540 | } |
| 2541 | |
| 2542 | return false; |
| 2543 | } |
| 2544 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2545 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2546 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2547 | |
| 2548 | unsigned numParams; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2549 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2550 | return; |
| 2551 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2552 | if (!isa<ObjCMethodDecl>(D)) { |
| 2553 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2554 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2555 | return; |
| 2556 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2557 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2558 | D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams)); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 2562 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2563 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 2564 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2565 | return true; |
| 2566 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2567 | if (Attr.getNumArgs() != 1) { |
| 2568 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2569 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2570 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2571 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2572 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2573 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2574 | llvm::APSInt NumParams(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2575 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2576 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2577 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2578 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2579 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2580 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2583 | if (Context.Target.getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2584 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2585 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2586 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2587 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2590 | numParams = NumParams.getZExtValue(); |
| 2591 | if (numParams > Context.Target.getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2592 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2593 | << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2594 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2595 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2598 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2601 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2602 | if (S.LangOpts.CUDA) { |
| 2603 | // check the attribute arguments. |
| 2604 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2605 | // FIXME: 0 is not okay. |
| 2606 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2607 | return; |
| 2608 | } |
| 2609 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2610 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2611 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2612 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2613 | return; |
| 2614 | } |
| 2615 | |
| 2616 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 2617 | llvm::APSInt MaxThreads(32); |
| 2618 | if (MaxThreadsExpr->isTypeDependent() || |
| 2619 | MaxThreadsExpr->isValueDependent() || |
| 2620 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 2621 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2622 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 2623 | return; |
| 2624 | } |
| 2625 | |
| 2626 | llvm::APSInt MinBlocks(32); |
| 2627 | if (Attr.getNumArgs() > 1) { |
| 2628 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 2629 | if (MinBlocksExpr->isTypeDependent() || |
| 2630 | MinBlocksExpr->isValueDependent() || |
| 2631 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 2632 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2633 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 2634 | return; |
| 2635 | } |
| 2636 | } |
| 2637 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2638 | D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context, |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2639 | MaxThreads.getZExtValue(), |
| 2640 | MinBlocks.getZExtValue())); |
| 2641 | } else { |
| 2642 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 2643 | } |
| 2644 | } |
| 2645 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2646 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2647 | // Checker-specific attribute handlers. |
| 2648 | //===----------------------------------------------------------------------===// |
| 2649 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2650 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
| 2651 | return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type); |
| 2652 | } |
| 2653 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
| 2654 | return type->isPointerType() || isValidSubjectOfNSAttribute(S, type); |
| 2655 | } |
| 2656 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2657 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2658 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2659 | if (!param) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2660 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| 2661 | << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2662 | return; |
| 2663 | } |
| 2664 | |
| 2665 | bool typeOK, cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2666 | if (Attr.getKind() == AttributeList::AT_ns_consumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2667 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 2668 | cf = false; |
| 2669 | } else { |
| 2670 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 2671 | cf = true; |
| 2672 | } |
| 2673 | |
| 2674 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2675 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 2676 | << SourceRange(Attr.getLoc()) << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2677 | return; |
| 2678 | } |
| 2679 | |
| 2680 | if (cf) |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2681 | param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2682 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2683 | param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2686 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 2687 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2688 | if (!isa<ObjCMethodDecl>(D)) { |
| 2689 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| 2690 | << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2691 | return; |
| 2692 | } |
| 2693 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2694 | D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2697 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 2698 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2699 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2700 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2701 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2702 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2703 | returnType = MD->getResultType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2704 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
Fariborz Jahanian | f4105f5 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 2705 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2706 | else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) && |
| 2707 | (Attr.getKind() == AttributeList::AT_ns_returns_retained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2708 | return; // ignore: was handled as a type attribute |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2709 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2710 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2711 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2712 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| 2713 | << SourceRange(Attr.getLoc()) << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2714 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2715 | return; |
| 2716 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2717 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2718 | bool typeOK; |
| 2719 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2720 | switch (Attr.getKind()) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2721 | default: llvm_unreachable("invalid ownership attribute"); return; |
| 2722 | case AttributeList::AT_ns_returns_autoreleased: |
| 2723 | case AttributeList::AT_ns_returns_retained: |
| 2724 | case AttributeList::AT_ns_returns_not_retained: |
| 2725 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 2726 | cf = false; |
| 2727 | break; |
| 2728 | |
| 2729 | case AttributeList::AT_cf_returns_retained: |
| 2730 | case AttributeList::AT_cf_returns_not_retained: |
| 2731 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 2732 | cf = true; |
| 2733 | break; |
| 2734 | } |
| 2735 | |
| 2736 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2737 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 2738 | << SourceRange(Attr.getLoc()) |
| 2739 | << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2740 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2741 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2742 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2743 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2744 | default: |
| 2745 | assert(0 && "invalid ownership attribute"); |
| 2746 | return; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2747 | case AttributeList::AT_ns_returns_autoreleased: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2748 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(), |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2749 | S.Context)); |
| 2750 | return; |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2751 | case AttributeList::AT_cf_returns_not_retained: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2752 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2753 | S.Context)); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2754 | return; |
| 2755 | case AttributeList::AT_ns_returns_not_retained: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2756 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2757 | S.Context)); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2758 | return; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2759 | case AttributeList::AT_cf_returns_retained: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2760 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2761 | S.Context)); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2762 | return; |
| 2763 | case AttributeList::AT_ns_returns_retained: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2764 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2765 | S.Context)); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2766 | return; |
| 2767 | }; |
| 2768 | } |
| 2769 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2770 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 2771 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2772 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2773 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2774 | SourceLocation L = Attr.getLoc(); |
| 2775 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 2776 | << SourceRange(L, L) << Attr.getName() << 12 /* variable */; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2779 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 2780 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2781 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
| 2782 | SourceLocation L = Attr.getLoc(); |
| 2783 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 2784 | << SourceRange(L, L) << Attr.getName() << 12 /* variable */; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2785 | return; |
| 2786 | } |
| 2787 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2788 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2789 | QualType type = vd->getType(); |
| 2790 | |
| 2791 | if (!type->isDependentType() && |
| 2792 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2793 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2794 | << type; |
| 2795 | return; |
| 2796 | } |
| 2797 | |
| 2798 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 2799 | |
| 2800 | // If we have no lifetime yet, check the lifetime we're presumably |
| 2801 | // going to infer. |
| 2802 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 2803 | lifetime = type->getObjCARCImplicitLifetime(); |
| 2804 | |
| 2805 | switch (lifetime) { |
| 2806 | case Qualifiers::OCL_None: |
| 2807 | assert(type->isDependentType() && |
| 2808 | "didn't infer lifetime for non-dependent type?"); |
| 2809 | break; |
| 2810 | |
| 2811 | case Qualifiers::OCL_Weak: // meaningful |
| 2812 | case Qualifiers::OCL_Strong: // meaningful |
| 2813 | break; |
| 2814 | |
| 2815 | case Qualifiers::OCL_ExplicitNone: |
| 2816 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2817 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2818 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 2819 | break; |
| 2820 | } |
| 2821 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2822 | D->addAttr(::new (S.Context) |
| 2823 | ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 2826 | static bool isKnownDeclSpecAttr(const AttributeList &Attr) { |
| 2827 | return Attr.getKind() == AttributeList::AT_dllimport || |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2828 | Attr.getKind() == AttributeList::AT_dllexport || |
| 2829 | Attr.getKind() == AttributeList::AT_uuid; |
| 2830 | } |
| 2831 | |
| 2832 | //===----------------------------------------------------------------------===// |
| 2833 | // Microsoft specific attribute handlers. |
| 2834 | //===----------------------------------------------------------------------===// |
| 2835 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2836 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2837 | if (S.LangOpts.Microsoft || S.LangOpts.Borland) { |
| 2838 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2839 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2840 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame^] | 2841 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2842 | Expr *Arg = Attr.getArg(0); |
| 2843 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2844 | if (Str == 0 || Str->isWide()) { |
| 2845 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 2846 | << "uuid" << 1; |
| 2847 | return; |
| 2848 | } |
| 2849 | |
| 2850 | llvm::StringRef StrRef = Str->getString(); |
| 2851 | |
| 2852 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 2853 | StrRef.back() == '}'; |
| 2854 | |
| 2855 | // Validate GUID length. |
| 2856 | if (IsCurly && StrRef.size() != 38) { |
| 2857 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2858 | return; |
| 2859 | } |
| 2860 | if (!IsCurly && StrRef.size() != 36) { |
| 2861 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2862 | return; |
| 2863 | } |
| 2864 | |
| 2865 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 2866 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 2867 | llvm::StringRef::iterator I = StrRef.begin(); |
| 2868 | if (IsCurly) // Skip the optional '{' |
| 2869 | ++I; |
| 2870 | |
| 2871 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2872 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 2873 | if (*I != '-') { |
| 2874 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2875 | return; |
| 2876 | } |
| 2877 | } else if (!isxdigit(*I)) { |
| 2878 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2879 | return; |
| 2880 | } |
| 2881 | I++; |
| 2882 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2883 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2884 | D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context, |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2885 | Str->getString())); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2886 | } else |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2887 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2890 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2891 | // Top Level Sema Entry Points |
| 2892 | //===----------------------------------------------------------------------===// |
| 2893 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2894 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 2895 | const AttributeList &Attr) { |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2896 | switch (Attr.getKind()) { |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2897 | case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break; |
| 2898 | case AttributeList::AT_host: handleHostAttr (S, D, Attr); break; |
| 2899 | case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2900 | default: |
| 2901 | break; |
| 2902 | } |
| 2903 | } |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2904 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2905 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 2906 | const AttributeList &Attr) { |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2907 | switch (Attr.getKind()) { |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2908 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 2909 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 2910 | case AttributeList::AT_IBOutletCollection: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2911 | handleIBOutletCollection(S, D, Attr); break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2912 | case AttributeList::AT_address_space: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2913 | case AttributeList::AT_opencl_image_access: |
Fariborz Jahanian | 257eac6 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 2914 | case AttributeList::AT_objc_gc: |
John Thompson | 4798122 | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 2915 | case AttributeList::AT_vector_size: |
Bob Wilson | 118baf7 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 2916 | case AttributeList::AT_neon_vector_type: |
| 2917 | case AttributeList::AT_neon_polyvector_type: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2918 | // Ignore these, these are type attributes, handled by |
| 2919 | // ProcessTypeAttributes. |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2920 | break; |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2921 | case AttributeList::AT_device: |
| 2922 | case AttributeList::AT_host: |
| 2923 | case AttributeList::AT_overloadable: |
| 2924 | // Ignore, this is a non-inheritable attribute, handled |
| 2925 | // by ProcessNonInheritableDeclAttr. |
| 2926 | break; |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2927 | case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break; |
| 2928 | case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2929 | case AttributeList::AT_always_inline: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2930 | handleAlwaysInlineAttr (S, D, Attr); break; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 2931 | case AttributeList::AT_analyzer_noreturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2932 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
| 2933 | case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break; |
| 2934 | case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2935 | case AttributeList::AT_carries_dependency: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2936 | handleDependencyAttr (S, D, Attr); break; |
| 2937 | case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break; |
| 2938 | case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break; |
| 2939 | case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break; |
| 2940 | case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break; |
| 2941 | case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2942 | case AttributeList::AT_ext_vector_type: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2943 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2944 | break; |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2945 | case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break; |
| 2946 | case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break; |
| 2947 | case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break; |
| 2948 | case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2949 | case AttributeList::AT_launch_bounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2950 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2951 | break; |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2952 | case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break; |
| 2953 | case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break; |
| 2954 | case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break; |
| 2955 | case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break; |
| 2956 | case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 2957 | case AttributeList::AT_ownership_returns: |
| 2958 | case AttributeList::AT_ownership_takes: |
| 2959 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2960 | handleOwnershipAttr (S, D, Attr); break; |
| 2961 | case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break; |
| 2962 | case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break; |
| 2963 | case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break; |
| 2964 | case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break; |
| 2965 | case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2966 | |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 2967 | case AttributeList::AT_objc_ownership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2968 | handleObjCOwnershipAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2969 | case AttributeList::AT_objc_precise_lifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2970 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2971 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2972 | // Checker-specific. |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2973 | case AttributeList::AT_cf_consumed: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2974 | case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2975 | case AttributeList::AT_ns_consumes_self: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2976 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2977 | |
| 2978 | case AttributeList::AT_ns_returns_autoreleased: |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2979 | case AttributeList::AT_ns_returns_not_retained: |
| 2980 | case AttributeList::AT_cf_returns_not_retained: |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2981 | case AttributeList::AT_ns_returns_retained: |
| 2982 | case AttributeList::AT_cf_returns_retained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2983 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2984 | |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2985 | case AttributeList::AT_reqd_wg_size: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2986 | handleReqdWorkGroupSize(S, D, Attr); break; |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2987 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2988 | case AttributeList::AT_init_priority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2989 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2990 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2991 | case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break; |
| 2992 | case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break; |
| 2993 | case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break; |
| 2994 | case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break; |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2995 | case AttributeList::AT_arc_weakref_unavailable: |
| 2996 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 2997 | break; |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2998 | case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break; |
| 2999 | case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break; |
| 3000 | case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break; |
| 3001 | case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 3002 | break; |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3003 | case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break; |
| 3004 | case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break; |
| 3005 | case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3006 | case AttributeList::AT_transparent_union: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3007 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3008 | break; |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 3009 | case AttributeList::AT_objc_exception: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3010 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 3011 | break; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 3012 | case AttributeList::AT_objc_method_family: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3013 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 3014 | break; |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3015 | case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break; |
| 3016 | case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break; |
| 3017 | case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break; |
| 3018 | case AttributeList::AT_const: handleConstAttr (S, D, Attr); break; |
| 3019 | case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break; |
| 3020 | case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break; |
| 3021 | case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break; |
| 3022 | case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break; |
| 3023 | case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3024 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | b4f3134 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 3025 | // Just ignore |
| 3026 | break; |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3027 | case AttributeList::AT_no_instrument_function: // Interacts with -pg. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3028 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3029 | break; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3030 | case AttributeList::AT_stdcall: |
| 3031 | case AttributeList::AT_cdecl: |
| 3032 | case AttributeList::AT_fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3033 | case AttributeList::AT_thiscall: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3034 | case AttributeList::AT_pascal: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3035 | case AttributeList::AT_pcs: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3036 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3037 | break; |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3038 | case AttributeList::AT_opencl_kernel_function: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3039 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3040 | break; |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3041 | case AttributeList::AT_uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3042 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3043 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3044 | default: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3045 | // Ask target about the attribute. |
| 3046 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 3047 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Chandler Carruth | dd1bc0f | 2010-07-08 09:42:26 +0000 | [diff] [blame] | 3048 | S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) |
| 3049 | << Attr.getName(); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3050 | break; |
| 3051 | } |
| 3052 | } |
| 3053 | |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3054 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 3055 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 3056 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 3057 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3058 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 3059 | const AttributeList &Attr, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3060 | bool NonInheritable, bool Inheritable) { |
| 3061 | if (Attr.isInvalid()) |
| 3062 | return; |
| 3063 | |
| 3064 | if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) |
| 3065 | // FIXME: Try to deal with other __declspec attributes! |
| 3066 | return; |
| 3067 | |
| 3068 | if (NonInheritable) |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3069 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3070 | |
| 3071 | if (Inheritable) |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3072 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3075 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 3076 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3077 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3078 | const AttributeList *AttrList, |
| 3079 | bool NonInheritable, bool Inheritable) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3080 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3081 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | // GCC accepts |
| 3085 | // static int a9 __attribute__((weakref)); |
| 3086 | // but that looks really pointless. We reject it. |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3087 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3088 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 3089 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3090 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3094 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
| 3095 | /// #pragma weak needs a non-definition decl and source may not have one |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3096 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 3097 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3098 | NamedDecl *NewD = 0; |
| 3099 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 3100 | NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3101 | FD->getInnerLocStart(), |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3102 | FD->getLocation(), DeclarationName(II), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3103 | FD->getType(), FD->getTypeSourceInfo()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3104 | if (FD->getQualifier()) { |
| 3105 | FunctionDecl *NewFD = cast<FunctionDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3106 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3107 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3108 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 3109 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3110 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3111 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3112 | VD->getStorageClass(), |
| 3113 | VD->getStorageClassAsWritten()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3114 | if (VD->getQualifier()) { |
| 3115 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3116 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3117 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3118 | } |
| 3119 | return NewD; |
| 3120 | } |
| 3121 | |
| 3122 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak |
| 3123 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 3124 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 3125 | if (W.getUsed()) return; // only do this once |
| 3126 | W.setUsed(true); |
| 3127 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 3128 | IdentifierInfo *NDId = ND->getIdentifier(); |
| 3129 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias()); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3130 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 3131 | NDId->getName())); |
| 3132 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 3133 | WeakTopLevelDecl.push_back(NewD); |
| 3134 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 3135 | // to insert Decl at TU scope, sorry. |
| 3136 | DeclContext *SavedContext = CurContext; |
| 3137 | CurContext = Context.getTranslationUnitDecl(); |
| 3138 | PushOnScopeChains(NewD, S); |
| 3139 | CurContext = SavedContext; |
| 3140 | } else { // just add weak to existing |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3141 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3142 | } |
| 3143 | } |
| 3144 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3145 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 3146 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 3147 | /// specified in many different places, and we need to find and apply them all. |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3148 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 3149 | bool NonInheritable, bool Inheritable) { |
John McCall | 6fe0240 | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 3150 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 3151 | // have to do this. |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3152 | if (Inheritable && !WeakUndeclaredIdentifiers.empty()) { |
John McCall | 6fe0240 | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 3153 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 3154 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 3155 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 3156 | = WeakUndeclaredIdentifiers.find(Id); |
| 3157 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 3158 | WeakInfo W = I->second; |
| 3159 | DeclApplyPragmaWeak(S, ND, W); |
| 3160 | WeakUndeclaredIdentifiers[Id] = W; |
| 3161 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3166 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3167 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3168 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3169 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3170 | // Walk the declarator structure, applying decl attributes that were in a type |
| 3171 | // position to the decl itself. This handles cases like: |
| 3172 | // int *__attr__(x)** D; |
| 3173 | // when X is a decl attribute. |
| 3174 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 3175 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3176 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3177 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3178 | // Finally, apply any attributes on the decl itself. |
| 3179 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3180 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3181 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3182 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3183 | /// Is the given declaration allowed to use a forbidden type? |
| 3184 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 3185 | // Private ivars are always okay. Unfortunately, people don't |
| 3186 | // always properly make their ivars private, even in system headers. |
| 3187 | // Plus we need to make fields okay, too. |
| 3188 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl)) |
| 3189 | return false; |
| 3190 | |
| 3191 | // Require it to be declared in a system header. |
| 3192 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 3193 | } |
| 3194 | |
| 3195 | /// Handle a delayed forbidden-type diagnostic. |
| 3196 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 3197 | Decl *decl) { |
| 3198 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 3199 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 3200 | "this system declaration uses an unsupported type")); |
| 3201 | return; |
| 3202 | } |
| 3203 | |
| 3204 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 3205 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 3206 | diag.Triggered = true; |
| 3207 | } |
| 3208 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3209 | // This duplicates a vector push_back but hides the need to know the |
| 3210 | // size of the type. |
| 3211 | void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) { |
| 3212 | assert(StackSize <= StackCapacity); |
| 3213 | |
| 3214 | // Grow the stack if necessary. |
| 3215 | if (StackSize == StackCapacity) { |
| 3216 | unsigned newCapacity = 2 * StackCapacity + 2; |
| 3217 | char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)]; |
| 3218 | const char *oldBuffer = (const char*) Stack; |
| 3219 | |
| 3220 | if (StackCapacity) |
| 3221 | memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic)); |
| 3222 | |
| 3223 | delete[] oldBuffer; |
| 3224 | Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer); |
| 3225 | StackCapacity = newCapacity; |
| 3226 | } |
| 3227 | |
| 3228 | assert(StackSize < StackCapacity); |
| 3229 | new (&Stack[StackSize++]) DelayedDiagnostic(diag); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3232 | void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state, |
| 3233 | Decl *decl) { |
| 3234 | DelayedDiagnostics &DD = S.DelayedDiagnostics; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3235 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3236 | // Check the invariants. |
| 3237 | assert(DD.StackSize >= state.SavedStackSize); |
| 3238 | assert(state.SavedStackSize >= DD.ActiveStackBase); |
| 3239 | assert(DD.ParsingDepth > 0); |
| 3240 | |
| 3241 | // Drop the parsing depth. |
| 3242 | DD.ParsingDepth--; |
| 3243 | |
| 3244 | // If there are no active diagnostics, we're done. |
| 3245 | if (DD.StackSize == DD.ActiveStackBase) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3246 | return; |
| 3247 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3248 | // We only want to actually emit delayed diagnostics when we |
| 3249 | // successfully parsed a decl. |
Argyrios Kyrtzidis | d8a2771 | 2011-06-24 19:59:27 +0000 | [diff] [blame] | 3250 | if (decl && !decl->isInvalidDecl()) { |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3251 | // We emit all the active diagnostics, not just those starting |
| 3252 | // from the saved state. The idea is this: we get one push for a |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3253 | // decl spec and another for each declarator; in a decl group like: |
| 3254 | // deprecated_typedef foo, *bar, baz(); |
| 3255 | // only the declarator pops will be passed decls. This is correct; |
| 3256 | // we really do need to consider delayed diagnostics from the decl spec |
| 3257 | // for each of the different declarations. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3258 | for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) { |
| 3259 | DelayedDiagnostic &diag = DD.Stack[i]; |
| 3260 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3261 | continue; |
| 3262 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3263 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3264 | case DelayedDiagnostic::Deprecation: |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3265 | S.HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3266 | break; |
| 3267 | |
| 3268 | case DelayedDiagnostic::Access: |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3269 | S.HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3270 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3271 | |
| 3272 | case DelayedDiagnostic::ForbiddenType: |
| 3273 | handleDelayedForbiddenType(S, diag, decl); |
| 3274 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3275 | } |
| 3276 | } |
| 3277 | } |
| 3278 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3279 | // Destroy all the delayed diagnostics we're about to pop off. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3280 | for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i) |
Douglas Gregor | 899b68f | 2011-03-23 15:13:44 +0000 | [diff] [blame] | 3281 | DD.Stack[i].Destroy(); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3282 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3283 | DD.StackSize = state.SavedStackSize; |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | static bool isDeclDeprecated(Decl *D) { |
| 3287 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 3288 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3289 | return true; |
| 3290 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 3291 | return false; |
| 3292 | } |
| 3293 | |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 3294 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3295 | Decl *Ctx) { |
| 3296 | if (isDeclDeprecated(Ctx)) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3297 | return; |
| 3298 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3299 | DD.Triggered = true; |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3300 | if (!DD.getDeprecationMessage().empty()) |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3301 | Diag(DD.Loc, diag::warn_deprecated_message) |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3302 | << DD.getDeprecationDecl()->getDeclName() |
| 3303 | << DD.getDeprecationMessage(); |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3304 | else |
| 3305 | Diag(DD.Loc, diag::warn_deprecated) |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3306 | << DD.getDeprecationDecl()->getDeclName(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3309 | void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message, |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3310 | SourceLocation Loc, |
Fariborz Jahanian | dbbdd2f | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 3311 | const ObjCInterfaceDecl *UnknownObjCClass) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3312 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3313 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 3314 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3315 | return; |
| 3316 | } |
| 3317 | |
| 3318 | // Otherwise, don't warn if our current context is deprecated. |
| 3319 | if (isDeclDeprecated(cast<Decl>(CurContext))) |
| 3320 | return; |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3321 | if (!Message.empty()) |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3322 | Diag(Loc, diag::warn_deprecated_message) << D->getDeclName() |
| 3323 | << Message; |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3324 | else { |
Peter Collingbourne | ed12ffb | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 3325 | if (!UnknownObjCClass) |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3326 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
Fariborz Jahanian | dbbdd2f | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 3327 | else { |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3328 | Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName(); |
Fariborz Jahanian | dbbdd2f | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 3329 | Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 3330 | } |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3331 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3332 | } |