blob: 0214ba456c7e3899e189beaf0dab404f5b466476 [file] [log] [blame]
Bill Wendling44426052012-12-20 19:22:21 +00001//===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
Chris Lattner2eccbc12009-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 Lattner31180bb2009-02-17 01:09:29 +000010// This file implements semantic analysis for non-trivial attributes and
11// pragmas.
Chris Lattner2eccbc12009-02-17 00:57:29 +000012//
13//===----------------------------------------------------------------------===//
14
John McCall83024632010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Reid Klecknere43f0fe2013-05-08 13:44:39 +000016#include "clang/AST/ASTConsumer.h"
Alexis Huntdcfba7b2010-08-18 23:23:40 +000017#include "clang/AST/Attr.h"
Chris Lattner2eccbc12009-02-17 00:57:29 +000018#include "clang/AST/Expr.h"
Daniel Dunbarbd606522010-05-27 00:35:16 +000019#include "clang/Basic/TargetInfo.h"
20#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Sema/Lookup.h"
Chris Lattner2eccbc12009-02-17 00:57:29 +000022using namespace clang;
23
Chris Lattner31180bb2009-02-17 01:09:29 +000024//===----------------------------------------------------------------------===//
Daniel Dunbar69dac582010-05-27 00:04:40 +000025// Pragma 'pack' and 'options align'
Chris Lattner31180bb2009-02-17 01:09:29 +000026//===----------------------------------------------------------------------===//
27
28namespace {
Daniel Dunbar4dbe15d2010-05-27 02:25:27 +000029 struct PackStackEntry {
Daniel Dunbar6da10982010-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 Dunbar4dbe15d2010-05-27 02:25:27 +000034 unsigned Alignment;
35 IdentifierInfo *Name;
36 };
37
Chris Lattner31180bb2009-02-17 01:09:29 +000038 /// PragmaPackStack - Simple class to wrap the stack used by #pragma
39 /// pack.
40 class PragmaPackStack {
Daniel Dunbar4dbe15d2010-05-27 02:25:27 +000041 typedef std::vector<PackStackEntry> stack_ty;
Chris Lattner31180bb2009-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 Stump11289f42009-09-09 15:08:12 +000049
50 public:
Chris Lattner31180bb2009-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 Dunbar4dbe15d2010-05-27 02:25:27 +000059 PackStackEntry PSE = { Alignment, Name };
60 Stack.push_back(PSE);
Chris Lattner31180bb2009-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 Dunbar84336ba2010-07-16 04:54:16 +000067 bool pop(IdentifierInfo *Name, bool IsReset);
Chris Lattner31180bb2009-02-17 01:09:29 +000068 };
69} // end anonymous namespace.
70
Daniel Dunbar84336ba2010-07-16 04:54:16 +000071bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
Chris Lattner31180bb2009-02-17 01:09:29 +000072 // If name is empty just pop top.
73 if (!Name) {
Daniel Dunbar84336ba2010-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 Lattner31180bb2009-02-17 01:09:29 +000091 return true;
Mike Stump11289f42009-09-09 15:08:12 +000092 }
93
Chris Lattner31180bb2009-02-17 01:09:29 +000094 // Otherwise, find the named record.
95 for (unsigned i = Stack.size(); i != 0; ) {
96 --i;
Daniel Dunbar4dbe15d2010-05-27 02:25:27 +000097 if (Stack[i].Name == Name) {
Chris Lattner31180bb2009-02-17 01:09:29 +000098 // Found it, pop up to and including this record.
Daniel Dunbar4dbe15d2010-05-27 02:25:27 +000099 Alignment = Stack[i].Alignment;
Chris Lattner31180bb2009-02-17 01:09:29 +0000100 Stack.erase(Stack.begin() + i, Stack.end());
101 return true;
102 }
103 }
Mike Stump11289f42009-09-09 15:08:12 +0000104
Chris Lattner31180bb2009-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 Dunbar8804f2e2010-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 Dunbar6da10982010-05-27 05:45:51 +0000123 if (unsigned Alignment = Stack->getAlignment()) {
124 if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
Aaron Ballman36a53502014-01-16 13:03:14 +0000125 RD->addAttr(AlignMac68kAttr::CreateImplicit(Context));
Daniel Dunbar6da10982010-05-27 05:45:51 +0000126 else
Aaron Ballman36a53502014-01-16 13:03:14 +0000127 RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000128 Alignment * 8));
Daniel Dunbar6da10982010-05-27 05:45:51 +0000129 }
Chris Lattner31180bb2009-02-17 01:09:29 +0000130}
131
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000132void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
133 if (!MSStructPragmaOn)
134 return;
Aaron Ballman36a53502014-01-16 13:03:14 +0000135 RD->addAttr(MsStructAttr::CreateImplicit(Context));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000136}
137
Daniel Dunbar69dac582010-05-27 00:04:40 +0000138void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
Eli Friedman68be1642012-10-04 02:36:51 +0000139 SourceLocation PragmaLoc) {
Daniel Dunbar69dac582010-05-27 00:04:40 +0000140 if (PackContext == 0)
141 PackContext = new PragmaPackStack();
142
143 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
144
Daniel Dunbar69dac582010-05-27 00:04:40 +0000145 switch (Kind) {
Daniel Dunbar663e8092010-05-27 18:42:09 +0000146 // For all targets we support native and natural are the same.
147 //
148 // FIXME: This is not true on Darwin/PPC.
149 case POAK_Native:
Daniel Dunbar5794c6f2010-05-28 19:43:33 +0000150 case POAK_Power:
Daniel Dunbara6885662010-05-28 20:08:00 +0000151 case POAK_Natural:
Daniel Dunbar5794c6f2010-05-28 19:43:33 +0000152 Context->push(0);
153 Context->setAlignment(0);
154 break;
155
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +0000156 // Note that '#pragma options align=packed' is not equivalent to attribute
157 // packed, it has a different precedence relative to attribute aligned.
158 case POAK_Packed:
159 Context->push(0);
160 Context->setAlignment(1);
161 break;
162
Daniel Dunbarbd606522010-05-27 00:35:16 +0000163 case POAK_Mac68k:
164 // Check if the target supports this.
165 if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
166 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
167 return;
Daniel Dunbarbd606522010-05-27 00:35:16 +0000168 }
Daniel Dunbar6da10982010-05-27 05:45:51 +0000169 Context->push(0);
170 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
Daniel Dunbarbd606522010-05-27 00:35:16 +0000171 break;
172
Eli Friedman68be1642012-10-04 02:36:51 +0000173 case POAK_Reset:
174 // Reset just pops the top of the stack, or resets the current alignment to
175 // default.
176 if (!Context->pop(0, /*IsReset=*/true)) {
177 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
178 << "stack empty";
179 }
Daniel Dunbar69dac582010-05-27 00:04:40 +0000180 break;
181 }
182}
183
Mike Stump11289f42009-09-09 15:08:12 +0000184void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
Richard Trieu2bd04012011-09-09 02:00:50 +0000185 Expr *alignment, SourceLocation PragmaLoc,
Chris Lattner2eccbc12009-02-17 00:57:29 +0000186 SourceLocation LParenLoc, SourceLocation RParenLoc) {
187 Expr *Alignment = static_cast<Expr *>(alignment);
188
189 // If specified then alignment must be a "small" power of two.
190 unsigned AlignmentVal = 0;
191 if (Alignment) {
192 llvm::APSInt Val;
Mike Stump11289f42009-09-09 15:08:12 +0000193
Daniel Dunbare03c6102009-03-06 20:45:54 +0000194 // pack(0) is like pack(), which just works out since that is what
195 // we use 0 for in PackAttr.
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000196 if (Alignment->isTypeDependent() ||
197 Alignment->isValueDependent() ||
198 !Alignment->isIntegerConstantExpr(Val, Context) ||
Daniel Dunbare03c6102009-03-06 20:45:54 +0000199 !(Val == 0 || Val.isPowerOf2()) ||
Chris Lattner2eccbc12009-02-17 00:57:29 +0000200 Val.getZExtValue() > 16) {
201 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000202 return; // Ignore
203 }
204
205 AlignmentVal = (unsigned) Val.getZExtValue();
206 }
Mike Stump11289f42009-09-09 15:08:12 +0000207
Chris Lattner31180bb2009-02-17 01:09:29 +0000208 if (PackContext == 0)
209 PackContext = new PragmaPackStack();
Mike Stump11289f42009-09-09 15:08:12 +0000210
Chris Lattner31180bb2009-02-17 01:09:29 +0000211 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
Mike Stump11289f42009-09-09 15:08:12 +0000212
Chris Lattner2eccbc12009-02-17 00:57:29 +0000213 switch (Kind) {
John McCallfaf5fb42010-08-26 23:41:50 +0000214 case Sema::PPK_Default: // pack([n])
Chris Lattner31180bb2009-02-17 01:09:29 +0000215 Context->setAlignment(AlignmentVal);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000216 break;
217
John McCallfaf5fb42010-08-26 23:41:50 +0000218 case Sema::PPK_Show: // pack(show)
Chris Lattner2eccbc12009-02-17 00:57:29 +0000219 // Show the current alignment, making sure to show the right value
220 // for the default.
Chris Lattner31180bb2009-02-17 01:09:29 +0000221 AlignmentVal = Context->getAlignment();
Chris Lattner2eccbc12009-02-17 00:57:29 +0000222 // FIXME: This should come from the target.
223 if (AlignmentVal == 0)
224 AlignmentVal = 8;
Daniel Dunbar6da10982010-05-27 05:45:51 +0000225 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
226 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
227 else
228 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
Chris Lattner2eccbc12009-02-17 00:57:29 +0000229 break;
230
John McCallfaf5fb42010-08-26 23:41:50 +0000231 case Sema::PPK_Push: // pack(push [, id] [, [n])
Chris Lattner31180bb2009-02-17 01:09:29 +0000232 Context->push(Name);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000233 // Set the new alignment if specified.
234 if (Alignment)
Mike Stump11289f42009-09-09 15:08:12 +0000235 Context->setAlignment(AlignmentVal);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000236 break;
237
John McCallfaf5fb42010-08-26 23:41:50 +0000238 case Sema::PPK_Pop: // pack(pop [, id] [, n])
Chris Lattner2eccbc12009-02-17 00:57:29 +0000239 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
240 // "#pragma pack(pop, identifier, n) is undefined"
241 if (Alignment && Name)
Mike Stump11289f42009-09-09 15:08:12 +0000242 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
243
Chris Lattner2eccbc12009-02-17 00:57:29 +0000244 // Do the pop.
Daniel Dunbar84336ba2010-07-16 04:54:16 +0000245 if (!Context->pop(Name, /*IsReset=*/false)) {
Chris Lattner2eccbc12009-02-17 00:57:29 +0000246 // If a name was specified then failure indicates the name
247 // wasn't found. Otherwise failure indicates the stack was
248 // empty.
249 Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
250 << (Name ? "no record matching name" : "stack empty");
251
252 // FIXME: Warn about popping named records as MSVC does.
253 } else {
254 // Pop succeeded, set the new alignment if specified.
255 if (Alignment)
Chris Lattner31180bb2009-02-17 01:09:29 +0000256 Context->setAlignment(AlignmentVal);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000257 }
258 break;
Chris Lattner2eccbc12009-02-17 00:57:29 +0000259 }
260}
261
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000262void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
263 MSStructPragmaOn = (Kind == PMSST_ON);
264}
265
Robert Wilhelma65911a2013-08-10 13:29:01 +0000266void Sema::ActOnPragmaMSComment(PragmaMSCommentKind Kind, StringRef Arg) {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000267 // FIXME: Serialize this.
268 switch (Kind) {
269 case PCK_Unknown:
270 llvm_unreachable("unexpected pragma comment kind");
271 case PCK_Linker:
272 Consumer.HandleLinkerOptionPragma(Arg);
273 return;
Aaron Ballmanef50ee92013-05-24 15:06:56 +0000274 case PCK_Lib:
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000275 Consumer.HandleDependentLibrary(Arg);
276 return;
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000277 case PCK_Compiler:
278 case PCK_ExeStr:
279 case PCK_User:
280 return; // We ignore all of these.
281 }
282 llvm_unreachable("invalid pragma comment kind");
283}
284
Robert Wilhelma65911a2013-08-10 13:29:01 +0000285void Sema::ActOnPragmaDetectMismatch(StringRef Name, StringRef Value) {
Aaron Ballman5d041be2013-06-04 02:07:14 +0000286 // FIXME: Serialize this.
287 Consumer.HandleDetectMismatch(Name, Value);
288}
289
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000290void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
291 SourceLocation PragmaLoc) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000292
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000293 IdentifierInfo *Name = IdTok.getIdentifierInfo();
294 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
295 LookupParsedName(Lookup, curScope, NULL, true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000296
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000297 if (Lookup.empty()) {
298 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
299 << Name << SourceRange(IdTok.getLocation());
300 return;
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000301 }
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000302
303 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
Argyrios Kyrtzidisff115a22011-01-27 18:16:48 +0000304 if (!VD) {
305 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000306 << Name << SourceRange(IdTok.getLocation());
307 return;
308 }
309
310 // Warn if this was used before being marked unused.
311 if (VD->isUsed())
312 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
313
Aaron Ballman36a53502014-01-16 13:03:14 +0000314 VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation()));
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000315}
Eli Friedman570024a2010-08-05 06:57:20 +0000316
John McCall32f5fe12011-09-30 05:12:12 +0000317void Sema::AddCFAuditedAttribute(Decl *D) {
318 SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
319 if (!Loc.isValid()) return;
320
321 // Don't add a redundant or conflicting attribute.
322 if (D->hasAttr<CFAuditedTransferAttr>() ||
323 D->hasAttr<CFUnknownTransferAttr>())
324 return;
325
Aaron Ballman36a53502014-01-16 13:03:14 +0000326 D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc));
John McCall32f5fe12011-09-30 05:12:12 +0000327}
328
John McCall2faf32c2010-12-10 02:59:44 +0000329typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
330enum { NoVisibility = (unsigned) -1 };
Eli Friedman570024a2010-08-05 06:57:20 +0000331
332void Sema::AddPushedVisibilityAttribute(Decl *D) {
333 if (!VisContext)
334 return;
335
Rafael Espindola54606d52012-12-25 07:31:49 +0000336 NamedDecl *ND = dyn_cast<NamedDecl>(D);
John McCalld041a9b2013-02-20 01:54:26 +0000337 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
Eli Friedman570024a2010-08-05 06:57:20 +0000338 return;
339
340 VisStack *Stack = static_cast<VisStack*>(VisContext);
John McCall2faf32c2010-12-10 02:59:44 +0000341 unsigned rawType = Stack->back().first;
342 if (rawType == NoVisibility) return;
343
344 VisibilityAttr::VisibilityType type
345 = (VisibilityAttr::VisibilityType) rawType;
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000346 SourceLocation loc = Stack->back().second;
Eli Friedman570024a2010-08-05 06:57:20 +0000347
Aaron Ballman36a53502014-01-16 13:03:14 +0000348 D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
Eli Friedman570024a2010-08-05 06:57:20 +0000349}
350
351/// FreeVisContext - Deallocate and null out VisContext.
352void Sema::FreeVisContext() {
353 delete static_cast<VisStack*>(VisContext);
354 VisContext = 0;
355}
356
John McCall2faf32c2010-12-10 02:59:44 +0000357static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
John McCallb1be5232010-08-26 09:15:37 +0000358 // Put visibility on stack.
359 if (!S.VisContext)
360 S.VisContext = new VisStack;
361
362 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
363 Stack->push_back(std::make_pair(type, loc));
364}
365
Rafael Espindolade15baf2012-01-21 05:43:40 +0000366void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
Eli Friedman570024a2010-08-05 06:57:20 +0000367 SourceLocation PragmaLoc) {
Rafael Espindolade15baf2012-01-21 05:43:40 +0000368 if (VisType) {
Eli Friedman570024a2010-08-05 06:57:20 +0000369 // Compute visibility to use.
Aaron Ballman682ee422013-09-11 19:47:58 +0000370 VisibilityAttr::VisibilityType T;
371 if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
372 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
Eli Friedman570024a2010-08-05 06:57:20 +0000373 return;
374 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000375 PushPragmaVisibility(*this, T, PragmaLoc);
Eli Friedman570024a2010-08-05 06:57:20 +0000376 } else {
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000377 PopPragmaVisibility(false, PragmaLoc);
Eli Friedman570024a2010-08-05 06:57:20 +0000378 }
379}
380
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000381void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
382 switch (OOS) {
383 case tok::OOS_ON:
384 FPFeatures.fp_contract = 1;
385 break;
386 case tok::OOS_OFF:
387 FPFeatures.fp_contract = 0;
388 break;
389 case tok::OOS_DEFAULT:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000390 FPFeatures.fp_contract = getLangOpts().DefaultFPContract;
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000391 break;
392 }
393}
394
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000395void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
396 SourceLocation Loc) {
John McCall2faf32c2010-12-10 02:59:44 +0000397 // Visibility calculations will consider the namespace's visibility.
398 // Here we just want to note that we're in a visibility context
399 // which overrides any enclosing #pragma context, but doesn't itself
400 // contribute visibility.
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000401 PushPragmaVisibility(*this, NoVisibility, Loc);
Eli Friedman570024a2010-08-05 06:57:20 +0000402}
403
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000404void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
405 if (!VisContext) {
406 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
407 return;
Eli Friedman570024a2010-08-05 06:57:20 +0000408 }
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000409
410 // Pop visibility from stack
411 VisStack *Stack = static_cast<VisStack*>(VisContext);
412
413 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
414 bool StartsWithPragma = Back->first != NoVisibility;
415 if (StartsWithPragma && IsNamespaceEnd) {
416 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
417 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
418
419 // For better error recovery, eat all pushes inside the namespace.
420 do {
421 Stack->pop_back();
422 Back = &Stack->back();
423 StartsWithPragma = Back->first != NoVisibility;
424 } while (StartsWithPragma);
425 } else if (!StartsWithPragma && !IsNamespaceEnd) {
426 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
427 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
428 return;
429 }
430
431 Stack->pop_back();
432 // To simplify the implementation, never keep around an empty stack.
433 if (Stack->empty())
434 FreeVisContext();
Eli Friedman570024a2010-08-05 06:57:20 +0000435}