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