| 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" |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
| Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/Expr.h" |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
| John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 23 | #include "clang/Sema/DeclSpec.h" |
| John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 24 | #include "clang/Sema/DelayedDiagnostic.h" |
| John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 25 | #include "clang/Sema/Lookup.h" |
| Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 27 | using namespace clang; |
| John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 28 | using namespace sema; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 29 | |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 30 | /// These constants match the enumerated choices of |
| 31 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 32 | enum AttributeDeclKind { |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 33 | ExpectedFunction, |
| 34 | ExpectedUnion, |
| 35 | ExpectedVariableOrFunction, |
| 36 | ExpectedFunctionOrMethod, |
| 37 | ExpectedParameter, |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 38 | ExpectedFunctionMethodOrBlock, |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 39 | ExpectedFunctionMethodOrParameter, |
| 40 | ExpectedClass, |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 41 | ExpectedVariable, |
| 42 | ExpectedMethod, |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 43 | ExpectedVariableFunctionOrLabel, |
| Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 44 | ExpectedFieldOrGlobalVar, |
| 45 | ExpectedStruct |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 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 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 197 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 198 | /// output an error. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 199 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 200 | unsigned int Num) { |
| 201 | if (Attr.getNumArgs() != Num) { |
| 202 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 209 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 210 | /// \brief Check if the attribute has at least as many args as Num. May |
| 211 | /// output an error. |
| 212 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 213 | unsigned int Num) { |
| 214 | if (Attr.getNumArgs() < Num) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 215 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 216 | return false; |
| 217 | } |
| 218 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 219 | return true; |
| 220 | } |
| 221 | |
| 222 | /// |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 223 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 224 | /// \return true if the Decl is a field or potentially shared global variable |
| 225 | /// |
| Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 226 | static bool mayBeSharedVariable(const Decl *D) { |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 227 | if (isa<FieldDecl>(D)) |
| 228 | return true; |
| Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 229 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 230 | return (vd->hasGlobalStorage() && !(vd->isThreadSpecified())); |
| 231 | |
| 232 | return false; |
| 233 | } |
| 234 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 235 | /// \brief Check if the passed-in expression is of type int or bool. |
| 236 | static bool isIntOrBool(Expr *Exp) { |
| 237 | QualType QT = Exp->getType(); |
| 238 | return QT->isBooleanType() || QT->isIntegerType(); |
| 239 | } |
| 240 | |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 241 | /// |
| 242 | /// \brief Check if passed in Decl is a pointer type. |
| 243 | /// Note that this function may produce an error message. |
| 244 | /// \return true if the Decl is a pointer type; false otherwise |
| 245 | /// |
| Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 246 | static bool checkIsPointer(Sema &S, const Decl *D, const AttributeList &Attr) { |
| 247 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 248 | QualType QT = vd->getType(); |
| Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 249 | if (QT->isAnyPointerType()) |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 250 | return true; |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 251 | S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type) |
| 252 | << Attr.getName()->getName() << QT; |
| 253 | } else { |
| 254 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 255 | << Attr.getName(); |
| 256 | } |
| 257 | return false; |
| 258 | } |
| 259 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 260 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 261 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
| Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 262 | static const RecordType *getRecordType(QualType QT) { |
| 263 | if (const RecordType *RT = QT->getAs<RecordType>()) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 264 | return RT; |
| Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 265 | |
| 266 | // Now check if we point to record type. |
| 267 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 268 | return PT->getPointeeType()->getAs<RecordType>(); |
| 269 | |
| 270 | return 0; |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 273 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
| 274 | /// resolves to a lockable object. May flag an error. |
| DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 275 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 276 | QualType Ty) { |
| 277 | const RecordType *RT = getRecordType(Ty); |
| 278 | |
| 279 | // Warn if could not get record type for this argument. |
| Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 280 | if (!RT) { |
| DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 281 | S.Diag(Attr.getLoc(), diag::warn_attribute_argument_not_class) |
| 282 | << Attr.getName() << Ty.getAsString(); |
| 283 | return; |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 284 | } |
| DeLesley Hutchins | 3509f29 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 285 | // Don't check for lockable if the class hasn't been defined yet. |
| 286 | if (RT->isIncompleteType()) |
| DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 287 | return; |
| 288 | // Warn if the type is not lockable. |
| Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 289 | if (!RT->getDecl()->getAttr<LockableAttr>()) { |
| DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 290 | S.Diag(Attr.getLoc(), diag::warn_attribute_argument_not_lockable) |
| 291 | << Attr.getName() << Ty.getAsString(); |
| 292 | return; |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 293 | } |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 296 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
| 297 | /// from Sidx, resolve to a lockable object. May flag an error. |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 298 | /// \param Sidx The attribute argument index to start checking with. |
| 299 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 300 | /// parameter list. |
| 301 | static bool checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
| 302 | const AttributeList &Attr, |
| 303 | SmallVectorImpl<Expr*> &Args, |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 304 | int Sidx = 0, |
| 305 | bool ParamIdxOk = false) { |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 306 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 307 | Expr *ArgExp = Attr.getArg(Idx); |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 308 | |
| Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 309 | if (ArgExp->isTypeDependent()) { |
| 310 | // FIXME -- need to processs this again on template instantiation |
| 311 | Args.push_back(ArgExp); |
| 312 | continue; |
| 313 | } |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 314 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 315 | QualType ArgTy = ArgExp->getType(); |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 316 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 317 | // First see if we can just cast to record type, or point to record type. |
| 318 | const RecordType *RT = getRecordType(ArgTy); |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 319 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 320 | // Now check if we index into a record type function param. |
| 321 | if(!RT && ParamIdxOk) { |
| 322 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 323 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 324 | if(FD && IL) { |
| 325 | unsigned int NumParams = FD->getNumParams(); |
| 326 | llvm::APInt ArgValue = IL->getValue(); |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 327 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 328 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 329 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 330 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 331 | << Attr.getName() << Idx + 1 << NumParams; |
| 332 | return false; |
| 333 | } |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 334 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 338 | checkForLockableRecord(S, D, Attr, ArgTy); |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 339 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 340 | Args.push_back(ArgExp); |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 341 | } |
| 342 | return true; |
| 343 | } |
| 344 | |
| Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 345 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 346 | // Attribute Implementations |
| 347 | //===----------------------------------------------------------------------===// |
| 348 | |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 349 | // FIXME: All this manual attribute parsing code is gross. At the |
| 350 | // least add some helper functions to check most argument patterns (# |
| 351 | // and types of args). |
| 352 | |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 353 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 354 | bool pointer = false) { |
| 355 | assert(!Attr.isInvalid()); |
| 356 | |
| 357 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 358 | return; |
| 359 | |
| 360 | // D must be either a member field or global (potentially shared) variable. |
| 361 | if (!mayBeSharedVariable(D)) { |
| 362 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 363 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | |
| 367 | if (pointer && !checkIsPointer(S, D, Attr)) |
| 368 | return; |
| 369 | |
| 370 | if (pointer) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 371 | D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context)); |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 372 | else |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 373 | D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context)); |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 376 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 377 | bool pointer = false) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 378 | assert(!Attr.isInvalid()); |
| 379 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 380 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 381 | return; |
| 382 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 383 | Expr *Arg = Attr.getArg(0); |
| 384 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 385 | // D must be either a member field or global (potentially shared) variable. |
| 386 | if (!mayBeSharedVariable(D)) { |
| 387 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 388 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 389 | return; |
| 390 | } |
| 391 | |
| 392 | if (pointer && !checkIsPointer(S, D, Attr)) |
| 393 | return; |
| 394 | |
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 395 | if (!Arg->isTypeDependent()) { |
| DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 396 | checkForLockableRecord(S, D, Attr, Arg->getType()); |
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 397 | } |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 398 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 399 | if (pointer) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 400 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 401 | S.Context, Arg)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 402 | else |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 403 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 407 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 408 | bool scoped = false) { |
| 409 | assert(!Attr.isInvalid()); |
| 410 | |
| 411 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 412 | return; |
| 413 | |
| Caitlin Sadowski | 086fb95 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 414 | // FIXME: Lockable structs for C code. |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 415 | if (!isa<CXXRecordDecl>(D)) { |
| 416 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 417 | << Attr.getName() << ExpectedClass; |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | if (scoped) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 422 | D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context)); |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 423 | else |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 424 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void handleNoThreadSafetyAttr(Sema &S, Decl *D, |
| 428 | const AttributeList &Attr) { |
| 429 | assert(!Attr.isInvalid()); |
| 430 | |
| 431 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 432 | return; |
| 433 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 434 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 435 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 436 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 437 | return; |
| 438 | } |
| 439 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 440 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 441 | S.Context)); |
| 442 | } |
| 443 | |
| Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 444 | static void handleNoAddressSafetyAttr(Sema &S, Decl *D, |
| 445 | const AttributeList &Attr) { |
| 446 | assert(!Attr.isInvalid()); |
| 447 | |
| 448 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 449 | return; |
| 450 | |
| 451 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 452 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 453 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(), |
| 458 | S.Context)); |
| 459 | } |
| 460 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 461 | static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 462 | bool before) { |
| 463 | assert(!Attr.isInvalid()); |
| 464 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 465 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 466 | return; |
| 467 | |
| 468 | // D must be either a member field or global (potentially shared) variable. |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 469 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 470 | if (!VD || !mayBeSharedVariable(D)) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 471 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 472 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 476 | // Check that this attribute only applies to lockable types |
| 477 | QualType QT = VD->getType(); |
| 478 | if (!QT->isDependentType()) { |
| 479 | const RecordType *RT = getRecordType(QT); |
| 480 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
| DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 481 | S.Diag(Attr.getLoc(), diag::warn_attribute_decl_not_lockable) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 482 | << Attr.getName(); |
| 483 | return; |
| 484 | } |
| 485 | } |
| 486 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 487 | SmallVector<Expr*, 1> Args; |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 488 | // check that all arguments are lockable objects |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 489 | if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args)) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 490 | return; |
| 491 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 492 | unsigned Size = Args.size(); |
| 493 | assert(Size == Attr.getNumArgs()); |
| 494 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 495 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 496 | if (before) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 497 | D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 498 | StartArg, Size)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 499 | else |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 500 | D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context, |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 501 | StartArg, Size)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 505 | bool exclusive = false) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 506 | assert(!Attr.isInvalid()); |
| 507 | |
| 508 | // zero or more arguments ok |
| 509 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 510 | // check that the attribute is applied to a function |
| 511 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 512 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 513 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 514 | return; |
| 515 | } |
| 516 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 517 | // check that all arguments are lockable objects |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 518 | SmallVector<Expr*, 1> Args; |
| 519 | if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true)) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 520 | return; |
| 521 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 522 | unsigned Size = Args.size(); |
| 523 | assert(Size == Attr.getNumArgs()); |
| 524 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 525 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 526 | if (exclusive) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 527 | D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(), |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 528 | S.Context, StartArg, |
| 529 | Size)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 530 | else |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 531 | D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(), |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 532 | S.Context, StartArg, |
| 533 | Size)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 537 | bool exclusive = false) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 538 | assert(!Attr.isInvalid()); |
| 539 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 540 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 541 | return; |
| 542 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 543 | |
| 544 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 545 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 546 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 547 | return; |
| 548 | } |
| 549 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 550 | if (!isIntOrBool(Attr.getArg(0))) { |
| 551 | S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool) |
| 552 | << Attr.getName(); |
| 553 | return; |
| 554 | } |
| 555 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 556 | SmallVector<Expr*, 2> Args; |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 557 | // check that all arguments are lockable objects |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 558 | if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1)) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 559 | return; |
| 560 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 561 | unsigned Size = Args.size(); |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 562 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 563 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 564 | if (exclusive) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 565 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(), |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 566 | S.Context, |
| Caitlin Sadowski | bf06c72 | 2011-09-15 17:50:19 +0000 | [diff] [blame] | 567 | Attr.getArg(0), |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 568 | StartArg, Size)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 569 | else |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 570 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(), |
| Caitlin Sadowski | bf06c72 | 2011-09-15 17:50:19 +0000 | [diff] [blame] | 571 | S.Context, |
| 572 | Attr.getArg(0), |
| 573 | StartArg, Size)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 577 | bool exclusive = false) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 578 | assert(!Attr.isInvalid()); |
| 579 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 580 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 581 | return; |
| 582 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 583 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 584 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 585 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 586 | return; |
| 587 | } |
| 588 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 589 | // check that all arguments are lockable objects |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 590 | SmallVector<Expr*, 1> Args; |
| 591 | if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args)) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 592 | return; |
| 593 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 594 | unsigned Size = Args.size(); |
| 595 | assert(Size == Attr.getNumArgs()); |
| 596 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 597 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 598 | if (exclusive) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 599 | D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(), |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 600 | S.Context, StartArg, |
| 601 | Size)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 602 | else |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 603 | D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(), |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 604 | S.Context, StartArg, |
| 605 | Size)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 608 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 609 | const AttributeList &Attr) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 610 | assert(!Attr.isInvalid()); |
| 611 | |
| 612 | // zero or more arguments ok |
| 613 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 614 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 615 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 616 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 617 | return; |
| 618 | } |
| 619 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 620 | // check that all arguments are lockable objects |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 621 | SmallVector<Expr*, 1> Args; |
| 622 | if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true)) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 623 | return; |
| 624 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 625 | unsigned Size = Args.size(); |
| 626 | assert(Size == Attr.getNumArgs()); |
| 627 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 628 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 629 | D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context, |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 630 | StartArg, Size)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 634 | const AttributeList &Attr) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 635 | assert(!Attr.isInvalid()); |
| 636 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 637 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 638 | return; |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 639 | Expr *Arg = Attr.getArg(0); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 640 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 641 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 642 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 643 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 644 | return; |
| 645 | } |
| 646 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 647 | if (Arg->isTypeDependent()) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 648 | return; |
| 649 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 650 | // check that the argument is lockable object |
| DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 651 | checkForLockableRecord(S, D, Attr, Arg->getType()); |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 652 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 653 | D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 657 | const AttributeList &Attr) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 658 | assert(!Attr.isInvalid()); |
| 659 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 660 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 661 | return; |
| 662 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 663 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 664 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 665 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 666 | return; |
| 667 | } |
| 668 | |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 669 | // check that all arguments are lockable objects |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 670 | SmallVector<Expr*, 1> Args; |
| 671 | if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args)) |
| Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 672 | return; |
| 673 | |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 674 | unsigned Size = Args.size(); |
| 675 | assert(Size == Attr.getNumArgs()); |
| 676 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 677 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 678 | D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context, |
| Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 679 | StartArg, Size)); |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 683 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 684 | const AttributeList &Attr) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 685 | TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D); |
| Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 686 | if (tDecl == 0) { |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 687 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
| Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 688 | return; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 689 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 690 | |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 691 | QualType curType = tDecl->getUnderlyingType(); |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 692 | |
| 693 | Expr *sizeExpr; |
| 694 | |
| 695 | // Special case where the argument is a template id. |
| 696 | if (Attr.getParameterName()) { |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 697 | CXXScopeSpec SS; |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 698 | SourceLocation TemplateKWLoc; |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 699 | UnqualifiedId id; |
| 700 | id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 701 | |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 702 | ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id, |
| 703 | false, false); |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 704 | if (Size.isInvalid()) |
| 705 | return; |
| 706 | |
| 707 | sizeExpr = Size.get(); |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 708 | } else { |
| 709 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 710 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 711 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 712 | |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 713 | sizeExpr = Attr.getArg(0); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 714 | } |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 715 | |
| 716 | // Instantiate/Install the vector type, and let Sema build the type for us. |
| 717 | // This will run the reguired checks. |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 718 | QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 719 | if (!T.isNull()) { |
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 720 | // FIXME: preserve the old source info. |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 721 | tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 722 | |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 723 | // Remember this typedef decl, we will need it later for diagnostics. |
| 724 | S.ExtVectorDecls.push_back(tDecl); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 725 | } |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 728 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 729 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 730 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 731 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 732 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 733 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 734 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 735 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 736 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 737 | // has no effect. |
| 738 | if (!FD->getType()->isIncompleteType() && |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 739 | S.Context.getTypeAlign(FD->getType()) <= 8) |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 740 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 741 | << Attr.getName() << FD->getType(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 742 | else |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 743 | FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 744 | } else |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 745 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 748 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 749 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 750 | TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context)); |
| Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 751 | else |
| 752 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 753 | } |
| 754 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 755 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
| Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 756 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 757 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 758 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 759 | |
| Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 760 | // The IBAction attributes only apply to instance methods. |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 761 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 762 | if (MD->isInstanceMethod()) { |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 763 | D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context)); |
| Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 764 | return; |
| 765 | } |
| 766 | |
| Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 767 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
| Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 770 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 771 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 772 | // variables or properties of Objective-C classes. The outlet must also |
| 773 | // have an object reference type. |
| 774 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 775 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
| Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 776 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
| Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 777 | << Attr.getName() << VD->getType() << 0; |
| 778 | return false; |
| 779 | } |
| 780 | } |
| 781 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 782 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
| Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 783 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
| Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 784 | << Attr.getName() << PD->getType() << 1; |
| 785 | return false; |
| 786 | } |
| 787 | } |
| 788 | else { |
| 789 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 790 | return false; |
| 791 | } |
| Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 792 | |
| Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 793 | return true; |
| 794 | } |
| 795 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 796 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
| Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 797 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 798 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 799 | return; |
| Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 800 | |
| 801 | if (!checkIBOutletCommon(S, D, Attr)) |
| Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 802 | return; |
| Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 803 | |
| Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 804 | D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context)); |
| Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 807 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 808 | const AttributeList &Attr) { |
| Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 809 | |
| 810 | // The iboutletcollection attribute can have zero or one arguments. |
| Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 811 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
| Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 812 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 813 | return; |
| 814 | } |
| 815 | |
| Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 816 | if (!checkIBOutletCommon(S, D, Attr)) |
| Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 817 | return; |
| Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 818 | |
| Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 819 | IdentifierInfo *II = Attr.getParameterName(); |
| 820 | if (!II) |
| Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 821 | II = &S.Context.Idents.get("NSObject"); |
| Fariborz Jahanian | 798f832 | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 822 | |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 823 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 824 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 825 | if (!TypeRep) { |
| 826 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 827 | return; |
| 828 | } |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 829 | QualType QT = TypeRep.get(); |
| Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 830 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 831 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 832 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 833 | // treated as __attribute__((iboutletcollection())). |
| Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 834 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
| Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 835 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 836 | return; |
| 837 | } |
| Argyrios Kyrtzidis | 8db67df | 2011-09-13 18:41:59 +0000 | [diff] [blame] | 838 | D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 839 | QT, Attr.getParameterLoc())); |
| Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 842 | static void possibleTransparentUnionPointerType(QualType &T) { |
| Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 843 | if (const RecordType *UT = T->getAsUnionType()) |
| 844 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 845 | RecordDecl *UD = UT->getDecl(); |
| 846 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 847 | itend = UD->field_end(); it != itend; ++it) { |
| 848 | QualType QT = it->getType(); |
| 849 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 850 | T = QT; |
| 851 | return; |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 857 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 858 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 859 | // ignore it as well |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 860 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 861 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 862 | << Attr.getName() << ExpectedFunction; |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 863 | return; |
| 864 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 865 | |
| Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 866 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 867 | // counted from one. |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 868 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 869 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 870 | |
| 871 | // The nonnull attribute only applies to pointers. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 872 | SmallVector<unsigned, 10> NonNullArgs; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 873 | |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 874 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 875 | E=Attr.arg_end(); I!=E; ++I) { |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 876 | |
| 877 | |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 878 | // The argument must be an integer constant expression. |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 879 | Expr *Ex = *I; |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 880 | llvm::APSInt ArgNum(32); |
| Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 881 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 882 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 883 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 884 | << "nonnull" << Ex->getSourceRange(); |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 885 | return; |
| 886 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 887 | |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 888 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 889 | |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 890 | if (x < 1 || x > NumArgs) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 891 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| Chris Lattner | 91aea71 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 892 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 893 | return; |
| 894 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 895 | |
| Ted Kremenek | 5224e6a | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 896 | --x; |
| Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 897 | if (HasImplicitThisParam) { |
| 898 | if (x == 0) { |
| 899 | S.Diag(Attr.getLoc(), |
| 900 | diag::err_attribute_invalid_implicit_this_argument) |
| 901 | << "nonnull" << Ex->getSourceRange(); |
| 902 | return; |
| 903 | } |
| 904 | --x; |
| 905 | } |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 906 | |
| 907 | // Is the function argument a pointer type? |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 908 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 909 | possibleTransparentUnionPointerType(T); |
| Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 910 | |
| Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 911 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 912 | // FIXME: Should also highlight argument in decl. |
| Douglas Gregor | 62157e5 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 913 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 914 | << "nonnull" << Ex->getSourceRange(); |
| Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 915 | continue; |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 916 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 917 | |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 918 | NonNullArgs.push_back(x); |
| 919 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 920 | |
| 921 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 922 | // arguments have a nonnull attribute. |
| Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 923 | if (NonNullArgs.empty()) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 924 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) { |
| 925 | QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType(); |
| Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 926 | possibleTransparentUnionPointerType(T); |
| Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 927 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
| Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 928 | NonNullArgs.push_back(I); |
| Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 929 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 930 | |
| Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 931 | // No pointer arguments? |
| Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 932 | if (NonNullArgs.empty()) { |
| 933 | // Warn the trivial case only if attribute is not coming from a |
| 934 | // macro instantiation. |
| 935 | if (Attr.getLoc().isFileID()) |
| 936 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
| Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 937 | return; |
| Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 938 | } |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 939 | } |
| Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 940 | |
| 941 | unsigned* start = &NonNullArgs[0]; |
| 942 | unsigned size = NonNullArgs.size(); |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 943 | llvm::array_pod_sort(start, start + size); |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 944 | D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start, |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 945 | size)); |
| Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 948 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 949 | // This attribute must be applied to a function declaration. |
| 950 | // The first argument to the attribute must be a string, |
| 951 | // the name of the resource, for example "malloc". |
| 952 | // The following arguments must be argument indexes, the arguments must be |
| 953 | // of integer type for Returns, otherwise of pointer type. |
| 954 | // 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] | 955 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 956 | // a list append function may well be __attribute((ownership_holds)). |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 957 | |
| 958 | if (!AL.getParameterName()) { |
| 959 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 960 | << AL.getName()->getName() << 1; |
| 961 | return; |
| 962 | } |
| 963 | // Figure out our Kind, and check arguments while we're at it. |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 964 | OwnershipAttr::OwnershipKind K; |
| Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 965 | switch (AL.getKind()) { |
| 966 | case AttributeList::AT_ownership_takes: |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 967 | K = OwnershipAttr::Takes; |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 968 | if (AL.getNumArgs() < 1) { |
| 969 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 970 | return; |
| 971 | } |
| Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 972 | break; |
| 973 | case AttributeList::AT_ownership_holds: |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 974 | K = OwnershipAttr::Holds; |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 975 | if (AL.getNumArgs() < 1) { |
| 976 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 977 | return; |
| 978 | } |
| Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 979 | break; |
| 980 | case AttributeList::AT_ownership_returns: |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 981 | K = OwnershipAttr::Returns; |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 982 | if (AL.getNumArgs() > 1) { |
| 983 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 984 | << AL.getNumArgs() + 1; |
| 985 | return; |
| 986 | } |
| Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 987 | break; |
| 988 | default: |
| 989 | // This should never happen given how we are called. |
| 990 | llvm_unreachable("Unknown ownership attribute"); |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 993 | if (!isFunction(D) || !hasFunctionProto(D)) { |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 994 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 995 | << AL.getName() << ExpectedFunction; |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 996 | return; |
| 997 | } |
| 998 | |
| Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 999 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1000 | // counted from one. |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1001 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1002 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1003 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1004 | StringRef Module = AL.getParameterName()->getName(); |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Normalize the argument, __foo__ becomes foo. |
| 1007 | if (Module.startswith("__") && Module.endswith("__")) |
| 1008 | Module = Module.substr(2, Module.size() - 4); |
| 1009 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1010 | SmallVector<unsigned, 10> OwnershipArgs; |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1011 | |
| Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1012 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 1013 | ++I) { |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1014 | |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1015 | Expr *IdxExpr = *I; |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1016 | llvm::APSInt ArgNum(32); |
| 1017 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1018 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1019 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 1020 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 1021 | continue; |
| 1022 | } |
| 1023 | |
| 1024 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 1025 | |
| 1026 | if (x > NumArgs || x < 1) { |
| 1027 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1028 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 1029 | continue; |
| 1030 | } |
| 1031 | --x; |
| Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1032 | if (HasImplicitThisParam) { |
| 1033 | if (x == 0) { |
| 1034 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1035 | << "ownership" << IdxExpr->getSourceRange(); |
| 1036 | return; |
| 1037 | } |
| 1038 | --x; |
| 1039 | } |
| 1040 | |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1041 | switch (K) { |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1042 | case OwnershipAttr::Takes: |
| 1043 | case OwnershipAttr::Holds: { |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1044 | // Is the function argument a pointer type? |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1045 | QualType T = getFunctionOrMethodArgType(D, x); |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1046 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1047 | // FIXME: Should also highlight argument in decl. |
| 1048 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1049 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1050 | << "pointer" |
| 1051 | << IdxExpr->getSourceRange(); |
| 1052 | continue; |
| 1053 | } |
| 1054 | break; |
| 1055 | } |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1056 | case OwnershipAttr::Returns: { |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1057 | if (AL.getNumArgs() > 1) { |
| 1058 | // Is the function argument an integer type? |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1059 | Expr *IdxExpr = AL.getArg(0); |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1060 | llvm::APSInt ArgNum(32); |
| 1061 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1062 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1063 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1064 | << "ownership_returns" << "integer" |
| 1065 | << IdxExpr->getSourceRange(); |
| 1066 | return; |
| 1067 | } |
| 1068 | } |
| 1069 | break; |
| 1070 | } |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1071 | } // switch |
| 1072 | |
| 1073 | // Check we don't have a conflict with another ownership attribute. |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1074 | for (specific_attr_iterator<OwnershipAttr> |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1075 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1076 | e = D->specific_attr_end<OwnershipAttr>(); |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1077 | i != e; ++i) { |
| 1078 | if ((*i)->getOwnKind() != K) { |
| 1079 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1080 | I!=E; ++I) { |
| 1081 | if (x == *I) { |
| 1082 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1083 | << AL.getName()->getName() << "ownership_*"; |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | OwnershipArgs.push_back(x); |
| 1089 | } |
| 1090 | |
| 1091 | unsigned* start = OwnershipArgs.data(); |
| 1092 | unsigned size = OwnershipArgs.size(); |
| 1093 | llvm::array_pod_sort(start, start + size); |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 1096 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1097 | return; |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1098 | } |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1099 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1100 | D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1101 | start, size)); |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1104 | /// Whether this declaration has internal linkage for the purposes of |
| 1105 | /// things that want to complain about things not have internal linkage. |
| 1106 | static bool hasEffectivelyInternalLinkage(NamedDecl *D) { |
| 1107 | switch (D->getLinkage()) { |
| 1108 | case NoLinkage: |
| 1109 | case InternalLinkage: |
| 1110 | return true; |
| 1111 | |
| 1112 | // Template instantiations that go from external to unique-external |
| 1113 | // shouldn't get diagnosed. |
| 1114 | case UniqueExternalLinkage: |
| 1115 | return true; |
| 1116 | |
| 1117 | case ExternalLinkage: |
| 1118 | return false; |
| 1119 | } |
| 1120 | llvm_unreachable("unknown linkage kind!"); |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1123 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1124 | // Check the attribute arguments. |
| 1125 | if (Attr.getNumArgs() > 1) { |
| 1126 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1127 | return; |
| 1128 | } |
| 1129 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1130 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
| John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1131 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1132 | << Attr.getName() << ExpectedVariableOrFunction; |
| John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1136 | NamedDecl *nd = cast<NamedDecl>(D); |
| John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1137 | |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1138 | // gcc rejects |
| 1139 | // class c { |
| 1140 | // static int a __attribute__((weakref ("v2"))); |
| 1141 | // static int b() __attribute__((weakref ("f3"))); |
| 1142 | // }; |
| 1143 | // and ignores the attributes of |
| 1144 | // void f(void) { |
| 1145 | // static int a __attribute__((weakref ("v2"))); |
| 1146 | // } |
| 1147 | // we reject them |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1148 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1149 | if (!Ctx->isFileContext()) { |
| 1150 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
| John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1151 | nd->getNameAsString(); |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1152 | return; |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | // The GCC manual says |
| 1156 | // |
| 1157 | // At present, a declaration to which `weakref' is attached can only |
| 1158 | // be `static'. |
| 1159 | // |
| 1160 | // It also says |
| 1161 | // |
| 1162 | // Without a TARGET, |
| 1163 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1164 | // equivalent to `weak'. |
| 1165 | // |
| 1166 | // gcc 4.4.1 will accept |
| 1167 | // int a7 __attribute__((weakref)); |
| 1168 | // as |
| 1169 | // int a7 __attribute__((weak)); |
| 1170 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1171 | // it if this behaviour is actually used. |
| 1172 | |
| John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1173 | if (!hasEffectivelyInternalLinkage(nd)) { |
| 1174 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | // GCC rejects |
| 1179 | // static ((alias ("y"), weakref)). |
| 1180 | // Should we? How to check that weakref is before or after alias? |
| 1181 | |
| 1182 | if (Attr.getNumArgs() == 1) { |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1183 | Expr *Arg = Attr.getArg(0); |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1184 | Arg = Arg->IgnoreParenCasts(); |
| 1185 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1186 | |
| Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1187 | if (!Str || !Str->isAscii()) { |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1188 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1189 | << "weakref" << 1; |
| 1190 | return; |
| 1191 | } |
| 1192 | // GCC will accept anything as the argument of weakref. Should we |
| 1193 | // check for an existing decl? |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1194 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1195 | Str->getString())); |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1198 | D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context)); |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1201 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1202 | // check the attribute arguments. |
| Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1203 | if (Attr.getNumArgs() != 1) { |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1204 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1205 | return; |
| 1206 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1207 | |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1208 | Expr *Arg = Attr.getArg(0); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1209 | Arg = Arg->IgnoreParenCasts(); |
| 1210 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1211 | |
| Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1212 | if (!Str || !Str->isAscii()) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1213 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1214 | << "alias" << 1; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1215 | return; |
| 1216 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1217 | |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1218 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
| Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1219 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1220 | return; |
| 1221 | } |
| 1222 | |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1223 | // FIXME: check if target symbol exists in current file |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1224 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1225 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1226 | Str->getString())); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1229 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1230 | // Check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1231 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1232 | return; |
| Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1233 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1234 | if (!isa<FunctionDecl>(D)) { |
| Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1235 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1236 | << Attr.getName() << ExpectedFunction; |
| Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1237 | return; |
| 1238 | } |
| 1239 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1240 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context)); |
| Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1243 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1244 | const AttributeList &Attr) { |
| Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1245 | // Check the attribute arguments. |
| Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1246 | if (Attr.hasParameterOrArguments()) { |
| Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1247 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1248 | return; |
| 1249 | } |
| 1250 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1251 | if (!isa<FunctionDecl>(D)) { |
| Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1252 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1253 | << Attr.getName() << ExpectedFunction; |
| Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1254 | return; |
| 1255 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1256 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1257 | D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context)); |
| Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1260 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1261 | // Check the attribute arguments. |
| Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1262 | if (Attr.hasParameterOrArguments()) { |
| Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1263 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1264 | return; |
| 1265 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1267 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | QualType RetTy = FD->getResultType(); |
| Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1269 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1270 | D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context)); |
| Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1271 | return; |
| 1272 | } |
| Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1275 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
| Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1278 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1279 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1280 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1281 | return; |
| Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1282 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1283 | D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context)); |
| Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1286 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chandler Carruth | 9312c64 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1287 | assert(!Attr.isInvalid()); |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1288 | if (isa<VarDecl>(D)) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1289 | D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context)); |
| Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1290 | else |
| 1291 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1292 | << Attr.getName() << ExpectedVariable; |
| Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1295 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chandler Carruth | 9312c64 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1296 | assert(!Attr.isInvalid()); |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1297 | if (isa<VarDecl>(D)) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1298 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context)); |
| Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1299 | else |
| 1300 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1301 | << Attr.getName() << ExpectedVariable; |
| Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1304 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1305 | if (hasDeclarator(D)) return; |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1306 | |
| 1307 | if (S.CheckNoReturnAttr(attr)) return; |
| 1308 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1309 | if (!isa<ObjCMethodDecl>(D)) { |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1310 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1311 | << attr.getName() << ExpectedFunctionOrMethod; |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1312 | return; |
| 1313 | } |
| 1314 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1315 | D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context)); |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
| Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1319 | if (attr.hasParameterOrArguments()) { |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1320 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1321 | attr.setInvalid(); |
| 1322 | return true; |
| 1323 | } |
| 1324 | |
| 1325 | return false; |
| Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1328 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1329 | const AttributeList &Attr) { |
| Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1330 | |
| 1331 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1332 | // because 'analyzer_noreturn' does not impact the type. |
| 1333 | |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1334 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1335 | return; |
| Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1336 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1337 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1338 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1339 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1340 | && !VD->getType()->isFunctionPointerType())) { |
| 1341 | S.Diag(Attr.getLoc(), |
| 1342 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 1343 | : diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1344 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
| Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1345 | return; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1349 | D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context)); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1352 | // PS3 PPU-specific. |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1353 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1354 | /* |
| 1355 | Returning a Vector Class in Registers |
| 1356 | |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1357 | According to the PPU ABI specifications, a class with a single member of |
| 1358 | vector type is returned in memory when used as the return value of a function. |
| 1359 | This results in inefficient code when implementing vector classes. To return |
| 1360 | the value in a single vector register, add the vecreturn attribute to the |
| 1361 | class definition. This attribute is also applicable to struct types. |
| John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1362 | |
| 1363 | Example: |
| 1364 | |
| 1365 | struct Vector |
| 1366 | { |
| 1367 | __vector float xyzw; |
| 1368 | } __attribute__((vecreturn)); |
| 1369 | |
| 1370 | Vector Add(Vector lhs, Vector rhs) |
| 1371 | { |
| 1372 | Vector result; |
| 1373 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1374 | return result; // This will be returned in a register |
| 1375 | } |
| 1376 | */ |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1377 | if (!isa<RecordDecl>(D)) { |
| John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1378 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1379 | << Attr.getName() << ExpectedClass; |
| John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1380 | return; |
| 1381 | } |
| 1382 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1383 | if (D->getAttr<VecReturnAttr>()) { |
| John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1384 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1385 | return; |
| 1386 | } |
| 1387 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1388 | RecordDecl *record = cast<RecordDecl>(D); |
| John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1389 | int count = 0; |
| 1390 | |
| 1391 | if (!isa<CXXRecordDecl>(record)) { |
| 1392 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1393 | return; |
| 1394 | } |
| 1395 | |
| 1396 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1397 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1398 | return; |
| 1399 | } |
| 1400 | |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1401 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1402 | iter != record->field_end(); iter++) { |
| John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1403 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1404 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1405 | return; |
| 1406 | } |
| 1407 | count++; |
| 1408 | } |
| 1409 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1410 | D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context)); |
| John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1413 | static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1414 | if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) { |
| Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1415 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1416 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
| Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1417 | return; |
| 1418 | } |
| 1419 | // FIXME: Actually store the attribute on the declaration |
| 1420 | } |
| 1421 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1422 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1423 | // check the attribute arguments. |
| Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1424 | if (Attr.hasParameterOrArguments()) { |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1425 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1426 | return; |
| 1427 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1428 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1429 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
| 1430 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1431 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1432 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
| Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1433 | return; |
| 1434 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1435 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1436 | D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context)); |
| Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1439 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1440 | const AttributeList &Attr) { |
| 1441 | // check the attribute arguments. |
| 1442 | if (Attr.hasParameterOrArguments()) { |
| 1443 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1444 | return; |
| 1445 | } |
| 1446 | |
| 1447 | if (!isa<FunctionDecl>(D)) { |
| 1448 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1449 | << Attr.getName() << ExpectedFunction; |
| 1450 | return; |
| 1451 | } |
| 1452 | |
| 1453 | D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context)); |
| 1454 | } |
| 1455 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1456 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1457 | // check the attribute arguments. |
| Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1458 | if (Attr.hasParameterOrArguments()) { |
| Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1459 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1460 | return; |
| 1461 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1462 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1463 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| Daniel Dunbar | 311bf29 | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1464 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
| Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1465 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1466 | return; |
| 1467 | } |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1468 | } else if (!isFunctionOrMethod(D)) { |
| Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1469 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1470 | << Attr.getName() << ExpectedVariableOrFunction; |
| Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1471 | return; |
| 1472 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1473 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1474 | D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context)); |
| Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1477 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1478 | // check the attribute arguments. |
| John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1479 | if (Attr.getNumArgs() > 1) { |
| 1480 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1481 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1482 | } |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1483 | |
| 1484 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1485 | if (Attr.getNumArgs() > 0) { |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1486 | Expr *E = Attr.getArg(0); |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1487 | llvm::APSInt Idx(32); |
| Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1488 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1489 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1490 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1491 | << "constructor" << 1 << E->getSourceRange(); |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1492 | return; |
| 1493 | } |
| 1494 | priority = Idx.getZExtValue(); |
| 1495 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1496 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1497 | if (!isa<FunctionDecl>(D)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1498 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1499 | << Attr.getName() << ExpectedFunction; |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1500 | return; |
| 1501 | } |
| 1502 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1503 | D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context, |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1504 | priority)); |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1507 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1508 | // check the attribute arguments. |
| John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1509 | if (Attr.getNumArgs() > 1) { |
| 1510 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1511 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1512 | } |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1513 | |
| 1514 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1515 | if (Attr.getNumArgs() > 0) { |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1516 | Expr *E = Attr.getArg(0); |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1517 | llvm::APSInt Idx(32); |
| Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1518 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1519 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1520 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1521 | << "destructor" << 1 << E->getSourceRange(); |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1522 | return; |
| 1523 | } |
| 1524 | priority = Idx.getZExtValue(); |
| 1525 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1526 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1527 | if (!isa<FunctionDecl>(D)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1528 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1529 | << Attr.getName() << ExpectedFunction; |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1530 | return; |
| 1531 | } |
| 1532 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1533 | D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context, |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1534 | priority)); |
| Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1537 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1538 | unsigned NumArgs = Attr.getNumArgs(); |
| 1539 | if (NumArgs > 1) { |
| John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1540 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1541 | return; |
| 1542 | } |
| Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1543 | |
| Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1544 | // Handle the case where deprecated attribute has a text message. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1545 | StringRef Str; |
| Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1546 | if (NumArgs == 1) { |
| 1547 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
| Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1548 | if (!SE) { |
| Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1549 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
| 1550 | << "deprecated"; |
| Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1551 | return; |
| 1552 | } |
| Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1553 | Str = SE->getString(); |
| Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1554 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1555 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1556 | D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str)); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1559 | static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1560 | unsigned NumArgs = Attr.getNumArgs(); |
| 1561 | if (NumArgs > 1) { |
| John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1562 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
| Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1563 | return; |
| 1564 | } |
| Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1565 | |
| Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1566 | // Handle the case where unavailable attribute has a text message. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1567 | StringRef Str; |
| Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1568 | if (NumArgs == 1) { |
| 1569 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
| Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1570 | if (!SE) { |
| Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1571 | S.Diag(Attr.getArg(0)->getLocStart(), |
| Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1572 | diag::err_attribute_not_string) << "unavailable"; |
| 1573 | return; |
| 1574 | } |
| Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1575 | Str = SE->getString(); |
| Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1576 | } |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1577 | D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str)); |
| Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1580 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1581 | const AttributeList &Attr) { |
| 1582 | unsigned NumArgs = Attr.getNumArgs(); |
| 1583 | if (NumArgs > 0) { |
| 1584 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr( |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1589 | Attr.getRange(), S.Context)); |
| Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1592 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 1593 | const AttributeList &Attr) { |
| 1594 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1595 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1596 | return; |
| 1597 | } |
| 1598 | |
| 1599 | unsigned NumArgs = Attr.getNumArgs(); |
| 1600 | if (NumArgs > 0) { |
| 1601 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1602 | return; |
| 1603 | } |
| 1604 | |
| 1605 | D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context)); |
| 1606 | } |
| 1607 | |
| Ted Kremenek | 0c2c90b | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1608 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1609 | const AttributeList &Attr) { |
| Fariborz Jahanian | 7249e36 | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 1610 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1611 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 1612 | return; |
| 1613 | } |
| 1614 | |
| Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1615 | unsigned NumArgs = Attr.getNumArgs(); |
| 1616 | if (NumArgs > 0) { |
| 1617 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1618 | return; |
| 1619 | } |
| 1620 | |
| Ted Kremenek | 0c2c90b | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1621 | D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr( |
| Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1622 | Attr.getRange(), S.Context)); |
| 1623 | } |
| 1624 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1625 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1626 | const AttributeList &Attr) { |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1627 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 1628 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
| 1629 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1630 | StringRef PlatformName |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1631 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1632 | if (PlatformName.empty()) { |
| 1633 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 1634 | << Platform; |
| 1635 | |
| 1636 | PlatformName = Platform->getName(); |
| 1637 | } |
| 1638 | |
| 1639 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1640 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1641 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
| Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1642 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1643 | |
| Douglas Gregor | 05a51ae | 2011-08-10 16:09:55 +0000 | [diff] [blame] | 1644 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1645 | // of these steps are needed). |
| 1646 | if (Introduced.isValid() && Deprecated.isValid() && |
| Douglas Gregor | 6f47e5c | 2011-08-10 15:31:35 +0000 | [diff] [blame] | 1647 | !(Introduced.Version <= Deprecated.Version)) { |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1648 | S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) |
| 1649 | << 1 << PlatformName << Deprecated.Version.getAsString() |
| 1650 | << 0 << Introduced.Version.getAsString(); |
| 1651 | return; |
| 1652 | } |
| 1653 | |
| 1654 | if (Introduced.isValid() && Obsoleted.isValid() && |
| Douglas Gregor | 6f47e5c | 2011-08-10 15:31:35 +0000 | [diff] [blame] | 1655 | !(Introduced.Version <= Obsoleted.Version)) { |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1656 | S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) |
| 1657 | << 2 << PlatformName << Obsoleted.Version.getAsString() |
| 1658 | << 0 << Introduced.Version.getAsString(); |
| 1659 | return; |
| 1660 | } |
| 1661 | |
| 1662 | if (Deprecated.isValid() && Obsoleted.isValid() && |
| Douglas Gregor | 6f47e5c | 2011-08-10 15:31:35 +0000 | [diff] [blame] | 1663 | !(Deprecated.Version <= Obsoleted.Version)) { |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1664 | S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering) |
| 1665 | << 2 << PlatformName << Obsoleted.Version.getAsString() |
| 1666 | << 1 << Deprecated.Version.getAsString(); |
| 1667 | return; |
| 1668 | } |
| 1669 | |
| Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1670 | StringRef Str; |
| 1671 | const StringLiteral *SE = |
| 1672 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 1673 | if (SE) |
| 1674 | Str = SE->getString(); |
| 1675 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1676 | D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context, |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1677 | Platform, |
| 1678 | Introduced.Version, |
| 1679 | Deprecated.Version, |
| Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1680 | Obsoleted.Version, |
| Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1681 | IsUnavailable, |
| 1682 | Str)); |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1685 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1686 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1687 | if(!checkAttributeNumArgs(S, Attr, 1)) |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1688 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1689 | |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1690 | Expr *Arg = Attr.getArg(0); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1691 | Arg = Arg->IgnoreParenCasts(); |
| 1692 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1693 | |
| Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1694 | if (!Str || !Str->isAscii()) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1695 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1696 | << "visibility" << 1; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1697 | return; |
| 1698 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1699 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1700 | StringRef TypeStr = Str->getString(); |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1701 | VisibilityAttr::VisibilityType type; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1702 | |
| Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1703 | if (TypeStr == "default") |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1704 | type = VisibilityAttr::Default; |
| Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1705 | else if (TypeStr == "hidden") |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1706 | type = VisibilityAttr::Hidden; |
| Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1707 | else if (TypeStr == "internal") |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1708 | type = VisibilityAttr::Hidden; // FIXME |
| John McCall | eed64c7 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 1709 | else if (TypeStr == "protected") { |
| 1710 | // Complain about attempts to use protected visibility on targets |
| 1711 | // (like Darwin) that don't support it. |
| 1712 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 1713 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 1714 | type = VisibilityAttr::Default; |
| 1715 | } else { |
| 1716 | type = VisibilityAttr::Protected; |
| 1717 | } |
| 1718 | } else { |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1719 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1720 | return; |
| 1721 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1722 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1723 | D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type)); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1726 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 1727 | const AttributeList &Attr) { |
| John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1728 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 1729 | if (!method) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1730 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1731 | << ExpectedMethod; |
| John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1732 | return; |
| 1733 | } |
| 1734 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1735 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 1736 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 1737 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1738 | << "objc_method_family" << 1; |
| 1739 | } else { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1740 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1741 | } |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1742 | Attr.setInvalid(); |
| John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1743 | return; |
| 1744 | } |
| 1745 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1746 | StringRef param = Attr.getParameterName()->getName(); |
| John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1747 | ObjCMethodFamilyAttr::FamilyKind family; |
| 1748 | if (param == "none") |
| 1749 | family = ObjCMethodFamilyAttr::OMF_None; |
| 1750 | else if (param == "alloc") |
| 1751 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 1752 | else if (param == "copy") |
| 1753 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 1754 | else if (param == "init") |
| 1755 | family = ObjCMethodFamilyAttr::OMF_init; |
| 1756 | else if (param == "mutableCopy") |
| 1757 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 1758 | else if (param == "new") |
| 1759 | family = ObjCMethodFamilyAttr::OMF_new; |
| 1760 | else { |
| 1761 | // Just warn and ignore it. This is future-proof against new |
| 1762 | // families being used in system headers. |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1763 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
| John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1764 | return; |
| 1765 | } |
| 1766 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1767 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 1768 | !method->getResultType()->isObjCObjectPointerType()) { |
| 1769 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 1770 | << method->getResultType(); |
| 1771 | // Ignore the attribute. |
| 1772 | return; |
| 1773 | } |
| 1774 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1775 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1776 | S.Context, family)); |
| John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1779 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 1780 | const AttributeList &Attr) { |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1781 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1782 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1783 | |
| Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1784 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 1785 | if (OCI == 0) { |
| 1786 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1787 | return; |
| 1788 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1789 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1790 | D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context)); |
| Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1793 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
| Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1794 | if (Attr.getNumArgs() != 0) { |
| John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1795 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1796 | return; |
| 1797 | } |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1798 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1799 | QualType T = TD->getUnderlyingType(); |
| 1800 | if (!T->isPointerType() || |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1801 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
| Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1802 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 1803 | return; |
| 1804 | } |
| 1805 | } |
| Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 1806 | else if (!isa<ObjCPropertyDecl>(D)) { |
| 1807 | // It is okay to include this attribute on properties, e.g.: |
| 1808 | // |
| 1809 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 1810 | // |
| 1811 | // In this case it follows tradition and suppresses an error in the above |
| 1812 | // case. |
| Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 1813 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
| Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 1814 | } |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1815 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context)); |
| Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1818 | static void |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1819 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1820 | if (Attr.getNumArgs() != 0) { |
| John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1821 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1822 | return; |
| 1823 | } |
| 1824 | |
| 1825 | if (!isa<FunctionDecl>(D)) { |
| 1826 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 1827 | return; |
| 1828 | } |
| 1829 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1830 | D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context)); |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1833 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1834 | if (!Attr.getParameterName()) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1835 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1836 | << "blocks" << 1; |
| Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1837 | return; |
| 1838 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1839 | |
| Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1840 | if (Attr.getNumArgs() != 0) { |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1841 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1842 | return; |
| 1843 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1844 | |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1845 | BlocksAttr::BlockType type; |
| Chris Lattner | 68e4868 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 1846 | if (Attr.getParameterName()->isStr("byref")) |
| Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1847 | type = BlocksAttr::ByRef; |
| 1848 | else { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1849 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1850 | << "blocks" << Attr.getParameterName(); |
| Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1851 | return; |
| 1852 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1853 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1854 | D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type)); |
| Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1857 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1858 | // check the attribute arguments. |
| 1859 | if (Attr.getNumArgs() > 2) { |
| John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1860 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1861 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
| John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 1864 | unsigned sentinel = 0; |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1865 | if (Attr.getNumArgs() > 0) { |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1866 | Expr *E = Attr.getArg(0); |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1867 | llvm::APSInt Idx(32); |
| Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1868 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1869 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1870 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1871 | << "sentinel" << 1 << E->getSourceRange(); |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1872 | return; |
| 1873 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1874 | |
| John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 1875 | if (Idx.isSigned() && Idx.isNegative()) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1876 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 1877 | << E->getSourceRange(); |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1878 | return; |
| 1879 | } |
| John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 1880 | |
| 1881 | sentinel = Idx.getZExtValue(); |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
| John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 1884 | unsigned nullPos = 0; |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1885 | if (Attr.getNumArgs() > 1) { |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1886 | Expr *E = Attr.getArg(1); |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1887 | llvm::APSInt Idx(32); |
| Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1888 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1889 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1890 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1891 | << "sentinel" << 2 << E->getSourceRange(); |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1892 | return; |
| 1893 | } |
| 1894 | nullPos = Idx.getZExtValue(); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1895 | |
| John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 1896 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1897 | // FIXME: This error message could be improved, it would be nice |
| 1898 | // to say what the bounds actually are. |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1899 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 1900 | << E->getSourceRange(); |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1901 | return; |
| 1902 | } |
| 1903 | } |
| 1904 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1905 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 1906 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
| Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1907 | if (isa<FunctionNoProtoType>(FT)) { |
| 1908 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 1909 | return; |
| 1910 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1911 | |
| Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1912 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
| Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1913 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1914 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1915 | } |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1916 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1917 | if (!MD->isVariadic()) { |
| Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1918 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1919 | return; |
| Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1920 | } |
| Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 1921 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 1922 | if (!BD->isVariadic()) { |
| 1923 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 1924 | return; |
| 1925 | } |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1926 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
| Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1927 | QualType Ty = V->getType(); |
| Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 1928 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1929 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1930 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
| Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1931 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
| Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1932 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 1933 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
| Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1934 | return; |
| 1935 | } |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1936 | } else { |
| Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1937 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1938 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
| Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1939 | return; |
| 1940 | } |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1941 | } else { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1942 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1943 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1944 | return; |
| 1945 | } |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1946 | D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel, |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1947 | nullPos)); |
| Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1950 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1951 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1952 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1953 | return; |
| Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1954 | |
| Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1955 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { |
| Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1956 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1957 | << Attr.getName() << ExpectedFunctionOrMethod; |
| Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1958 | return; |
| 1959 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1960 | |
| Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1961 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 1962 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1963 | << Attr.getName() << 0; |
| Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 1964 | return; |
| 1965 | } |
| Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1966 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 1967 | if (MD->getResultType()->isVoidType()) { |
| 1968 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1969 | << Attr.getName() << 1; |
| 1970 | return; |
| 1971 | } |
| 1972 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1973 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context)); |
| Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1976 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1977 | // check the attribute arguments. |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1978 | if (Attr.hasParameterOrArguments()) { |
| 1979 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1980 | return; |
| 1981 | } |
| Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1982 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1983 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
| Fariborz Jahanian | 47f9a73 | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 1984 | if (isa<CXXRecordDecl>(D)) { |
| 1985 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 1986 | return; |
| 1987 | } |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1988 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1989 | << Attr.getName() << ExpectedVariableOrFunction; |
| Fariborz Jahanian | 41136ee | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 1990 | return; |
| 1991 | } |
| 1992 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1993 | NamedDecl *nd = cast<NamedDecl>(D); |
| John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1994 | |
| 1995 | // 'weak' only applies to declarations with external linkage. |
| 1996 | if (hasEffectivelyInternalLinkage(nd)) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1997 | S.Diag(Attr.getLoc(), diag::err_attribute_weak_static); |
| Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1998 | return; |
| 1999 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2000 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2001 | nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2004 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2005 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2006 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2007 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2008 | |
| Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2009 | |
| 2010 | // weak_import only applies to variable & function declarations. |
| 2011 | bool isDef = false; |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2012 | if (!D->canBeWeakImported(isDef)) { |
| 2013 | if (isDef) |
| 2014 | S.Diag(Attr.getLoc(), |
| 2015 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 2016 | << "weak_import" << 2 /*variable and function*/; |
| Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2017 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2018 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
| Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2019 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
| Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2020 | // Nothing to warn about here. |
| 2021 | } else |
| Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2022 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2023 | << Attr.getName() << ExpectedVariableOrFunction; |
| Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2024 | |
| Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2025 | return; |
| 2026 | } |
| 2027 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2028 | D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context)); |
| Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2031 | static void handleReqdWorkGroupSize(Sema &S, Decl *D, |
| 2032 | const AttributeList &Attr) { |
| Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2033 | // Attribute has 3 arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2034 | if (!checkAttributeNumArgs(S, Attr, 3)) |
| Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2035 | return; |
| Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2036 | |
| 2037 | unsigned WGSize[3]; |
| 2038 | for (unsigned i = 0; i < 3; ++i) { |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2039 | Expr *E = Attr.getArg(i); |
| Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2040 | llvm::APSInt ArgNum(32); |
| Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2041 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2042 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
| Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2043 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2044 | << "reqd_work_group_size" << E->getSourceRange(); |
| 2045 | return; |
| 2046 | } |
| 2047 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2048 | } |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2049 | D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2050 | WGSize[0], WGSize[1], |
| Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2051 | WGSize[2])); |
| 2052 | } |
| 2053 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2054 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2055 | // Attribute has no arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2056 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2057 | return; |
| Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2058 | |
| 2059 | // Make sure that there is a string literal as the sections's single |
| 2060 | // argument. |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2061 | Expr *ArgExpr = Attr.getArg(0); |
| Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2062 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
| Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2063 | if (!SE) { |
| Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2064 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
| Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2065 | return; |
| 2066 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
| Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2068 | // If the target wants to validate the section specifier, make it happen. |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2069 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
| Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2070 | if (!Error.empty()) { |
| 2071 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2072 | << Error; |
| Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2073 | return; |
| 2074 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | |
| Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2076 | // This attribute cannot be applied to local variables. |
| 2077 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2078 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2079 | return; |
| 2080 | } |
| 2081 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2082 | D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context, |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2083 | SE->getString())); |
| Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2086 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2087 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2088 | // check the attribute arguments. |
| Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2089 | if (Attr.hasParameterOrArguments()) { |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2090 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2091 | return; |
| 2092 | } |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2093 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2094 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2095 | if (Existing->getLocation().isInvalid()) |
| Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2096 | Existing->setRange(Attr.getRange()); |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2097 | } else { |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2098 | D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context)); |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2099 | } |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2102 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2103 | // check the attribute arguments. |
| Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2104 | if (Attr.hasParameterOrArguments()) { |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2105 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2106 | return; |
| 2107 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2108 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2109 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2110 | if (Existing->getLocation().isInvalid()) |
| Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2111 | Existing->setRange(Attr.getRange()); |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2112 | } else { |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2113 | D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context)); |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2114 | } |
| Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2117 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2118 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2119 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2120 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2121 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2122 | D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context)); |
| Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2125 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2126 | if (!Attr.getParameterName()) { |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2127 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2128 | return; |
| 2129 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2130 | |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2131 | if (Attr.getNumArgs() != 0) { |
| 2132 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2133 | return; |
| 2134 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2135 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2136 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2137 | |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2138 | if (!VD || !VD->hasLocalStorage()) { |
| 2139 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2140 | return; |
| 2141 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2142 | |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2143 | // Look up the function |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2144 | // FIXME: Lookup probably isn't looking in the right place |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2145 | NamedDecl *CleanupDecl |
| Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2146 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2147 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2148 | if (!CleanupDecl) { |
| Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2149 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2150 | Attr.getParameterName(); |
| 2151 | return; |
| 2152 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2153 | |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2154 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2155 | if (!FD) { |
| Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2156 | S.Diag(Attr.getParameterLoc(), |
| 2157 | diag::err_attribute_cleanup_arg_not_function) |
| 2158 | << Attr.getParameterName(); |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2159 | return; |
| 2160 | } |
| 2161 | |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2162 | if (FD->getNumParams() != 1) { |
| Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2163 | S.Diag(Attr.getParameterLoc(), |
| 2164 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2165 | << Attr.getParameterName(); |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2166 | return; |
| 2167 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2168 | |
| Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2169 | // We're currently more strict than GCC about what function types we accept. |
| 2170 | // If this ever proves to be a problem it should be easy to fix. |
| 2171 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2172 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
| Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2173 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2174 | ParamTy, Ty) != Sema::Compatible) { |
| Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2175 | S.Diag(Attr.getParameterLoc(), |
| Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2176 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2177 | Attr.getParameterName() << ParamTy << Ty; |
| 2178 | return; |
| 2179 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2180 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2181 | D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD)); |
| Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2182 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
| Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2185 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 2186 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2187 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2188 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2189 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2190 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2191 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2192 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2193 | << Attr.getName() << ExpectedFunction; |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2194 | return; |
| 2195 | } |
| Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2196 | |
| 2197 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2198 | // counted from one. |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2199 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2200 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2201 | unsigned FirstIdx = 1; |
| Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2202 | |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2203 | // checks for the 2nd argument |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2204 | Expr *IdxExpr = Attr.getArg(0); |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2205 | llvm::APSInt Idx(32); |
| Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2206 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2207 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2208 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2209 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2210 | return; |
| 2211 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2212 | |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2213 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 2214 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 2215 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2216 | return; |
| 2217 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2218 | |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2219 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2220 | |
| Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2221 | if (HasImplicitThisParam) { |
| 2222 | if (ArgIdx == 0) { |
| 2223 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 2224 | << "format_arg" << IdxExpr->getSourceRange(); |
| 2225 | return; |
| 2226 | } |
| 2227 | ArgIdx--; |
| 2228 | } |
| 2229 | |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2230 | // make sure the format string is really a string |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2231 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2232 | |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2233 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2234 | if (not_nsstring_type && |
| 2235 | !isCFStringType(Ty, S.Context) && |
| 2236 | (!Ty->isPointerType() || |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2237 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2238 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2239 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2240 | << (not_nsstring_type ? "a string type" : "an NSString") |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2241 | << IdxExpr->getSourceRange(); |
| 2242 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2243 | } |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2244 | Ty = getFunctionOrMethodResultType(D); |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2245 | if (!isNSStringType(Ty, S.Context) && |
| 2246 | !isCFStringType(Ty, S.Context) && |
| 2247 | (!Ty->isPointerType() || |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2248 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2249 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2250 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2251 | << (not_nsstring_type ? "string type" : "NSString") |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2252 | << IdxExpr->getSourceRange(); |
| 2253 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2256 | D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context, |
| Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2257 | Idx.getZExtValue())); |
| Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2260 | enum FormatAttrKind { |
| 2261 | CFStringFormat, |
| 2262 | NSStringFormat, |
| 2263 | StrftimeFormat, |
| 2264 | SupportedFormat, |
| Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2265 | IgnoredFormat, |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2266 | InvalidFormat |
| 2267 | }; |
| 2268 | |
| 2269 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2270 | /// types. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2271 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2272 | // Check for formats that get handled specially. |
| 2273 | if (Format == "NSString") |
| 2274 | return NSStringFormat; |
| 2275 | if (Format == "CFString") |
| 2276 | return CFStringFormat; |
| 2277 | if (Format == "strftime") |
| 2278 | return StrftimeFormat; |
| 2279 | |
| 2280 | // Otherwise, check for supported formats. |
| 2281 | if (Format == "scanf" || Format == "printf" || Format == "printf0" || |
| Jean-Daniel Dupas | fc0da1a | 2012-01-27 09:14:17 +0000 | [diff] [blame] | 2282 | Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" || |
| Chris Lattner | 0ddd0ae | 2011-02-18 17:05:55 +0000 | [diff] [blame] | 2283 | Format == "zcmn_err" || |
| 2284 | Format == "kprintf") // OpenBSD. |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2285 | return SupportedFormat; |
| 2286 | |
| Duncan Sands | de4fe35 | 2010-03-23 14:44:19 +0000 | [diff] [blame] | 2287 | if (Format == "gcc_diag" || Format == "gcc_cdiag" || |
| 2288 | Format == "gcc_cxxdiag" || Format == "gcc_tdiag") |
| Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2289 | return IgnoredFormat; |
| 2290 | |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2291 | return InvalidFormat; |
| 2292 | } |
| 2293 | |
| Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2294 | /// Handle __attribute__((init_priority(priority))) attributes based on |
| 2295 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2296 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2297 | const AttributeList &Attr) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2298 | if (!S.getLangOpts().CPlusPlus) { |
| Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2299 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2300 | return; |
| 2301 | } |
| 2302 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2303 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
| Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2304 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2305 | Attr.setInvalid(); |
| 2306 | return; |
| 2307 | } |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2308 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
| Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2309 | if (S.Context.getAsArrayType(T)) |
| 2310 | T = S.Context.getBaseElementType(T); |
| 2311 | if (!T->getAs<RecordType>()) { |
| 2312 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2313 | Attr.setInvalid(); |
| 2314 | return; |
| 2315 | } |
| 2316 | |
| Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2317 | if (Attr.getNumArgs() != 1) { |
| 2318 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2319 | Attr.setInvalid(); |
| 2320 | return; |
| 2321 | } |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2322 | Expr *priorityExpr = Attr.getArg(0); |
| Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2323 | |
| Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2324 | llvm::APSInt priority(32); |
| 2325 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 2326 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 2327 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2328 | << "init_priority" << priorityExpr->getSourceRange(); |
| 2329 | Attr.setInvalid(); |
| 2330 | return; |
| 2331 | } |
| Fariborz Jahanian | 9f2a4ee | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 2332 | unsigned prioritynum = priority.getZExtValue(); |
| Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2333 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2334 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 2335 | << priorityExpr->getSourceRange(); |
| 2336 | Attr.setInvalid(); |
| 2337 | return; |
| 2338 | } |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2339 | D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context, |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2340 | prioritynum)); |
| Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2343 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 2344 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2345 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2346 | |
| Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2347 | if (!Attr.getParameterName()) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2348 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2349 | << "format" << 1; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2350 | return; |
| 2351 | } |
| 2352 | |
| Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2353 | if (Attr.getNumArgs() != 2) { |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2354 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2355 | return; |
| 2356 | } |
| 2357 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2358 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2359 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2360 | << Attr.getName() << ExpectedFunction; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2361 | return; |
| 2362 | } |
| 2363 | |
| Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2364 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2365 | // counted from one. |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2366 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2367 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2368 | unsigned FirstIdx = 1; |
| 2369 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2370 | StringRef Format = Attr.getParameterName()->getName(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2371 | |
| 2372 | // Normalize the argument, __foo__ becomes foo. |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2373 | if (Format.startswith("__") && Format.endswith("__")) |
| 2374 | Format = Format.substr(2, Format.size() - 4); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2375 | |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2376 | // Check for supported formats. |
| 2377 | FormatAttrKind Kind = getFormatAttrKind(Format); |
| Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2378 | |
| 2379 | if (Kind == IgnoredFormat) |
| 2380 | return; |
| 2381 | |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2382 | if (Kind == InvalidFormat) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2383 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2384 | << "format" << Attr.getParameterName()->getName(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2385 | return; |
| 2386 | } |
| 2387 | |
| 2388 | // checks for the 2nd argument |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2389 | Expr *IdxExpr = Attr.getArg(0); |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2390 | llvm::APSInt Idx(32); |
| Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2391 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2392 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2393 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2394 | << "format" << 2 << IdxExpr->getSourceRange(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2395 | return; |
| 2396 | } |
| 2397 | |
| 2398 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2399 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2400 | << "format" << 2 << IdxExpr->getSourceRange(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2401 | return; |
| 2402 | } |
| 2403 | |
| 2404 | // FIXME: Do we need to bounds check? |
| 2405 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2406 | |
| Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2407 | if (HasImplicitThisParam) { |
| 2408 | if (ArgIdx == 0) { |
| Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2409 | S.Diag(Attr.getLoc(), |
| 2410 | diag::err_format_attribute_implicit_this_format_string) |
| 2411 | << IdxExpr->getSourceRange(); |
| Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2412 | return; |
| 2413 | } |
| 2414 | ArgIdx--; |
| 2415 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2416 | |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2417 | // make sure the format string is really a string |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2418 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2419 | |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2420 | if (Kind == CFStringFormat) { |
| Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2421 | if (!isCFStringType(Ty, S.Context)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2422 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2423 | << "a CFString" << IdxExpr->getSourceRange(); |
| Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2424 | return; |
| 2425 | } |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2426 | } else if (Kind == NSStringFormat) { |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2427 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2428 | // semantics? |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2429 | if (!isNSStringType(Ty, S.Context)) { |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2430 | // FIXME: Should highlight the actual expression that has the wrong type. |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2431 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2432 | << "an NSString" << IdxExpr->getSourceRange(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2433 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2434 | } |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2435 | } else if (!Ty->isPointerType() || |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2436 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2437 | // FIXME: Should highlight the actual expression that has the wrong type. |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2438 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2439 | << "a string type" << IdxExpr->getSourceRange(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2440 | return; |
| 2441 | } |
| 2442 | |
| 2443 | // check the 3rd argument |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2444 | Expr *FirstArgExpr = Attr.getArg(1); |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2445 | llvm::APSInt FirstArg(32); |
| Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2446 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 2447 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2448 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2449 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2450 | return; |
| 2451 | } |
| 2452 | |
| 2453 | // check if the function is variadic if the 3rd argument non-zero |
| 2454 | if (FirstArg != 0) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2455 | if (isFunctionOrMethodVariadic(D)) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2456 | ++NumArgs; // +1 for ... |
| 2457 | } else { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2458 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2459 | return; |
| 2460 | } |
| 2461 | } |
| 2462 | |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2463 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2464 | // variable the input is just the current time + the format string. |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2465 | if (Kind == StrftimeFormat) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2466 | if (FirstArg != 0) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2467 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2468 | << FirstArgExpr->getSourceRange(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2469 | return; |
| 2470 | } |
| 2471 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2472 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2473 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2474 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2475 | return; |
| 2476 | } |
| 2477 | |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2478 | // Check whether we already have an equivalent format attribute. |
| 2479 | for (specific_attr_iterator<FormatAttr> |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2480 | i = D->specific_attr_begin<FormatAttr>(), |
| 2481 | e = D->specific_attr_end<FormatAttr>(); |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2482 | i != e ; ++i) { |
| 2483 | FormatAttr *f = *i; |
| 2484 | if (f->getType() == Format && |
| 2485 | f->getFormatIdx() == (int)Idx.getZExtValue() && |
| 2486 | f->getFirstArg() == (int)FirstArg.getZExtValue()) { |
| 2487 | // If we don't have a valid location for this attribute, adopt the |
| 2488 | // location. |
| 2489 | if (f->getLocation().isInvalid()) |
| Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2490 | f->setRange(Attr.getRange()); |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2491 | return; |
| 2492 | } |
| 2493 | } |
| 2494 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2495 | D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format, |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2496 | Idx.getZExtValue(), |
| Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2497 | FirstArg.getZExtValue())); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2500 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2501 | const AttributeList &Attr) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2502 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2503 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2504 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2505 | |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2506 | |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2507 | // Try to find the underlying union declaration. |
| 2508 | RecordDecl *RD = 0; |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2509 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2510 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2511 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2512 | else |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2513 | RD = dyn_cast<RecordDecl>(D); |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2514 | |
| 2515 | if (!RD || !RD->isUnion()) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2516 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2517 | << Attr.getName() << ExpectedUnion; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2518 | return; |
| 2519 | } |
| 2520 | |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2521 | if (!RD->isCompleteDefinition()) { |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2522 | S.Diag(Attr.getLoc(), |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2523 | diag::warn_transparent_union_attribute_not_definition); |
| 2524 | return; |
| 2525 | } |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2526 | |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2527 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2528 | FieldEnd = RD->field_end(); |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2529 | if (Field == FieldEnd) { |
| 2530 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2531 | return; |
| 2532 | } |
| Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2533 | |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2534 | FieldDecl *FirstField = *Field; |
| 2535 | QualType FirstType = FirstField->getType(); |
| Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2536 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2537 | S.Diag(FirstField->getLocation(), |
| Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2538 | diag::warn_transparent_union_attribute_floating) |
| 2539 | << FirstType->isVectorType() << FirstType; |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2540 | return; |
| 2541 | } |
| 2542 | |
| 2543 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2544 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2545 | for (; Field != FieldEnd; ++Field) { |
| 2546 | QualType FieldType = Field->getType(); |
| 2547 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 2548 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 2549 | // Warn if we drop the attribute. |
| 2550 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2551 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2552 | : S.Context.getTypeAlign(FieldType); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2553 | S.Diag(Field->getLocation(), |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2554 | diag::warn_transparent_union_attribute_field_size_align) |
| 2555 | << isSize << Field->getDeclName() << FieldBits; |
| 2556 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2557 | S.Diag(FirstField->getLocation(), |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2558 | diag::note_transparent_union_first_field_size_align) |
| 2559 | << isSize << FirstBits; |
| Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2560 | return; |
| 2561 | } |
| 2562 | } |
| 2563 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2564 | RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context)); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2567 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2568 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2569 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2570 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2571 | |
| Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2572 | Expr *ArgExpr = Attr.getArg(0); |
| Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2573 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2574 | |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2575 | // Make sure that there is a string literal as the annotation's single |
| 2576 | // argument. |
| 2577 | if (!SE) { |
| Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2578 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
| Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2581 | |
| 2582 | // Don't duplicate annotations that are already set. |
| 2583 | for (specific_attr_iterator<AnnotateAttr> |
| 2584 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 2585 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 2586 | if ((*i)->getAnnotation() == SE->getString()) |
| 2587 | return; |
| 2588 | } |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2589 | D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context, |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2590 | SE->getString())); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2593 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2594 | // check the attribute arguments. |
| Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2595 | if (Attr.getNumArgs() > 1) { |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2596 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2597 | return; |
| 2598 | } |
| Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2599 | |
| 2600 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 2601 | // than GNU's, and should error out when it is used to specify a |
| 2602 | // weaker alignment, rather than being silently ignored. |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2603 | |
| Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2604 | if (Attr.getNumArgs() == 0) { |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2605 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0)); |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2606 | return; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2607 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2608 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2609 | S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0)); |
| Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2612 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) { |
| Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2613 | // FIXME: Handle pack-expansions here. |
| 2614 | if (DiagnoseUnexpandedParameterPack(E)) |
| 2615 | return; |
| 2616 | |
| Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2617 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2618 | // Save dependent expressions in the AST to be instantiated. |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2619 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E)); |
| Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2620 | return; |
| 2621 | } |
| 2622 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2623 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2624 | // FIXME: Cache the number on the Attr object? |
| Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2625 | llvm::APSInt Alignment(32); |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2626 | ExprResult ICE = |
| 2627 | VerifyIntegerConstantExpression(E, &Alignment, |
| 2628 | PDiag(diag::err_attribute_argument_not_int) << "aligned", |
| 2629 | /*AllowFold*/ false); |
| 2630 | if (ICE.isInvalid()) |
| Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2631 | return; |
| Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2632 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
| Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2633 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2634 | << E->getSourceRange(); |
| Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2635 | return; |
| 2636 | } |
| 2637 | |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2638 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take())); |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2641 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) { |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2642 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2643 | // FIXME: Perform checking of type validity |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2644 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS)); |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2645 | return; |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2646 | } |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2647 | |
| Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2648 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2649 | /// type. |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2650 | /// |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2651 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2652 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2653 | /// HImode, not an intermediate pointer. |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2654 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2655 | // This attribute isn't documented, but glibc uses it. It changes |
| 2656 | // the width of an int or unsigned int to the specified size. |
| 2657 | |
| 2658 | // Check that there aren't any arguments |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2659 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2660 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2661 | |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2662 | |
| 2663 | IdentifierInfo *Name = Attr.getParameterName(); |
| 2664 | if (!Name) { |
| Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2665 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2666 | return; |
| 2667 | } |
| Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2668 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2669 | StringRef Str = Attr.getParameterName()->getName(); |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2670 | |
| 2671 | // Normalize the attribute name, __foo__ becomes foo. |
| Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2672 | if (Str.startswith("__") && Str.endswith("__")) |
| 2673 | Str = Str.substr(2, Str.size() - 4); |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2674 | |
| 2675 | unsigned DestWidth = 0; |
| 2676 | bool IntegerMode = true; |
| Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2677 | bool ComplexMode = false; |
| Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2678 | switch (Str.size()) { |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2679 | case 2: |
| Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2680 | switch (Str[0]) { |
| 2681 | case 'Q': DestWidth = 8; break; |
| 2682 | case 'H': DestWidth = 16; break; |
| 2683 | case 'S': DestWidth = 32; break; |
| 2684 | case 'D': DestWidth = 64; break; |
| 2685 | case 'X': DestWidth = 96; break; |
| 2686 | case 'T': DestWidth = 128; break; |
| 2687 | } |
| 2688 | if (Str[1] == 'F') { |
| 2689 | IntegerMode = false; |
| 2690 | } else if (Str[1] == 'C') { |
| 2691 | IntegerMode = false; |
| 2692 | ComplexMode = true; |
| 2693 | } else if (Str[1] != 'I') { |
| 2694 | DestWidth = 0; |
| 2695 | } |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2696 | break; |
| 2697 | case 4: |
| 2698 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2699 | // pointer on PIC16 and other embedded platforms. |
| Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2700 | if (Str == "word") |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2701 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
| Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2702 | else if (Str == "byte") |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2703 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2704 | break; |
| 2705 | case 7: |
| Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2706 | if (Str == "pointer") |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2707 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2708 | break; |
| 2709 | } |
| 2710 | |
| 2711 | QualType OldTy; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2712 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2713 | OldTy = TD->getUnderlyingType(); |
| 2714 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2715 | OldTy = VD->getType(); |
| 2716 | else { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2717 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2718 | << "mode" << Attr.getRange(); |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2719 | return; |
| 2720 | } |
| Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2721 | |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2722 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
| Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2723 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2724 | else if (IntegerMode) { |
| Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2725 | if (!OldTy->isIntegralOrEnumerationType()) |
| Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2726 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2727 | } else if (ComplexMode) { |
| 2728 | if (!OldTy->isComplexType()) |
| 2729 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2730 | } else { |
| 2731 | if (!OldTy->isFloatingType()) |
| 2732 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2733 | } |
| 2734 | |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2735 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2736 | // and friends, at least with glibc. |
| 2737 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 2738 | // width on unusual platforms. |
| Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2739 | // FIXME: Make sure floating-point mappings are accurate |
| 2740 | // FIXME: Support XF and TF types |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2741 | QualType NewTy; |
| 2742 | switch (DestWidth) { |
| 2743 | case 0: |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2744 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2745 | return; |
| 2746 | default: |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2747 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2748 | return; |
| 2749 | case 8: |
| Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2750 | if (!IntegerMode) { |
| 2751 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2752 | return; |
| 2753 | } |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2754 | if (OldTy->isSignedIntegerType()) |
| Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2755 | NewTy = S.Context.SignedCharTy; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2756 | else |
| Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2757 | NewTy = S.Context.UnsignedCharTy; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2758 | break; |
| 2759 | case 16: |
| Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2760 | if (!IntegerMode) { |
| 2761 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2762 | return; |
| 2763 | } |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2764 | if (OldTy->isSignedIntegerType()) |
| Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2765 | NewTy = S.Context.ShortTy; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2766 | else |
| Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2767 | NewTy = S.Context.UnsignedShortTy; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2768 | break; |
| 2769 | case 32: |
| 2770 | if (!IntegerMode) |
| Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2771 | NewTy = S.Context.FloatTy; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2772 | else if (OldTy->isSignedIntegerType()) |
| Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2773 | NewTy = S.Context.IntTy; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2774 | else |
| Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2775 | NewTy = S.Context.UnsignedIntTy; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2776 | break; |
| 2777 | case 64: |
| 2778 | if (!IntegerMode) |
| Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2779 | NewTy = S.Context.DoubleTy; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2780 | else if (OldTy->isSignedIntegerType()) |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2781 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
| Chandler Carruth | 7234370 | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2782 | NewTy = S.Context.LongTy; |
| 2783 | else |
| 2784 | NewTy = S.Context.LongLongTy; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2785 | else |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2786 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
| Chandler Carruth | 7234370 | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2787 | NewTy = S.Context.UnsignedLongTy; |
| 2788 | else |
| 2789 | NewTy = S.Context.UnsignedLongLongTy; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2790 | break; |
| Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2791 | case 96: |
| 2792 | NewTy = S.Context.LongDoubleTy; |
| 2793 | break; |
| Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2794 | case 128: |
| 2795 | if (!IntegerMode) { |
| 2796 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2797 | return; |
| 2798 | } |
| Anders Carlsson | 88ea245 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 2799 | if (OldTy->isSignedIntegerType()) |
| 2800 | NewTy = S.Context.Int128Ty; |
| 2801 | else |
| 2802 | NewTy = S.Context.UnsignedInt128Ty; |
| Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2803 | break; |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
| Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2806 | if (ComplexMode) { |
| 2807 | NewTy = S.Context.getComplexType(NewTy); |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | // Install the new type. |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2811 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2812 | // FIXME: preserve existing source info. |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2813 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2814 | } else |
| Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2815 | cast<ValueDecl>(D)->setType(NewTy); |
| 2816 | } |
| Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2817 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2818 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2819 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2820 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2821 | return; |
| Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 2822 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2823 | if (!isFunctionOrMethod(D)) { |
| Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2824 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2825 | << Attr.getName() << ExpectedFunction; |
| Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2826 | return; |
| 2827 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2828 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2829 | D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context)); |
| Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2832 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2833 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2834 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2835 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2836 | |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2837 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2838 | if (!isa<FunctionDecl>(D)) { |
| Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2839 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2840 | << Attr.getName() << ExpectedFunction; |
| Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2841 | return; |
| 2842 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2843 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2844 | D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context)); |
| Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2847 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 2848 | const AttributeList &Attr) { |
| Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2849 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2850 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2851 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2852 | |
| Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2853 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2854 | if (!isa<FunctionDecl>(D)) { |
| Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2855 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2856 | << Attr.getName() << ExpectedFunction; |
| Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2857 | return; |
| 2858 | } |
| 2859 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2860 | D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(), |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2861 | S.Context)); |
| Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2864 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2865 | if (S.LangOpts.CUDA) { |
| 2866 | // check the attribute arguments. |
| Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2867 | if (Attr.hasParameterOrArguments()) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2868 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2869 | return; |
| 2870 | } |
| 2871 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2872 | if (!isa<VarDecl>(D)) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2873 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2874 | << Attr.getName() << ExpectedVariable; |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2875 | return; |
| 2876 | } |
| 2877 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2878 | D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context)); |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2879 | } else { |
| 2880 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 2881 | } |
| 2882 | } |
| 2883 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2884 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2885 | if (S.LangOpts.CUDA) { |
| 2886 | // check the attribute arguments. |
| 2887 | if (Attr.getNumArgs() != 0) { |
| 2888 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2889 | return; |
| 2890 | } |
| 2891 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2892 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2893 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2894 | << Attr.getName() << ExpectedVariableOrFunction; |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2895 | return; |
| 2896 | } |
| 2897 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2898 | D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context)); |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2899 | } else { |
| 2900 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 2901 | } |
| 2902 | } |
| 2903 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2904 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2905 | if (S.LangOpts.CUDA) { |
| 2906 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2907 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2908 | return; |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2909 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2910 | if (!isa<FunctionDecl>(D)) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2911 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2912 | << Attr.getName() << ExpectedFunction; |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2913 | return; |
| 2914 | } |
| 2915 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2916 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2917 | if (!FD->getResultType()->isVoidType()) { |
| Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 2918 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
| Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2919 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 2920 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2921 | << FD->getType() |
| 2922 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 2923 | "void"); |
| 2924 | } else { |
| 2925 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2926 | << FD->getType(); |
| 2927 | } |
| 2928 | return; |
| 2929 | } |
| 2930 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2931 | D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context)); |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2932 | } else { |
| 2933 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 2934 | } |
| 2935 | } |
| 2936 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2937 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2938 | if (S.LangOpts.CUDA) { |
| 2939 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2940 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2941 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2942 | |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2943 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2944 | if (!isa<FunctionDecl>(D)) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2945 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2946 | << Attr.getName() << ExpectedFunction; |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2947 | return; |
| 2948 | } |
| 2949 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2950 | D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context)); |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2951 | } else { |
| 2952 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 2953 | } |
| 2954 | } |
| 2955 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2956 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2957 | if (S.LangOpts.CUDA) { |
| 2958 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2959 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2960 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2961 | |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2962 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2963 | if (!isa<VarDecl>(D)) { |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2964 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2965 | << Attr.getName() << ExpectedVariable; |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2966 | return; |
| 2967 | } |
| 2968 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2969 | D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context)); |
| Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2970 | } else { |
| 2971 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 2972 | } |
| 2973 | } |
| 2974 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2975 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2976 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2977 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2978 | return; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2979 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2980 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
| Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2981 | if (Fn == 0) { |
| Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2982 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2983 | << Attr.getName() << ExpectedFunction; |
| Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2984 | return; |
| 2985 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2986 | |
| Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 2987 | if (!Fn->isInlineSpecified()) { |
| Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 2988 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
| Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2989 | return; |
| 2990 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2991 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2992 | D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context)); |
| Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2995 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2996 | if (hasDeclarator(D)) return; |
| Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2997 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2998 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2999 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3000 | CallingConv CC; |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3001 | if (S.CheckCallingConvAttr(Attr, CC)) |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3002 | return; |
| 3003 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3004 | if (!isa<ObjCMethodDecl>(D)) { |
| 3005 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3006 | << Attr.getName() << ExpectedFunctionOrMethod; |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3007 | return; |
| 3008 | } |
| 3009 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3010 | switch (Attr.getKind()) { |
| Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3011 | case AttributeList::AT_fastcall: |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3012 | D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context)); |
| Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3013 | return; |
| 3014 | case AttributeList::AT_stdcall: |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3015 | D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context)); |
| Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3016 | return; |
| Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3017 | case AttributeList::AT_thiscall: |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3018 | D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context)); |
| Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3019 | return; |
| Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3020 | case AttributeList::AT_cdecl: |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3021 | D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context)); |
| Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3022 | return; |
| Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3023 | case AttributeList::AT_pascal: |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3024 | D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context)); |
| Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3025 | return; |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3026 | case AttributeList::AT_pcs: { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3027 | Expr *Arg = Attr.getArg(0); |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3028 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3029 | if (!Str || !Str->isAscii()) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3030 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3031 | << "pcs" << 1; |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3032 | Attr.setInvalid(); |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3033 | return; |
| 3034 | } |
| 3035 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3036 | StringRef StrRef = Str->getString(); |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3037 | PcsAttr::PCSType PCS; |
| 3038 | if (StrRef == "aapcs") |
| 3039 | PCS = PcsAttr::AAPCS; |
| 3040 | else if (StrRef == "aapcs-vfp") |
| 3041 | PCS = PcsAttr::AAPCS_VFP; |
| 3042 | else { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3043 | S.Diag(Attr.getLoc(), diag::err_invalid_pcs); |
| 3044 | Attr.setInvalid(); |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3045 | return; |
| 3046 | } |
| 3047 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3048 | D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS)); |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3049 | } |
| Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3050 | default: |
| 3051 | llvm_unreachable("unexpected attribute kind"); |
| Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3052 | } |
| 3053 | } |
| 3054 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3055 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
| Chandler Carruth | 9312c64 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 3056 | assert(!Attr.isInvalid()); |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3057 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
| Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3060 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { |
| 3061 | if (attr.isInvalid()) |
| 3062 | return true; |
| 3063 | |
| Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3064 | if ((attr.getNumArgs() != 0 && |
| 3065 | !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) || |
| 3066 | attr.getParameterName()) { |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3067 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3068 | attr.setInvalid(); |
| 3069 | return true; |
| 3070 | } |
| 3071 | |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3072 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3073 | // move to TargetAttributesSema one day. |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3074 | switch (attr.getKind()) { |
| 3075 | case AttributeList::AT_cdecl: CC = CC_C; break; |
| 3076 | case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; |
| 3077 | case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; |
| 3078 | case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break; |
| 3079 | case AttributeList::AT_pascal: CC = CC_X86Pascal; break; |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3080 | case AttributeList::AT_pcs: { |
| 3081 | Expr *Arg = attr.getArg(0); |
| 3082 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3083 | if (!Str || !Str->isAscii()) { |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3084 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3085 | << "pcs" << 1; |
| 3086 | attr.setInvalid(); |
| 3087 | return true; |
| 3088 | } |
| 3089 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3090 | StringRef StrRef = Str->getString(); |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3091 | if (StrRef == "aapcs") { |
| 3092 | CC = CC_AAPCS; |
| 3093 | break; |
| 3094 | } else if (StrRef == "aapcs-vfp") { |
| 3095 | CC = CC_AAPCS_VFP; |
| 3096 | break; |
| 3097 | } |
| 3098 | // FALLS THROUGH |
| 3099 | } |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3100 | default: llvm_unreachable("unexpected attribute kind"); |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | return false; |
| 3104 | } |
| 3105 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3106 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3107 | if (hasDeclarator(D)) return; |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3108 | |
| 3109 | unsigned numParams; |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3110 | if (S.CheckRegparmAttr(Attr, numParams)) |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3111 | return; |
| 3112 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3113 | if (!isa<ObjCMethodDecl>(D)) { |
| 3114 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3115 | << Attr.getName() << ExpectedFunctionOrMethod; |
| Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3116 | return; |
| 3117 | } |
| Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3118 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3119 | D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams)); |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3123 | /// otherwise setting numParams to the appropriate value. |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3124 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3125 | if (Attr.isInvalid()) |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3126 | return true; |
| 3127 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3128 | if (Attr.getNumArgs() != 1) { |
| 3129 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3130 | Attr.setInvalid(); |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3131 | return true; |
| Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3132 | } |
| Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3133 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3134 | Expr *NumParamsExpr = Attr.getArg(0); |
| Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3135 | llvm::APSInt NumParams(32); |
| Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3136 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3137 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3138 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3139 | << "regparm" << NumParamsExpr->getSourceRange(); |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3140 | Attr.setInvalid(); |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3141 | return true; |
| Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3144 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3145 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
| Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3146 | << NumParamsExpr->getSourceRange(); |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3147 | Attr.setInvalid(); |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3148 | return true; |
| Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3151 | numParams = NumParams.getZExtValue(); |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3152 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3153 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3154 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3155 | Attr.setInvalid(); |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3156 | return true; |
| Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3159 | return false; |
| Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3162 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
| Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3163 | if (S.LangOpts.CUDA) { |
| 3164 | // check the attribute arguments. |
| 3165 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
| John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 3166 | // FIXME: 0 is not okay. |
| 3167 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
| Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3168 | return; |
| 3169 | } |
| 3170 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3171 | if (!isFunctionOrMethod(D)) { |
| Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3172 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3173 | << Attr.getName() << ExpectedFunctionOrMethod; |
| Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3174 | return; |
| 3175 | } |
| 3176 | |
| 3177 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 3178 | llvm::APSInt MaxThreads(32); |
| 3179 | if (MaxThreadsExpr->isTypeDependent() || |
| 3180 | MaxThreadsExpr->isValueDependent() || |
| 3181 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 3182 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3183 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 3184 | return; |
| 3185 | } |
| 3186 | |
| 3187 | llvm::APSInt MinBlocks(32); |
| 3188 | if (Attr.getNumArgs() > 1) { |
| 3189 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 3190 | if (MinBlocksExpr->isTypeDependent() || |
| 3191 | MinBlocksExpr->isValueDependent() || |
| 3192 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 3193 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3194 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 3195 | return; |
| 3196 | } |
| 3197 | } |
| 3198 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3199 | D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3200 | MaxThreads.getZExtValue(), |
| 3201 | MinBlocks.getZExtValue())); |
| 3202 | } else { |
| 3203 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 3204 | } |
| 3205 | } |
| 3206 | |
| Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3207 | //===----------------------------------------------------------------------===// |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3208 | // Checker-specific attribute handlers. |
| 3209 | //===----------------------------------------------------------------------===// |
| 3210 | |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3211 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
| Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3212 | return type->isDependentType() || |
| 3213 | type->isObjCObjectPointerType() || |
| 3214 | S.Context.isObjCNSObjectType(type); |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3215 | } |
| 3216 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
| Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3217 | return type->isDependentType() || |
| 3218 | type->isPointerType() || |
| 3219 | isValidSubjectOfNSAttribute(S, type); |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3222 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3223 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3224 | if (!param) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3225 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3226 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3227 | return; |
| 3228 | } |
| 3229 | |
| 3230 | bool typeOK, cf; |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3231 | if (Attr.getKind() == AttributeList::AT_ns_consumed) { |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3232 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3233 | cf = false; |
| 3234 | } else { |
| 3235 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3236 | cf = true; |
| 3237 | } |
| 3238 | |
| 3239 | if (!typeOK) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3240 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3241 | << Attr.getRange() << Attr.getName() << cf; |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3242 | return; |
| 3243 | } |
| 3244 | |
| 3245 | if (cf) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3246 | param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context)); |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3247 | else |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3248 | param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context)); |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3251 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 3252 | const AttributeList &Attr) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3253 | if (!isa<ObjCMethodDecl>(D)) { |
| 3254 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3255 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3256 | return; |
| 3257 | } |
| 3258 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3259 | D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context)); |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3262 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3263 | const AttributeList &Attr) { |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3264 | |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3265 | QualType returnType; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3266 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3267 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3268 | returnType = MD->getResultType(); |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3269 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| Fariborz Jahanian | f4105f5 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 3270 | returnType = PD->getType(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3271 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3272 | (Attr.getKind() == AttributeList::AT_ns_returns_retained)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3273 | return; // ignore: was handled as a type attribute |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3274 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3275 | returnType = FD->getResultType(); |
| Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3276 | else { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3277 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3278 | << Attr.getRange() << Attr.getName() |
| John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3279 | << ExpectedFunctionOrMethod; |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3280 | return; |
| 3281 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3282 | |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3283 | bool typeOK; |
| 3284 | bool cf; |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3285 | switch (Attr.getKind()) { |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3286 | default: llvm_unreachable("invalid ownership attribute"); |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3287 | case AttributeList::AT_ns_returns_autoreleased: |
| 3288 | case AttributeList::AT_ns_returns_retained: |
| 3289 | case AttributeList::AT_ns_returns_not_retained: |
| 3290 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3291 | cf = false; |
| 3292 | break; |
| 3293 | |
| 3294 | case AttributeList::AT_cf_returns_retained: |
| 3295 | case AttributeList::AT_cf_returns_not_retained: |
| 3296 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3297 | cf = true; |
| 3298 | break; |
| 3299 | } |
| 3300 | |
| 3301 | if (!typeOK) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3302 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3303 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3304 | return; |
| Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3305 | } |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3306 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3307 | switch (Attr.getKind()) { |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3308 | default: |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3309 | llvm_unreachable("invalid ownership attribute"); |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3310 | case AttributeList::AT_ns_returns_autoreleased: |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3311 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(), |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3312 | S.Context)); |
| 3313 | return; |
| Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3314 | case AttributeList::AT_cf_returns_not_retained: |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3315 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(), |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3316 | S.Context)); |
| Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3317 | return; |
| 3318 | case AttributeList::AT_ns_returns_not_retained: |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3319 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(), |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3320 | S.Context)); |
| Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3321 | return; |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3322 | case AttributeList::AT_cf_returns_retained: |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3323 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(), |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3324 | S.Context)); |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3325 | return; |
| 3326 | case AttributeList::AT_ns_returns_retained: |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3327 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(), |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3328 | S.Context)); |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3329 | return; |
| 3330 | }; |
| 3331 | } |
| 3332 | |
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3333 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3334 | const AttributeList &attr) { |
| 3335 | SourceLocation loc = attr.getLoc(); |
| 3336 | |
| 3337 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 3338 | |
| 3339 | if (!isa<ObjCMethodDecl>(method)) { |
| 3340 | S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type) |
| Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3341 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3342 | return; |
| 3343 | } |
| 3344 | |
| 3345 | // Check that the method returns a normal pointer. |
| 3346 | QualType resultType = method->getResultType(); |
| Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3347 | |
| 3348 | if (!resultType->isReferenceType() && |
| 3349 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3350 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 3351 | << SourceRange(loc) |
| 3352 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 3353 | |
| 3354 | // Drop the attribute. |
| 3355 | return; |
| 3356 | } |
| 3357 | |
| 3358 | method->addAttr( |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3359 | ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context)); |
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
| John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3362 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 3363 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 3364 | if (!isa<FunctionDecl>(D)) { |
| 3365 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3366 | << A.getRange() << A.getName() << ExpectedFunction; |
| John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3367 | return; |
| 3368 | } |
| 3369 | |
| 3370 | bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer); |
| 3371 | |
| 3372 | // Check whether there's a conflicting attribute already present. |
| 3373 | Attr *Existing; |
| 3374 | if (IsAudited) { |
| 3375 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 3376 | } else { |
| 3377 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 3378 | } |
| 3379 | if (Existing) { |
| 3380 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 3381 | << A.getName() |
| 3382 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 3383 | << A.getRange() << Existing->getRange(); |
| 3384 | return; |
| 3385 | } |
| 3386 | |
| 3387 | // All clear; add the attribute. |
| 3388 | if (IsAudited) { |
| 3389 | D->addAttr( |
| 3390 | ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context)); |
| 3391 | } else { |
| 3392 | D->addAttr( |
| 3393 | ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context)); |
| 3394 | } |
| 3395 | } |
| 3396 | |
| John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3397 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3398 | const AttributeList &Attr) { |
| 3399 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 3400 | if (!RD || RD->isUnion()) { |
| 3401 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3402 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
| John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 3406 | |
| 3407 | // In Objective-C, verify that the type names an Objective-C type. |
| 3408 | // We don't want to check this outside of ObjC because people sometimes |
| 3409 | // do crazy C declarations of Objective-C types. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3410 | if (ParmName && S.getLangOpts().ObjC1) { |
| John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3411 | // Check for an existing type with this name. |
| 3412 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 3413 | Sema::LookupOrdinaryName); |
| 3414 | if (S.LookupName(R, Sc)) { |
| 3415 | NamedDecl *Target = R.getFoundDecl(); |
| 3416 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 3417 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 3418 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 3419 | } |
| 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context, |
| 3424 | ParmName)); |
| 3425 | } |
| 3426 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3427 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3428 | const AttributeList &Attr) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3429 | if (hasDeclarator(D)) return; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3430 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3431 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3432 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3435 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3436 | const AttributeList &Attr) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3437 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3438 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3439 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3440 | return; |
| 3441 | } |
| 3442 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3443 | ValueDecl *vd = cast<ValueDecl>(D); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3444 | QualType type = vd->getType(); |
| 3445 | |
| 3446 | if (!type->isDependentType() && |
| 3447 | !type->isObjCLifetimeType()) { |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3448 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3449 | << type; |
| 3450 | return; |
| 3451 | } |
| 3452 | |
| 3453 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3454 | |
| 3455 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3456 | // going to infer. |
| 3457 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3458 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3459 | |
| 3460 | switch (lifetime) { |
| 3461 | case Qualifiers::OCL_None: |
| 3462 | assert(type->isDependentType() && |
| 3463 | "didn't infer lifetime for non-dependent type?"); |
| 3464 | break; |
| 3465 | |
| 3466 | case Qualifiers::OCL_Weak: // meaningful |
| 3467 | case Qualifiers::OCL_Strong: // meaningful |
| 3468 | break; |
| 3469 | |
| 3470 | case Qualifiers::OCL_ExplicitNone: |
| 3471 | case Qualifiers::OCL_Autoreleasing: |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3472 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3473 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3474 | break; |
| 3475 | } |
| 3476 | |
| Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3477 | D->addAttr(::new (S.Context) |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3478 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context)); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3481 | static bool isKnownDeclSpecAttr(const AttributeList &Attr) { |
| Aaron Ballman | 0c84ebb | 2012-02-23 22:46:33 +0000 | [diff] [blame] | 3482 | switch (Attr.getKind()) { |
| 3483 | default: |
| 3484 | return false; |
| 3485 | case AttributeList::AT_dllimport: |
| 3486 | case AttributeList::AT_dllexport: |
| 3487 | case AttributeList::AT_uuid: |
| 3488 | case AttributeList::AT_deprecated: |
| 3489 | case AttributeList::AT_noreturn: |
| 3490 | case AttributeList::AT_nothrow: |
| 3491 | case AttributeList::AT_naked: |
| 3492 | case AttributeList::AT_noinline: |
| 3493 | return true; |
| 3494 | } |
| Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | //===----------------------------------------------------------------------===// |
| 3498 | // Microsoft specific attribute handlers. |
| 3499 | //===----------------------------------------------------------------------===// |
| 3500 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3501 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3502 | if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) { |
| Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3503 | // check the attribute arguments. |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3504 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3505 | return; |
| Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3506 | |
| Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3507 | Expr *Arg = Attr.getArg(0); |
| 3508 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3509 | if (!Str || !Str->isAscii()) { |
| Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3510 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3511 | << "uuid" << 1; |
| 3512 | return; |
| 3513 | } |
| 3514 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3515 | StringRef StrRef = Str->getString(); |
| Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3516 | |
| 3517 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 3518 | StrRef.back() == '}'; |
| Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3519 | |
| Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3520 | // Validate GUID length. |
| 3521 | if (IsCurly && StrRef.size() != 38) { |
| 3522 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3523 | return; |
| 3524 | } |
| 3525 | if (!IsCurly && StrRef.size() != 36) { |
| 3526 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3527 | return; |
| 3528 | } |
| 3529 | |
| Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3530 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3531 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3532 | StringRef::iterator I = StrRef.begin(); |
| Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3533 | if (IsCurly) // Skip the optional '{' |
| 3534 | ++I; |
| 3535 | |
| 3536 | for (int i = 0; i < 36; ++i) { |
| Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3537 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 3538 | if (*I != '-') { |
| 3539 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3540 | return; |
| 3541 | } |
| 3542 | } else if (!isxdigit(*I)) { |
| 3543 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3544 | return; |
| 3545 | } |
| 3546 | I++; |
| 3547 | } |
| Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3548 | |
| Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3549 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, |
| Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3550 | Str->getString())); |
| Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3551 | } else |
| Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3552 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
| Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3555 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3556 | // Top Level Sema Entry Points |
| 3557 | //===----------------------------------------------------------------------===// |
| 3558 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3559 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 3560 | const AttributeList &Attr) { |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3561 | switch (Attr.getKind()) { |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3562 | case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break; |
| 3563 | case AttributeList::AT_host: handleHostAttr (S, D, Attr); break; |
| 3564 | case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break; |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3565 | default: |
| 3566 | break; |
| 3567 | } |
| 3568 | } |
| Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3569 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3570 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 3571 | const AttributeList &Attr) { |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3572 | switch (Attr.getKind()) { |
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3573 | case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break; |
| 3574 | case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break; |
| 3575 | case AttributeList::AT_iboutletcollection: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3576 | handleIBOutletCollection(S, D, Attr); break; |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3577 | case AttributeList::AT_address_space: |
| Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3578 | case AttributeList::AT_opencl_image_access: |
| Fariborz Jahanian | 257eac6 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 3579 | case AttributeList::AT_objc_gc: |
| John Thompson | 4798122 | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3580 | case AttributeList::AT_vector_size: |
| Bob Wilson | 118baf7 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3581 | case AttributeList::AT_neon_vector_type: |
| 3582 | case AttributeList::AT_neon_polyvector_type: |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3583 | // Ignore these, these are type attributes, handled by |
| 3584 | // ProcessTypeAttributes. |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3585 | break; |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3586 | case AttributeList::AT_device: |
| 3587 | case AttributeList::AT_host: |
| 3588 | case AttributeList::AT_overloadable: |
| 3589 | // Ignore, this is a non-inheritable attribute, handled |
| 3590 | // by ProcessNonInheritableDeclAttr. |
| 3591 | break; |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3592 | case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break; |
| 3593 | case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3594 | case AttributeList::AT_always_inline: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3595 | handleAlwaysInlineAttr (S, D, Attr); break; |
| Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 3596 | case AttributeList::AT_analyzer_noreturn: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3597 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
| 3598 | case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break; |
| 3599 | case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break; |
| Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3600 | case AttributeList::AT_carries_dependency: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3601 | handleDependencyAttr (S, D, Attr); break; |
| 3602 | case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break; |
| 3603 | case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break; |
| 3604 | case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break; |
| 3605 | case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break; |
| 3606 | case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break; |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3607 | case AttributeList::AT_ext_vector_type: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3608 | handleExtVectorTypeAttr(S, scope, D, Attr); |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3609 | break; |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3610 | case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break; |
| 3611 | case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break; |
| 3612 | case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break; |
| 3613 | case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break; |
| Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3614 | case AttributeList::AT_launch_bounds: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3615 | handleLaunchBoundsAttr(S, D, Attr); |
| Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3616 | break; |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3617 | case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break; |
| 3618 | case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break; |
| 3619 | case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break; |
| 3620 | case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break; |
| 3621 | case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break; |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 3622 | case AttributeList::AT_ownership_returns: |
| 3623 | case AttributeList::AT_ownership_takes: |
| 3624 | case AttributeList::AT_ownership_holds: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3625 | handleOwnershipAttr (S, D, Attr); break; |
| 3626 | case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break; |
| 3627 | case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break; |
| 3628 | case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break; |
| 3629 | case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break; |
| 3630 | case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break; |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3631 | |
| Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3632 | case AttributeList::AT_objc_ownership: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3633 | handleObjCOwnershipAttr(S, D, Attr); break; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3634 | case AttributeList::AT_objc_precise_lifetime: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3635 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3636 | |
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3637 | case AttributeList::AT_objc_returns_inner_pointer: |
| 3638 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 3639 | |
| John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3640 | case AttributeList::AT_ns_bridged: |
| 3641 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 3642 | |
| John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3643 | case AttributeList::AT_cf_audited_transfer: |
| 3644 | case AttributeList::AT_cf_unknown_transfer: |
| 3645 | handleCFTransferAttr(S, D, Attr); break; |
| 3646 | |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3647 | // Checker-specific. |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3648 | case AttributeList::AT_cf_consumed: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3649 | case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break; |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3650 | case AttributeList::AT_ns_consumes_self: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3651 | handleNSConsumesSelfAttr(S, D, Attr); break; |
| John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3652 | |
| 3653 | case AttributeList::AT_ns_returns_autoreleased: |
| Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3654 | case AttributeList::AT_ns_returns_not_retained: |
| 3655 | case AttributeList::AT_cf_returns_not_retained: |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3656 | case AttributeList::AT_ns_returns_retained: |
| 3657 | case AttributeList::AT_cf_returns_retained: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3658 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
| Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3659 | |
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3660 | case AttributeList::AT_reqd_work_group_size: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3661 | handleReqdWorkGroupSize(S, D, Attr); break; |
| Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 3662 | |
| Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3663 | case AttributeList::AT_init_priority: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3664 | handleInitPriorityAttr(S, D, Attr); break; |
| Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3665 | |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3666 | case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break; |
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3667 | case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break; |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3668 | case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break; |
| 3669 | case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break; |
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3670 | case AttributeList::AT_objc_arc_weak_reference_unavailable: |
| Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 3671 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 3672 | break; |
| Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 3673 | case AttributeList::AT_objc_root_class: |
| 3674 | handleObjCRootClassAttr(S, D, Attr); |
| 3675 | break; |
| Ted Kremenek | 0c2c90b | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 3676 | case AttributeList::AT_objc_requires_property_definitions: |
| 3677 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
| Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 3678 | break; |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3679 | case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break; |
| Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 3680 | case AttributeList::AT_returns_twice: |
| 3681 | handleReturnsTwiceAttr(S, D, Attr); |
| 3682 | break; |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3683 | case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break; |
| 3684 | case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break; |
| 3685 | case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr); |
| Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 3686 | break; |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3687 | case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break; |
| 3688 | case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break; |
| 3689 | case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break; |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3690 | case AttributeList::AT_transparent_union: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3691 | handleTransparentUnionAttr(S, D, Attr); |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3692 | break; |
| Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 3693 | case AttributeList::AT_objc_exception: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3694 | handleObjCExceptionAttr(S, D, Attr); |
| Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 3695 | break; |
| John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 3696 | case AttributeList::AT_objc_method_family: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3697 | handleObjCMethodFamilyAttr(S, D, Attr); |
| John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 3698 | break; |
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3699 | case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break; |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3700 | case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break; |
| 3701 | case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break; |
| 3702 | case AttributeList::AT_const: handleConstAttr (S, D, Attr); break; |
| 3703 | case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break; |
| 3704 | case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break; |
| 3705 | case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break; |
| 3706 | case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break; |
| 3707 | case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break; |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3708 | case AttributeList::IgnoredAttribute: |
| Anders Carlsson | b4f3134 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 3709 | // Just ignore |
| 3710 | break; |
| Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3711 | case AttributeList::AT_no_instrument_function: // Interacts with -pg. |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3712 | handleNoInstrumentFunctionAttr(S, D, Attr); |
| Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3713 | break; |
| John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3714 | case AttributeList::AT_stdcall: |
| 3715 | case AttributeList::AT_cdecl: |
| 3716 | case AttributeList::AT_fastcall: |
| Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3717 | case AttributeList::AT_thiscall: |
| Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3718 | case AttributeList::AT_pascal: |
| Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3719 | case AttributeList::AT_pcs: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3720 | handleCallConvAttr(S, D, Attr); |
| John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3721 | break; |
| Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3722 | case AttributeList::AT_opencl_kernel_function: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3723 | handleOpenCLKernelAttr(S, D, Attr); |
| Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3724 | break; |
| Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3725 | case AttributeList::AT_uuid: |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3726 | handleUuidAttr(S, D, Attr); |
| Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3727 | break; |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 3728 | |
| 3729 | // Thread safety attributes: |
| 3730 | case AttributeList::AT_guarded_var: |
| 3731 | handleGuardedVarAttr(S, D, Attr); |
| 3732 | break; |
| 3733 | case AttributeList::AT_pt_guarded_var: |
| 3734 | handleGuardedVarAttr(S, D, Attr, /*pointer = */true); |
| 3735 | break; |
| 3736 | case AttributeList::AT_scoped_lockable: |
| 3737 | handleLockableAttr(S, D, Attr, /*scoped = */true); |
| 3738 | break; |
| Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 3739 | case AttributeList::AT_no_address_safety_analysis: |
| 3740 | handleNoAddressSafetyAttr(S, D, Attr); |
| 3741 | break; |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 3742 | case AttributeList::AT_no_thread_safety_analysis: |
| 3743 | handleNoThreadSafetyAttr(S, D, Attr); |
| 3744 | break; |
| 3745 | case AttributeList::AT_lockable: |
| 3746 | handleLockableAttr(S, D, Attr); |
| 3747 | break; |
| Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 3748 | case AttributeList::AT_guarded_by: |
| 3749 | handleGuardedByAttr(S, D, Attr); |
| 3750 | break; |
| 3751 | case AttributeList::AT_pt_guarded_by: |
| 3752 | handleGuardedByAttr(S, D, Attr, /*pointer = */true); |
| 3753 | break; |
| 3754 | case AttributeList::AT_exclusive_lock_function: |
| 3755 | handleLockFunAttr(S, D, Attr, /*exclusive = */true); |
| 3756 | break; |
| 3757 | case AttributeList::AT_exclusive_locks_required: |
| 3758 | handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true); |
| 3759 | break; |
| 3760 | case AttributeList::AT_exclusive_trylock_function: |
| 3761 | handleTrylockFunAttr(S, D, Attr, /*exclusive = */true); |
| 3762 | break; |
| 3763 | case AttributeList::AT_lock_returned: |
| 3764 | handleLockReturnedAttr(S, D, Attr); |
| 3765 | break; |
| 3766 | case AttributeList::AT_locks_excluded: |
| 3767 | handleLocksExcludedAttr(S, D, Attr); |
| 3768 | break; |
| 3769 | case AttributeList::AT_shared_lock_function: |
| 3770 | handleLockFunAttr(S, D, Attr); |
| 3771 | break; |
| 3772 | case AttributeList::AT_shared_locks_required: |
| 3773 | handleLocksRequiredAttr(S, D, Attr); |
| 3774 | break; |
| 3775 | case AttributeList::AT_shared_trylock_function: |
| 3776 | handleTrylockFunAttr(S, D, Attr); |
| 3777 | break; |
| 3778 | case AttributeList::AT_unlock_function: |
| 3779 | handleUnlockFunAttr(S, D, Attr); |
| 3780 | break; |
| 3781 | case AttributeList::AT_acquired_before: |
| 3782 | handleAcquireOrderAttr(S, D, Attr, /*before = */true); |
| 3783 | break; |
| 3784 | case AttributeList::AT_acquired_after: |
| 3785 | handleAcquireOrderAttr(S, D, Attr, /*before = */false); |
| 3786 | break; |
| Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 3787 | |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3788 | default: |
| Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3789 | // Ask target about the attribute. |
| 3790 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 3791 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
| Chandler Carruth | dd1bc0f | 2010-07-08 09:42:26 +0000 | [diff] [blame] | 3792 | S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) |
| 3793 | << Attr.getName(); |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3794 | break; |
| 3795 | } |
| 3796 | } |
| 3797 | |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3798 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 3799 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 3800 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 3801 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3802 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 3803 | const AttributeList &Attr, |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3804 | bool NonInheritable, bool Inheritable) { |
| 3805 | if (Attr.isInvalid()) |
| 3806 | return; |
| 3807 | |
| 3808 | if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) |
| 3809 | // FIXME: Try to deal with other __declspec attributes! |
| 3810 | return; |
| 3811 | |
| 3812 | if (NonInheritable) |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3813 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3814 | |
| 3815 | if (Inheritable) |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3816 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3819 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 3820 | /// attribute list to the specified decl, ignoring any type attributes. |
| Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3821 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3822 | const AttributeList *AttrList, |
| 3823 | bool NonInheritable, bool Inheritable) { |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3824 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3825 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable); |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | // GCC accepts |
| 3829 | // static int a9 __attribute__((weakref)); |
| 3830 | // but that looks really pointless. We reject it. |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3831 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3832 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
| Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 3833 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
| Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3834 | return; |
| Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3835 | } |
| 3836 | } |
| 3837 | |
| Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3838 | // Annotation attributes are the only attributes allowed after an access |
| 3839 | // specifier. |
| 3840 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 3841 | const AttributeList *AttrList) { |
| 3842 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| 3843 | if (l->getKind() == AttributeList::AT_annotate) { |
| 3844 | handleAnnotateAttr(*this, ASDecl, *l); |
| 3845 | } else { |
| 3846 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 3847 | return true; |
| 3848 | } |
| 3849 | } |
| 3850 | |
| 3851 | return false; |
| 3852 | } |
| 3853 | |
| John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3854 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 3855 | /// contains any decl attributes that we should warn about. |
| 3856 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 3857 | for ( ; A; A = A->getNext()) { |
| 3858 | // Only warn if the attribute is an unignored, non-type attribute. |
| 3859 | if (A->isUsedAsTypeAttr()) continue; |
| 3860 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 3861 | |
| 3862 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 3863 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 3864 | << A->getName() << A->getRange(); |
| 3865 | } else { |
| 3866 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 3867 | << A->getName() << A->getRange(); |
| 3868 | } |
| 3869 | } |
| 3870 | } |
| 3871 | |
| 3872 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 3873 | /// used to build a declaration, complain about any decl attributes |
| 3874 | /// which might be lying around on it. |
| 3875 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 3876 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 3877 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 3878 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 3879 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 3880 | } |
| 3881 | |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3882 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
| 3883 | /// #pragma weak needs a non-definition decl and source may not have one |
| Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 3884 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 3885 | SourceLocation Loc) { |
| Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 3886 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3887 | NamedDecl *NewD = 0; |
| 3888 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 3889 | FunctionDecl *NewFD; |
| 3890 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 3891 | // FIXME: Mangling? |
| 3892 | // FIXME: Is the qualifier info correct? |
| 3893 | // FIXME: Is the DeclContext correct? |
| 3894 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 3895 | Loc, Loc, DeclarationName(II), |
| 3896 | FD->getType(), FD->getTypeSourceInfo(), |
| 3897 | SC_None, SC_None, |
| 3898 | false/*isInlineSpecified*/, |
| 3899 | FD->hasPrototype(), |
| 3900 | false/*isConstexprSpecified*/); |
| 3901 | NewD = NewFD; |
| 3902 | |
| 3903 | if (FD->getQualifier()) |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3904 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
| Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 3905 | |
| 3906 | // Fake up parameter variables; they are declared as if this were |
| 3907 | // a typedef. |
| 3908 | QualType FDTy = FD->getType(); |
| 3909 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 3910 | SmallVector<ParmVarDecl*, 16> Params; |
| 3911 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 3912 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 3913 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 3914 | Param->setScopeInfo(0, Params.size()); |
| 3915 | Params.push_back(Param); |
| 3916 | } |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3917 | NewFD->setParams(Params); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3918 | } |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3919 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 3920 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3921 | VD->getInnerLocStart(), VD->getLocation(), II, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3922 | VD->getType(), VD->getTypeSourceInfo(), |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3923 | VD->getStorageClass(), |
| 3924 | VD->getStorageClassAsWritten()); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3925 | if (VD->getQualifier()) { |
| 3926 | VarDecl *NewVD = cast<VarDecl>(NewD); |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3927 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3928 | } |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3929 | } |
| 3930 | return NewD; |
| 3931 | } |
| 3932 | |
| 3933 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak |
| 3934 | /// applied to it, possibly with an alias. |
| Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 3935 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
| Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 3936 | if (W.getUsed()) return; // only do this once |
| 3937 | W.setUsed(true); |
| 3938 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 3939 | IdentifierInfo *NDId = ND->getIdentifier(); |
| Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 3940 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3941 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 3942 | NDId->getName())); |
| 3943 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
| Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 3944 | WeakTopLevelDecl.push_back(NewD); |
| 3945 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 3946 | // to insert Decl at TU scope, sorry. |
| 3947 | DeclContext *SavedContext = CurContext; |
| 3948 | CurContext = Context.getTranslationUnitDecl(); |
| 3949 | PushOnScopeChains(NewD, S); |
| 3950 | CurContext = SavedContext; |
| 3951 | } else { // just add weak to existing |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3952 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3953 | } |
| 3954 | } |
| 3955 | |
| Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3956 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 3957 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 3958 | /// 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] | 3959 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 3960 | bool NonInheritable, bool Inheritable) { |
| John McCall | 6fe0240 | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 3961 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 3962 | // have to do this. |
| Douglas Gregor | 1c4bfe5 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 3963 | if (Inheritable) { |
| 3964 | LoadExternalWeakUndeclaredIdentifiers(); |
| 3965 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 3966 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 3967 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 3968 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 3969 | = WeakUndeclaredIdentifiers.find(Id); |
| 3970 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 3971 | WeakInfo W = I->second; |
| 3972 | DeclApplyPragmaWeak(S, ND, W); |
| 3973 | WeakUndeclaredIdentifiers[Id] = W; |
| 3974 | } |
| John McCall | 6fe0240 | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 3975 | } |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3976 | } |
| 3977 | } |
| 3978 | } |
| 3979 | |
| Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3980 | // Apply decl attributes from the DeclSpec if present. |
| John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3981 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3982 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3983 | |
| Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3984 | // Walk the declarator structure, applying decl attributes that were in a type |
| 3985 | // position to the decl itself. This handles cases like: |
| 3986 | // int *__attr__(x)** D; |
| 3987 | // when X is a decl attribute. |
| 3988 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 3989 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3990 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
| Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3991 | |
| Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3992 | // Finally, apply any attributes on the decl itself. |
| 3993 | if (const AttributeList *Attrs = PD.getAttributes()) |
| Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3994 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
| Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3995 | } |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3996 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3997 | /// Is the given declaration allowed to use a forbidden type? |
| 3998 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 3999 | // Private ivars are always okay. Unfortunately, people don't |
| 4000 | // always properly make their ivars private, even in system headers. |
| 4001 | // Plus we need to make fields okay, too. |
| Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4002 | // Function declarations in sys headers will be marked unavailable. |
| 4003 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4004 | !isa<FunctionDecl>(decl)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4005 | return false; |
| 4006 | |
| 4007 | // Require it to be declared in a system header. |
| 4008 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4009 | } |
| 4010 | |
| 4011 | /// Handle a delayed forbidden-type diagnostic. |
| 4012 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4013 | Decl *decl) { |
| 4014 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 4015 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 4016 | "this system declaration uses an unsupported type")); |
| 4017 | return; |
| 4018 | } |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4019 | if (S.getLangOpts().ObjCAutoRefCount) |
| Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4020 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
| 4021 | // FIXME. we may want to supress diagnostics for all |
| 4022 | // kind of forbidden type messages on unavailable functions. |
| 4023 | if (FD->hasAttr<UnavailableAttr>() && |
| 4024 | diag.getForbiddenTypeDiagnostic() == |
| 4025 | diag::err_arc_array_param_no_ownership) { |
| 4026 | diag.Triggered = true; |
| 4027 | return; |
| 4028 | } |
| 4029 | } |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4030 | |
| 4031 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4032 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4033 | diag.Triggered = true; |
| 4034 | } |
| 4035 | |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4036 | // This duplicates a vector push_back but hides the need to know the |
| 4037 | // size of the type. |
| 4038 | void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) { |
| 4039 | assert(StackSize <= StackCapacity); |
| 4040 | |
| 4041 | // Grow the stack if necessary. |
| 4042 | if (StackSize == StackCapacity) { |
| 4043 | unsigned newCapacity = 2 * StackCapacity + 2; |
| 4044 | char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)]; |
| 4045 | const char *oldBuffer = (const char*) Stack; |
| 4046 | |
| 4047 | if (StackCapacity) |
| 4048 | memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic)); |
| 4049 | |
| 4050 | delete[] oldBuffer; |
| 4051 | Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer); |
| 4052 | StackCapacity = newCapacity; |
| 4053 | } |
| 4054 | |
| 4055 | assert(StackSize < StackCapacity); |
| 4056 | new (&Stack[StackSize++]) DelayedDiagnostic(diag); |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4059 | void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state, |
| 4060 | Decl *decl) { |
| 4061 | DelayedDiagnostics &DD = S.DelayedDiagnostics; |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4062 | |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4063 | // Check the invariants. |
| 4064 | assert(DD.StackSize >= state.SavedStackSize); |
| 4065 | assert(state.SavedStackSize >= DD.ActiveStackBase); |
| 4066 | assert(DD.ParsingDepth > 0); |
| 4067 | |
| 4068 | // Drop the parsing depth. |
| 4069 | DD.ParsingDepth--; |
| 4070 | |
| 4071 | // If there are no active diagnostics, we're done. |
| 4072 | if (DD.StackSize == DD.ActiveStackBase) |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4073 | return; |
| 4074 | |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4075 | // We only want to actually emit delayed diagnostics when we |
| 4076 | // successfully parsed a decl. |
| John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4077 | if (decl) { |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4078 | // We emit all the active diagnostics, not just those starting |
| 4079 | // 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] | 4080 | // decl spec and another for each declarator; in a decl group like: |
| 4081 | // deprecated_typedef foo, *bar, baz(); |
| 4082 | // only the declarator pops will be passed decls. This is correct; |
| 4083 | // we really do need to consider delayed diagnostics from the decl spec |
| 4084 | // for each of the different declarations. |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4085 | for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) { |
| 4086 | DelayedDiagnostic &diag = DD.Stack[i]; |
| 4087 | if (diag.Triggered) |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4088 | continue; |
| 4089 | |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4090 | switch (diag.Kind) { |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4091 | case DelayedDiagnostic::Deprecation: |
| John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4092 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 4093 | if (!decl->isInvalidDecl()) |
| 4094 | S.HandleDelayedDeprecationCheck(diag, decl); |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4095 | break; |
| 4096 | |
| 4097 | case DelayedDiagnostic::Access: |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4098 | S.HandleDelayedAccessCheck(diag, decl); |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4099 | break; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4100 | |
| 4101 | case DelayedDiagnostic::ForbiddenType: |
| 4102 | handleDelayedForbiddenType(S, diag, decl); |
| 4103 | break; |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4104 | } |
| 4105 | } |
| 4106 | } |
| 4107 | |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4108 | // Destroy all the delayed diagnostics we're about to pop off. |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4109 | for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i) |
| Douglas Gregor | 899b68f | 2011-03-23 15:13:44 +0000 | [diff] [blame] | 4110 | DD.Stack[i].Destroy(); |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4111 | |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4112 | DD.StackSize = state.SavedStackSize; |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | static bool isDeclDeprecated(Decl *D) { |
| 4116 | do { |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4117 | if (D->isDeprecated()) |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4118 | return true; |
| Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4119 | // A category implicitly has the availability of the interface. |
| 4120 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4121 | return CatD->getClassInterface()->isDeprecated(); |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4122 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4123 | return false; |
| 4124 | } |
| 4125 | |
| John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 4126 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4127 | Decl *Ctx) { |
| 4128 | if (isDeclDeprecated(Ctx)) |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4129 | return; |
| 4130 | |
| John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4131 | DD.Triggered = true; |
| Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 4132 | if (!DD.getDeprecationMessage().empty()) |
| Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 4133 | Diag(DD.Loc, diag::warn_deprecated_message) |
| Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 4134 | << DD.getDeprecationDecl()->getDeclName() |
| 4135 | << DD.getDeprecationMessage(); |
| Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4136 | else if (DD.getUnknownObjCClass()) { |
| 4137 | Diag(DD.Loc, diag::warn_deprecated_fwdclass_message) |
| 4138 | << DD.getDeprecationDecl()->getDeclName(); |
| 4139 | Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class); |
| 4140 | } |
| Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 4141 | else |
| 4142 | Diag(DD.Loc, diag::warn_deprecated) |
| Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 4143 | << DD.getDeprecationDecl()->getDeclName(); |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4146 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
| Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4147 | SourceLocation Loc, |
| Fariborz Jahanian | dbbdd2f | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 4148 | const ObjCInterfaceDecl *UnknownObjCClass) { |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4149 | // Delay if we're currently parsing a declaration. |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4150 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
| Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4151 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 4152 | UnknownObjCClass, |
| 4153 | Message)); |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4154 | return; |
| 4155 | } |
| 4156 | |
| 4157 | // Otherwise, don't warn if our current context is deprecated. |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 4158 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4159 | return; |
| Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 4160 | if (!Message.empty()) |
| Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 4161 | Diag(Loc, diag::warn_deprecated_message) << D->getDeclName() |
| 4162 | << Message; |
| Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4163 | else { |
| Peter Collingbourne | ed12ffb | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 4164 | if (!UnknownObjCClass) |
| Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4165 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| Fariborz Jahanian | dbbdd2f | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 4166 | else { |
| Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4167 | Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName(); |
| Fariborz Jahanian | dbbdd2f | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 4168 | Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4169 | } |
| Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4170 | } |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4171 | } |