blob: c6e3cc88631669ba65cc5d08548344b08f67e696 [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
Reid Klecknere43f0fe2013-05-08 13:44:39 +000015#include "clang/AST/ASTConsumer.h"
Alexis Huntdcfba7b2010-08-18 23:23:40 +000016#include "clang/AST/Attr.h"
Chris Lattner2eccbc12009-02-17 00:57:29 +000017#include "clang/AST/Expr.h"
Daniel Dunbarbd606522010-05-27 00:35:16 +000018#include "clang/Basic/TargetInfo.h"
19#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Sema/Lookup.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000021#include "clang/Sema/SemaInternal.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
Denis Zobnin2290dac2016-04-29 11:27:00 +000028Sema::PragmaStackSentinelRAII::PragmaStackSentinelRAII(Sema &S,
29 StringRef SlotLabel,
30 bool ShouldAct)
31 : S(S), SlotLabel(SlotLabel), ShouldAct(ShouldAct) {
32 if (ShouldAct) {
33 S.VtorDispStack.SentinelAction(PSK_Push, SlotLabel);
34 S.DataSegStack.SentinelAction(PSK_Push, SlotLabel);
35 S.BSSSegStack.SentinelAction(PSK_Push, SlotLabel);
36 S.ConstSegStack.SentinelAction(PSK_Push, SlotLabel);
37 S.CodeSegStack.SentinelAction(PSK_Push, SlotLabel);
38 }
39}
40
41Sema::PragmaStackSentinelRAII::~PragmaStackSentinelRAII() {
42 if (ShouldAct) {
43 S.VtorDispStack.SentinelAction(PSK_Pop, SlotLabel);
44 S.DataSegStack.SentinelAction(PSK_Pop, SlotLabel);
45 S.BSSSegStack.SentinelAction(PSK_Pop, SlotLabel);
46 S.ConstSegStack.SentinelAction(PSK_Pop, SlotLabel);
47 S.CodeSegStack.SentinelAction(PSK_Pop, SlotLabel);
48 }
49}
Chris Lattner31180bb2009-02-17 01:09:29 +000050
Daniel Dunbar8804f2e2010-05-27 01:53:40 +000051void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
Denis Zobnin10c4f452016-04-29 18:17:40 +000052 // If there is no pack value, we don't need any attributes.
53 if (!PackStack.CurrentValue)
Daniel Dunbar8804f2e2010-05-27 01:53:40 +000054 return;
55
Daniel Dunbar8804f2e2010-05-27 01:53:40 +000056 // Otherwise, check to see if we need a max field alignment attribute.
Denis Zobnin10c4f452016-04-29 18:17:40 +000057 if (unsigned Alignment = PackStack.CurrentValue) {
58 if (Alignment == Sema::kMac68kAlignmentSentinel)
Aaron Ballman36a53502014-01-16 13:03:14 +000059 RD->addAttr(AlignMac68kAttr::CreateImplicit(Context));
Daniel Dunbar6da10982010-05-27 05:45:51 +000060 else
Aaron Ballman36a53502014-01-16 13:03:14 +000061 RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +000062 Alignment * 8));
Daniel Dunbar6da10982010-05-27 05:45:51 +000063 }
Chris Lattner31180bb2009-02-17 01:09:29 +000064}
65
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +000066void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +000067 if (MSStructPragmaOn)
David Majnemer8ab003a2015-02-02 19:30:52 +000068 RD->addAttr(MSStructAttr::CreateImplicit(Context));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +000069
70 // FIXME: We should merge AddAlignmentAttributesForRecord with
71 // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes
72 // all active pragmas and applies them as attributes to class definitions.
Denis Zobnin2290dac2016-04-29 11:27:00 +000073 if (VtorDispStack.CurrentValue != getLangOpts().VtorDispMode)
Reid Klecknerc0dca6d2014-02-12 23:50:26 +000074 RD->addAttr(
Denis Zobnin2290dac2016-04-29 11:27:00 +000075 MSVtorDispAttr::CreateImplicit(Context, VtorDispStack.CurrentValue));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +000076}
77
Daniel Dunbar69dac582010-05-27 00:04:40 +000078void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
Eli Friedman68be1642012-10-04 02:36:51 +000079 SourceLocation PragmaLoc) {
Denis Zobnin10c4f452016-04-29 18:17:40 +000080 PragmaMsStackAction Action = Sema::PSK_Reset;
Denis Zobnin3f287c22016-04-29 22:50:16 +000081 unsigned Alignment = 0;
Daniel Dunbar69dac582010-05-27 00:04:40 +000082 switch (Kind) {
Daniel Dunbar663e8092010-05-27 18:42:09 +000083 // For all targets we support native and natural are the same.
84 //
85 // FIXME: This is not true on Darwin/PPC.
86 case POAK_Native:
Daniel Dunbar5794c6f2010-05-28 19:43:33 +000087 case POAK_Power:
Daniel Dunbara6885662010-05-28 20:08:00 +000088 case POAK_Natural:
Denis Zobnin10c4f452016-04-29 18:17:40 +000089 Action = Sema::PSK_Push_Set;
90 Alignment = 0;
Daniel Dunbar5794c6f2010-05-28 19:43:33 +000091 break;
92
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +000093 // Note that '#pragma options align=packed' is not equivalent to attribute
94 // packed, it has a different precedence relative to attribute aligned.
95 case POAK_Packed:
Denis Zobnin10c4f452016-04-29 18:17:40 +000096 Action = Sema::PSK_Push_Set;
97 Alignment = 1;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +000098 break;
99
Daniel Dunbarbd606522010-05-27 00:35:16 +0000100 case POAK_Mac68k:
101 // Check if the target supports this.
Alp Tokerb6cc5922014-05-03 03:45:55 +0000102 if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) {
Daniel Dunbarbd606522010-05-27 00:35:16 +0000103 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
104 return;
Daniel Dunbarbd606522010-05-27 00:35:16 +0000105 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000106 Action = Sema::PSK_Push_Set;
107 Alignment = Sema::kMac68kAlignmentSentinel;
Daniel Dunbarbd606522010-05-27 00:35:16 +0000108 break;
109
Eli Friedman68be1642012-10-04 02:36:51 +0000110 case POAK_Reset:
111 // Reset just pops the top of the stack, or resets the current alignment to
112 // default.
Denis Zobnin10c4f452016-04-29 18:17:40 +0000113 Action = Sema::PSK_Pop;
114 if (PackStack.Stack.empty()) {
115 if (PackStack.CurrentValue) {
116 Action = Sema::PSK_Reset;
117 } else {
118 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
119 << "stack empty";
120 return;
121 }
Eli Friedman68be1642012-10-04 02:36:51 +0000122 }
Daniel Dunbar69dac582010-05-27 00:04:40 +0000123 break;
124 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000125
126 PackStack.Act(PragmaLoc, Action, StringRef(), Alignment);
Daniel Dunbar69dac582010-05-27 00:04:40 +0000127}
128
Denis Zobnin10c4f452016-04-29 18:17:40 +0000129void Sema::ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action,
130 StringRef SlotLabel, Expr *alignment) {
Chris Lattner2eccbc12009-02-17 00:57:29 +0000131 Expr *Alignment = static_cast<Expr *>(alignment);
132
133 // If specified then alignment must be a "small" power of two.
134 unsigned AlignmentVal = 0;
135 if (Alignment) {
136 llvm::APSInt Val;
Mike Stump11289f42009-09-09 15:08:12 +0000137
Daniel Dunbare03c6102009-03-06 20:45:54 +0000138 // pack(0) is like pack(), which just works out since that is what
139 // we use 0 for in PackAttr.
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000140 if (Alignment->isTypeDependent() ||
141 Alignment->isValueDependent() ||
142 !Alignment->isIntegerConstantExpr(Val, Context) ||
Daniel Dunbare03c6102009-03-06 20:45:54 +0000143 !(Val == 0 || Val.isPowerOf2()) ||
Chris Lattner2eccbc12009-02-17 00:57:29 +0000144 Val.getZExtValue() > 16) {
145 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000146 return; // Ignore
147 }
148
149 AlignmentVal = (unsigned) Val.getZExtValue();
150 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000151 if (Action == Sema::PSK_Show) {
Chris Lattner2eccbc12009-02-17 00:57:29 +0000152 // Show the current alignment, making sure to show the right value
153 // for the default.
Chris Lattner2eccbc12009-02-17 00:57:29 +0000154 // FIXME: This should come from the target.
Denis Zobnin10c4f452016-04-29 18:17:40 +0000155 AlignmentVal = PackStack.CurrentValue;
Chris Lattner2eccbc12009-02-17 00:57:29 +0000156 if (AlignmentVal == 0)
157 AlignmentVal = 8;
Denis Zobnin10c4f452016-04-29 18:17:40 +0000158 if (AlignmentVal == Sema::kMac68kAlignmentSentinel)
Daniel Dunbar6da10982010-05-27 05:45:51 +0000159 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
160 else
161 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
Chris Lattner2eccbc12009-02-17 00:57:29 +0000162 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000163 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
164 // "#pragma pack(pop, identifier, n) is undefined"
165 if (Action & Sema::PSK_Pop) {
166 if (Alignment && !SlotLabel.empty())
167 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
168 if (PackStack.Stack.empty())
169 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "pack" << "stack empty";
170 }
171
172 PackStack.Act(PragmaLoc, Action, SlotLabel, AlignmentVal);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000173}
174
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000175void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
176 MSStructPragmaOn = (Kind == PMSST_ON);
177}
178
Nico Weber66220292016-03-02 17:28:48 +0000179void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc,
180 PragmaMSCommentKind Kind, StringRef Arg) {
181 auto *PCD = PragmaCommentDecl::Create(
182 Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg);
183 Context.getTranslationUnitDecl()->addDecl(PCD);
184 Consumer.HandleTopLevelDecl(DeclGroupRef(PCD));
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000185}
186
Nico Webercbbaeb12016-03-02 19:28:54 +0000187void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name,
188 StringRef Value) {
189 auto *PDMD = PragmaDetectMismatchDecl::Create(
190 Context, Context.getTranslationUnitDecl(), Loc, Name, Value);
191 Context.getTranslationUnitDecl()->addDecl(PDMD);
192 Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD));
Aaron Ballman5d041be2013-06-04 02:07:14 +0000193}
194
David Majnemer4bb09802014-02-10 19:50:15 +0000195void Sema::ActOnPragmaMSPointersToMembers(
David Majnemer86c318f2014-02-11 21:05:00 +0000196 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,
David Majnemer4bb09802014-02-10 19:50:15 +0000197 SourceLocation PragmaLoc) {
198 MSPointerToMemberRepresentationMethod = RepresentationMethod;
199 ImplicitMSInheritanceAttrLoc = PragmaLoc;
200}
201
Denis Zobnin2290dac2016-04-29 11:27:00 +0000202void Sema::ActOnPragmaMSVtorDisp(PragmaMsStackAction Action,
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000203 SourceLocation PragmaLoc,
204 MSVtorDispAttr::Mode Mode) {
Denis Zobnin2290dac2016-04-29 11:27:00 +0000205 if (Action & PSK_Pop && VtorDispStack.Stack.empty())
206 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
207 << "stack empty";
208 VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000209}
210
Warren Huntc3b18962014-04-08 22:30:47 +0000211template<typename ValueType>
212void Sema::PragmaStack<ValueType>::Act(SourceLocation PragmaLocation,
213 PragmaMsStackAction Action,
214 llvm::StringRef StackSlotLabel,
215 ValueType Value) {
216 if (Action == PSK_Reset) {
Denis Zobnin2290dac2016-04-29 11:27:00 +0000217 CurrentValue = DefaultValue;
Alex Lorenz7d7e1e02017-03-31 15:36:21 +0000218 CurrentPragmaLocation = PragmaLocation;
Warren Huntc3b18962014-04-08 22:30:47 +0000219 return;
220 }
221 if (Action & PSK_Push)
222 Stack.push_back(Slot(StackSlotLabel, CurrentValue, CurrentPragmaLocation));
223 else if (Action & PSK_Pop) {
224 if (!StackSlotLabel.empty()) {
225 // If we've got a label, try to find it and jump there.
David Majnemerf7e36092016-06-23 00:15:04 +0000226 auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) {
227 return x.StackSlotLabel == StackSlotLabel;
228 });
Warren Huntc3b18962014-04-08 22:30:47 +0000229 // If we found the label so pop from there.
230 if (I != Stack.rend()) {
231 CurrentValue = I->Value;
232 CurrentPragmaLocation = I->PragmaLocation;
233 Stack.erase(std::prev(I.base()), Stack.end());
234 }
235 } else if (!Stack.empty()) {
236 // We don't have a label, just pop the last entry.
237 CurrentValue = Stack.back().Value;
238 CurrentPragmaLocation = Stack.back().PragmaLocation;
239 Stack.pop_back();
240 }
241 }
242 if (Action & PSK_Set) {
243 CurrentValue = Value;
244 CurrentPragmaLocation = PragmaLocation;
245 }
246}
247
Craig Topperbf3e3272014-08-30 16:55:52 +0000248bool Sema::UnifySection(StringRef SectionName,
Warren Huntc3b18962014-04-08 22:30:47 +0000249 int SectionFlags,
250 DeclaratorDecl *Decl) {
Hans Wennborg899ded92014-10-16 20:52:46 +0000251 auto Section = Context.SectionInfos.find(SectionName);
252 if (Section == Context.SectionInfos.end()) {
253 Context.SectionInfos[SectionName] =
254 ASTContext::SectionInfo(Decl, SourceLocation(), SectionFlags);
Warren Huntc3b18962014-04-08 22:30:47 +0000255 return false;
256 }
257 // A pre-declared section takes precedence w/o diagnostic.
258 if (Section->second.SectionFlags == SectionFlags ||
Hans Wennborg899ded92014-10-16 20:52:46 +0000259 !(Section->second.SectionFlags & ASTContext::PSF_Implicit))
Warren Huntc3b18962014-04-08 22:30:47 +0000260 return false;
261 auto OtherDecl = Section->second.Decl;
262 Diag(Decl->getLocation(), diag::err_section_conflict)
263 << Decl << OtherDecl;
264 Diag(OtherDecl->getLocation(), diag::note_declared_at)
265 << OtherDecl->getName();
266 if (auto A = Decl->getAttr<SectionAttr>())
267 if (A->isImplicit())
268 Diag(A->getLocation(), diag::note_pragma_entered_here);
269 if (auto A = OtherDecl->getAttr<SectionAttr>())
270 if (A->isImplicit())
271 Diag(A->getLocation(), diag::note_pragma_entered_here);
Ehsan Akhgari0b510602014-09-22 19:46:39 +0000272 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000273}
274
Craig Topperbf3e3272014-08-30 16:55:52 +0000275bool Sema::UnifySection(StringRef SectionName,
Warren Huntc3b18962014-04-08 22:30:47 +0000276 int SectionFlags,
277 SourceLocation PragmaSectionLocation) {
Hans Wennborg899ded92014-10-16 20:52:46 +0000278 auto Section = Context.SectionInfos.find(SectionName);
279 if (Section != Context.SectionInfos.end()) {
Warren Huntc3b18962014-04-08 22:30:47 +0000280 if (Section->second.SectionFlags == SectionFlags)
281 return false;
Hans Wennborg899ded92014-10-16 20:52:46 +0000282 if (!(Section->second.SectionFlags & ASTContext::PSF_Implicit)) {
Warren Huntc3b18962014-04-08 22:30:47 +0000283 Diag(PragmaSectionLocation, diag::err_section_conflict)
284 << "this" << "a prior #pragma section";
285 Diag(Section->second.PragmaSectionLocation,
286 diag::note_pragma_entered_here);
287 return true;
288 }
289 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000290 Context.SectionInfos[SectionName] =
291 ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags);
Warren Huntc3b18962014-04-08 22:30:47 +0000292 return false;
293}
294
295/// \brief Called on well formed \#pragma bss_seg().
296void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation,
297 PragmaMsStackAction Action,
298 llvm::StringRef StackSlotLabel,
299 StringLiteral *SegmentName,
300 llvm::StringRef PragmaName) {
301 PragmaStack<StringLiteral *> *Stack =
302 llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName)
303 .Case("data_seg", &DataSegStack)
304 .Case("bss_seg", &BSSSegStack)
305 .Case("const_seg", &ConstSegStack)
306 .Case("code_seg", &CodeSegStack);
307 if (Action & PSK_Pop && Stack->Stack.empty())
308 Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName
309 << "stack empty";
Reid Kleckner2a133222015-03-04 23:39:17 +0000310 if (SegmentName &&
311 !checkSectionName(SegmentName->getLocStart(), SegmentName->getString()))
312 return;
Warren Huntc3b18962014-04-08 22:30:47 +0000313 Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName);
314}
315
316/// \brief Called on well formed \#pragma bss_seg().
317void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation,
318 int SectionFlags, StringLiteral *SegmentName) {
319 UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation);
320}
321
Reid Kleckner1a711b12014-07-22 00:53:05 +0000322void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,
323 StringLiteral *SegmentName) {
324 // There's no stack to maintain, so we just have a current section. When we
325 // see the default section, reset our current section back to null so we stop
326 // tacking on unnecessary attributes.
327 CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName;
328 CurInitSegLoc = PragmaLocation;
329}
330
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000331void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
332 SourceLocation PragmaLoc) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000333
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000334 IdentifierInfo *Name = IdTok.getIdentifierInfo();
335 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
Craig Topperc3ec1492014-05-26 06:22:03 +0000336 LookupParsedName(Lookup, curScope, nullptr, true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000337
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000338 if (Lookup.empty()) {
339 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
340 << Name << SourceRange(IdTok.getLocation());
341 return;
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000342 }
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000343
344 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
Argyrios Kyrtzidisff115a22011-01-27 18:16:48 +0000345 if (!VD) {
346 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000347 << Name << SourceRange(IdTok.getLocation());
348 return;
349 }
350
351 // Warn if this was used before being marked unused.
352 if (VD->isUsed())
353 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
354
Aaron Ballman0bcd6c12016-03-09 16:48:08 +0000355 VD->addAttr(UnusedAttr::CreateImplicit(Context, UnusedAttr::GNU_unused,
356 IdTok.getLocation()));
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000357}
Eli Friedman570024a2010-08-05 06:57:20 +0000358
John McCall32f5fe12011-09-30 05:12:12 +0000359void Sema::AddCFAuditedAttribute(Decl *D) {
360 SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
361 if (!Loc.isValid()) return;
362
363 // Don't add a redundant or conflicting attribute.
364 if (D->hasAttr<CFAuditedTransferAttr>() ||
365 D->hasAttr<CFUnknownTransferAttr>())
366 return;
367
Aaron Ballman36a53502014-01-16 13:03:14 +0000368 D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc));
John McCall32f5fe12011-09-30 05:12:12 +0000369}
370
Dario Domizioli13a0a382014-05-23 12:13:25 +0000371void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) {
372 if(On)
373 OptimizeOffPragmaLocation = SourceLocation();
374 else
375 OptimizeOffPragmaLocation = PragmaLoc;
376}
377
378void Sema::AddRangeBasedOptnone(FunctionDecl *FD) {
379 // In the future, check other pragmas if they're implemented (e.g. pragma
380 // optimize 0 will probably map to this functionality too).
381 if(OptimizeOffPragmaLocation.isValid())
382 AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation);
383}
384
385void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD,
386 SourceLocation Loc) {
387 // Don't add a conflicting attribute. No diagnostic is needed.
388 if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>())
389 return;
390
391 // Add attributes only if required. Optnone requires noinline as well, but if
392 // either is already present then don't bother adding them.
393 if (!FD->hasAttr<OptimizeNoneAttr>())
394 FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc));
395 if (!FD->hasAttr<NoInlineAttr>())
396 FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc));
397}
398
John McCall2faf32c2010-12-10 02:59:44 +0000399typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
Alp Tokerceb95c42014-03-02 03:20:16 +0000400enum : unsigned { NoVisibility = ~0U };
Eli Friedman570024a2010-08-05 06:57:20 +0000401
402void Sema::AddPushedVisibilityAttribute(Decl *D) {
403 if (!VisContext)
404 return;
405
Rafael Espindola54606d52012-12-25 07:31:49 +0000406 NamedDecl *ND = dyn_cast<NamedDecl>(D);
John McCalld041a9b2013-02-20 01:54:26 +0000407 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
Eli Friedman570024a2010-08-05 06:57:20 +0000408 return;
409
410 VisStack *Stack = static_cast<VisStack*>(VisContext);
John McCall2faf32c2010-12-10 02:59:44 +0000411 unsigned rawType = Stack->back().first;
412 if (rawType == NoVisibility) return;
413
414 VisibilityAttr::VisibilityType type
415 = (VisibilityAttr::VisibilityType) rawType;
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000416 SourceLocation loc = Stack->back().second;
Eli Friedman570024a2010-08-05 06:57:20 +0000417
Aaron Ballman36a53502014-01-16 13:03:14 +0000418 D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
Eli Friedman570024a2010-08-05 06:57:20 +0000419}
420
421/// FreeVisContext - Deallocate and null out VisContext.
422void Sema::FreeVisContext() {
423 delete static_cast<VisStack*>(VisContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000424 VisContext = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000425}
426
John McCall2faf32c2010-12-10 02:59:44 +0000427static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
John McCallb1be5232010-08-26 09:15:37 +0000428 // Put visibility on stack.
429 if (!S.VisContext)
430 S.VisContext = new VisStack;
431
432 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
433 Stack->push_back(std::make_pair(type, loc));
434}
435
Rafael Espindolade15baf2012-01-21 05:43:40 +0000436void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
Eli Friedman570024a2010-08-05 06:57:20 +0000437 SourceLocation PragmaLoc) {
Rafael Espindolade15baf2012-01-21 05:43:40 +0000438 if (VisType) {
Eli Friedman570024a2010-08-05 06:57:20 +0000439 // Compute visibility to use.
Aaron Ballman682ee422013-09-11 19:47:58 +0000440 VisibilityAttr::VisibilityType T;
441 if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
442 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
Eli Friedman570024a2010-08-05 06:57:20 +0000443 return;
444 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000445 PushPragmaVisibility(*this, T, PragmaLoc);
Eli Friedman570024a2010-08-05 06:57:20 +0000446 } else {
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000447 PopPragmaVisibility(false, PragmaLoc);
Eli Friedman570024a2010-08-05 06:57:20 +0000448 }
449}
450
Adam Nemet60d32642017-04-04 21:18:36 +0000451void Sema::ActOnPragmaFPContract(LangOptions::FPContractModeKind FPC) {
452 switch (FPC) {
453 case LangOptions::FPC_On:
Adam Nemet049a31d2017-03-29 21:54:24 +0000454 FPFeatures.setAllowFPContractWithinStatement();
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000455 break;
Adam Nemet60d32642017-04-04 21:18:36 +0000456 case LangOptions::FPC_Fast:
457 FPFeatures.setAllowFPContractAcrossStatement();
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000458 break;
Adam Nemet60d32642017-04-04 21:18:36 +0000459 case LangOptions::FPC_Off:
460 FPFeatures.setDisallowFPContract();
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000461 break;
462 }
463}
464
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000465void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
466 SourceLocation Loc) {
John McCall2faf32c2010-12-10 02:59:44 +0000467 // Visibility calculations will consider the namespace's visibility.
468 // Here we just want to note that we're in a visibility context
469 // which overrides any enclosing #pragma context, but doesn't itself
470 // contribute visibility.
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000471 PushPragmaVisibility(*this, NoVisibility, Loc);
Eli Friedman570024a2010-08-05 06:57:20 +0000472}
473
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000474void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
475 if (!VisContext) {
476 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
477 return;
Eli Friedman570024a2010-08-05 06:57:20 +0000478 }
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000479
480 // Pop visibility from stack
481 VisStack *Stack = static_cast<VisStack*>(VisContext);
482
483 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
484 bool StartsWithPragma = Back->first != NoVisibility;
485 if (StartsWithPragma && IsNamespaceEnd) {
486 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
487 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
488
489 // For better error recovery, eat all pushes inside the namespace.
490 do {
491 Stack->pop_back();
492 Back = &Stack->back();
493 StartsWithPragma = Back->first != NoVisibility;
494 } while (StartsWithPragma);
495 } else if (!StartsWithPragma && !IsNamespaceEnd) {
496 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
497 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
498 return;
499 }
500
501 Stack->pop_back();
502 // To simplify the implementation, never keep around an empty stack.
503 if (Stack->empty())
504 FreeVisContext();
Eli Friedman570024a2010-08-05 06:57:20 +0000505}