blob: e1929e3ec2d7f7fd0672315776c7bff3a3c88b0f [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner2eccbc12009-02-17 00:57:29 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattner31180bb2009-02-17 01:09:29 +00009// This file implements semantic analysis for non-trivial attributes and
10// pragmas.
Chris Lattner2eccbc12009-02-17 00:57:29 +000011//
12//===----------------------------------------------------------------------===//
13
Reid Klecknere43f0fe2013-05-08 13:44:39 +000014#include "clang/AST/ASTConsumer.h"
Alexis Huntdcfba7b2010-08-18 23:23:40 +000015#include "clang/AST/Attr.h"
Chris Lattner2eccbc12009-02-17 00:57:29 +000016#include "clang/AST/Expr.h"
Daniel Dunbarbd606522010-05-27 00:35:16 +000017#include "clang/Basic/TargetInfo.h"
18#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Sema/Lookup.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000020#include "clang/Sema/SemaInternal.h"
Chris Lattner2eccbc12009-02-17 00:57:29 +000021using namespace clang;
22
Chris Lattner31180bb2009-02-17 01:09:29 +000023//===----------------------------------------------------------------------===//
Daniel Dunbar69dac582010-05-27 00:04:40 +000024// Pragma 'pack' and 'options align'
Chris Lattner31180bb2009-02-17 01:09:29 +000025//===----------------------------------------------------------------------===//
26
Denis Zobnin2290dac2016-04-29 11:27:00 +000027Sema::PragmaStackSentinelRAII::PragmaStackSentinelRAII(Sema &S,
28 StringRef SlotLabel,
29 bool ShouldAct)
30 : S(S), SlotLabel(SlotLabel), ShouldAct(ShouldAct) {
31 if (ShouldAct) {
32 S.VtorDispStack.SentinelAction(PSK_Push, SlotLabel);
33 S.DataSegStack.SentinelAction(PSK_Push, SlotLabel);
34 S.BSSSegStack.SentinelAction(PSK_Push, SlotLabel);
35 S.ConstSegStack.SentinelAction(PSK_Push, SlotLabel);
36 S.CodeSegStack.SentinelAction(PSK_Push, SlotLabel);
37 }
38}
39
40Sema::PragmaStackSentinelRAII::~PragmaStackSentinelRAII() {
41 if (ShouldAct) {
42 S.VtorDispStack.SentinelAction(PSK_Pop, SlotLabel);
43 S.DataSegStack.SentinelAction(PSK_Pop, SlotLabel);
44 S.BSSSegStack.SentinelAction(PSK_Pop, SlotLabel);
45 S.ConstSegStack.SentinelAction(PSK_Pop, SlotLabel);
46 S.CodeSegStack.SentinelAction(PSK_Pop, SlotLabel);
47 }
48}
Chris Lattner31180bb2009-02-17 01:09:29 +000049
Daniel Dunbar8804f2e2010-05-27 01:53:40 +000050void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
Denis Zobnin10c4f452016-04-29 18:17:40 +000051 // If there is no pack value, we don't need any attributes.
52 if (!PackStack.CurrentValue)
Daniel Dunbar8804f2e2010-05-27 01:53:40 +000053 return;
54
Daniel Dunbar8804f2e2010-05-27 01:53:40 +000055 // Otherwise, check to see if we need a max field alignment attribute.
Denis Zobnin10c4f452016-04-29 18:17:40 +000056 if (unsigned Alignment = PackStack.CurrentValue) {
57 if (Alignment == Sema::kMac68kAlignmentSentinel)
Aaron Ballman36a53502014-01-16 13:03:14 +000058 RD->addAttr(AlignMac68kAttr::CreateImplicit(Context));
Daniel Dunbar6da10982010-05-27 05:45:51 +000059 else
Aaron Ballman36a53502014-01-16 13:03:14 +000060 RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +000061 Alignment * 8));
Daniel Dunbar6da10982010-05-27 05:45:51 +000062 }
Alex Lorenz45b40142017-07-28 14:41:21 +000063 if (PackIncludeStack.empty())
64 return;
65 // The #pragma pack affected a record in an included file, so Clang should
66 // warn when that pragma was written in a file that included the included
67 // file.
68 for (auto &PackedInclude : llvm::reverse(PackIncludeStack)) {
69 if (PackedInclude.CurrentPragmaLocation != PackStack.CurrentPragmaLocation)
70 break;
71 if (PackedInclude.HasNonDefaultValue)
72 PackedInclude.ShouldWarnOnInclude = true;
73 }
Chris Lattner31180bb2009-02-17 01:09:29 +000074}
75
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +000076void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +000077 if (MSStructPragmaOn)
David Majnemer8ab003a2015-02-02 19:30:52 +000078 RD->addAttr(MSStructAttr::CreateImplicit(Context));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +000079
80 // FIXME: We should merge AddAlignmentAttributesForRecord with
81 // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes
82 // all active pragmas and applies them as attributes to class definitions.
Denis Zobnin2290dac2016-04-29 11:27:00 +000083 if (VtorDispStack.CurrentValue != getLangOpts().VtorDispMode)
Reid Klecknerc0dca6d2014-02-12 23:50:26 +000084 RD->addAttr(
Denis Zobnin2290dac2016-04-29 11:27:00 +000085 MSVtorDispAttr::CreateImplicit(Context, VtorDispStack.CurrentValue));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +000086}
87
Daniel Dunbar69dac582010-05-27 00:04:40 +000088void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
Eli Friedman68be1642012-10-04 02:36:51 +000089 SourceLocation PragmaLoc) {
Denis Zobnin10c4f452016-04-29 18:17:40 +000090 PragmaMsStackAction Action = Sema::PSK_Reset;
Denis Zobnin3f287c22016-04-29 22:50:16 +000091 unsigned Alignment = 0;
Daniel Dunbar69dac582010-05-27 00:04:40 +000092 switch (Kind) {
Daniel Dunbar663e8092010-05-27 18:42:09 +000093 // For all targets we support native and natural are the same.
94 //
95 // FIXME: This is not true on Darwin/PPC.
96 case POAK_Native:
Daniel Dunbar5794c6f2010-05-28 19:43:33 +000097 case POAK_Power:
Daniel Dunbara6885662010-05-28 20:08:00 +000098 case POAK_Natural:
Denis Zobnin10c4f452016-04-29 18:17:40 +000099 Action = Sema::PSK_Push_Set;
100 Alignment = 0;
Daniel Dunbar5794c6f2010-05-28 19:43:33 +0000101 break;
102
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +0000103 // Note that '#pragma options align=packed' is not equivalent to attribute
104 // packed, it has a different precedence relative to attribute aligned.
105 case POAK_Packed:
Denis Zobnin10c4f452016-04-29 18:17:40 +0000106 Action = Sema::PSK_Push_Set;
107 Alignment = 1;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +0000108 break;
109
Daniel Dunbarbd606522010-05-27 00:35:16 +0000110 case POAK_Mac68k:
111 // Check if the target supports this.
Alp Tokerb6cc5922014-05-03 03:45:55 +0000112 if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) {
Daniel Dunbarbd606522010-05-27 00:35:16 +0000113 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
114 return;
Daniel Dunbarbd606522010-05-27 00:35:16 +0000115 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000116 Action = Sema::PSK_Push_Set;
117 Alignment = Sema::kMac68kAlignmentSentinel;
Daniel Dunbarbd606522010-05-27 00:35:16 +0000118 break;
119
Eli Friedman68be1642012-10-04 02:36:51 +0000120 case POAK_Reset:
121 // Reset just pops the top of the stack, or resets the current alignment to
122 // default.
Denis Zobnin10c4f452016-04-29 18:17:40 +0000123 Action = Sema::PSK_Pop;
124 if (PackStack.Stack.empty()) {
125 if (PackStack.CurrentValue) {
126 Action = Sema::PSK_Reset;
127 } else {
128 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
129 << "stack empty";
130 return;
131 }
Eli Friedman68be1642012-10-04 02:36:51 +0000132 }
Daniel Dunbar69dac582010-05-27 00:04:40 +0000133 break;
134 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000135
136 PackStack.Act(PragmaLoc, Action, StringRef(), Alignment);
Daniel Dunbar69dac582010-05-27 00:04:40 +0000137}
138
Javed Absar2a67c9e2017-06-05 10:11:57 +0000139void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc, PragmaClangSectionAction Action,
140 PragmaClangSectionKind SecKind, StringRef SecName) {
141 PragmaClangSection *CSec;
142 switch (SecKind) {
143 case PragmaClangSectionKind::PCSK_BSS:
144 CSec = &PragmaClangBSSSection;
145 break;
146 case PragmaClangSectionKind::PCSK_Data:
147 CSec = &PragmaClangDataSection;
148 break;
149 case PragmaClangSectionKind::PCSK_Rodata:
150 CSec = &PragmaClangRodataSection;
151 break;
152 case PragmaClangSectionKind::PCSK_Text:
153 CSec = &PragmaClangTextSection;
154 break;
155 default:
156 llvm_unreachable("invalid clang section kind");
157 }
158
159 if (Action == PragmaClangSectionAction::PCSA_Clear) {
160 CSec->Valid = false;
161 return;
162 }
163
164 CSec->Valid = true;
165 CSec->SectionName = SecName;
166 CSec->PragmaLocation = PragmaLoc;
167}
168
Denis Zobnin10c4f452016-04-29 18:17:40 +0000169void Sema::ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action,
170 StringRef SlotLabel, Expr *alignment) {
Chris Lattner2eccbc12009-02-17 00:57:29 +0000171 Expr *Alignment = static_cast<Expr *>(alignment);
172
173 // If specified then alignment must be a "small" power of two.
174 unsigned AlignmentVal = 0;
175 if (Alignment) {
176 llvm::APSInt Val;
Mike Stump11289f42009-09-09 15:08:12 +0000177
Daniel Dunbare03c6102009-03-06 20:45:54 +0000178 // pack(0) is like pack(), which just works out since that is what
179 // we use 0 for in PackAttr.
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000180 if (Alignment->isTypeDependent() ||
181 Alignment->isValueDependent() ||
182 !Alignment->isIntegerConstantExpr(Val, Context) ||
Daniel Dunbare03c6102009-03-06 20:45:54 +0000183 !(Val == 0 || Val.isPowerOf2()) ||
Chris Lattner2eccbc12009-02-17 00:57:29 +0000184 Val.getZExtValue() > 16) {
185 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000186 return; // Ignore
187 }
188
189 AlignmentVal = (unsigned) Val.getZExtValue();
190 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000191 if (Action == Sema::PSK_Show) {
Chris Lattner2eccbc12009-02-17 00:57:29 +0000192 // Show the current alignment, making sure to show the right value
193 // for the default.
Chris Lattner2eccbc12009-02-17 00:57:29 +0000194 // FIXME: This should come from the target.
Denis Zobnin10c4f452016-04-29 18:17:40 +0000195 AlignmentVal = PackStack.CurrentValue;
Chris Lattner2eccbc12009-02-17 00:57:29 +0000196 if (AlignmentVal == 0)
197 AlignmentVal = 8;
Denis Zobnin10c4f452016-04-29 18:17:40 +0000198 if (AlignmentVal == Sema::kMac68kAlignmentSentinel)
Daniel Dunbar6da10982010-05-27 05:45:51 +0000199 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
200 else
201 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
Chris Lattner2eccbc12009-02-17 00:57:29 +0000202 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000203 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
204 // "#pragma pack(pop, identifier, n) is undefined"
205 if (Action & Sema::PSK_Pop) {
206 if (Alignment && !SlotLabel.empty())
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000207 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifier_and_alignment);
Denis Zobnin10c4f452016-04-29 18:17:40 +0000208 if (PackStack.Stack.empty())
209 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "pack" << "stack empty";
210 }
211
212 PackStack.Act(PragmaLoc, Action, SlotLabel, AlignmentVal);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000213}
214
Alex Lorenz45b40142017-07-28 14:41:21 +0000215void Sema::DiagnoseNonDefaultPragmaPack(PragmaPackDiagnoseKind Kind,
216 SourceLocation IncludeLoc) {
217 if (Kind == PragmaPackDiagnoseKind::NonDefaultStateAtInclude) {
218 SourceLocation PrevLocation = PackStack.CurrentPragmaLocation;
219 // Warn about non-default alignment at #includes (without redundant
220 // warnings for the same directive in nested includes).
221 // The warning is delayed until the end of the file to avoid warnings
222 // for files that don't have any records that are affected by the modified
223 // alignment.
224 bool HasNonDefaultValue =
225 PackStack.hasValue() &&
226 (PackIncludeStack.empty() ||
227 PackIncludeStack.back().CurrentPragmaLocation != PrevLocation);
228 PackIncludeStack.push_back(
229 {PackStack.CurrentValue,
230 PackStack.hasValue() ? PrevLocation : SourceLocation(),
231 HasNonDefaultValue, /*ShouldWarnOnInclude*/ false});
232 return;
233 }
234
235 assert(Kind == PragmaPackDiagnoseKind::ChangedStateAtExit && "invalid kind");
236 PackIncludeState PrevPackState = PackIncludeStack.pop_back_val();
237 if (PrevPackState.ShouldWarnOnInclude) {
238 // Emit the delayed non-default alignment at #include warning.
239 Diag(IncludeLoc, diag::warn_pragma_pack_non_default_at_include);
240 Diag(PrevPackState.CurrentPragmaLocation, diag::note_pragma_pack_here);
241 }
242 // Warn about modified alignment after #includes.
243 if (PrevPackState.CurrentValue != PackStack.CurrentValue) {
244 Diag(IncludeLoc, diag::warn_pragma_pack_modified_after_include);
245 Diag(PackStack.CurrentPragmaLocation, diag::note_pragma_pack_here);
246 }
247}
248
249void Sema::DiagnoseUnterminatedPragmaPack() {
250 if (PackStack.Stack.empty())
251 return;
Alex Lorenza1479d72017-07-31 13:37:50 +0000252 bool IsInnermost = true;
253 for (const auto &StackSlot : llvm::reverse(PackStack.Stack)) {
Alex Lorenz45b40142017-07-28 14:41:21 +0000254 Diag(StackSlot.PragmaPushLocation, diag::warn_pragma_pack_no_pop_eof);
Alex Lorenza1479d72017-07-31 13:37:50 +0000255 // The user might have already reset the alignment, so suggest replacing
256 // the reset with a pop.
257 if (IsInnermost && PackStack.CurrentValue == PackStack.DefaultValue) {
258 DiagnosticBuilder DB = Diag(PackStack.CurrentPragmaLocation,
259 diag::note_pragma_pack_pop_instead_reset);
260 SourceLocation FixItLoc = Lexer::findLocationAfterToken(
261 PackStack.CurrentPragmaLocation, tok::l_paren, SourceMgr, LangOpts,
262 /*SkipTrailing=*/false);
263 if (FixItLoc.isValid())
264 DB << FixItHint::CreateInsertion(FixItLoc, "pop");
265 }
266 IsInnermost = false;
267 }
Alex Lorenz45b40142017-07-28 14:41:21 +0000268}
269
Fangrui Song6907ce22018-07-30 19:24:48 +0000270void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000271 MSStructPragmaOn = (Kind == PMSST_ON);
272}
273
Nico Weber66220292016-03-02 17:28:48 +0000274void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc,
275 PragmaMSCommentKind Kind, StringRef Arg) {
276 auto *PCD = PragmaCommentDecl::Create(
277 Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg);
278 Context.getTranslationUnitDecl()->addDecl(PCD);
279 Consumer.HandleTopLevelDecl(DeclGroupRef(PCD));
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000280}
281
Nico Webercbbaeb12016-03-02 19:28:54 +0000282void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name,
283 StringRef Value) {
284 auto *PDMD = PragmaDetectMismatchDecl::Create(
285 Context, Context.getTranslationUnitDecl(), Loc, Name, Value);
286 Context.getTranslationUnitDecl()->addDecl(PDMD);
287 Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD));
Aaron Ballman5d041be2013-06-04 02:07:14 +0000288}
289
David Majnemer4bb09802014-02-10 19:50:15 +0000290void Sema::ActOnPragmaMSPointersToMembers(
David Majnemer86c318f2014-02-11 21:05:00 +0000291 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,
David Majnemer4bb09802014-02-10 19:50:15 +0000292 SourceLocation PragmaLoc) {
293 MSPointerToMemberRepresentationMethod = RepresentationMethod;
294 ImplicitMSInheritanceAttrLoc = PragmaLoc;
295}
296
Denis Zobnin2290dac2016-04-29 11:27:00 +0000297void Sema::ActOnPragmaMSVtorDisp(PragmaMsStackAction Action,
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000298 SourceLocation PragmaLoc,
299 MSVtorDispAttr::Mode Mode) {
Denis Zobnin2290dac2016-04-29 11:27:00 +0000300 if (Action & PSK_Pop && VtorDispStack.Stack.empty())
301 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
302 << "stack empty";
303 VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000304}
305
Warren Huntc3b18962014-04-08 22:30:47 +0000306template<typename ValueType>
307void Sema::PragmaStack<ValueType>::Act(SourceLocation PragmaLocation,
308 PragmaMsStackAction Action,
309 llvm::StringRef StackSlotLabel,
310 ValueType Value) {
311 if (Action == PSK_Reset) {
Denis Zobnin2290dac2016-04-29 11:27:00 +0000312 CurrentValue = DefaultValue;
Alex Lorenz7d7e1e02017-03-31 15:36:21 +0000313 CurrentPragmaLocation = PragmaLocation;
Warren Huntc3b18962014-04-08 22:30:47 +0000314 return;
315 }
316 if (Action & PSK_Push)
Alex Lorenz45b40142017-07-28 14:41:21 +0000317 Stack.emplace_back(StackSlotLabel, CurrentValue, CurrentPragmaLocation,
318 PragmaLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000319 else if (Action & PSK_Pop) {
320 if (!StackSlotLabel.empty()) {
321 // If we've got a label, try to find it and jump there.
David Majnemerf7e36092016-06-23 00:15:04 +0000322 auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) {
323 return x.StackSlotLabel == StackSlotLabel;
324 });
Warren Huntc3b18962014-04-08 22:30:47 +0000325 // If we found the label so pop from there.
326 if (I != Stack.rend()) {
327 CurrentValue = I->Value;
328 CurrentPragmaLocation = I->PragmaLocation;
329 Stack.erase(std::prev(I.base()), Stack.end());
330 }
331 } else if (!Stack.empty()) {
George Burgess IVa75c71d2018-05-26 02:29:14 +0000332 // We do not have a label, just pop the last entry.
Warren Huntc3b18962014-04-08 22:30:47 +0000333 CurrentValue = Stack.back().Value;
334 CurrentPragmaLocation = Stack.back().PragmaLocation;
335 Stack.pop_back();
336 }
337 }
338 if (Action & PSK_Set) {
339 CurrentValue = Value;
340 CurrentPragmaLocation = PragmaLocation;
341 }
342}
343
Craig Topperbf3e3272014-08-30 16:55:52 +0000344bool Sema::UnifySection(StringRef SectionName,
Warren Huntc3b18962014-04-08 22:30:47 +0000345 int SectionFlags,
346 DeclaratorDecl *Decl) {
Hans Wennborg899ded92014-10-16 20:52:46 +0000347 auto Section = Context.SectionInfos.find(SectionName);
348 if (Section == Context.SectionInfos.end()) {
349 Context.SectionInfos[SectionName] =
350 ASTContext::SectionInfo(Decl, SourceLocation(), SectionFlags);
Warren Huntc3b18962014-04-08 22:30:47 +0000351 return false;
352 }
353 // A pre-declared section takes precedence w/o diagnostic.
354 if (Section->second.SectionFlags == SectionFlags ||
Hans Wennborg899ded92014-10-16 20:52:46 +0000355 !(Section->second.SectionFlags & ASTContext::PSF_Implicit))
Warren Huntc3b18962014-04-08 22:30:47 +0000356 return false;
357 auto OtherDecl = Section->second.Decl;
358 Diag(Decl->getLocation(), diag::err_section_conflict)
359 << Decl << OtherDecl;
360 Diag(OtherDecl->getLocation(), diag::note_declared_at)
361 << OtherDecl->getName();
362 if (auto A = Decl->getAttr<SectionAttr>())
363 if (A->isImplicit())
364 Diag(A->getLocation(), diag::note_pragma_entered_here);
365 if (auto A = OtherDecl->getAttr<SectionAttr>())
366 if (A->isImplicit())
367 Diag(A->getLocation(), diag::note_pragma_entered_here);
Ehsan Akhgari0b510602014-09-22 19:46:39 +0000368 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000369}
370
Craig Topperbf3e3272014-08-30 16:55:52 +0000371bool Sema::UnifySection(StringRef SectionName,
Warren Huntc3b18962014-04-08 22:30:47 +0000372 int SectionFlags,
373 SourceLocation PragmaSectionLocation) {
Hans Wennborg899ded92014-10-16 20:52:46 +0000374 auto Section = Context.SectionInfos.find(SectionName);
375 if (Section != Context.SectionInfos.end()) {
Warren Huntc3b18962014-04-08 22:30:47 +0000376 if (Section->second.SectionFlags == SectionFlags)
377 return false;
Hans Wennborg899ded92014-10-16 20:52:46 +0000378 if (!(Section->second.SectionFlags & ASTContext::PSF_Implicit)) {
Warren Huntc3b18962014-04-08 22:30:47 +0000379 Diag(PragmaSectionLocation, diag::err_section_conflict)
380 << "this" << "a prior #pragma section";
381 Diag(Section->second.PragmaSectionLocation,
382 diag::note_pragma_entered_here);
383 return true;
384 }
385 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000386 Context.SectionInfos[SectionName] =
387 ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags);
Warren Huntc3b18962014-04-08 22:30:47 +0000388 return false;
389}
390
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000391/// Called on well formed \#pragma bss_seg().
Warren Huntc3b18962014-04-08 22:30:47 +0000392void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation,
393 PragmaMsStackAction Action,
394 llvm::StringRef StackSlotLabel,
395 StringLiteral *SegmentName,
396 llvm::StringRef PragmaName) {
397 PragmaStack<StringLiteral *> *Stack =
398 llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName)
399 .Case("data_seg", &DataSegStack)
400 .Case("bss_seg", &BSSSegStack)
401 .Case("const_seg", &ConstSegStack)
402 .Case("code_seg", &CodeSegStack);
403 if (Action & PSK_Pop && Stack->Stack.empty())
404 Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName
405 << "stack empty";
Reid Kleckner2a133222015-03-04 23:39:17 +0000406 if (SegmentName &&
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000407 !checkSectionName(SegmentName->getBeginLoc(), SegmentName->getString()))
Reid Kleckner2a133222015-03-04 23:39:17 +0000408 return;
Warren Huntc3b18962014-04-08 22:30:47 +0000409 Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName);
410}
411
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000412/// Called on well formed \#pragma bss_seg().
Warren Huntc3b18962014-04-08 22:30:47 +0000413void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation,
414 int SectionFlags, StringLiteral *SegmentName) {
415 UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation);
416}
417
Reid Kleckner1a711b12014-07-22 00:53:05 +0000418void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,
419 StringLiteral *SegmentName) {
420 // There's no stack to maintain, so we just have a current section. When we
421 // see the default section, reset our current section back to null so we stop
422 // tacking on unnecessary attributes.
423 CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName;
424 CurInitSegLoc = PragmaLocation;
425}
426
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000427void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
428 SourceLocation PragmaLoc) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000429
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000430 IdentifierInfo *Name = IdTok.getIdentifierInfo();
431 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
Craig Topperc3ec1492014-05-26 06:22:03 +0000432 LookupParsedName(Lookup, curScope, nullptr, true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000433
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000434 if (Lookup.empty()) {
435 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
436 << Name << SourceRange(IdTok.getLocation());
437 return;
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000438 }
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000439
440 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
Argyrios Kyrtzidisff115a22011-01-27 18:16:48 +0000441 if (!VD) {
442 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000443 << Name << SourceRange(IdTok.getLocation());
444 return;
445 }
446
447 // Warn if this was used before being marked unused.
448 if (VD->isUsed())
449 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
450
Aaron Ballman0bcd6c12016-03-09 16:48:08 +0000451 VD->addAttr(UnusedAttr::CreateImplicit(Context, UnusedAttr::GNU_unused,
452 IdTok.getLocation()));
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000453}
Eli Friedman570024a2010-08-05 06:57:20 +0000454
John McCall32f5fe12011-09-30 05:12:12 +0000455void Sema::AddCFAuditedAttribute(Decl *D) {
456 SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
457 if (!Loc.isValid()) return;
458
459 // Don't add a redundant or conflicting attribute.
460 if (D->hasAttr<CFAuditedTransferAttr>() ||
461 D->hasAttr<CFUnknownTransferAttr>())
462 return;
463
Aaron Ballman36a53502014-01-16 13:03:14 +0000464 D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc));
John McCall32f5fe12011-09-30 05:12:12 +0000465}
466
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000467namespace {
468
469Optional<attr::SubjectMatchRule>
470getParentAttrMatcherRule(attr::SubjectMatchRule Rule) {
471 using namespace attr;
472 switch (Rule) {
473 default:
474 return None;
475#define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
476#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \
477 case Value: \
478 return Parent;
479#include "clang/Basic/AttrSubMatchRulesList.inc"
480 }
481}
482
483bool isNegatedAttrMatcherSubRule(attr::SubjectMatchRule Rule) {
484 using namespace attr;
485 switch (Rule) {
486 default:
487 return false;
488#define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
489#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \
490 case Value: \
491 return IsNegated;
492#include "clang/Basic/AttrSubMatchRulesList.inc"
493 }
494}
495
496CharSourceRange replacementRangeForListElement(const Sema &S,
497 SourceRange Range) {
498 // Make sure that the ',' is removed as well.
499 SourceLocation AfterCommaLoc = Lexer::findLocationAfterToken(
500 Range.getEnd(), tok::comma, S.getSourceManager(), S.getLangOpts(),
501 /*SkipTrailingWhitespaceAndNewLine=*/false);
502 if (AfterCommaLoc.isValid())
503 return CharSourceRange::getCharRange(Range.getBegin(), AfterCommaLoc);
504 else
505 return CharSourceRange::getTokenRange(Range);
506}
507
508std::string
509attrMatcherRuleListToString(ArrayRef<attr::SubjectMatchRule> Rules) {
510 std::string Result;
511 llvm::raw_string_ostream OS(Result);
512 for (const auto &I : llvm::enumerate(Rules)) {
513 if (I.index())
514 OS << (I.index() == Rules.size() - 1 ? ", and " : ", ");
515 OS << "'" << attr::getSubjectMatchRuleSpelling(I.value()) << "'";
516 }
517 return OS.str();
518}
519
520} // end anonymous namespace
521
Erik Pilkington7d180942018-10-29 17:38:42 +0000522void Sema::ActOnPragmaAttributeAttribute(
523 ParsedAttr &Attribute, SourceLocation PragmaLoc,
524 attr::ParsedSubjectMatchRuleSet Rules) {
Alex Lorenz3cfe9d52019-01-24 19:14:39 +0000525 Attribute.setIsPragmaClangAttribute();
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000526 SmallVector<attr::SubjectMatchRule, 4> SubjectMatchRules;
527 // Gather the subject match rules that are supported by the attribute.
528 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4>
529 StrictSubjectMatchRuleSet;
530 Attribute.getMatchRules(LangOpts, StrictSubjectMatchRuleSet);
531
532 // Figure out which subject matching rules are valid.
533 if (StrictSubjectMatchRuleSet.empty()) {
534 // Check for contradicting match rules. Contradicting match rules are
535 // either:
536 // - a top-level rule and one of its sub-rules. E.g. variable and
537 // variable(is_parameter).
538 // - a sub-rule and a sibling that's negated. E.g.
539 // variable(is_thread_local) and variable(unless(is_parameter))
Alex Lorenz26b47652017-04-18 20:54:23 +0000540 llvm::SmallDenseMap<int, std::pair<int, SourceRange>, 2>
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000541 RulesToFirstSpecifiedNegatedSubRule;
542 for (const auto &Rule : Rules) {
Alex Lorenz26b47652017-04-18 20:54:23 +0000543 attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000544 Optional<attr::SubjectMatchRule> ParentRule =
Alex Lorenz26b47652017-04-18 20:54:23 +0000545 getParentAttrMatcherRule(MatchRule);
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000546 if (!ParentRule)
547 continue;
548 auto It = Rules.find(*ParentRule);
549 if (It != Rules.end()) {
550 // A sub-rule contradicts a parent rule.
551 Diag(Rule.second.getBegin(),
552 diag::err_pragma_attribute_matcher_subrule_contradicts_rule)
Alex Lorenz26b47652017-04-18 20:54:23 +0000553 << attr::getSubjectMatchRuleSpelling(MatchRule)
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000554 << attr::getSubjectMatchRuleSpelling(*ParentRule) << It->second
555 << FixItHint::CreateRemoval(
556 replacementRangeForListElement(*this, Rule.second));
557 // Keep going without removing this rule as it won't change the set of
558 // declarations that receive the attribute.
559 continue;
560 }
Alex Lorenz26b47652017-04-18 20:54:23 +0000561 if (isNegatedAttrMatcherSubRule(MatchRule))
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000562 RulesToFirstSpecifiedNegatedSubRule.insert(
563 std::make_pair(*ParentRule, Rule));
564 }
565 bool IgnoreNegatedSubRules = false;
566 for (const auto &Rule : Rules) {
Alex Lorenz26b47652017-04-18 20:54:23 +0000567 attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000568 Optional<attr::SubjectMatchRule> ParentRule =
Alex Lorenz26b47652017-04-18 20:54:23 +0000569 getParentAttrMatcherRule(MatchRule);
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000570 if (!ParentRule)
571 continue;
572 auto It = RulesToFirstSpecifiedNegatedSubRule.find(*ParentRule);
573 if (It != RulesToFirstSpecifiedNegatedSubRule.end() &&
574 It->second != Rule) {
575 // Negated sub-rule contradicts another sub-rule.
576 Diag(
577 It->second.second.getBegin(),
578 diag::
579 err_pragma_attribute_matcher_negated_subrule_contradicts_subrule)
Alex Lorenz26b47652017-04-18 20:54:23 +0000580 << attr::getSubjectMatchRuleSpelling(
581 attr::SubjectMatchRule(It->second.first))
582 << attr::getSubjectMatchRuleSpelling(MatchRule) << Rule.second
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000583 << FixItHint::CreateRemoval(
584 replacementRangeForListElement(*this, It->second.second));
585 // Keep going but ignore all of the negated sub-rules.
586 IgnoreNegatedSubRules = true;
587 RulesToFirstSpecifiedNegatedSubRule.erase(It);
588 }
589 }
590
591 if (!IgnoreNegatedSubRules) {
592 for (const auto &Rule : Rules)
Alex Lorenz26b47652017-04-18 20:54:23 +0000593 SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000594 } else {
595 for (const auto &Rule : Rules) {
Alex Lorenz26b47652017-04-18 20:54:23 +0000596 if (!isNegatedAttrMatcherSubRule(attr::SubjectMatchRule(Rule.first)))
597 SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000598 }
599 }
600 Rules.clear();
601 } else {
602 for (const auto &Rule : StrictSubjectMatchRuleSet) {
603 if (Rules.erase(Rule.first)) {
604 // Add the rule to the set of attribute receivers only if it's supported
605 // in the current language mode.
606 if (Rule.second)
607 SubjectMatchRules.push_back(Rule.first);
608 }
609 }
610 }
611
612 if (!Rules.empty()) {
613 auto Diagnostic =
614 Diag(PragmaLoc, diag::err_pragma_attribute_invalid_matchers)
615 << Attribute.getName();
616 SmallVector<attr::SubjectMatchRule, 2> ExtraRules;
617 for (const auto &Rule : Rules) {
Alex Lorenz26b47652017-04-18 20:54:23 +0000618 ExtraRules.push_back(attr::SubjectMatchRule(Rule.first));
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000619 Diagnostic << FixItHint::CreateRemoval(
620 replacementRangeForListElement(*this, Rule.second));
621 }
622 Diagnostic << attrMatcherRuleListToString(ExtraRules);
623 }
624
Erik Pilkington7d180942018-10-29 17:38:42 +0000625 if (PragmaAttributeStack.empty()) {
626 Diag(PragmaLoc, diag::err_pragma_attr_attr_no_push);
627 return;
628 }
629
630 PragmaAttributeStack.back().Entries.push_back(
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000631 {PragmaLoc, &Attribute, std::move(SubjectMatchRules), /*IsUsed=*/false});
632}
633
Erik Pilkington0876cae2018-12-20 22:32:04 +0000634void Sema::ActOnPragmaAttributeEmptyPush(SourceLocation PragmaLoc,
635 const IdentifierInfo *Namespace) {
Erik Pilkington7d180942018-10-29 17:38:42 +0000636 PragmaAttributeStack.emplace_back();
637 PragmaAttributeStack.back().Loc = PragmaLoc;
Erik Pilkington0876cae2018-12-20 22:32:04 +0000638 PragmaAttributeStack.back().Namespace = Namespace;
Erik Pilkington7d180942018-10-29 17:38:42 +0000639}
640
Erik Pilkington0876cae2018-12-20 22:32:04 +0000641void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc,
642 const IdentifierInfo *Namespace) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000643 if (PragmaAttributeStack.empty()) {
Erik Pilkington0876cae2018-12-20 22:32:04 +0000644 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000645 return;
646 }
Erik Pilkington7d180942018-10-29 17:38:42 +0000647
Erik Pilkington0876cae2018-12-20 22:32:04 +0000648 // Dig back through the stack trying to find the most recently pushed group
649 // that in Namespace. Note that this works fine if no namespace is present,
650 // think of push/pops without namespaces as having an implicit "nullptr"
651 // namespace.
652 for (size_t Index = PragmaAttributeStack.size(); Index;) {
653 --Index;
654 if (PragmaAttributeStack[Index].Namespace == Namespace) {
655 for (const PragmaAttributeEntry &Entry :
656 PragmaAttributeStack[Index].Entries) {
657 if (!Entry.IsUsed) {
658 assert(Entry.Attribute && "Expected an attribute");
659 Diag(Entry.Attribute->getLoc(), diag::warn_pragma_attribute_unused)
660 << *Entry.Attribute;
661 Diag(PragmaLoc, diag::note_pragma_attribute_region_ends_here);
662 }
663 }
664 PragmaAttributeStack.erase(PragmaAttributeStack.begin() + Index);
665 return;
Erik Pilkington7d180942018-10-29 17:38:42 +0000666 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000667 }
Erik Pilkington7d180942018-10-29 17:38:42 +0000668
Erik Pilkington0876cae2018-12-20 22:32:04 +0000669 if (Namespace)
670 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch)
671 << 0 << Namespace->getName();
672 else
673 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000674}
675
676void Sema::AddPragmaAttributes(Scope *S, Decl *D) {
677 if (PragmaAttributeStack.empty())
678 return;
Erik Pilkington7d180942018-10-29 17:38:42 +0000679 for (auto &Group : PragmaAttributeStack) {
680 for (auto &Entry : Group.Entries) {
681 ParsedAttr *Attribute = Entry.Attribute;
682 assert(Attribute && "Expected an attribute");
Alex Lorenz3cfe9d52019-01-24 19:14:39 +0000683 assert(Attribute->isPragmaClangAttribute() &&
684 "expected #pragma clang attribute");
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000685
Erik Pilkington7d180942018-10-29 17:38:42 +0000686 // Ensure that the attribute can be applied to the given declaration.
687 bool Applies = false;
688 for (const auto &Rule : Entry.MatchRules) {
689 if (Attribute->appliesToDecl(D, Rule)) {
690 Applies = true;
691 break;
692 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000693 }
Erik Pilkington7d180942018-10-29 17:38:42 +0000694 if (!Applies)
695 continue;
696 Entry.IsUsed = true;
697 PragmaAttributeCurrentTargetDecl = D;
698 ParsedAttributesView Attrs;
699 Attrs.addAtEnd(Attribute);
700 ProcessDeclAttributeList(S, D, Attrs);
701 PragmaAttributeCurrentTargetDecl = nullptr;
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000702 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000703 }
704}
705
706void Sema::PrintPragmaAttributeInstantiationPoint() {
707 assert(PragmaAttributeCurrentTargetDecl && "Expected an active declaration");
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000708 Diags.Report(PragmaAttributeCurrentTargetDecl->getBeginLoc(),
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000709 diag::note_pragma_attribute_applied_decl_here);
710}
711
712void Sema::DiagnoseUnterminatedPragmaAttribute() {
713 if (PragmaAttributeStack.empty())
714 return;
715 Diag(PragmaAttributeStack.back().Loc, diag::err_pragma_attribute_no_pop_eof);
716}
717
Dario Domizioli13a0a382014-05-23 12:13:25 +0000718void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) {
719 if(On)
720 OptimizeOffPragmaLocation = SourceLocation();
721 else
722 OptimizeOffPragmaLocation = PragmaLoc;
723}
724
725void Sema::AddRangeBasedOptnone(FunctionDecl *FD) {
726 // In the future, check other pragmas if they're implemented (e.g. pragma
727 // optimize 0 will probably map to this functionality too).
728 if(OptimizeOffPragmaLocation.isValid())
729 AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation);
730}
731
Fangrui Song6907ce22018-07-30 19:24:48 +0000732void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD,
Dario Domizioli13a0a382014-05-23 12:13:25 +0000733 SourceLocation Loc) {
734 // Don't add a conflicting attribute. No diagnostic is needed.
735 if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>())
736 return;
737
738 // Add attributes only if required. Optnone requires noinline as well, but if
739 // either is already present then don't bother adding them.
740 if (!FD->hasAttr<OptimizeNoneAttr>())
741 FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc));
742 if (!FD->hasAttr<NoInlineAttr>())
743 FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc));
744}
745
John McCall2faf32c2010-12-10 02:59:44 +0000746typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
Alp Tokerceb95c42014-03-02 03:20:16 +0000747enum : unsigned { NoVisibility = ~0U };
Eli Friedman570024a2010-08-05 06:57:20 +0000748
749void Sema::AddPushedVisibilityAttribute(Decl *D) {
750 if (!VisContext)
751 return;
752
Rafael Espindola54606d52012-12-25 07:31:49 +0000753 NamedDecl *ND = dyn_cast<NamedDecl>(D);
John McCalld041a9b2013-02-20 01:54:26 +0000754 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
Eli Friedman570024a2010-08-05 06:57:20 +0000755 return;
756
757 VisStack *Stack = static_cast<VisStack*>(VisContext);
John McCall2faf32c2010-12-10 02:59:44 +0000758 unsigned rawType = Stack->back().first;
759 if (rawType == NoVisibility) return;
760
761 VisibilityAttr::VisibilityType type
762 = (VisibilityAttr::VisibilityType) rawType;
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000763 SourceLocation loc = Stack->back().second;
Eli Friedman570024a2010-08-05 06:57:20 +0000764
Aaron Ballman36a53502014-01-16 13:03:14 +0000765 D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
Eli Friedman570024a2010-08-05 06:57:20 +0000766}
767
768/// FreeVisContext - Deallocate and null out VisContext.
769void Sema::FreeVisContext() {
770 delete static_cast<VisStack*>(VisContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000771 VisContext = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000772}
773
John McCall2faf32c2010-12-10 02:59:44 +0000774static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
John McCallb1be5232010-08-26 09:15:37 +0000775 // Put visibility on stack.
776 if (!S.VisContext)
777 S.VisContext = new VisStack;
778
779 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
780 Stack->push_back(std::make_pair(type, loc));
781}
782
Rafael Espindolade15baf2012-01-21 05:43:40 +0000783void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
Eli Friedman570024a2010-08-05 06:57:20 +0000784 SourceLocation PragmaLoc) {
Rafael Espindolade15baf2012-01-21 05:43:40 +0000785 if (VisType) {
Eli Friedman570024a2010-08-05 06:57:20 +0000786 // Compute visibility to use.
Aaron Ballman682ee422013-09-11 19:47:58 +0000787 VisibilityAttr::VisibilityType T;
788 if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
789 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
Eli Friedman570024a2010-08-05 06:57:20 +0000790 return;
791 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000792 PushPragmaVisibility(*this, T, PragmaLoc);
Eli Friedman570024a2010-08-05 06:57:20 +0000793 } else {
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000794 PopPragmaVisibility(false, PragmaLoc);
Eli Friedman570024a2010-08-05 06:57:20 +0000795 }
796}
797
Adam Nemet60d32642017-04-04 21:18:36 +0000798void Sema::ActOnPragmaFPContract(LangOptions::FPContractModeKind FPC) {
799 switch (FPC) {
800 case LangOptions::FPC_On:
Adam Nemet049a31d2017-03-29 21:54:24 +0000801 FPFeatures.setAllowFPContractWithinStatement();
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000802 break;
Adam Nemet60d32642017-04-04 21:18:36 +0000803 case LangOptions::FPC_Fast:
804 FPFeatures.setAllowFPContractAcrossStatement();
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000805 break;
Adam Nemet60d32642017-04-04 21:18:36 +0000806 case LangOptions::FPC_Off:
807 FPFeatures.setDisallowFPContract();
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000808 break;
809 }
810}
811
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000812void Sema::ActOnPragmaFEnvAccess(LangOptions::FEnvAccessModeKind FPC) {
813 switch (FPC) {
814 case LangOptions::FEA_On:
815 FPFeatures.setAllowFEnvAccess();
816 break;
817 case LangOptions::FEA_Off:
818 FPFeatures.setDisallowFEnvAccess();
819 break;
820 }
821}
822
823
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000824void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
825 SourceLocation Loc) {
John McCall2faf32c2010-12-10 02:59:44 +0000826 // Visibility calculations will consider the namespace's visibility.
827 // Here we just want to note that we're in a visibility context
828 // which overrides any enclosing #pragma context, but doesn't itself
829 // contribute visibility.
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000830 PushPragmaVisibility(*this, NoVisibility, Loc);
Eli Friedman570024a2010-08-05 06:57:20 +0000831}
832
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000833void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
834 if (!VisContext) {
835 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
836 return;
Eli Friedman570024a2010-08-05 06:57:20 +0000837 }
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000838
839 // Pop visibility from stack
840 VisStack *Stack = static_cast<VisStack*>(VisContext);
841
842 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
843 bool StartsWithPragma = Back->first != NoVisibility;
844 if (StartsWithPragma && IsNamespaceEnd) {
845 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
846 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
847
848 // For better error recovery, eat all pushes inside the namespace.
849 do {
850 Stack->pop_back();
851 Back = &Stack->back();
852 StartsWithPragma = Back->first != NoVisibility;
853 } while (StartsWithPragma);
854 } else if (!StartsWithPragma && !IsNamespaceEnd) {
855 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
856 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
857 return;
858 }
859
860 Stack->pop_back();
861 // To simplify the implementation, never keep around an empty stack.
862 if (Stack->empty())
863 FreeVisContext();
Eli Friedman570024a2010-08-05 06:57:20 +0000864}