Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1 | //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===// |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for non-trivial attributes and |
| 11 | // pragmas. |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Lookup.h" |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 25 | // Pragma 'pack' and 'options align' |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | namespace { |
Daniel Dunbar | 4dbe15d | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 29 | struct PackStackEntry { |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 30 | // We just use a sentinel to represent when the stack is set to mac68k |
| 31 | // alignment. |
| 32 | static const unsigned kMac68kAlignmentSentinel = ~0U; |
| 33 | |
Daniel Dunbar | 4dbe15d | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 34 | unsigned Alignment; |
| 35 | IdentifierInfo *Name; |
| 36 | }; |
| 37 | |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 38 | /// PragmaPackStack - Simple class to wrap the stack used by #pragma |
| 39 | /// pack. |
| 40 | class PragmaPackStack { |
Daniel Dunbar | 4dbe15d | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 41 | typedef std::vector<PackStackEntry> stack_ty; |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 42 | |
| 43 | /// Alignment - The current user specified alignment. |
| 44 | unsigned Alignment; |
| 45 | |
| 46 | /// Stack - Entries in the #pragma pack stack, consisting of saved |
| 47 | /// alignments and optional names. |
| 48 | stack_ty Stack; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
| 50 | public: |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 51 | PragmaPackStack() : Alignment(0) {} |
| 52 | |
| 53 | void setAlignment(unsigned A) { Alignment = A; } |
| 54 | unsigned getAlignment() { return Alignment; } |
| 55 | |
| 56 | /// push - Push the current alignment onto the stack, optionally |
| 57 | /// using the given \arg Name for the record, if non-zero. |
| 58 | void push(IdentifierInfo *Name) { |
Daniel Dunbar | 4dbe15d | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 59 | PackStackEntry PSE = { Alignment, Name }; |
| 60 | Stack.push_back(PSE); |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /// pop - Pop a record from the stack and restore the current |
| 64 | /// alignment to the previous value. If \arg Name is non-zero then |
| 65 | /// the first such named record is popped, otherwise the top record |
| 66 | /// is popped. Returns true if the pop succeeded. |
Daniel Dunbar | 84336ba | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 67 | bool pop(IdentifierInfo *Name, bool IsReset); |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 68 | }; |
| 69 | } // end anonymous namespace. |
| 70 | |
Daniel Dunbar | 84336ba | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 71 | bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) { |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 72 | // If name is empty just pop top. |
| 73 | if (!Name) { |
Daniel Dunbar | 84336ba | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 74 | // An empty stack is a special case... |
| 75 | if (Stack.empty()) { |
| 76 | // If this isn't a reset, it is always an error. |
| 77 | if (!IsReset) |
| 78 | return false; |
| 79 | |
| 80 | // Otherwise, it is an error only if some alignment has been set. |
| 81 | if (!Alignment) |
| 82 | return false; |
| 83 | |
| 84 | // Otherwise, reset to the default alignment. |
| 85 | Alignment = 0; |
| 86 | } else { |
| 87 | Alignment = Stack.back().Alignment; |
| 88 | Stack.pop_back(); |
| 89 | } |
| 90 | |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 91 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 94 | // Otherwise, find the named record. |
| 95 | for (unsigned i = Stack.size(); i != 0; ) { |
| 96 | --i; |
Daniel Dunbar | 4dbe15d | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 97 | if (Stack[i].Name == Name) { |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 98 | // Found it, pop up to and including this record. |
Daniel Dunbar | 4dbe15d | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 99 | Alignment = Stack[i].Alignment; |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 100 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 101 | return true; |
| 102 | } |
| 103 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /// FreePackedContext - Deallocate and null out PackContext. |
| 110 | void Sema::FreePackedContext() { |
| 111 | delete static_cast<PragmaPackStack*>(PackContext); |
| 112 | PackContext = 0; |
| 113 | } |
| 114 | |
Daniel Dunbar | 8804f2e | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 115 | void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) { |
| 116 | // If there is no pack context, we don't need any attributes. |
| 117 | if (!PackContext) |
| 118 | return; |
| 119 | |
| 120 | PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext); |
| 121 | |
| 122 | // Otherwise, check to see if we need a max field alignment attribute. |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 123 | if (unsigned Alignment = Stack->getAlignment()) { |
| 124 | if (Alignment == PackStackEntry::kMac68kAlignmentSentinel) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 125 | RD->addAttr(AlignMac68kAttr::CreateImplicit(Context)); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 126 | else |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 127 | RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context, |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 128 | Alignment * 8)); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 132 | void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) { |
| 133 | if (!MSStructPragmaOn) |
| 134 | return; |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 135 | RD->addAttr(MsStructAttr::CreateImplicit(Context)); |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 138 | void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 139 | SourceLocation PragmaLoc) { |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 140 | if (PackContext == 0) |
| 141 | PackContext = new PragmaPackStack(); |
| 142 | |
| 143 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
| 144 | |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 145 | switch (Kind) { |
Daniel Dunbar | 663e809 | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 146 | // For all targets we support native and natural are the same. |
| 147 | // |
| 148 | // FIXME: This is not true on Darwin/PPC. |
| 149 | case POAK_Native: |
Daniel Dunbar | 5794c6f | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 150 | case POAK_Power: |
Daniel Dunbar | a688566 | 2010-05-28 20:08:00 +0000 | [diff] [blame] | 151 | case POAK_Natural: |
Daniel Dunbar | 5794c6f | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 152 | Context->push(0); |
| 153 | Context->setAlignment(0); |
| 154 | break; |
| 155 | |
Daniel Dunbar | 9c84d4a | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 156 | // Note that '#pragma options align=packed' is not equivalent to attribute |
| 157 | // packed, it has a different precedence relative to attribute aligned. |
| 158 | case POAK_Packed: |
| 159 | Context->push(0); |
| 160 | Context->setAlignment(1); |
| 161 | break; |
| 162 | |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 163 | case POAK_Mac68k: |
| 164 | // Check if the target supports this. |
| 165 | if (!PP.getTargetInfo().hasAlignMac68kSupport()) { |
| 166 | Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); |
| 167 | return; |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 168 | } |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 169 | Context->push(0); |
| 170 | Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel); |
Daniel Dunbar | bd60652 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 171 | break; |
| 172 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 173 | case POAK_Reset: |
| 174 | // Reset just pops the top of the stack, or resets the current alignment to |
| 175 | // default. |
| 176 | if (!Context->pop(0, /*IsReset=*/true)) { |
| 177 | Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) |
| 178 | << "stack empty"; |
| 179 | } |
Daniel Dunbar | 69dac58 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 185 | Expr *alignment, SourceLocation PragmaLoc, |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 186 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 187 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 188 | |
| 189 | // If specified then alignment must be a "small" power of two. |
| 190 | unsigned AlignmentVal = 0; |
| 191 | if (Alignment) { |
| 192 | llvm::APSInt Val; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Daniel Dunbar | e03c610 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 194 | // pack(0) is like pack(), which just works out since that is what |
| 195 | // we use 0 for in PackAttr. |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 196 | if (Alignment->isTypeDependent() || |
| 197 | Alignment->isValueDependent() || |
| 198 | !Alignment->isIntegerConstantExpr(Val, Context) || |
Daniel Dunbar | e03c610 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 199 | !(Val == 0 || Val.isPowerOf2()) || |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 200 | Val.getZExtValue() > 16) { |
| 201 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 202 | return; // Ignore |
| 203 | } |
| 204 | |
| 205 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 206 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 208 | if (PackContext == 0) |
| 209 | PackContext = new PragmaPackStack(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 211 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 213 | switch (Kind) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 214 | case Sema::PPK_Default: // pack([n]) |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 215 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 216 | break; |
| 217 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 218 | case Sema::PPK_Show: // pack(show) |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 219 | // Show the current alignment, making sure to show the right value |
| 220 | // for the default. |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 221 | AlignmentVal = Context->getAlignment(); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 222 | // FIXME: This should come from the target. |
| 223 | if (AlignmentVal == 0) |
| 224 | AlignmentVal = 8; |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 225 | if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel) |
| 226 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; |
| 227 | else |
| 228 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 229 | break; |
| 230 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 231 | case Sema::PPK_Push: // pack(push [, id] [, [n]) |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 232 | Context->push(Name); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 233 | // Set the new alignment if specified. |
| 234 | if (Alignment) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 236 | break; |
| 237 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 238 | case Sema::PPK_Pop: // pack(pop [, id] [, n]) |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 239 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 240 | // "#pragma pack(pop, identifier, n) is undefined" |
| 241 | if (Alignment && Name) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 243 | |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 244 | // Do the pop. |
Daniel Dunbar | 84336ba | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 245 | if (!Context->pop(Name, /*IsReset=*/false)) { |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 246 | // If a name was specified then failure indicates the name |
| 247 | // wasn't found. Otherwise failure indicates the stack was |
| 248 | // empty. |
| 249 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed) |
| 250 | << (Name ? "no record matching name" : "stack empty"); |
| 251 | |
| 252 | // FIXME: Warn about popping named records as MSVC does. |
| 253 | } else { |
| 254 | // Pop succeeded, set the new alignment if specified. |
| 255 | if (Alignment) |
Chris Lattner | 31180bb | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 256 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 257 | } |
| 258 | break; |
Chris Lattner | 2eccbc1 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 262 | void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) { |
| 263 | MSStructPragmaOn = (Kind == PMSST_ON); |
| 264 | } |
| 265 | |
Robert Wilhelm | a65911a | 2013-08-10 13:29:01 +0000 | [diff] [blame] | 266 | void Sema::ActOnPragmaMSComment(PragmaMSCommentKind Kind, StringRef Arg) { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 267 | // FIXME: Serialize this. |
| 268 | switch (Kind) { |
| 269 | case PCK_Unknown: |
| 270 | llvm_unreachable("unexpected pragma comment kind"); |
| 271 | case PCK_Linker: |
| 272 | Consumer.HandleLinkerOptionPragma(Arg); |
| 273 | return; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 274 | case PCK_Lib: |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 275 | Consumer.HandleDependentLibrary(Arg); |
| 276 | return; |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 277 | case PCK_Compiler: |
| 278 | case PCK_ExeStr: |
| 279 | case PCK_User: |
| 280 | return; // We ignore all of these. |
| 281 | } |
| 282 | llvm_unreachable("invalid pragma comment kind"); |
| 283 | } |
| 284 | |
Robert Wilhelm | a65911a | 2013-08-10 13:29:01 +0000 | [diff] [blame] | 285 | void Sema::ActOnPragmaDetectMismatch(StringRef Name, StringRef Value) { |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 286 | // FIXME: Serialize this. |
| 287 | Consumer.HandleDetectMismatch(Name, Value); |
| 288 | } |
| 289 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 290 | void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope, |
| 291 | SourceLocation PragmaLoc) { |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 292 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 293 | IdentifierInfo *Name = IdTok.getIdentifierInfo(); |
| 294 | LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName); |
| 295 | LookupParsedName(Lookup, curScope, NULL, true); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 296 | |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 297 | if (Lookup.empty()) { |
| 298 | Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) |
| 299 | << Name << SourceRange(IdTok.getLocation()); |
| 300 | return; |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 301 | } |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 302 | |
| 303 | VarDecl *VD = Lookup.getAsSingle<VarDecl>(); |
Argyrios Kyrtzidis | ff115a2 | 2011-01-27 18:16:48 +0000 | [diff] [blame] | 304 | if (!VD) { |
| 305 | Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg) |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 306 | << Name << SourceRange(IdTok.getLocation()); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | // Warn if this was used before being marked unused. |
| 311 | if (VD->isUsed()) |
| 312 | Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; |
| 313 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 314 | VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation())); |
Ted Kremenek | fd14fad | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 315 | } |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 316 | |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 317 | void Sema::AddCFAuditedAttribute(Decl *D) { |
| 318 | SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc(); |
| 319 | if (!Loc.isValid()) return; |
| 320 | |
| 321 | // Don't add a redundant or conflicting attribute. |
| 322 | if (D->hasAttr<CFAuditedTransferAttr>() || |
| 323 | D->hasAttr<CFUnknownTransferAttr>()) |
| 324 | return; |
| 325 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 326 | D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc)); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 327 | } |
| 328 | |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 329 | typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack; |
| 330 | enum { NoVisibility = (unsigned) -1 }; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 331 | |
| 332 | void Sema::AddPushedVisibilityAttribute(Decl *D) { |
| 333 | if (!VisContext) |
| 334 | return; |
| 335 | |
Rafael Espindola | 54606d5 | 2012-12-25 07:31:49 +0000 | [diff] [blame] | 336 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 337 | if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue)) |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 338 | return; |
| 339 | |
| 340 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 341 | unsigned rawType = Stack->back().first; |
| 342 | if (rawType == NoVisibility) return; |
| 343 | |
| 344 | VisibilityAttr::VisibilityType type |
| 345 | = (VisibilityAttr::VisibilityType) rawType; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 346 | SourceLocation loc = Stack->back().second; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 347 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 348 | D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc)); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /// FreeVisContext - Deallocate and null out VisContext. |
| 352 | void Sema::FreeVisContext() { |
| 353 | delete static_cast<VisStack*>(VisContext); |
| 354 | VisContext = 0; |
| 355 | } |
| 356 | |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 357 | static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) { |
John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 358 | // Put visibility on stack. |
| 359 | if (!S.VisContext) |
| 360 | S.VisContext = new VisStack; |
| 361 | |
| 362 | VisStack *Stack = static_cast<VisStack*>(S.VisContext); |
| 363 | Stack->push_back(std::make_pair(type, loc)); |
| 364 | } |
| 365 | |
Rafael Espindola | de15baf | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 366 | void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType, |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 367 | SourceLocation PragmaLoc) { |
Rafael Espindola | de15baf | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 368 | if (VisType) { |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 369 | // Compute visibility to use. |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 370 | VisibilityAttr::VisibilityType T; |
| 371 | if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) { |
| 372 | Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 373 | return; |
| 374 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 375 | PushPragmaVisibility(*this, T, PragmaLoc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 376 | } else { |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 377 | PopPragmaVisibility(false, PragmaLoc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 381 | void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) { |
| 382 | switch (OOS) { |
| 383 | case tok::OOS_ON: |
| 384 | FPFeatures.fp_contract = 1; |
| 385 | break; |
| 386 | case tok::OOS_OFF: |
| 387 | FPFeatures.fp_contract = 0; |
| 388 | break; |
| 389 | case tok::OOS_DEFAULT: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 390 | FPFeatures.fp_contract = getLangOpts().DefaultFPContract; |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | } |
| 394 | |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 395 | void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr, |
| 396 | SourceLocation Loc) { |
John McCall | 2faf32c | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 397 | // Visibility calculations will consider the namespace's visibility. |
| 398 | // Here we just want to note that we're in a visibility context |
| 399 | // which overrides any enclosing #pragma context, but doesn't itself |
| 400 | // contribute visibility. |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 401 | PushPragmaVisibility(*this, NoVisibility, Loc); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 404 | void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) { |
| 405 | if (!VisContext) { |
| 406 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 407 | return; |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 408 | } |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 409 | |
| 410 | // Pop visibility from stack |
| 411 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
| 412 | |
| 413 | const std::pair<unsigned, SourceLocation> *Back = &Stack->back(); |
| 414 | bool StartsWithPragma = Back->first != NoVisibility; |
| 415 | if (StartsWithPragma && IsNamespaceEnd) { |
| 416 | Diag(Back->second, diag::err_pragma_push_visibility_mismatch); |
| 417 | Diag(EndLoc, diag::note_surrounding_namespace_ends_here); |
| 418 | |
| 419 | // For better error recovery, eat all pushes inside the namespace. |
| 420 | do { |
| 421 | Stack->pop_back(); |
| 422 | Back = &Stack->back(); |
| 423 | StartsWithPragma = Back->first != NoVisibility; |
| 424 | } while (StartsWithPragma); |
| 425 | } else if (!StartsWithPragma && !IsNamespaceEnd) { |
| 426 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 427 | Diag(Back->second, diag::note_surrounding_namespace_starts_here); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | Stack->pop_back(); |
| 432 | // To simplify the implementation, never keep around an empty stack. |
| 433 | if (Stack->empty()) |
| 434 | FreeVisContext(); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 435 | } |