Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1 | //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===// |
Chris Lattner | 5a0c351 | 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 | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for non-trivial attributes and |
| 11 | // pragmas. |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Lookup.h" |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 25 | // Pragma 'pack' and 'options align' |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | namespace { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 29 | struct PackStackEntry { |
Daniel Dunbar | c6082fe | 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 | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 34 | unsigned Alignment; |
| 35 | IdentifierInfo *Name; |
| 36 | }; |
| 37 | |
Chris Lattner | 574aa40 | 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 | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 41 | typedef std::vector<PackStackEntry> stack_ty; |
Chris Lattner | 574aa40 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
| 50 | public: |
Chris Lattner | 574aa40 | 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 | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 59 | PackStackEntry PSE = { Alignment, Name }; |
| 60 | Stack.push_back(PSE); |
Chris Lattner | 574aa40 | 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 | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 67 | bool pop(IdentifierInfo *Name, bool IsReset); |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 68 | }; |
| 69 | } // end anonymous namespace. |
| 70 | |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 71 | bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) { |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 72 | // If name is empty just pop top. |
| 73 | if (!Name) { |
Daniel Dunbar | ddc6ff6 | 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 | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 91 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 574aa40 | 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 | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 97 | if (Stack[i].Name == Name) { |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 98 | // Found it, pop up to and including this record. |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 99 | Alignment = Stack[i].Alignment; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 100 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 101 | return true; |
| 102 | } |
| 103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 574aa40 | 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); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 112 | PackContext = nullptr; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Daniel Dunbar | 9f21f89 | 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 | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 123 | if (unsigned Alignment = Stack->getAlignment()) { |
| 124 | if (Alignment == PackStackEntry::kMac68kAlignmentSentinel) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 125 | RD->addAttr(AlignMac68kAttr::CreateImplicit(Context)); |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 126 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 127 | RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 128 | Alignment * 8)); |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 132 | void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 133 | if (MSStructPragmaOn) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 134 | RD->addAttr(MSStructAttr::CreateImplicit(Context)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 135 | |
| 136 | // FIXME: We should merge AddAlignmentAttributesForRecord with |
| 137 | // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes |
| 138 | // all active pragmas and applies them as attributes to class definitions. |
| 139 | if (VtorDispModeStack.back() != getLangOpts().VtorDispMode) |
| 140 | RD->addAttr( |
| 141 | MSVtorDispAttr::CreateImplicit(Context, VtorDispModeStack.back())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 144 | void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 145 | SourceLocation PragmaLoc) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 146 | if (!PackContext) |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 147 | PackContext = new PragmaPackStack(); |
| 148 | |
| 149 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
| 150 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 151 | switch (Kind) { |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 152 | // For all targets we support native and natural are the same. |
| 153 | // |
| 154 | // FIXME: This is not true on Darwin/PPC. |
| 155 | case POAK_Native: |
Daniel Dunbar | 450f793 | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 156 | case POAK_Power: |
Daniel Dunbar | d6b305d | 2010-05-28 20:08:00 +0000 | [diff] [blame] | 157 | case POAK_Natural: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 158 | Context->push(nullptr); |
Daniel Dunbar | 450f793 | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 159 | Context->setAlignment(0); |
| 160 | break; |
| 161 | |
Daniel Dunbar | 6f73914 | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 162 | // Note that '#pragma options align=packed' is not equivalent to attribute |
| 163 | // packed, it has a different precedence relative to attribute aligned. |
| 164 | case POAK_Packed: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 165 | Context->push(nullptr); |
Daniel Dunbar | 6f73914 | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 166 | Context->setAlignment(1); |
| 167 | break; |
| 168 | |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 169 | case POAK_Mac68k: |
| 170 | // Check if the target supports this. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 171 | if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) { |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 172 | Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); |
| 173 | return; |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 174 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 175 | Context->push(nullptr); |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 176 | Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel); |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 177 | break; |
| 178 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 179 | case POAK_Reset: |
| 180 | // Reset just pops the top of the stack, or resets the current alignment to |
| 181 | // default. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 182 | if (!Context->pop(nullptr, /*IsReset=*/true)) { |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 183 | Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) |
| 184 | << "stack empty"; |
| 185 | } |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 191 | Expr *alignment, SourceLocation PragmaLoc, |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 192 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 193 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 194 | |
| 195 | // If specified then alignment must be a "small" power of two. |
| 196 | unsigned AlignmentVal = 0; |
| 197 | if (Alignment) { |
| 198 | llvm::APSInt Val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 200 | // pack(0) is like pack(), which just works out since that is what |
| 201 | // we use 0 for in PackAttr. |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 202 | if (Alignment->isTypeDependent() || |
| 203 | Alignment->isValueDependent() || |
| 204 | !Alignment->isIntegerConstantExpr(Val, Context) || |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 205 | !(Val == 0 || Val.isPowerOf2()) || |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 206 | Val.getZExtValue() > 16) { |
| 207 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 208 | return; // Ignore |
| 209 | } |
| 210 | |
| 211 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 212 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 214 | if (!PackContext) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 215 | PackContext = new PragmaPackStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 217 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 219 | switch (Kind) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 220 | case Sema::PPK_Default: // pack([n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 221 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 222 | break; |
| 223 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 224 | case Sema::PPK_Show: // pack(show) |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 225 | // Show the current alignment, making sure to show the right value |
| 226 | // for the default. |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 227 | AlignmentVal = Context->getAlignment(); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 228 | // FIXME: This should come from the target. |
| 229 | if (AlignmentVal == 0) |
| 230 | AlignmentVal = 8; |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 231 | if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel) |
| 232 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; |
| 233 | else |
| 234 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 235 | break; |
| 236 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 237 | case Sema::PPK_Push: // pack(push [, id] [, [n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 238 | Context->push(Name); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 239 | // Set the new alignment if specified. |
| 240 | if (Alignment) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 242 | break; |
| 243 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 244 | case Sema::PPK_Pop: // pack(pop [, id] [, n]) |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 245 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 246 | // "#pragma pack(pop, identifier, n) is undefined" |
| 247 | if (Alignment && Name) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 249 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 250 | // Do the pop. |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 251 | if (!Context->pop(Name, /*IsReset=*/false)) { |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 252 | // If a name was specified then failure indicates the name |
| 253 | // wasn't found. Otherwise failure indicates the stack was |
| 254 | // empty. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 255 | Diag(PragmaLoc, diag::warn_pragma_pop_failed) |
| 256 | << "pack" << (Name ? "no record matching name" : "stack empty"); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 257 | |
| 258 | // FIXME: Warn about popping named records as MSVC does. |
| 259 | } else { |
| 260 | // Pop succeeded, set the new alignment if specified. |
| 261 | if (Alignment) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 262 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 263 | } |
| 264 | break; |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Fariborz Jahanian | 62c9258 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 268 | void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) { |
| 269 | MSStructPragmaOn = (Kind == PMSST_ON); |
| 270 | } |
| 271 | |
Robert Wilhelm | 30d2375 | 2013-08-10 13:29:01 +0000 | [diff] [blame] | 272 | void Sema::ActOnPragmaMSComment(PragmaMSCommentKind Kind, StringRef Arg) { |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 273 | // FIXME: Serialize this. |
| 274 | switch (Kind) { |
| 275 | case PCK_Unknown: |
| 276 | llvm_unreachable("unexpected pragma comment kind"); |
| 277 | case PCK_Linker: |
| 278 | Consumer.HandleLinkerOptionPragma(Arg); |
| 279 | return; |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 280 | case PCK_Lib: |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 281 | Consumer.HandleDependentLibrary(Arg); |
| 282 | return; |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 283 | case PCK_Compiler: |
| 284 | case PCK_ExeStr: |
| 285 | case PCK_User: |
| 286 | return; // We ignore all of these. |
| 287 | } |
| 288 | llvm_unreachable("invalid pragma comment kind"); |
| 289 | } |
| 290 | |
Robert Wilhelm | 30d2375 | 2013-08-10 13:29:01 +0000 | [diff] [blame] | 291 | void Sema::ActOnPragmaDetectMismatch(StringRef Name, StringRef Value) { |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 292 | // FIXME: Serialize this. |
| 293 | Consumer.HandleDetectMismatch(Name, Value); |
| 294 | } |
| 295 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 296 | void Sema::ActOnPragmaMSPointersToMembers( |
| 297 | LangOptions::PragmaMSPointersToMembersKind RepresentationMethod, |
| 298 | SourceLocation PragmaLoc) { |
| 299 | MSPointerToMemberRepresentationMethod = RepresentationMethod; |
| 300 | ImplicitMSInheritanceAttrLoc = PragmaLoc; |
| 301 | } |
| 302 | |
| 303 | void Sema::ActOnPragmaMSVtorDisp(PragmaVtorDispKind Kind, |
| 304 | SourceLocation PragmaLoc, |
| 305 | MSVtorDispAttr::Mode Mode) { |
| 306 | switch (Kind) { |
| 307 | case PVDK_Set: |
| 308 | VtorDispModeStack.back() = Mode; |
| 309 | break; |
| 310 | case PVDK_Push: |
| 311 | VtorDispModeStack.push_back(Mode); |
| 312 | break; |
| 313 | case PVDK_Reset: |
| 314 | VtorDispModeStack.clear(); |
| 315 | VtorDispModeStack.push_back(MSVtorDispAttr::Mode(LangOpts.VtorDispMode)); |
| 316 | break; |
| 317 | case PVDK_Pop: |
| 318 | VtorDispModeStack.pop_back(); |
| 319 | if (VtorDispModeStack.empty()) { |
| 320 | Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp" |
| 321 | << "stack empty"; |
| 322 | VtorDispModeStack.push_back(MSVtorDispAttr::Mode(LangOpts.VtorDispMode)); |
| 323 | } |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 328 | template<typename ValueType> |
| 329 | void Sema::PragmaStack<ValueType>::Act(SourceLocation PragmaLocation, |
| 330 | PragmaMsStackAction Action, |
| 331 | llvm::StringRef StackSlotLabel, |
| 332 | ValueType Value) { |
| 333 | if (Action == PSK_Reset) { |
| 334 | CurrentValue = nullptr; |
| 335 | return; |
| 336 | } |
| 337 | if (Action & PSK_Push) |
| 338 | Stack.push_back(Slot(StackSlotLabel, CurrentValue, CurrentPragmaLocation)); |
| 339 | else if (Action & PSK_Pop) { |
| 340 | if (!StackSlotLabel.empty()) { |
| 341 | // If we've got a label, try to find it and jump there. |
| 342 | auto I = std::find_if(Stack.rbegin(), Stack.rend(), |
| 343 | [&](const Slot &x) { return x.StackSlotLabel == StackSlotLabel; }); |
| 344 | // If we found the label so pop from there. |
| 345 | if (I != Stack.rend()) { |
| 346 | CurrentValue = I->Value; |
| 347 | CurrentPragmaLocation = I->PragmaLocation; |
| 348 | Stack.erase(std::prev(I.base()), Stack.end()); |
| 349 | } |
| 350 | } else if (!Stack.empty()) { |
| 351 | // We don't have a label, just pop the last entry. |
| 352 | CurrentValue = Stack.back().Value; |
| 353 | CurrentPragmaLocation = Stack.back().PragmaLocation; |
| 354 | Stack.pop_back(); |
| 355 | } |
| 356 | } |
| 357 | if (Action & PSK_Set) { |
| 358 | CurrentValue = Value; |
| 359 | CurrentPragmaLocation = PragmaLocation; |
| 360 | } |
| 361 | } |
| 362 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 363 | bool Sema::UnifySection(StringRef SectionName, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 364 | int SectionFlags, |
| 365 | DeclaratorDecl *Decl) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 366 | auto Section = Context.SectionInfos.find(SectionName); |
| 367 | if (Section == Context.SectionInfos.end()) { |
| 368 | Context.SectionInfos[SectionName] = |
| 369 | ASTContext::SectionInfo(Decl, SourceLocation(), SectionFlags); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | // A pre-declared section takes precedence w/o diagnostic. |
| 373 | if (Section->second.SectionFlags == SectionFlags || |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 374 | !(Section->second.SectionFlags & ASTContext::PSF_Implicit)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 375 | return false; |
| 376 | auto OtherDecl = Section->second.Decl; |
| 377 | Diag(Decl->getLocation(), diag::err_section_conflict) |
| 378 | << Decl << OtherDecl; |
| 379 | Diag(OtherDecl->getLocation(), diag::note_declared_at) |
| 380 | << OtherDecl->getName(); |
| 381 | if (auto A = Decl->getAttr<SectionAttr>()) |
| 382 | if (A->isImplicit()) |
| 383 | Diag(A->getLocation(), diag::note_pragma_entered_here); |
| 384 | if (auto A = OtherDecl->getAttr<SectionAttr>()) |
| 385 | if (A->isImplicit()) |
| 386 | Diag(A->getLocation(), diag::note_pragma_entered_here); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 387 | return true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 390 | bool Sema::UnifySection(StringRef SectionName, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 391 | int SectionFlags, |
| 392 | SourceLocation PragmaSectionLocation) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 393 | auto Section = Context.SectionInfos.find(SectionName); |
| 394 | if (Section != Context.SectionInfos.end()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 395 | if (Section->second.SectionFlags == SectionFlags) |
| 396 | return false; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 397 | if (!(Section->second.SectionFlags & ASTContext::PSF_Implicit)) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 398 | Diag(PragmaSectionLocation, diag::err_section_conflict) |
| 399 | << "this" << "a prior #pragma section"; |
| 400 | Diag(Section->second.PragmaSectionLocation, |
| 401 | diag::note_pragma_entered_here); |
| 402 | return true; |
| 403 | } |
| 404 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 405 | Context.SectionInfos[SectionName] = |
| 406 | ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | |
| 410 | /// \brief Called on well formed \#pragma bss_seg(). |
| 411 | void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation, |
| 412 | PragmaMsStackAction Action, |
| 413 | llvm::StringRef StackSlotLabel, |
| 414 | StringLiteral *SegmentName, |
| 415 | llvm::StringRef PragmaName) { |
| 416 | PragmaStack<StringLiteral *> *Stack = |
| 417 | llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName) |
| 418 | .Case("data_seg", &DataSegStack) |
| 419 | .Case("bss_seg", &BSSSegStack) |
| 420 | .Case("const_seg", &ConstSegStack) |
| 421 | .Case("code_seg", &CodeSegStack); |
| 422 | if (Action & PSK_Pop && Stack->Stack.empty()) |
| 423 | Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName |
| 424 | << "stack empty"; |
| 425 | Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName); |
| 426 | } |
| 427 | |
| 428 | /// \brief Called on well formed \#pragma bss_seg(). |
| 429 | void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation, |
| 430 | int SectionFlags, StringLiteral *SegmentName) { |
| 431 | UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation); |
| 432 | } |
| 433 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 434 | void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation, |
| 435 | StringLiteral *SegmentName) { |
| 436 | // There's no stack to maintain, so we just have a current section. When we |
| 437 | // see the default section, reset our current section back to null so we stop |
| 438 | // tacking on unnecessary attributes. |
| 439 | CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName; |
| 440 | CurInitSegLoc = PragmaLocation; |
| 441 | } |
| 442 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 443 | void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope, |
| 444 | SourceLocation PragmaLoc) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 445 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 446 | IdentifierInfo *Name = IdTok.getIdentifierInfo(); |
| 447 | LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 448 | LookupParsedName(Lookup, curScope, nullptr, true); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 449 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 450 | if (Lookup.empty()) { |
| 451 | Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) |
| 452 | << Name << SourceRange(IdTok.getLocation()); |
| 453 | return; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 454 | } |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 455 | |
| 456 | VarDecl *VD = Lookup.getAsSingle<VarDecl>(); |
Argyrios Kyrtzidis | 2a5c45b | 2011-01-27 18:16:48 +0000 | [diff] [blame] | 457 | if (!VD) { |
| 458 | Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg) |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 459 | << Name << SourceRange(IdTok.getLocation()); |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | // Warn if this was used before being marked unused. |
| 464 | if (VD->isUsed()) |
| 465 | Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; |
| 466 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 467 | VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation())); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 468 | } |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 469 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 470 | void Sema::AddCFAuditedAttribute(Decl *D) { |
| 471 | SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc(); |
| 472 | if (!Loc.isValid()) return; |
| 473 | |
| 474 | // Don't add a redundant or conflicting attribute. |
| 475 | if (D->hasAttr<CFAuditedTransferAttr>() || |
| 476 | D->hasAttr<CFUnknownTransferAttr>()) |
| 477 | return; |
| 478 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 479 | D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc)); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 482 | void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) { |
| 483 | if(On) |
| 484 | OptimizeOffPragmaLocation = SourceLocation(); |
| 485 | else |
| 486 | OptimizeOffPragmaLocation = PragmaLoc; |
| 487 | } |
| 488 | |
| 489 | void Sema::AddRangeBasedOptnone(FunctionDecl *FD) { |
| 490 | // In the future, check other pragmas if they're implemented (e.g. pragma |
| 491 | // optimize 0 will probably map to this functionality too). |
| 492 | if(OptimizeOffPragmaLocation.isValid()) |
| 493 | AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation); |
| 494 | } |
| 495 | |
| 496 | void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD, |
| 497 | SourceLocation Loc) { |
| 498 | // Don't add a conflicting attribute. No diagnostic is needed. |
| 499 | if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>()) |
| 500 | return; |
| 501 | |
| 502 | // Add attributes only if required. Optnone requires noinline as well, but if |
| 503 | // either is already present then don't bother adding them. |
| 504 | if (!FD->hasAttr<OptimizeNoneAttr>()) |
| 505 | FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc)); |
| 506 | if (!FD->hasAttr<NoInlineAttr>()) |
| 507 | FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc)); |
| 508 | } |
| 509 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 510 | typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 511 | enum : unsigned { NoVisibility = ~0U }; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 512 | |
| 513 | void Sema::AddPushedVisibilityAttribute(Decl *D) { |
| 514 | if (!VisContext) |
| 515 | return; |
| 516 | |
Rafael Espindola | 140aadf | 2012-12-25 07:31:49 +0000 | [diff] [blame] | 517 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 518 | if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue)) |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 519 | return; |
| 520 | |
| 521 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 522 | unsigned rawType = Stack->back().first; |
| 523 | if (rawType == NoVisibility) return; |
| 524 | |
| 525 | VisibilityAttr::VisibilityType type |
| 526 | = (VisibilityAttr::VisibilityType) rawType; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 527 | SourceLocation loc = Stack->back().second; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 528 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 529 | D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc)); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /// FreeVisContext - Deallocate and null out VisContext. |
| 533 | void Sema::FreeVisContext() { |
| 534 | delete static_cast<VisStack*>(VisContext); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 535 | VisContext = nullptr; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 536 | } |
| 537 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 538 | static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) { |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 539 | // Put visibility on stack. |
| 540 | if (!S.VisContext) |
| 541 | S.VisContext = new VisStack; |
| 542 | |
| 543 | VisStack *Stack = static_cast<VisStack*>(S.VisContext); |
| 544 | Stack->push_back(std::make_pair(type, loc)); |
| 545 | } |
| 546 | |
Rafael Espindola | 3fc809d | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 547 | void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType, |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 548 | SourceLocation PragmaLoc) { |
Rafael Espindola | 3fc809d | 2012-01-21 05:43:40 +0000 | [diff] [blame] | 549 | if (VisType) { |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 550 | // Compute visibility to use. |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 551 | VisibilityAttr::VisibilityType T; |
| 552 | if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) { |
| 553 | Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 554 | return; |
| 555 | } |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 556 | PushPragmaVisibility(*this, T, PragmaLoc); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 557 | } else { |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 558 | PopPragmaVisibility(false, PragmaLoc); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Peter Collingbourne | 321b817 | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 562 | void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) { |
| 563 | switch (OOS) { |
| 564 | case tok::OOS_ON: |
| 565 | FPFeatures.fp_contract = 1; |
| 566 | break; |
| 567 | case tok::OOS_OFF: |
| 568 | FPFeatures.fp_contract = 0; |
| 569 | break; |
| 570 | case tok::OOS_DEFAULT: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 571 | FPFeatures.fp_contract = getLangOpts().DefaultFPContract; |
Peter Collingbourne | 321b817 | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 572 | break; |
| 573 | } |
| 574 | } |
| 575 | |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 576 | void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr, |
| 577 | SourceLocation Loc) { |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 578 | // Visibility calculations will consider the namespace's visibility. |
| 579 | // Here we just want to note that we're in a visibility context |
| 580 | // which overrides any enclosing #pragma context, but doesn't itself |
| 581 | // contribute visibility. |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 582 | PushPragmaVisibility(*this, NoVisibility, Loc); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 585 | void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) { |
| 586 | if (!VisContext) { |
| 587 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 588 | return; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 589 | } |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 590 | |
| 591 | // Pop visibility from stack |
| 592 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
| 593 | |
| 594 | const std::pair<unsigned, SourceLocation> *Back = &Stack->back(); |
| 595 | bool StartsWithPragma = Back->first != NoVisibility; |
| 596 | if (StartsWithPragma && IsNamespaceEnd) { |
| 597 | Diag(Back->second, diag::err_pragma_push_visibility_mismatch); |
| 598 | Diag(EndLoc, diag::note_surrounding_namespace_ends_here); |
| 599 | |
| 600 | // For better error recovery, eat all pushes inside the namespace. |
| 601 | do { |
| 602 | Stack->pop_back(); |
| 603 | Back = &Stack->back(); |
| 604 | StartsWithPragma = Back->first != NoVisibility; |
| 605 | } while (StartsWithPragma); |
| 606 | } else if (!StartsWithPragma && !IsNamespaceEnd) { |
| 607 | Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); |
| 608 | Diag(Back->second, diag::note_surrounding_namespace_starts_here); |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | Stack->pop_back(); |
| 613 | // To simplify the implementation, never keep around an empty stack. |
| 614 | if (Stack->empty()) |
| 615 | FreeVisContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 616 | } |