blob: 3fbb11f2fde1c294b479b248d67e0671ad57cfdc [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;
Aaron Ballman89735b92013-05-24 15:06:56 +0000275 case PCK_Lib:
Reid Kleckner3190ca92013-05-08 13:44:39 +0000276 Consumer.HandleDependentLibrary(Arg);
277 return;
Reid Kleckner3190ca92013-05-08 13:44:39 +0000278 case PCK_Compiler:
279 case PCK_ExeStr:
280 case PCK_User:
281 return; // We ignore all of these.
282 }
283 llvm_unreachable("invalid pragma comment kind");
284}
285
Aaron Ballmana7ff62f2013-06-04 02:07:14 +0000286void Sema::ActOnPragmaDetectMismatch(llvm::StringRef Name,
287 llvm::StringRef Value) {
288 // FIXME: Serialize this.
289 Consumer.HandleDetectMismatch(Name, Value);
290}
291
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000292void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
293 SourceLocation PragmaLoc) {
Ted Kremenek4726d032009-03-23 22:28:25 +0000294
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000295 IdentifierInfo *Name = IdTok.getIdentifierInfo();
296 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
297 LookupParsedName(Lookup, curScope, NULL, true);
Ted Kremenek4726d032009-03-23 22:28:25 +0000298
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000299 if (Lookup.empty()) {
300 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
301 << Name << SourceRange(IdTok.getLocation());
302 return;
Ted Kremenek4726d032009-03-23 22:28:25 +0000303 }
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000304
305 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
Argyrios Kyrtzidis2a5c45b2011-01-27 18:16:48 +0000306 if (!VD) {
307 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000308 << Name << SourceRange(IdTok.getLocation());
309 return;
310 }
311
312 // Warn if this was used before being marked unused.
313 if (VD->isUsed())
314 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
315
316 VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context));
Ted Kremenek4726d032009-03-23 22:28:25 +0000317}
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000318
John McCall8dfac0b2011-09-30 05:12:12 +0000319void Sema::AddCFAuditedAttribute(Decl *D) {
320 SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
321 if (!Loc.isValid()) return;
322
323 // Don't add a redundant or conflicting attribute.
324 if (D->hasAttr<CFAuditedTransferAttr>() ||
325 D->hasAttr<CFUnknownTransferAttr>())
326 return;
327
328 D->addAttr(::new (Context) CFAuditedTransferAttr(Loc, Context));
329}
330
John McCall90f14502010-12-10 02:59:44 +0000331typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
332enum { NoVisibility = (unsigned) -1 };
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000333
334void Sema::AddPushedVisibilityAttribute(Decl *D) {
335 if (!VisContext)
336 return;
337
Rafael Espindola140aadf2012-12-25 07:31:49 +0000338 NamedDecl *ND = dyn_cast<NamedDecl>(D);
John McCalld4c3d662013-02-20 01:54:26 +0000339 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000340 return;
341
342 VisStack *Stack = static_cast<VisStack*>(VisContext);
John McCall90f14502010-12-10 02:59:44 +0000343 unsigned rawType = Stack->back().first;
344 if (rawType == NoVisibility) return;
345
346 VisibilityAttr::VisibilityType type
347 = (VisibilityAttr::VisibilityType) rawType;
Sean Huntcf807c42010-08-18 23:23:40 +0000348 SourceLocation loc = Stack->back().second;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000349
Sean Huntcf807c42010-08-18 23:23:40 +0000350 D->addAttr(::new (Context) VisibilityAttr(loc, Context, type));
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000351}
352
353/// FreeVisContext - Deallocate and null out VisContext.
354void Sema::FreeVisContext() {
355 delete static_cast<VisStack*>(VisContext);
356 VisContext = 0;
357}
358
John McCall90f14502010-12-10 02:59:44 +0000359static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
John McCallea318642010-08-26 09:15:37 +0000360 // Put visibility on stack.
361 if (!S.VisContext)
362 S.VisContext = new VisStack;
363
364 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
365 Stack->push_back(std::make_pair(type, loc));
366}
367
Rafael Espindola3fc809d2012-01-21 05:43:40 +0000368void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000369 SourceLocation PragmaLoc) {
Rafael Espindola3fc809d2012-01-21 05:43:40 +0000370 if (VisType) {
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000371 // Compute visibility to use.
Sean Huntcf807c42010-08-18 23:23:40 +0000372 VisibilityAttr::VisibilityType type;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000373 if (VisType->isStr("default"))
Sean Huntcf807c42010-08-18 23:23:40 +0000374 type = VisibilityAttr::Default;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000375 else if (VisType->isStr("hidden"))
Sean Huntcf807c42010-08-18 23:23:40 +0000376 type = VisibilityAttr::Hidden;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000377 else if (VisType->isStr("internal"))
Sean Huntcf807c42010-08-18 23:23:40 +0000378 type = VisibilityAttr::Hidden; // FIXME
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000379 else if (VisType->isStr("protected"))
Sean Huntcf807c42010-08-18 23:23:40 +0000380 type = VisibilityAttr::Protected;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000381 else {
382 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) <<
383 VisType->getName();
384 return;
385 }
John McCallea318642010-08-26 09:15:37 +0000386 PushPragmaVisibility(*this, type, PragmaLoc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000387 } else {
Rafael Espindola20039ae2012-02-01 23:24:59 +0000388 PopPragmaVisibility(false, PragmaLoc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000389 }
390}
391
Peter Collingbourne321b8172011-02-14 01:42:35 +0000392void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
393 switch (OOS) {
394 case tok::OOS_ON:
395 FPFeatures.fp_contract = 1;
396 break;
397 case tok::OOS_OFF:
398 FPFeatures.fp_contract = 0;
399 break;
400 case tok::OOS_DEFAULT:
David Blaikie4e4d0842012-03-11 07:00:24 +0000401 FPFeatures.fp_contract = getLangOpts().DefaultFPContract;
Peter Collingbourne321b8172011-02-14 01:42:35 +0000402 break;
403 }
404}
405
Rafael Espindola20039ae2012-02-01 23:24:59 +0000406void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
407 SourceLocation Loc) {
John McCall90f14502010-12-10 02:59:44 +0000408 // Visibility calculations will consider the namespace's visibility.
409 // Here we just want to note that we're in a visibility context
410 // which overrides any enclosing #pragma context, but doesn't itself
411 // contribute visibility.
Rafael Espindola20039ae2012-02-01 23:24:59 +0000412 PushPragmaVisibility(*this, NoVisibility, Loc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000413}
414
Rafael Espindola20039ae2012-02-01 23:24:59 +0000415void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
416 if (!VisContext) {
417 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
418 return;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000419 }
Rafael Espindola20039ae2012-02-01 23:24:59 +0000420
421 // Pop visibility from stack
422 VisStack *Stack = static_cast<VisStack*>(VisContext);
423
424 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
425 bool StartsWithPragma = Back->first != NoVisibility;
426 if (StartsWithPragma && IsNamespaceEnd) {
427 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
428 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
429
430 // For better error recovery, eat all pushes inside the namespace.
431 do {
432 Stack->pop_back();
433 Back = &Stack->back();
434 StartsWithPragma = Back->first != NoVisibility;
435 } while (StartsWithPragma);
436 } else if (!StartsWithPragma && !IsNamespaceEnd) {
437 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
438 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
439 return;
440 }
441
442 Stack->pop_back();
443 // To simplify the implementation, never keep around an empty stack.
444 if (Stack->empty())
445 FreeVisContext();
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000446}