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