blob: 2e344ff930637b62d138263d27a13263b4a10ed2 [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)
Stephen Hines651f13c2014-04-23 16:59:28 -0700125 RD->addAttr(AlignMac68kAttr::CreateImplicit(Context));
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000126 else
Stephen Hines651f13c2014-04-23 16:59:28 -0700127 RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context,
Sean Huntcf807c42010-08-18 23:23:40 +0000128 Alignment * 8));
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000129 }
Chris Lattner574aa402009-02-17 01:09:29 +0000130}
131
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000132void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700133 if (MSStructPragmaOn)
134 RD->addAttr(MsStructAttr::CreateImplicit(Context));
135
136 // FIXME: We should merge AddAlignmentAttributesForRecord with
137 // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes
138 // all active pragmas and applies them as attributes to class definitions.
139 if (VtorDispModeStack.back() != getLangOpts().VtorDispMode)
140 RD->addAttr(
141 MSVtorDispAttr::CreateImplicit(Context, VtorDispModeStack.back()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000142}
143
Daniel Dunbarea75a822010-05-27 00:04:40 +0000144void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
Eli Friedman9595c7e2012-10-04 02:36:51 +0000145 SourceLocation PragmaLoc) {
Daniel Dunbarea75a822010-05-27 00:04:40 +0000146 if (PackContext == 0)
147 PackContext = new PragmaPackStack();
148
149 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
150
Daniel Dunbarea75a822010-05-27 00:04:40 +0000151 switch (Kind) {
Daniel Dunbar638e7cf2010-05-27 18:42:09 +0000152 // For all targets we support native and natural are the same.
153 //
154 // FIXME: This is not true on Darwin/PPC.
155 case POAK_Native:
Daniel Dunbar450f7932010-05-28 19:43:33 +0000156 case POAK_Power:
Daniel Dunbard6b305d2010-05-28 20:08:00 +0000157 case POAK_Natural:
Daniel Dunbar450f7932010-05-28 19:43:33 +0000158 Context->push(0);
159 Context->setAlignment(0);
160 break;
161
Daniel Dunbar6f739142010-05-27 18:42:17 +0000162 // Note that '#pragma options align=packed' is not equivalent to attribute
163 // packed, it has a different precedence relative to attribute aligned.
164 case POAK_Packed:
165 Context->push(0);
166 Context->setAlignment(1);
167 break;
168
Daniel Dunbar613fd672010-05-27 00:35:16 +0000169 case POAK_Mac68k:
170 // Check if the target supports this.
171 if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
172 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
173 return;
Daniel Dunbar613fd672010-05-27 00:35:16 +0000174 }
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000175 Context->push(0);
176 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
Daniel Dunbar613fd672010-05-27 00:35:16 +0000177 break;
178
Eli Friedman9595c7e2012-10-04 02:36:51 +0000179 case POAK_Reset:
180 // Reset just pops the top of the stack, or resets the current alignment to
181 // default.
182 if (!Context->pop(0, /*IsReset=*/true)) {
183 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
184 << "stack empty";
185 }
Daniel Dunbarea75a822010-05-27 00:04:40 +0000186 break;
187 }
188}
189
Mike Stump1eb44332009-09-09 15:08:12 +0000190void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
Richard Trieuf81e5a92011-09-09 02:00:50 +0000191 Expr *alignment, SourceLocation PragmaLoc,
Chris Lattner5a0c3512009-02-17 00:57:29 +0000192 SourceLocation LParenLoc, SourceLocation RParenLoc) {
193 Expr *Alignment = static_cast<Expr *>(alignment);
194
195 // If specified then alignment must be a "small" power of two.
196 unsigned AlignmentVal = 0;
197 if (Alignment) {
198 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000200 // pack(0) is like pack(), which just works out since that is what
201 // we use 0 for in PackAttr.
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000202 if (Alignment->isTypeDependent() ||
203 Alignment->isValueDependent() ||
204 !Alignment->isIntegerConstantExpr(Val, Context) ||
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000205 !(Val == 0 || Val.isPowerOf2()) ||
Chris Lattner5a0c3512009-02-17 00:57:29 +0000206 Val.getZExtValue() > 16) {
207 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000208 return; // Ignore
209 }
210
211 AlignmentVal = (unsigned) Val.getZExtValue();
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Chris Lattner574aa402009-02-17 01:09:29 +0000214 if (PackContext == 0)
215 PackContext = new PragmaPackStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Chris Lattner574aa402009-02-17 01:09:29 +0000217 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Chris Lattner5a0c3512009-02-17 00:57:29 +0000219 switch (Kind) {
John McCallf312b1e2010-08-26 23:41:50 +0000220 case Sema::PPK_Default: // pack([n])
Chris Lattner574aa402009-02-17 01:09:29 +0000221 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000222 break;
223
John McCallf312b1e2010-08-26 23:41:50 +0000224 case Sema::PPK_Show: // pack(show)
Chris Lattner5a0c3512009-02-17 00:57:29 +0000225 // Show the current alignment, making sure to show the right value
226 // for the default.
Chris Lattner574aa402009-02-17 01:09:29 +0000227 AlignmentVal = Context->getAlignment();
Chris Lattner5a0c3512009-02-17 00:57:29 +0000228 // FIXME: This should come from the target.
229 if (AlignmentVal == 0)
230 AlignmentVal = 8;
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000231 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
232 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
233 else
234 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
Chris Lattner5a0c3512009-02-17 00:57:29 +0000235 break;
236
John McCallf312b1e2010-08-26 23:41:50 +0000237 case Sema::PPK_Push: // pack(push [, id] [, [n])
Chris Lattner574aa402009-02-17 01:09:29 +0000238 Context->push(Name);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000239 // Set the new alignment if specified.
240 if (Alignment)
Mike Stump1eb44332009-09-09 15:08:12 +0000241 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000242 break;
243
John McCallf312b1e2010-08-26 23:41:50 +0000244 case Sema::PPK_Pop: // pack(pop [, id] [, n])
Chris Lattner5a0c3512009-02-17 00:57:29 +0000245 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
246 // "#pragma pack(pop, identifier, n) is undefined"
247 if (Alignment && Name)
Mike Stump1eb44332009-09-09 15:08:12 +0000248 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
249
Chris Lattner5a0c3512009-02-17 00:57:29 +0000250 // Do the pop.
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000251 if (!Context->pop(Name, /*IsReset=*/false)) {
Chris Lattner5a0c3512009-02-17 00:57:29 +0000252 // If a name was specified then failure indicates the name
253 // wasn't found. Otherwise failure indicates the stack was
254 // empty.
Stephen Hines651f13c2014-04-23 16:59:28 -0700255 Diag(PragmaLoc, diag::warn_pragma_pop_failed)
256 << "pack" << (Name ? "no record matching name" : "stack empty");
Chris Lattner5a0c3512009-02-17 00:57:29 +0000257
258 // FIXME: Warn about popping named records as MSVC does.
259 } else {
260 // Pop succeeded, set the new alignment if specified.
261 if (Alignment)
Chris Lattner574aa402009-02-17 01:09:29 +0000262 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000263 }
264 break;
Chris Lattner5a0c3512009-02-17 00:57:29 +0000265 }
266}
267
Fariborz Jahanian62c92582011-04-25 18:49:15 +0000268void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
269 MSStructPragmaOn = (Kind == PMSST_ON);
270}
271
Robert Wilhelm30d23752013-08-10 13:29:01 +0000272void Sema::ActOnPragmaMSComment(PragmaMSCommentKind Kind, StringRef Arg) {
Reid Kleckner3190ca92013-05-08 13:44:39 +0000273 // FIXME: Serialize this.
274 switch (Kind) {
275 case PCK_Unknown:
276 llvm_unreachable("unexpected pragma comment kind");
277 case PCK_Linker:
278 Consumer.HandleLinkerOptionPragma(Arg);
279 return;
Aaron Ballman89735b92013-05-24 15:06:56 +0000280 case PCK_Lib:
Reid Kleckner3190ca92013-05-08 13:44:39 +0000281 Consumer.HandleDependentLibrary(Arg);
282 return;
Reid Kleckner3190ca92013-05-08 13:44:39 +0000283 case PCK_Compiler:
284 case PCK_ExeStr:
285 case PCK_User:
286 return; // We ignore all of these.
287 }
288 llvm_unreachable("invalid pragma comment kind");
289}
290
Robert Wilhelm30d23752013-08-10 13:29:01 +0000291void Sema::ActOnPragmaDetectMismatch(StringRef Name, StringRef Value) {
Aaron Ballmana7ff62f2013-06-04 02:07:14 +0000292 // FIXME: Serialize this.
293 Consumer.HandleDetectMismatch(Name, Value);
294}
295
Stephen Hines651f13c2014-04-23 16:59:28 -0700296void Sema::ActOnPragmaMSPointersToMembers(
297 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,
298 SourceLocation PragmaLoc) {
299 MSPointerToMemberRepresentationMethod = RepresentationMethod;
300 ImplicitMSInheritanceAttrLoc = PragmaLoc;
301}
302
303void Sema::ActOnPragmaMSVtorDisp(PragmaVtorDispKind Kind,
304 SourceLocation PragmaLoc,
305 MSVtorDispAttr::Mode Mode) {
306 switch (Kind) {
307 case PVDK_Set:
308 VtorDispModeStack.back() = Mode;
309 break;
310 case PVDK_Push:
311 VtorDispModeStack.push_back(Mode);
312 break;
313 case PVDK_Reset:
314 VtorDispModeStack.clear();
315 VtorDispModeStack.push_back(MSVtorDispAttr::Mode(LangOpts.VtorDispMode));
316 break;
317 case PVDK_Pop:
318 VtorDispModeStack.pop_back();
319 if (VtorDispModeStack.empty()) {
320 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
321 << "stack empty";
322 VtorDispModeStack.push_back(MSVtorDispAttr::Mode(LangOpts.VtorDispMode));
323 }
324 break;
325 }
326}
327
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000328void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
329 SourceLocation PragmaLoc) {
Ted Kremenek4726d032009-03-23 22:28:25 +0000330
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000331 IdentifierInfo *Name = IdTok.getIdentifierInfo();
332 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
333 LookupParsedName(Lookup, curScope, NULL, true);
Ted Kremenek4726d032009-03-23 22:28:25 +0000334
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000335 if (Lookup.empty()) {
336 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
337 << Name << SourceRange(IdTok.getLocation());
338 return;
Ted Kremenek4726d032009-03-23 22:28:25 +0000339 }
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000340
341 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
Argyrios Kyrtzidis2a5c45b2011-01-27 18:16:48 +0000342 if (!VD) {
343 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000344 << Name << SourceRange(IdTok.getLocation());
345 return;
346 }
347
348 // Warn if this was used before being marked unused.
349 if (VD->isUsed())
350 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
351
Stephen Hines651f13c2014-04-23 16:59:28 -0700352 VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation()));
Ted Kremenek4726d032009-03-23 22:28:25 +0000353}
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000354
John McCall8dfac0b2011-09-30 05:12:12 +0000355void Sema::AddCFAuditedAttribute(Decl *D) {
356 SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
357 if (!Loc.isValid()) return;
358
359 // Don't add a redundant or conflicting attribute.
360 if (D->hasAttr<CFAuditedTransferAttr>() ||
361 D->hasAttr<CFUnknownTransferAttr>())
362 return;
363
Stephen Hines651f13c2014-04-23 16:59:28 -0700364 D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc));
John McCall8dfac0b2011-09-30 05:12:12 +0000365}
366
John McCall90f14502010-12-10 02:59:44 +0000367typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
Stephen Hines651f13c2014-04-23 16:59:28 -0700368enum : unsigned { NoVisibility = ~0U };
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000369
370void Sema::AddPushedVisibilityAttribute(Decl *D) {
371 if (!VisContext)
372 return;
373
Rafael Espindola140aadf2012-12-25 07:31:49 +0000374 NamedDecl *ND = dyn_cast<NamedDecl>(D);
John McCalld4c3d662013-02-20 01:54:26 +0000375 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000376 return;
377
378 VisStack *Stack = static_cast<VisStack*>(VisContext);
John McCall90f14502010-12-10 02:59:44 +0000379 unsigned rawType = Stack->back().first;
380 if (rawType == NoVisibility) return;
381
382 VisibilityAttr::VisibilityType type
383 = (VisibilityAttr::VisibilityType) rawType;
Sean Huntcf807c42010-08-18 23:23:40 +0000384 SourceLocation loc = Stack->back().second;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000385
Stephen Hines651f13c2014-04-23 16:59:28 -0700386 D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000387}
388
389/// FreeVisContext - Deallocate and null out VisContext.
390void Sema::FreeVisContext() {
391 delete static_cast<VisStack*>(VisContext);
392 VisContext = 0;
393}
394
John McCall90f14502010-12-10 02:59:44 +0000395static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
John McCallea318642010-08-26 09:15:37 +0000396 // Put visibility on stack.
397 if (!S.VisContext)
398 S.VisContext = new VisStack;
399
400 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
401 Stack->push_back(std::make_pair(type, loc));
402}
403
Rafael Espindola3fc809d2012-01-21 05:43:40 +0000404void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000405 SourceLocation PragmaLoc) {
Rafael Espindola3fc809d2012-01-21 05:43:40 +0000406 if (VisType) {
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000407 // Compute visibility to use.
Aaron Ballmand0686072013-09-11 19:47:58 +0000408 VisibilityAttr::VisibilityType T;
409 if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
410 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000411 return;
412 }
Aaron Ballmand0686072013-09-11 19:47:58 +0000413 PushPragmaVisibility(*this, T, PragmaLoc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000414 } else {
Rafael Espindola20039ae2012-02-01 23:24:59 +0000415 PopPragmaVisibility(false, PragmaLoc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000416 }
417}
418
Peter Collingbourne321b8172011-02-14 01:42:35 +0000419void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
420 switch (OOS) {
421 case tok::OOS_ON:
422 FPFeatures.fp_contract = 1;
423 break;
424 case tok::OOS_OFF:
425 FPFeatures.fp_contract = 0;
426 break;
427 case tok::OOS_DEFAULT:
David Blaikie4e4d0842012-03-11 07:00:24 +0000428 FPFeatures.fp_contract = getLangOpts().DefaultFPContract;
Peter Collingbourne321b8172011-02-14 01:42:35 +0000429 break;
430 }
431}
432
Rafael Espindola20039ae2012-02-01 23:24:59 +0000433void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
434 SourceLocation Loc) {
John McCall90f14502010-12-10 02:59:44 +0000435 // Visibility calculations will consider the namespace's visibility.
436 // Here we just want to note that we're in a visibility context
437 // which overrides any enclosing #pragma context, but doesn't itself
438 // contribute visibility.
Rafael Espindola20039ae2012-02-01 23:24:59 +0000439 PushPragmaVisibility(*this, NoVisibility, Loc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000440}
441
Rafael Espindola20039ae2012-02-01 23:24:59 +0000442void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
443 if (!VisContext) {
444 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
445 return;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000446 }
Rafael Espindola20039ae2012-02-01 23:24:59 +0000447
448 // Pop visibility from stack
449 VisStack *Stack = static_cast<VisStack*>(VisContext);
450
451 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
452 bool StartsWithPragma = Back->first != NoVisibility;
453 if (StartsWithPragma && IsNamespaceEnd) {
454 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
455 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
456
457 // For better error recovery, eat all pushes inside the namespace.
458 do {
459 Stack->pop_back();
460 Back = &Stack->back();
461 StartsWithPragma = Back->first != NoVisibility;
462 } while (StartsWithPragma);
463 } else if (!StartsWithPragma && !IsNamespaceEnd) {
464 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
465 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
466 return;
467 }
468
469 Stack->pop_back();
470 // To simplify the implementation, never keep around an empty stack.
471 if (Stack->empty())
472 FreeVisContext();
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000473}