blob: bc19b382989a48882f1768a35dc55718a59ed8cc [file] [log] [blame]
Bill Wendlingad017fa2012-12-20 19:22:21 +00001//===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
Chris Lattner5a0c3512009-02-17 00:57:29 +00002//
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 Lattner574aa402009-02-17 01:09:29 +000010// This file implements semantic analysis for non-trivial attributes and
11// pragmas.
Chris Lattner5a0c3512009-02-17 00:57:29 +000012//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Reid Kleckner3190ca92013-05-08 13:44:39 +000016#include "clang/AST/ASTConsumer.h"
Sean Huntcf807c42010-08-18 23:23:40 +000017#include "clang/AST/Attr.h"
Chris Lattner5a0c3512009-02-17 00:57:29 +000018#include "clang/AST/Expr.h"
Daniel Dunbar613fd672010-05-27 00:35:16 +000019#include "clang/Basic/TargetInfo.h"
20#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Sema/Lookup.h"
Chris Lattner5a0c3512009-02-17 00:57:29 +000022using namespace clang;
23
Chris Lattner574aa402009-02-17 01:09:29 +000024//===----------------------------------------------------------------------===//
Daniel Dunbarea75a822010-05-27 00:04:40 +000025// Pragma 'pack' and 'options align'
Chris Lattner574aa402009-02-17 01:09:29 +000026//===----------------------------------------------------------------------===//
27
28namespace {
Daniel Dunbarae2232b2010-05-27 02:25:27 +000029 struct PackStackEntry {
Daniel Dunbarc6082fe2010-05-27 05:45:51 +000030 // We just use a sentinel to represent when the stack is set to mac68k
31 // alignment.
32 static const unsigned kMac68kAlignmentSentinel = ~0U;
33
Daniel Dunbarae2232b2010-05-27 02:25:27 +000034 unsigned Alignment;
35 IdentifierInfo *Name;
36 };
37
Chris Lattner574aa402009-02-17 01:09:29 +000038 /// PragmaPackStack - Simple class to wrap the stack used by #pragma
39 /// pack.
40 class PragmaPackStack {
Daniel Dunbarae2232b2010-05-27 02:25:27 +000041 typedef std::vector<PackStackEntry> stack_ty;
Chris Lattner574aa402009-02-17 01:09:29 +000042
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 Stump1eb44332009-09-09 15:08:12 +000049
50 public:
Chris Lattner574aa402009-02-17 01:09:29 +000051 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 Dunbarae2232b2010-05-27 02:25:27 +000059 PackStackEntry PSE = { Alignment, Name };
60 Stack.push_back(PSE);
Chris Lattner574aa402009-02-17 01:09:29 +000061 }
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 Dunbarddc6ff62010-07-16 04:54:16 +000067 bool pop(IdentifierInfo *Name, bool IsReset);
Chris Lattner574aa402009-02-17 01:09:29 +000068 };
69} // end anonymous namespace.
70
Daniel Dunbarddc6ff62010-07-16 04:54:16 +000071bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
Chris Lattner574aa402009-02-17 01:09:29 +000072 // If name is empty just pop top.
73 if (!Name) {
Daniel Dunbarddc6ff62010-07-16 04:54:16 +000074 // 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 Lattner574aa402009-02-17 01:09:29 +000091 return true;
Mike Stump1eb44332009-09-09 15:08:12 +000092 }
93
Chris Lattner574aa402009-02-17 01:09:29 +000094 // Otherwise, find the named record.
95 for (unsigned i = Stack.size(); i != 0; ) {
96 --i;
Daniel Dunbarae2232b2010-05-27 02:25:27 +000097 if (Stack[i].Name == Name) {
Chris Lattner574aa402009-02-17 01:09:29 +000098 // Found it, pop up to and including this record.
Daniel Dunbarae2232b2010-05-27 02:25:27 +000099 Alignment = Stack[i].Alignment;
Chris Lattner574aa402009-02-17 01:09:29 +0000100 Stack.erase(Stack.begin() + i, Stack.end());
101 return true;
102 }
103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Chris Lattner574aa402009-02-17 01:09:29 +0000105 return false;
106}
107
108
109/// FreePackedContext - Deallocate and null out PackContext.
110void Sema::FreePackedContext() {
111 delete static_cast<PragmaPackStack*>(PackContext);
112 PackContext = 0;
113}
114
Daniel Dunbar9f21f892010-05-27 01:53:40 +0000115void 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 Dunbarc6082fe2010-05-27 05:45:51 +0000123 if (unsigned Alignment = Stack->getAlignment()) {
124 if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
Sean Huntcf807c42010-08-18 23:23:40 +0000125 RD->addAttr(::new (Context) AlignMac68kAttr(SourceLocation(), Context));
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000126 else
Sean Huntcf807c42010-08-18 23:23:40 +0000127 RD->addAttr(::new (Context) MaxFieldAlignmentAttr(SourceLocation(),
128 Context,
129 Alignment * 8));
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000130 }
Chris Lattner574aa402009-02-17 01:09:29 +0000131}
132
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000133void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
134 if (!MSStructPragmaOn)
135 return;
136 RD->addAttr(::new (Context) MsStructAttr(SourceLocation(), Context));
137}
138
Daniel Dunbarea75a822010-05-27 00:04:40 +0000139void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
Eli Friedman9595c7e2012-10-04 02:36:51 +0000140 SourceLocation PragmaLoc) {
Daniel Dunbarea75a822010-05-27 00:04:40 +0000141 if (PackContext == 0)
142 PackContext = new PragmaPackStack();
143
144 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
145
Daniel Dunbarea75a822010-05-27 00:04:40 +0000146 switch (Kind) {
Daniel Dunbar638e7cf2010-05-27 18:42:09 +0000147 // For all targets we support native and natural are the same.
148 //
149 // FIXME: This is not true on Darwin/PPC.
150 case POAK_Native:
Daniel Dunbar450f7932010-05-28 19:43:33 +0000151 case POAK_Power:
Daniel Dunbard6b305d2010-05-28 20:08:00 +0000152 case POAK_Natural:
Daniel Dunbar450f7932010-05-28 19:43:33 +0000153 Context->push(0);
154 Context->setAlignment(0);
155 break;
156
Daniel Dunbar6f739142010-05-27 18:42:17 +0000157 // Note that '#pragma options align=packed' is not equivalent to attribute
158 // packed, it has a different precedence relative to attribute aligned.
159 case POAK_Packed:
160 Context->push(0);
161 Context->setAlignment(1);
162 break;
163
Daniel Dunbar613fd672010-05-27 00:35:16 +0000164 case POAK_Mac68k:
165 // Check if the target supports this.
166 if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
167 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
168 return;
Daniel Dunbar613fd672010-05-27 00:35:16 +0000169 }
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000170 Context->push(0);
171 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
Daniel Dunbar613fd672010-05-27 00:35:16 +0000172 break;
173
Eli Friedman9595c7e2012-10-04 02:36:51 +0000174 case POAK_Reset:
175 // Reset just pops the top of the stack, or resets the current alignment to
176 // default.
177 if (!Context->pop(0, /*IsReset=*/true)) {
178 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
179 << "stack empty";
180 }
Daniel Dunbarea75a822010-05-27 00:04:40 +0000181 break;
182 }
183}
184
Mike Stump1eb44332009-09-09 15:08:12 +0000185void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
Richard Trieuf81e5a92011-09-09 02:00:50 +0000186 Expr *alignment, SourceLocation PragmaLoc,
Chris Lattner5a0c3512009-02-17 00:57:29 +0000187 SourceLocation LParenLoc, SourceLocation RParenLoc) {
188 Expr *Alignment = static_cast<Expr *>(alignment);
189
190 // If specified then alignment must be a "small" power of two.
191 unsigned AlignmentVal = 0;
192 if (Alignment) {
193 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000195 // pack(0) is like pack(), which just works out since that is what
196 // we use 0 for in PackAttr.
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000197 if (Alignment->isTypeDependent() ||
198 Alignment->isValueDependent() ||
199 !Alignment->isIntegerConstantExpr(Val, Context) ||
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000200 !(Val == 0 || Val.isPowerOf2()) ||
Chris Lattner5a0c3512009-02-17 00:57:29 +0000201 Val.getZExtValue() > 16) {
202 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000203 return; // Ignore
204 }
205
206 AlignmentVal = (unsigned) Val.getZExtValue();
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattner574aa402009-02-17 01:09:29 +0000209 if (PackContext == 0)
210 PackContext = new PragmaPackStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattner574aa402009-02-17 01:09:29 +0000212 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Chris Lattner5a0c3512009-02-17 00:57:29 +0000214 switch (Kind) {
John McCallf312b1e2010-08-26 23:41:50 +0000215 case Sema::PPK_Default: // pack([n])
Chris Lattner574aa402009-02-17 01:09:29 +0000216 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000217 break;
218
John McCallf312b1e2010-08-26 23:41:50 +0000219 case Sema::PPK_Show: // pack(show)
Chris Lattner5a0c3512009-02-17 00:57:29 +0000220 // Show the current alignment, making sure to show the right value
221 // for the default.
Chris Lattner574aa402009-02-17 01:09:29 +0000222 AlignmentVal = Context->getAlignment();
Chris Lattner5a0c3512009-02-17 00:57:29 +0000223 // FIXME: This should come from the target.
224 if (AlignmentVal == 0)
225 AlignmentVal = 8;
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000226 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
227 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
228 else
229 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
Chris Lattner5a0c3512009-02-17 00:57:29 +0000230 break;
231
John McCallf312b1e2010-08-26 23:41:50 +0000232 case Sema::PPK_Push: // pack(push [, id] [, [n])
Chris Lattner574aa402009-02-17 01:09:29 +0000233 Context->push(Name);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000234 // Set the new alignment if specified.
235 if (Alignment)
Mike Stump1eb44332009-09-09 15:08:12 +0000236 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000237 break;
238
John McCallf312b1e2010-08-26 23:41:50 +0000239 case Sema::PPK_Pop: // pack(pop [, id] [, n])
Chris Lattner5a0c3512009-02-17 00:57:29 +0000240 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
241 // "#pragma pack(pop, identifier, n) is undefined"
242 if (Alignment && Name)
Mike Stump1eb44332009-09-09 15:08:12 +0000243 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
244
Chris Lattner5a0c3512009-02-17 00:57:29 +0000245 // Do the pop.
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000246 if (!Context->pop(Name, /*IsReset=*/false)) {
Chris Lattner5a0c3512009-02-17 00:57:29 +0000247 // If a name was specified then failure indicates the name
248 // wasn't found. Otherwise failure indicates the stack was
249 // empty.
250 Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
251 << (Name ? "no record matching name" : "stack empty");
252
253 // FIXME: Warn about popping named records as MSVC does.
254 } else {
255 // Pop succeeded, set the new alignment if specified.
256 if (Alignment)
Chris Lattner574aa402009-02-17 01:09:29 +0000257 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000258 }
259 break;
Chris Lattner5a0c3512009-02-17 00:57:29 +0000260 }
261}
262
Fariborz Jahanian62c92582011-04-25 18:49:15 +0000263void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
264 MSStructPragmaOn = (Kind == PMSST_ON);
265}
266
Reid Kleckner3190ca92013-05-08 13:44:39 +0000267void Sema::ActOnPragmaMSComment(PragmaMSCommentKind Kind, llvm::StringRef Arg) {
268 // FIXME: Serialize this.
269 switch (Kind) {
270 case PCK_Unknown:
271 llvm_unreachable("unexpected pragma comment kind");
272 case PCK_Linker:
273 Consumer.HandleLinkerOptionPragma(Arg);
274 return;
275 case PCK_Lib: {
276 Consumer.HandleDependentLibrary(Arg);
277 return;
278 }
279 case PCK_Compiler:
280 case PCK_ExeStr:
281 case PCK_User:
282 return; // We ignore all of these.
283 }
284 llvm_unreachable("invalid pragma comment kind");
285}
286
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000287void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
288 SourceLocation PragmaLoc) {
Ted Kremenek4726d032009-03-23 22:28:25 +0000289
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000290 IdentifierInfo *Name = IdTok.getIdentifierInfo();
291 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
292 LookupParsedName(Lookup, curScope, NULL, true);
Ted Kremenek4726d032009-03-23 22:28:25 +0000293
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000294 if (Lookup.empty()) {
295 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
296 << Name << SourceRange(IdTok.getLocation());
297 return;
Ted Kremenek4726d032009-03-23 22:28:25 +0000298 }
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000299
300 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
Argyrios Kyrtzidis2a5c45b2011-01-27 18:16:48 +0000301 if (!VD) {
302 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000303 << Name << SourceRange(IdTok.getLocation());
304 return;
305 }
306
307 // Warn if this was used before being marked unused.
308 if (VD->isUsed())
309 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
310
311 VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context));
Ted Kremenek4726d032009-03-23 22:28:25 +0000312}
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000313
John McCall8dfac0b2011-09-30 05:12:12 +0000314void Sema::AddCFAuditedAttribute(Decl *D) {
315 SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
316 if (!Loc.isValid()) return;
317
318 // Don't add a redundant or conflicting attribute.
319 if (D->hasAttr<CFAuditedTransferAttr>() ||
320 D->hasAttr<CFUnknownTransferAttr>())
321 return;
322
323 D->addAttr(::new (Context) CFAuditedTransferAttr(Loc, Context));
324}
325
John McCall90f14502010-12-10 02:59:44 +0000326typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
327enum { NoVisibility = (unsigned) -1 };
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000328
329void Sema::AddPushedVisibilityAttribute(Decl *D) {
330 if (!VisContext)
331 return;
332
Rafael Espindola140aadf2012-12-25 07:31:49 +0000333 NamedDecl *ND = dyn_cast<NamedDecl>(D);
John McCalld4c3d662013-02-20 01:54:26 +0000334 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000335 return;
336
337 VisStack *Stack = static_cast<VisStack*>(VisContext);
John McCall90f14502010-12-10 02:59:44 +0000338 unsigned rawType = Stack->back().first;
339 if (rawType == NoVisibility) return;
340
341 VisibilityAttr::VisibilityType type
342 = (VisibilityAttr::VisibilityType) rawType;
Sean Huntcf807c42010-08-18 23:23:40 +0000343 SourceLocation loc = Stack->back().second;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000344
Sean Huntcf807c42010-08-18 23:23:40 +0000345 D->addAttr(::new (Context) VisibilityAttr(loc, Context, type));
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000346}
347
348/// FreeVisContext - Deallocate and null out VisContext.
349void Sema::FreeVisContext() {
350 delete static_cast<VisStack*>(VisContext);
351 VisContext = 0;
352}
353
John McCall90f14502010-12-10 02:59:44 +0000354static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
John McCallea318642010-08-26 09:15:37 +0000355 // Put visibility on stack.
356 if (!S.VisContext)
357 S.VisContext = new VisStack;
358
359 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
360 Stack->push_back(std::make_pair(type, loc));
361}
362
Rafael Espindola3fc809d2012-01-21 05:43:40 +0000363void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000364 SourceLocation PragmaLoc) {
Rafael Espindola3fc809d2012-01-21 05:43:40 +0000365 if (VisType) {
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000366 // Compute visibility to use.
Sean Huntcf807c42010-08-18 23:23:40 +0000367 VisibilityAttr::VisibilityType type;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000368 if (VisType->isStr("default"))
Sean Huntcf807c42010-08-18 23:23:40 +0000369 type = VisibilityAttr::Default;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000370 else if (VisType->isStr("hidden"))
Sean Huntcf807c42010-08-18 23:23:40 +0000371 type = VisibilityAttr::Hidden;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000372 else if (VisType->isStr("internal"))
Sean Huntcf807c42010-08-18 23:23:40 +0000373 type = VisibilityAttr::Hidden; // FIXME
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000374 else if (VisType->isStr("protected"))
Sean Huntcf807c42010-08-18 23:23:40 +0000375 type = VisibilityAttr::Protected;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000376 else {
377 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) <<
378 VisType->getName();
379 return;
380 }
John McCallea318642010-08-26 09:15:37 +0000381 PushPragmaVisibility(*this, type, PragmaLoc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000382 } else {
Rafael Espindola20039ae2012-02-01 23:24:59 +0000383 PopPragmaVisibility(false, PragmaLoc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000384 }
385}
386
Peter Collingbourne321b8172011-02-14 01:42:35 +0000387void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
388 switch (OOS) {
389 case tok::OOS_ON:
390 FPFeatures.fp_contract = 1;
391 break;
392 case tok::OOS_OFF:
393 FPFeatures.fp_contract = 0;
394 break;
395 case tok::OOS_DEFAULT:
David Blaikie4e4d0842012-03-11 07:00:24 +0000396 FPFeatures.fp_contract = getLangOpts().DefaultFPContract;
Peter Collingbourne321b8172011-02-14 01:42:35 +0000397 break;
398 }
399}
400
Rafael Espindola20039ae2012-02-01 23:24:59 +0000401void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
402 SourceLocation Loc) {
John McCall90f14502010-12-10 02:59:44 +0000403 // Visibility calculations will consider the namespace's visibility.
404 // Here we just want to note that we're in a visibility context
405 // which overrides any enclosing #pragma context, but doesn't itself
406 // contribute visibility.
Rafael Espindola20039ae2012-02-01 23:24:59 +0000407 PushPragmaVisibility(*this, NoVisibility, Loc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000408}
409
Rafael Espindola20039ae2012-02-01 23:24:59 +0000410void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
411 if (!VisContext) {
412 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
413 return;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000414 }
Rafael Espindola20039ae2012-02-01 23:24:59 +0000415
416 // Pop visibility from stack
417 VisStack *Stack = static_cast<VisStack*>(VisContext);
418
419 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
420 bool StartsWithPragma = Back->first != NoVisibility;
421 if (StartsWithPragma && IsNamespaceEnd) {
422 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
423 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
424
425 // For better error recovery, eat all pushes inside the namespace.
426 do {
427 Stack->pop_back();
428 Back = &Stack->back();
429 StartsWithPragma = Back->first != NoVisibility;
430 } while (StartsWithPragma);
431 } else if (!StartsWithPragma && !IsNamespaceEnd) {
432 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
433 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
434 return;
435 }
436
437 Stack->pop_back();
438 // To simplify the implementation, never keep around an empty stack.
439 if (Stack->empty())
440 FreeVisContext();
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000441}