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