1e5dd7070Spatrick //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This file implements semantic analysis for non-trivial attributes and
10e5dd7070Spatrick // pragmas.
11e5dd7070Spatrick //
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick
14e5dd7070Spatrick #include "clang/AST/ASTConsumer.h"
15e5dd7070Spatrick #include "clang/AST/Attr.h"
16e5dd7070Spatrick #include "clang/AST/Expr.h"
17e5dd7070Spatrick #include "clang/Basic/TargetInfo.h"
18e5dd7070Spatrick #include "clang/Lex/Preprocessor.h"
19e5dd7070Spatrick #include "clang/Sema/Lookup.h"
20e5dd7070Spatrick #include "clang/Sema/SemaInternal.h"
21*12c85518Srobert #include <optional>
22e5dd7070Spatrick using namespace clang;
23e5dd7070Spatrick
24e5dd7070Spatrick //===----------------------------------------------------------------------===//
25e5dd7070Spatrick // Pragma 'pack' and 'options align'
26e5dd7070Spatrick //===----------------------------------------------------------------------===//
27e5dd7070Spatrick
PragmaStackSentinelRAII(Sema & S,StringRef SlotLabel,bool ShouldAct)28e5dd7070Spatrick Sema::PragmaStackSentinelRAII::PragmaStackSentinelRAII(Sema &S,
29e5dd7070Spatrick StringRef SlotLabel,
30e5dd7070Spatrick bool ShouldAct)
31e5dd7070Spatrick : S(S), SlotLabel(SlotLabel), ShouldAct(ShouldAct) {
32e5dd7070Spatrick if (ShouldAct) {
33e5dd7070Spatrick S.VtorDispStack.SentinelAction(PSK_Push, SlotLabel);
34e5dd7070Spatrick S.DataSegStack.SentinelAction(PSK_Push, SlotLabel);
35e5dd7070Spatrick S.BSSSegStack.SentinelAction(PSK_Push, SlotLabel);
36e5dd7070Spatrick S.ConstSegStack.SentinelAction(PSK_Push, SlotLabel);
37e5dd7070Spatrick S.CodeSegStack.SentinelAction(PSK_Push, SlotLabel);
38*12c85518Srobert S.StrictGuardStackCheckStack.SentinelAction(PSK_Push, SlotLabel);
39e5dd7070Spatrick }
40e5dd7070Spatrick }
41e5dd7070Spatrick
~PragmaStackSentinelRAII()42e5dd7070Spatrick Sema::PragmaStackSentinelRAII::~PragmaStackSentinelRAII() {
43e5dd7070Spatrick if (ShouldAct) {
44e5dd7070Spatrick S.VtorDispStack.SentinelAction(PSK_Pop, SlotLabel);
45e5dd7070Spatrick S.DataSegStack.SentinelAction(PSK_Pop, SlotLabel);
46e5dd7070Spatrick S.BSSSegStack.SentinelAction(PSK_Pop, SlotLabel);
47e5dd7070Spatrick S.ConstSegStack.SentinelAction(PSK_Pop, SlotLabel);
48e5dd7070Spatrick S.CodeSegStack.SentinelAction(PSK_Pop, SlotLabel);
49*12c85518Srobert S.StrictGuardStackCheckStack.SentinelAction(PSK_Pop, SlotLabel);
50e5dd7070Spatrick }
51e5dd7070Spatrick }
52e5dd7070Spatrick
AddAlignmentAttributesForRecord(RecordDecl * RD)53e5dd7070Spatrick void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
54a9ac8606Spatrick AlignPackInfo InfoVal = AlignPackStack.CurrentValue;
55a9ac8606Spatrick AlignPackInfo::Mode M = InfoVal.getAlignMode();
56a9ac8606Spatrick bool IsPackSet = InfoVal.IsPackSet();
57a9ac8606Spatrick bool IsXLPragma = getLangOpts().XLPragmaPack;
58a9ac8606Spatrick
59a9ac8606Spatrick // If we are not under mac68k/natural alignment mode and also there is no pack
60a9ac8606Spatrick // value, we don't need any attributes.
61a9ac8606Spatrick if (!IsPackSet && M != AlignPackInfo::Mac68k && M != AlignPackInfo::Natural)
62e5dd7070Spatrick return;
63e5dd7070Spatrick
64a9ac8606Spatrick if (M == AlignPackInfo::Mac68k && (IsXLPragma || InfoVal.IsAlignAttr())) {
65e5dd7070Spatrick RD->addAttr(AlignMac68kAttr::CreateImplicit(Context));
66a9ac8606Spatrick } else if (IsPackSet) {
67a9ac8606Spatrick // Check to see if we need a max field alignment attribute.
68a9ac8606Spatrick RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(
69a9ac8606Spatrick Context, InfoVal.getPackNumber() * 8));
70e5dd7070Spatrick }
71a9ac8606Spatrick
72a9ac8606Spatrick if (IsXLPragma && M == AlignPackInfo::Natural)
73a9ac8606Spatrick RD->addAttr(AlignNaturalAttr::CreateImplicit(Context));
74a9ac8606Spatrick
75a9ac8606Spatrick if (AlignPackIncludeStack.empty())
76e5dd7070Spatrick return;
77a9ac8606Spatrick // The #pragma align/pack affected a record in an included file, so Clang
78a9ac8606Spatrick // should warn when that pragma was written in a file that included the
79a9ac8606Spatrick // included file.
80a9ac8606Spatrick for (auto &AlignPackedInclude : llvm::reverse(AlignPackIncludeStack)) {
81a9ac8606Spatrick if (AlignPackedInclude.CurrentPragmaLocation !=
82a9ac8606Spatrick AlignPackStack.CurrentPragmaLocation)
83e5dd7070Spatrick break;
84a9ac8606Spatrick if (AlignPackedInclude.HasNonDefaultValue)
85a9ac8606Spatrick AlignPackedInclude.ShouldWarnOnInclude = true;
86e5dd7070Spatrick }
87e5dd7070Spatrick }
88e5dd7070Spatrick
AddMsStructLayoutForRecord(RecordDecl * RD)89e5dd7070Spatrick void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
90e5dd7070Spatrick if (MSStructPragmaOn)
91e5dd7070Spatrick RD->addAttr(MSStructAttr::CreateImplicit(Context));
92e5dd7070Spatrick
93e5dd7070Spatrick // FIXME: We should merge AddAlignmentAttributesForRecord with
94e5dd7070Spatrick // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes
95e5dd7070Spatrick // all active pragmas and applies them as attributes to class definitions.
96e5dd7070Spatrick if (VtorDispStack.CurrentValue != getLangOpts().getVtorDispMode())
97e5dd7070Spatrick RD->addAttr(MSVtorDispAttr::CreateImplicit(
98e5dd7070Spatrick Context, unsigned(VtorDispStack.CurrentValue)));
99e5dd7070Spatrick }
100e5dd7070Spatrick
101e5dd7070Spatrick template <typename Attribute>
addGslOwnerPointerAttributeIfNotExisting(ASTContext & Context,CXXRecordDecl * Record)102e5dd7070Spatrick static void addGslOwnerPointerAttributeIfNotExisting(ASTContext &Context,
103e5dd7070Spatrick CXXRecordDecl *Record) {
104e5dd7070Spatrick if (Record->hasAttr<OwnerAttr>() || Record->hasAttr<PointerAttr>())
105e5dd7070Spatrick return;
106e5dd7070Spatrick
107e5dd7070Spatrick for (Decl *Redecl : Record->redecls())
108e5dd7070Spatrick Redecl->addAttr(Attribute::CreateImplicit(Context, /*DerefType=*/nullptr));
109e5dd7070Spatrick }
110e5dd7070Spatrick
inferGslPointerAttribute(NamedDecl * ND,CXXRecordDecl * UnderlyingRecord)111e5dd7070Spatrick void Sema::inferGslPointerAttribute(NamedDecl *ND,
112e5dd7070Spatrick CXXRecordDecl *UnderlyingRecord) {
113e5dd7070Spatrick if (!UnderlyingRecord)
114e5dd7070Spatrick return;
115e5dd7070Spatrick
116e5dd7070Spatrick const auto *Parent = dyn_cast<CXXRecordDecl>(ND->getDeclContext());
117e5dd7070Spatrick if (!Parent)
118e5dd7070Spatrick return;
119e5dd7070Spatrick
120e5dd7070Spatrick static llvm::StringSet<> Containers{
121e5dd7070Spatrick "array",
122e5dd7070Spatrick "basic_string",
123e5dd7070Spatrick "deque",
124e5dd7070Spatrick "forward_list",
125e5dd7070Spatrick "vector",
126e5dd7070Spatrick "list",
127e5dd7070Spatrick "map",
128e5dd7070Spatrick "multiset",
129e5dd7070Spatrick "multimap",
130e5dd7070Spatrick "priority_queue",
131e5dd7070Spatrick "queue",
132e5dd7070Spatrick "set",
133e5dd7070Spatrick "stack",
134e5dd7070Spatrick "unordered_set",
135e5dd7070Spatrick "unordered_map",
136e5dd7070Spatrick "unordered_multiset",
137e5dd7070Spatrick "unordered_multimap",
138e5dd7070Spatrick };
139e5dd7070Spatrick
140e5dd7070Spatrick static llvm::StringSet<> Iterators{"iterator", "const_iterator",
141e5dd7070Spatrick "reverse_iterator",
142e5dd7070Spatrick "const_reverse_iterator"};
143e5dd7070Spatrick
144e5dd7070Spatrick if (Parent->isInStdNamespace() && Iterators.count(ND->getName()) &&
145e5dd7070Spatrick Containers.count(Parent->getName()))
146e5dd7070Spatrick addGslOwnerPointerAttributeIfNotExisting<PointerAttr>(Context,
147e5dd7070Spatrick UnderlyingRecord);
148e5dd7070Spatrick }
149e5dd7070Spatrick
inferGslPointerAttribute(TypedefNameDecl * TD)150e5dd7070Spatrick void Sema::inferGslPointerAttribute(TypedefNameDecl *TD) {
151e5dd7070Spatrick
152e5dd7070Spatrick QualType Canonical = TD->getUnderlyingType().getCanonicalType();
153e5dd7070Spatrick
154e5dd7070Spatrick CXXRecordDecl *RD = Canonical->getAsCXXRecordDecl();
155e5dd7070Spatrick if (!RD) {
156e5dd7070Spatrick if (auto *TST =
157e5dd7070Spatrick dyn_cast<TemplateSpecializationType>(Canonical.getTypePtr())) {
158e5dd7070Spatrick
159e5dd7070Spatrick RD = dyn_cast_or_null<CXXRecordDecl>(
160e5dd7070Spatrick TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl());
161e5dd7070Spatrick }
162e5dd7070Spatrick }
163e5dd7070Spatrick
164e5dd7070Spatrick inferGslPointerAttribute(TD, RD);
165e5dd7070Spatrick }
166e5dd7070Spatrick
inferGslOwnerPointerAttribute(CXXRecordDecl * Record)167e5dd7070Spatrick void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) {
168e5dd7070Spatrick static llvm::StringSet<> StdOwners{
169e5dd7070Spatrick "any",
170e5dd7070Spatrick "array",
171e5dd7070Spatrick "basic_regex",
172e5dd7070Spatrick "basic_string",
173e5dd7070Spatrick "deque",
174e5dd7070Spatrick "forward_list",
175e5dd7070Spatrick "vector",
176e5dd7070Spatrick "list",
177e5dd7070Spatrick "map",
178e5dd7070Spatrick "multiset",
179e5dd7070Spatrick "multimap",
180e5dd7070Spatrick "optional",
181e5dd7070Spatrick "priority_queue",
182e5dd7070Spatrick "queue",
183e5dd7070Spatrick "set",
184e5dd7070Spatrick "stack",
185e5dd7070Spatrick "unique_ptr",
186e5dd7070Spatrick "unordered_set",
187e5dd7070Spatrick "unordered_map",
188e5dd7070Spatrick "unordered_multiset",
189e5dd7070Spatrick "unordered_multimap",
190e5dd7070Spatrick "variant",
191e5dd7070Spatrick };
192e5dd7070Spatrick static llvm::StringSet<> StdPointers{
193e5dd7070Spatrick "basic_string_view",
194e5dd7070Spatrick "reference_wrapper",
195e5dd7070Spatrick "regex_iterator",
196e5dd7070Spatrick };
197e5dd7070Spatrick
198e5dd7070Spatrick if (!Record->getIdentifier())
199e5dd7070Spatrick return;
200e5dd7070Spatrick
201e5dd7070Spatrick // Handle classes that directly appear in std namespace.
202e5dd7070Spatrick if (Record->isInStdNamespace()) {
203e5dd7070Spatrick if (Record->hasAttr<OwnerAttr>() || Record->hasAttr<PointerAttr>())
204e5dd7070Spatrick return;
205e5dd7070Spatrick
206e5dd7070Spatrick if (StdOwners.count(Record->getName()))
207e5dd7070Spatrick addGslOwnerPointerAttributeIfNotExisting<OwnerAttr>(Context, Record);
208e5dd7070Spatrick else if (StdPointers.count(Record->getName()))
209e5dd7070Spatrick addGslOwnerPointerAttributeIfNotExisting<PointerAttr>(Context, Record);
210e5dd7070Spatrick
211e5dd7070Spatrick return;
212e5dd7070Spatrick }
213e5dd7070Spatrick
214e5dd7070Spatrick // Handle nested classes that could be a gsl::Pointer.
215e5dd7070Spatrick inferGslPointerAttribute(Record, Record);
216e5dd7070Spatrick }
217e5dd7070Spatrick
ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,SourceLocation PragmaLoc)218e5dd7070Spatrick void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
219e5dd7070Spatrick SourceLocation PragmaLoc) {
220e5dd7070Spatrick PragmaMsStackAction Action = Sema::PSK_Reset;
221a9ac8606Spatrick AlignPackInfo::Mode ModeVal = AlignPackInfo::Native;
222a9ac8606Spatrick
223e5dd7070Spatrick switch (Kind) {
224a9ac8606Spatrick // For most of the platforms we support, native and natural are the same.
225a9ac8606Spatrick // With XL, native is the same as power, natural means something else.
226e5dd7070Spatrick //
227e5dd7070Spatrick // FIXME: This is not true on Darwin/PPC.
228e5dd7070Spatrick case POAK_Native:
229e5dd7070Spatrick case POAK_Power:
230a9ac8606Spatrick Action = Sema::PSK_Push_Set;
231a9ac8606Spatrick break;
232e5dd7070Spatrick case POAK_Natural:
233e5dd7070Spatrick Action = Sema::PSK_Push_Set;
234a9ac8606Spatrick ModeVal = AlignPackInfo::Natural;
235e5dd7070Spatrick break;
236e5dd7070Spatrick
237e5dd7070Spatrick // Note that '#pragma options align=packed' is not equivalent to attribute
238e5dd7070Spatrick // packed, it has a different precedence relative to attribute aligned.
239e5dd7070Spatrick case POAK_Packed:
240e5dd7070Spatrick Action = Sema::PSK_Push_Set;
241a9ac8606Spatrick ModeVal = AlignPackInfo::Packed;
242e5dd7070Spatrick break;
243e5dd7070Spatrick
244e5dd7070Spatrick case POAK_Mac68k:
245e5dd7070Spatrick // Check if the target supports this.
246e5dd7070Spatrick if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) {
247e5dd7070Spatrick Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
248e5dd7070Spatrick return;
249e5dd7070Spatrick }
250e5dd7070Spatrick Action = Sema::PSK_Push_Set;
251a9ac8606Spatrick ModeVal = AlignPackInfo::Mac68k;
252e5dd7070Spatrick break;
253e5dd7070Spatrick case POAK_Reset:
254e5dd7070Spatrick // Reset just pops the top of the stack, or resets the current alignment to
255e5dd7070Spatrick // default.
256e5dd7070Spatrick Action = Sema::PSK_Pop;
257a9ac8606Spatrick if (AlignPackStack.Stack.empty()) {
258a9ac8606Spatrick if (AlignPackStack.CurrentValue.getAlignMode() != AlignPackInfo::Native ||
259a9ac8606Spatrick AlignPackStack.CurrentValue.IsPackAttr()) {
260e5dd7070Spatrick Action = Sema::PSK_Reset;
261e5dd7070Spatrick } else {
262e5dd7070Spatrick Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
263e5dd7070Spatrick << "stack empty";
264e5dd7070Spatrick return;
265e5dd7070Spatrick }
266e5dd7070Spatrick }
267e5dd7070Spatrick break;
268e5dd7070Spatrick }
269e5dd7070Spatrick
270a9ac8606Spatrick AlignPackInfo Info(ModeVal, getLangOpts().XLPragmaPack);
271a9ac8606Spatrick
272a9ac8606Spatrick AlignPackStack.Act(PragmaLoc, Action, StringRef(), Info);
273e5dd7070Spatrick }
274e5dd7070Spatrick
ActOnPragmaClangSection(SourceLocation PragmaLoc,PragmaClangSectionAction Action,PragmaClangSectionKind SecKind,StringRef SecName)275a9ac8606Spatrick void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc,
276a9ac8606Spatrick PragmaClangSectionAction Action,
277a9ac8606Spatrick PragmaClangSectionKind SecKind,
278a9ac8606Spatrick StringRef SecName) {
279e5dd7070Spatrick PragmaClangSection *CSec;
280ec727ea7Spatrick int SectionFlags = ASTContext::PSF_Read;
281e5dd7070Spatrick switch (SecKind) {
282e5dd7070Spatrick case PragmaClangSectionKind::PCSK_BSS:
283e5dd7070Spatrick CSec = &PragmaClangBSSSection;
284ec727ea7Spatrick SectionFlags |= ASTContext::PSF_Write | ASTContext::PSF_ZeroInit;
285e5dd7070Spatrick break;
286e5dd7070Spatrick case PragmaClangSectionKind::PCSK_Data:
287e5dd7070Spatrick CSec = &PragmaClangDataSection;
288ec727ea7Spatrick SectionFlags |= ASTContext::PSF_Write;
289e5dd7070Spatrick break;
290e5dd7070Spatrick case PragmaClangSectionKind::PCSK_Rodata:
291e5dd7070Spatrick CSec = &PragmaClangRodataSection;
292e5dd7070Spatrick break;
293e5dd7070Spatrick case PragmaClangSectionKind::PCSK_Relro:
294e5dd7070Spatrick CSec = &PragmaClangRelroSection;
295e5dd7070Spatrick break;
296e5dd7070Spatrick case PragmaClangSectionKind::PCSK_Text:
297e5dd7070Spatrick CSec = &PragmaClangTextSection;
298ec727ea7Spatrick SectionFlags |= ASTContext::PSF_Execute;
299e5dd7070Spatrick break;
300e5dd7070Spatrick default:
301e5dd7070Spatrick llvm_unreachable("invalid clang section kind");
302e5dd7070Spatrick }
303e5dd7070Spatrick
304e5dd7070Spatrick if (Action == PragmaClangSectionAction::PCSA_Clear) {
305e5dd7070Spatrick CSec->Valid = false;
306e5dd7070Spatrick return;
307e5dd7070Spatrick }
308e5dd7070Spatrick
309a9ac8606Spatrick if (llvm::Error E = isValidSectionSpecifier(SecName)) {
310a9ac8606Spatrick Diag(PragmaLoc, diag::err_pragma_section_invalid_for_target)
311a9ac8606Spatrick << toString(std::move(E));
312a9ac8606Spatrick CSec->Valid = false;
313a9ac8606Spatrick return;
314a9ac8606Spatrick }
315a9ac8606Spatrick
316ec727ea7Spatrick if (UnifySection(SecName, SectionFlags, PragmaLoc))
317ec727ea7Spatrick return;
318ec727ea7Spatrick
319e5dd7070Spatrick CSec->Valid = true;
320ec727ea7Spatrick CSec->SectionName = std::string(SecName);
321e5dd7070Spatrick CSec->PragmaLocation = PragmaLoc;
322e5dd7070Spatrick }
323e5dd7070Spatrick
ActOnPragmaPack(SourceLocation PragmaLoc,PragmaMsStackAction Action,StringRef SlotLabel,Expr * alignment)324e5dd7070Spatrick void Sema::ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action,
325e5dd7070Spatrick StringRef SlotLabel, Expr *alignment) {
326a9ac8606Spatrick bool IsXLPragma = getLangOpts().XLPragmaPack;
327a9ac8606Spatrick // XL pragma pack does not support identifier syntax.
328a9ac8606Spatrick if (IsXLPragma && !SlotLabel.empty()) {
329a9ac8606Spatrick Diag(PragmaLoc, diag::err_pragma_pack_identifer_not_supported);
330a9ac8606Spatrick return;
331a9ac8606Spatrick }
332a9ac8606Spatrick
333a9ac8606Spatrick const AlignPackInfo CurVal = AlignPackStack.CurrentValue;
334e5dd7070Spatrick Expr *Alignment = static_cast<Expr *>(alignment);
335e5dd7070Spatrick
336e5dd7070Spatrick // If specified then alignment must be a "small" power of two.
337e5dd7070Spatrick unsigned AlignmentVal = 0;
338a9ac8606Spatrick AlignPackInfo::Mode ModeVal = CurVal.getAlignMode();
339a9ac8606Spatrick
340e5dd7070Spatrick if (Alignment) {
341*12c85518Srobert std::optional<llvm::APSInt> Val;
342a9ac8606Spatrick Val = Alignment->getIntegerConstantExpr(Context);
343e5dd7070Spatrick
344e5dd7070Spatrick // pack(0) is like pack(), which just works out since that is what
345e5dd7070Spatrick // we use 0 for in PackAttr.
346*12c85518Srobert if (Alignment->isTypeDependent() || !Val ||
347a9ac8606Spatrick !(*Val == 0 || Val->isPowerOf2()) || Val->getZExtValue() > 16) {
348e5dd7070Spatrick Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
349e5dd7070Spatrick return; // Ignore
350e5dd7070Spatrick }
351e5dd7070Spatrick
352a9ac8606Spatrick if (IsXLPragma && *Val == 0) {
353a9ac8606Spatrick // pack(0) does not work out with XL.
354a9ac8606Spatrick Diag(PragmaLoc, diag::err_pragma_pack_invalid_alignment);
355a9ac8606Spatrick return; // Ignore
356e5dd7070Spatrick }
357a9ac8606Spatrick
358a9ac8606Spatrick AlignmentVal = (unsigned)Val->getZExtValue();
359a9ac8606Spatrick }
360a9ac8606Spatrick
361e5dd7070Spatrick if (Action == Sema::PSK_Show) {
362e5dd7070Spatrick // Show the current alignment, making sure to show the right value
363e5dd7070Spatrick // for the default.
364e5dd7070Spatrick // FIXME: This should come from the target.
365a9ac8606Spatrick AlignmentVal = CurVal.IsPackSet() ? CurVal.getPackNumber() : 8;
366a9ac8606Spatrick if (ModeVal == AlignPackInfo::Mac68k &&
367a9ac8606Spatrick (IsXLPragma || CurVal.IsAlignAttr()))
368e5dd7070Spatrick Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
369e5dd7070Spatrick else
370e5dd7070Spatrick Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
371e5dd7070Spatrick }
372a9ac8606Spatrick
373e5dd7070Spatrick // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
374e5dd7070Spatrick // "#pragma pack(pop, identifier, n) is undefined"
375e5dd7070Spatrick if (Action & Sema::PSK_Pop) {
376e5dd7070Spatrick if (Alignment && !SlotLabel.empty())
377e5dd7070Spatrick Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifier_and_alignment);
378a9ac8606Spatrick if (AlignPackStack.Stack.empty()) {
379a9ac8606Spatrick assert(CurVal.getAlignMode() == AlignPackInfo::Native &&
380a9ac8606Spatrick "Empty pack stack can only be at Native alignment mode.");
381e5dd7070Spatrick Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "pack" << "stack empty";
382e5dd7070Spatrick }
383e5dd7070Spatrick }
384e5dd7070Spatrick
385a9ac8606Spatrick AlignPackInfo Info(ModeVal, AlignmentVal, IsXLPragma);
386a9ac8606Spatrick
387a9ac8606Spatrick AlignPackStack.Act(PragmaLoc, Action, SlotLabel, Info);
388a9ac8606Spatrick }
389a9ac8606Spatrick
ConstantFoldAttrArgs(const AttributeCommonInfo & CI,MutableArrayRef<Expr * > Args)390*12c85518Srobert bool Sema::ConstantFoldAttrArgs(const AttributeCommonInfo &CI,
391*12c85518Srobert MutableArrayRef<Expr *> Args) {
392*12c85518Srobert llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
393*12c85518Srobert for (unsigned Idx = 0; Idx < Args.size(); Idx++) {
394*12c85518Srobert Expr *&E = Args.begin()[Idx];
395*12c85518Srobert assert(E && "error are handled before");
396*12c85518Srobert if (E->isValueDependent() || E->isTypeDependent())
397*12c85518Srobert continue;
398*12c85518Srobert
399*12c85518Srobert // FIXME: Use DefaultFunctionArrayLValueConversion() in place of the logic
400*12c85518Srobert // that adds implicit casts here.
401*12c85518Srobert if (E->getType()->isArrayType())
402*12c85518Srobert E = ImpCastExprToType(E, Context.getPointerType(E->getType()),
403*12c85518Srobert clang::CK_ArrayToPointerDecay)
404*12c85518Srobert .get();
405*12c85518Srobert if (E->getType()->isFunctionType())
406*12c85518Srobert E = ImplicitCastExpr::Create(Context,
407*12c85518Srobert Context.getPointerType(E->getType()),
408*12c85518Srobert clang::CK_FunctionToPointerDecay, E, nullptr,
409*12c85518Srobert VK_PRValue, FPOptionsOverride());
410*12c85518Srobert if (E->isLValue())
411*12c85518Srobert E = ImplicitCastExpr::Create(Context, E->getType().getNonReferenceType(),
412*12c85518Srobert clang::CK_LValueToRValue, E, nullptr,
413*12c85518Srobert VK_PRValue, FPOptionsOverride());
414*12c85518Srobert
415*12c85518Srobert Expr::EvalResult Eval;
416*12c85518Srobert Notes.clear();
417*12c85518Srobert Eval.Diag = &Notes;
418*12c85518Srobert
419*12c85518Srobert bool Result = E->EvaluateAsConstantExpr(Eval, Context);
420*12c85518Srobert
421*12c85518Srobert /// Result means the expression can be folded to a constant.
422*12c85518Srobert /// Note.empty() means the expression is a valid constant expression in the
423*12c85518Srobert /// current language mode.
424*12c85518Srobert if (!Result || !Notes.empty()) {
425*12c85518Srobert Diag(E->getBeginLoc(), diag::err_attribute_argument_n_type)
426*12c85518Srobert << CI << (Idx + 1) << AANT_ArgumentConstantExpr;
427*12c85518Srobert for (auto &Note : Notes)
428*12c85518Srobert Diag(Note.first, Note.second);
429*12c85518Srobert return false;
430*12c85518Srobert }
431*12c85518Srobert assert(Eval.Val.hasValue());
432*12c85518Srobert E = ConstantExpr::Create(Context, E, Eval.Val);
433*12c85518Srobert }
434*12c85518Srobert
435*12c85518Srobert return true;
436*12c85518Srobert }
437*12c85518Srobert
DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,SourceLocation IncludeLoc)438a9ac8606Spatrick void Sema::DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,
439e5dd7070Spatrick SourceLocation IncludeLoc) {
440a9ac8606Spatrick if (Kind == PragmaAlignPackDiagnoseKind::NonDefaultStateAtInclude) {
441a9ac8606Spatrick SourceLocation PrevLocation = AlignPackStack.CurrentPragmaLocation;
442e5dd7070Spatrick // Warn about non-default alignment at #includes (without redundant
443e5dd7070Spatrick // warnings for the same directive in nested includes).
444e5dd7070Spatrick // The warning is delayed until the end of the file to avoid warnings
445e5dd7070Spatrick // for files that don't have any records that are affected by the modified
446e5dd7070Spatrick // alignment.
447e5dd7070Spatrick bool HasNonDefaultValue =
448a9ac8606Spatrick AlignPackStack.hasValue() &&
449a9ac8606Spatrick (AlignPackIncludeStack.empty() ||
450a9ac8606Spatrick AlignPackIncludeStack.back().CurrentPragmaLocation != PrevLocation);
451a9ac8606Spatrick AlignPackIncludeStack.push_back(
452a9ac8606Spatrick {AlignPackStack.CurrentValue,
453a9ac8606Spatrick AlignPackStack.hasValue() ? PrevLocation : SourceLocation(),
454e5dd7070Spatrick HasNonDefaultValue, /*ShouldWarnOnInclude*/ false});
455e5dd7070Spatrick return;
456e5dd7070Spatrick }
457e5dd7070Spatrick
458a9ac8606Spatrick assert(Kind == PragmaAlignPackDiagnoseKind::ChangedStateAtExit &&
459a9ac8606Spatrick "invalid kind");
460a9ac8606Spatrick AlignPackIncludeState PrevAlignPackState =
461a9ac8606Spatrick AlignPackIncludeStack.pop_back_val();
462a9ac8606Spatrick // FIXME: AlignPackStack may contain both #pragma align and #pragma pack
463a9ac8606Spatrick // information, diagnostics below might not be accurate if we have mixed
464a9ac8606Spatrick // pragmas.
465a9ac8606Spatrick if (PrevAlignPackState.ShouldWarnOnInclude) {
466e5dd7070Spatrick // Emit the delayed non-default alignment at #include warning.
467e5dd7070Spatrick Diag(IncludeLoc, diag::warn_pragma_pack_non_default_at_include);
468a9ac8606Spatrick Diag(PrevAlignPackState.CurrentPragmaLocation, diag::note_pragma_pack_here);
469e5dd7070Spatrick }
470e5dd7070Spatrick // Warn about modified alignment after #includes.
471a9ac8606Spatrick if (PrevAlignPackState.CurrentValue != AlignPackStack.CurrentValue) {
472e5dd7070Spatrick Diag(IncludeLoc, diag::warn_pragma_pack_modified_after_include);
473a9ac8606Spatrick Diag(AlignPackStack.CurrentPragmaLocation, diag::note_pragma_pack_here);
474e5dd7070Spatrick }
475e5dd7070Spatrick }
476e5dd7070Spatrick
DiagnoseUnterminatedPragmaAlignPack()477a9ac8606Spatrick void Sema::DiagnoseUnterminatedPragmaAlignPack() {
478a9ac8606Spatrick if (AlignPackStack.Stack.empty())
479e5dd7070Spatrick return;
480e5dd7070Spatrick bool IsInnermost = true;
481a9ac8606Spatrick
482a9ac8606Spatrick // FIXME: AlignPackStack may contain both #pragma align and #pragma pack
483a9ac8606Spatrick // information, diagnostics below might not be accurate if we have mixed
484a9ac8606Spatrick // pragmas.
485a9ac8606Spatrick for (const auto &StackSlot : llvm::reverse(AlignPackStack.Stack)) {
486e5dd7070Spatrick Diag(StackSlot.PragmaPushLocation, diag::warn_pragma_pack_no_pop_eof);
487e5dd7070Spatrick // The user might have already reset the alignment, so suggest replacing
488e5dd7070Spatrick // the reset with a pop.
489a9ac8606Spatrick if (IsInnermost &&
490a9ac8606Spatrick AlignPackStack.CurrentValue == AlignPackStack.DefaultValue) {
491a9ac8606Spatrick auto DB = Diag(AlignPackStack.CurrentPragmaLocation,
492e5dd7070Spatrick diag::note_pragma_pack_pop_instead_reset);
493a9ac8606Spatrick SourceLocation FixItLoc =
494a9ac8606Spatrick Lexer::findLocationAfterToken(AlignPackStack.CurrentPragmaLocation,
495a9ac8606Spatrick tok::l_paren, SourceMgr, LangOpts,
496e5dd7070Spatrick /*SkipTrailing=*/false);
497e5dd7070Spatrick if (FixItLoc.isValid())
498e5dd7070Spatrick DB << FixItHint::CreateInsertion(FixItLoc, "pop");
499e5dd7070Spatrick }
500e5dd7070Spatrick IsInnermost = false;
501e5dd7070Spatrick }
502e5dd7070Spatrick }
503e5dd7070Spatrick
ActOnPragmaMSStruct(PragmaMSStructKind Kind)504e5dd7070Spatrick void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
505e5dd7070Spatrick MSStructPragmaOn = (Kind == PMSST_ON);
506e5dd7070Spatrick }
507e5dd7070Spatrick
ActOnPragmaMSComment(SourceLocation CommentLoc,PragmaMSCommentKind Kind,StringRef Arg)508e5dd7070Spatrick void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc,
509e5dd7070Spatrick PragmaMSCommentKind Kind, StringRef Arg) {
510e5dd7070Spatrick auto *PCD = PragmaCommentDecl::Create(
511e5dd7070Spatrick Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg);
512e5dd7070Spatrick Context.getTranslationUnitDecl()->addDecl(PCD);
513e5dd7070Spatrick Consumer.HandleTopLevelDecl(DeclGroupRef(PCD));
514e5dd7070Spatrick }
515e5dd7070Spatrick
ActOnPragmaDetectMismatch(SourceLocation Loc,StringRef Name,StringRef Value)516e5dd7070Spatrick void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name,
517e5dd7070Spatrick StringRef Value) {
518e5dd7070Spatrick auto *PDMD = PragmaDetectMismatchDecl::Create(
519e5dd7070Spatrick Context, Context.getTranslationUnitDecl(), Loc, Name, Value);
520e5dd7070Spatrick Context.getTranslationUnitDecl()->addDecl(PDMD);
521e5dd7070Spatrick Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD));
522e5dd7070Spatrick }
523e5dd7070Spatrick
ActOnPragmaFPEvalMethod(SourceLocation Loc,LangOptions::FPEvalMethodKind Value)524*12c85518Srobert void Sema::ActOnPragmaFPEvalMethod(SourceLocation Loc,
525*12c85518Srobert LangOptions::FPEvalMethodKind Value) {
526*12c85518Srobert FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
527*12c85518Srobert switch (Value) {
528*12c85518Srobert default:
529*12c85518Srobert llvm_unreachable("invalid pragma eval_method kind");
530*12c85518Srobert case LangOptions::FEM_Source:
531*12c85518Srobert NewFPFeatures.setFPEvalMethodOverride(LangOptions::FEM_Source);
532*12c85518Srobert break;
533*12c85518Srobert case LangOptions::FEM_Double:
534*12c85518Srobert NewFPFeatures.setFPEvalMethodOverride(LangOptions::FEM_Double);
535*12c85518Srobert break;
536*12c85518Srobert case LangOptions::FEM_Extended:
537*12c85518Srobert NewFPFeatures.setFPEvalMethodOverride(LangOptions::FEM_Extended);
538*12c85518Srobert break;
539*12c85518Srobert }
540*12c85518Srobert if (getLangOpts().ApproxFunc)
541*12c85518Srobert Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 0;
542*12c85518Srobert if (getLangOpts().AllowFPReassoc)
543*12c85518Srobert Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 1;
544*12c85518Srobert if (getLangOpts().AllowRecip)
545*12c85518Srobert Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 2;
546*12c85518Srobert FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
547*12c85518Srobert CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
548*12c85518Srobert PP.setCurrentFPEvalMethod(Loc, Value);
549*12c85518Srobert }
550*12c85518Srobert
ActOnPragmaFloatControl(SourceLocation Loc,PragmaMsStackAction Action,PragmaFloatControlKind Value)551ec727ea7Spatrick void Sema::ActOnPragmaFloatControl(SourceLocation Loc,
552ec727ea7Spatrick PragmaMsStackAction Action,
553ec727ea7Spatrick PragmaFloatControlKind Value) {
554a9ac8606Spatrick FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
555ec727ea7Spatrick if ((Action == PSK_Push_Set || Action == PSK_Push || Action == PSK_Pop) &&
556*12c85518Srobert !CurContext->getRedeclContext()->isFileContext()) {
557*12c85518Srobert // Push and pop can only occur at file or namespace scope, or within a
558*12c85518Srobert // language linkage declaration.
559ec727ea7Spatrick Diag(Loc, diag::err_pragma_fc_pp_scope);
560ec727ea7Spatrick return;
561ec727ea7Spatrick }
562ec727ea7Spatrick switch (Value) {
563ec727ea7Spatrick default:
564ec727ea7Spatrick llvm_unreachable("invalid pragma float_control kind");
565ec727ea7Spatrick case PFC_Precise:
566ec727ea7Spatrick NewFPFeatures.setFPPreciseEnabled(true);
567a9ac8606Spatrick FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
568ec727ea7Spatrick break;
569ec727ea7Spatrick case PFC_NoPrecise:
570*12c85518Srobert if (CurFPFeatures.getExceptionMode() == LangOptions::FPE_Strict)
571ec727ea7Spatrick Diag(Loc, diag::err_pragma_fc_noprecise_requires_noexcept);
572ec727ea7Spatrick else if (CurFPFeatures.getAllowFEnvAccess())
573ec727ea7Spatrick Diag(Loc, diag::err_pragma_fc_noprecise_requires_nofenv);
574ec727ea7Spatrick else
575ec727ea7Spatrick NewFPFeatures.setFPPreciseEnabled(false);
576a9ac8606Spatrick FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
577ec727ea7Spatrick break;
578ec727ea7Spatrick case PFC_Except:
579ec727ea7Spatrick if (!isPreciseFPEnabled())
580ec727ea7Spatrick Diag(Loc, diag::err_pragma_fc_except_requires_precise);
581ec727ea7Spatrick else
582*12c85518Srobert NewFPFeatures.setSpecifiedExceptionModeOverride(LangOptions::FPE_Strict);
583a9ac8606Spatrick FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
584ec727ea7Spatrick break;
585ec727ea7Spatrick case PFC_NoExcept:
586*12c85518Srobert NewFPFeatures.setSpecifiedExceptionModeOverride(LangOptions::FPE_Ignore);
587a9ac8606Spatrick FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
588ec727ea7Spatrick break;
589ec727ea7Spatrick case PFC_Push:
590a9ac8606Spatrick FpPragmaStack.Act(Loc, Sema::PSK_Push_Set, StringRef(), NewFPFeatures);
591ec727ea7Spatrick break;
592ec727ea7Spatrick case PFC_Pop:
593ec727ea7Spatrick if (FpPragmaStack.Stack.empty()) {
594ec727ea7Spatrick Diag(Loc, diag::warn_pragma_pop_failed) << "float_control"
595ec727ea7Spatrick << "stack empty";
596ec727ea7Spatrick return;
597ec727ea7Spatrick }
598a9ac8606Spatrick FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
599a9ac8606Spatrick NewFPFeatures = FpPragmaStack.CurrentValue;
600ec727ea7Spatrick break;
601ec727ea7Spatrick }
602a9ac8606Spatrick CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
603ec727ea7Spatrick }
604ec727ea7Spatrick
ActOnPragmaMSPointersToMembers(LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,SourceLocation PragmaLoc)605e5dd7070Spatrick void Sema::ActOnPragmaMSPointersToMembers(
606e5dd7070Spatrick LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,
607e5dd7070Spatrick SourceLocation PragmaLoc) {
608e5dd7070Spatrick MSPointerToMemberRepresentationMethod = RepresentationMethod;
609e5dd7070Spatrick ImplicitMSInheritanceAttrLoc = PragmaLoc;
610e5dd7070Spatrick }
611e5dd7070Spatrick
ActOnPragmaMSVtorDisp(PragmaMsStackAction Action,SourceLocation PragmaLoc,MSVtorDispMode Mode)612e5dd7070Spatrick void Sema::ActOnPragmaMSVtorDisp(PragmaMsStackAction Action,
613e5dd7070Spatrick SourceLocation PragmaLoc,
614e5dd7070Spatrick MSVtorDispMode Mode) {
615e5dd7070Spatrick if (Action & PSK_Pop && VtorDispStack.Stack.empty())
616e5dd7070Spatrick Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
617e5dd7070Spatrick << "stack empty";
618e5dd7070Spatrick VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode);
619e5dd7070Spatrick }
620e5dd7070Spatrick
621a9ac8606Spatrick template <>
Act(SourceLocation PragmaLocation,PragmaMsStackAction Action,llvm::StringRef StackSlotLabel,AlignPackInfo Value)622a9ac8606Spatrick void Sema::PragmaStack<Sema::AlignPackInfo>::Act(SourceLocation PragmaLocation,
623a9ac8606Spatrick PragmaMsStackAction Action,
624a9ac8606Spatrick llvm::StringRef StackSlotLabel,
625a9ac8606Spatrick AlignPackInfo Value) {
626a9ac8606Spatrick if (Action == PSK_Reset) {
627a9ac8606Spatrick CurrentValue = DefaultValue;
628a9ac8606Spatrick CurrentPragmaLocation = PragmaLocation;
629a9ac8606Spatrick return;
630a9ac8606Spatrick }
631a9ac8606Spatrick if (Action & PSK_Push)
632a9ac8606Spatrick Stack.emplace_back(Slot(StackSlotLabel, CurrentValue, CurrentPragmaLocation,
633a9ac8606Spatrick PragmaLocation));
634a9ac8606Spatrick else if (Action & PSK_Pop) {
635a9ac8606Spatrick if (!StackSlotLabel.empty()) {
636a9ac8606Spatrick // If we've got a label, try to find it and jump there.
637a9ac8606Spatrick auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) {
638a9ac8606Spatrick return x.StackSlotLabel == StackSlotLabel;
639a9ac8606Spatrick });
640a9ac8606Spatrick // We found the label, so pop from there.
641a9ac8606Spatrick if (I != Stack.rend()) {
642a9ac8606Spatrick CurrentValue = I->Value;
643a9ac8606Spatrick CurrentPragmaLocation = I->PragmaLocation;
644a9ac8606Spatrick Stack.erase(std::prev(I.base()), Stack.end());
645a9ac8606Spatrick }
646a9ac8606Spatrick } else if (Value.IsXLStack() && Value.IsAlignAttr() &&
647a9ac8606Spatrick CurrentValue.IsPackAttr()) {
648a9ac8606Spatrick // XL '#pragma align(reset)' would pop the stack until
649a9ac8606Spatrick // a current in effect pragma align is popped.
650a9ac8606Spatrick auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) {
651a9ac8606Spatrick return x.Value.IsAlignAttr();
652a9ac8606Spatrick });
653a9ac8606Spatrick // If we found pragma align so pop from there.
654a9ac8606Spatrick if (I != Stack.rend()) {
655a9ac8606Spatrick Stack.erase(std::prev(I.base()), Stack.end());
656a9ac8606Spatrick if (Stack.empty()) {
657a9ac8606Spatrick CurrentValue = DefaultValue;
658a9ac8606Spatrick CurrentPragmaLocation = PragmaLocation;
659a9ac8606Spatrick } else {
660a9ac8606Spatrick CurrentValue = Stack.back().Value;
661a9ac8606Spatrick CurrentPragmaLocation = Stack.back().PragmaLocation;
662a9ac8606Spatrick Stack.pop_back();
663a9ac8606Spatrick }
664a9ac8606Spatrick }
665a9ac8606Spatrick } else if (!Stack.empty()) {
666a9ac8606Spatrick // xl '#pragma align' sets the baseline, and `#pragma pack` cannot pop
667a9ac8606Spatrick // over the baseline.
668a9ac8606Spatrick if (Value.IsXLStack() && Value.IsPackAttr() && CurrentValue.IsAlignAttr())
669a9ac8606Spatrick return;
670a9ac8606Spatrick
671a9ac8606Spatrick // We don't have a label, just pop the last entry.
672a9ac8606Spatrick CurrentValue = Stack.back().Value;
673a9ac8606Spatrick CurrentPragmaLocation = Stack.back().PragmaLocation;
674a9ac8606Spatrick Stack.pop_back();
675a9ac8606Spatrick }
676a9ac8606Spatrick }
677a9ac8606Spatrick if (Action & PSK_Set) {
678a9ac8606Spatrick CurrentValue = Value;
679a9ac8606Spatrick CurrentPragmaLocation = PragmaLocation;
680a9ac8606Spatrick }
681a9ac8606Spatrick }
682a9ac8606Spatrick
UnifySection(StringRef SectionName,int SectionFlags,NamedDecl * Decl)683a9ac8606Spatrick bool Sema::UnifySection(StringRef SectionName, int SectionFlags,
684a9ac8606Spatrick NamedDecl *Decl) {
685ec727ea7Spatrick SourceLocation PragmaLocation;
686ec727ea7Spatrick if (auto A = Decl->getAttr<SectionAttr>())
687ec727ea7Spatrick if (A->isImplicit())
688ec727ea7Spatrick PragmaLocation = A->getLocation();
689ec727ea7Spatrick auto SectionIt = Context.SectionInfos.find(SectionName);
690ec727ea7Spatrick if (SectionIt == Context.SectionInfos.end()) {
691e5dd7070Spatrick Context.SectionInfos[SectionName] =
692ec727ea7Spatrick ASTContext::SectionInfo(Decl, PragmaLocation, SectionFlags);
693e5dd7070Spatrick return false;
694e5dd7070Spatrick }
695e5dd7070Spatrick // A pre-declared section takes precedence w/o diagnostic.
696ec727ea7Spatrick const auto &Section = SectionIt->second;
697ec727ea7Spatrick if (Section.SectionFlags == SectionFlags ||
698ec727ea7Spatrick ((SectionFlags & ASTContext::PSF_Implicit) &&
699ec727ea7Spatrick !(Section.SectionFlags & ASTContext::PSF_Implicit)))
700e5dd7070Spatrick return false;
701ec727ea7Spatrick Diag(Decl->getLocation(), diag::err_section_conflict) << Decl << Section;
702ec727ea7Spatrick if (Section.Decl)
703ec727ea7Spatrick Diag(Section.Decl->getLocation(), diag::note_declared_at)
704ec727ea7Spatrick << Section.Decl->getName();
705ec727ea7Spatrick if (PragmaLocation.isValid())
706ec727ea7Spatrick Diag(PragmaLocation, diag::note_pragma_entered_here);
707ec727ea7Spatrick if (Section.PragmaSectionLocation.isValid())
708ec727ea7Spatrick Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here);
709e5dd7070Spatrick return true;
710e5dd7070Spatrick }
711e5dd7070Spatrick
UnifySection(StringRef SectionName,int SectionFlags,SourceLocation PragmaSectionLocation)712e5dd7070Spatrick bool Sema::UnifySection(StringRef SectionName,
713e5dd7070Spatrick int SectionFlags,
714e5dd7070Spatrick SourceLocation PragmaSectionLocation) {
715ec727ea7Spatrick auto SectionIt = Context.SectionInfos.find(SectionName);
716ec727ea7Spatrick if (SectionIt != Context.SectionInfos.end()) {
717ec727ea7Spatrick const auto &Section = SectionIt->second;
718ec727ea7Spatrick if (Section.SectionFlags == SectionFlags)
719e5dd7070Spatrick return false;
720ec727ea7Spatrick if (!(Section.SectionFlags & ASTContext::PSF_Implicit)) {
721e5dd7070Spatrick Diag(PragmaSectionLocation, diag::err_section_conflict)
722ec727ea7Spatrick << "this" << Section;
723ec727ea7Spatrick if (Section.Decl)
724ec727ea7Spatrick Diag(Section.Decl->getLocation(), diag::note_declared_at)
725ec727ea7Spatrick << Section.Decl->getName();
726ec727ea7Spatrick if (Section.PragmaSectionLocation.isValid())
727ec727ea7Spatrick Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here);
728e5dd7070Spatrick return true;
729e5dd7070Spatrick }
730e5dd7070Spatrick }
731e5dd7070Spatrick Context.SectionInfos[SectionName] =
732e5dd7070Spatrick ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags);
733e5dd7070Spatrick return false;
734e5dd7070Spatrick }
735e5dd7070Spatrick
736e5dd7070Spatrick /// Called on well formed \#pragma bss_seg().
ActOnPragmaMSSeg(SourceLocation PragmaLocation,PragmaMsStackAction Action,llvm::StringRef StackSlotLabel,StringLiteral * SegmentName,llvm::StringRef PragmaName)737e5dd7070Spatrick void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation,
738e5dd7070Spatrick PragmaMsStackAction Action,
739e5dd7070Spatrick llvm::StringRef StackSlotLabel,
740e5dd7070Spatrick StringLiteral *SegmentName,
741e5dd7070Spatrick llvm::StringRef PragmaName) {
742e5dd7070Spatrick PragmaStack<StringLiteral *> *Stack =
743e5dd7070Spatrick llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName)
744e5dd7070Spatrick .Case("data_seg", &DataSegStack)
745e5dd7070Spatrick .Case("bss_seg", &BSSSegStack)
746e5dd7070Spatrick .Case("const_seg", &ConstSegStack)
747e5dd7070Spatrick .Case("code_seg", &CodeSegStack);
748e5dd7070Spatrick if (Action & PSK_Pop && Stack->Stack.empty())
749e5dd7070Spatrick Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName
750e5dd7070Spatrick << "stack empty";
751e5dd7070Spatrick if (SegmentName) {
752e5dd7070Spatrick if (!checkSectionName(SegmentName->getBeginLoc(), SegmentName->getString()))
753e5dd7070Spatrick return;
754e5dd7070Spatrick
755e5dd7070Spatrick if (SegmentName->getString() == ".drectve" &&
756e5dd7070Spatrick Context.getTargetInfo().getCXXABI().isMicrosoft())
757e5dd7070Spatrick Diag(PragmaLocation, diag::warn_attribute_section_drectve) << PragmaName;
758e5dd7070Spatrick }
759e5dd7070Spatrick
760e5dd7070Spatrick Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName);
761e5dd7070Spatrick }
762e5dd7070Spatrick
763*12c85518Srobert /// Called on well formed \#pragma strict_gs_check().
ActOnPragmaMSStrictGuardStackCheck(SourceLocation PragmaLocation,PragmaMsStackAction Action,bool Value)764*12c85518Srobert void Sema::ActOnPragmaMSStrictGuardStackCheck(SourceLocation PragmaLocation,
765*12c85518Srobert PragmaMsStackAction Action,
766*12c85518Srobert bool Value) {
767*12c85518Srobert if (Action & PSK_Pop && StrictGuardStackCheckStack.Stack.empty())
768*12c85518Srobert Diag(PragmaLocation, diag::warn_pragma_pop_failed) << "strict_gs_check"
769*12c85518Srobert << "stack empty";
770*12c85518Srobert
771*12c85518Srobert StrictGuardStackCheckStack.Act(PragmaLocation, Action, StringRef(), Value);
772*12c85518Srobert }
773*12c85518Srobert
774e5dd7070Spatrick /// Called on well formed \#pragma bss_seg().
ActOnPragmaMSSection(SourceLocation PragmaLocation,int SectionFlags,StringLiteral * SegmentName)775e5dd7070Spatrick void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation,
776e5dd7070Spatrick int SectionFlags, StringLiteral *SegmentName) {
777e5dd7070Spatrick UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation);
778e5dd7070Spatrick }
779e5dd7070Spatrick
ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,StringLiteral * SegmentName)780e5dd7070Spatrick void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,
781e5dd7070Spatrick StringLiteral *SegmentName) {
782e5dd7070Spatrick // There's no stack to maintain, so we just have a current section. When we
783e5dd7070Spatrick // see the default section, reset our current section back to null so we stop
784e5dd7070Spatrick // tacking on unnecessary attributes.
785e5dd7070Spatrick CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName;
786e5dd7070Spatrick CurInitSegLoc = PragmaLocation;
787e5dd7070Spatrick }
788e5dd7070Spatrick
ActOnPragmaMSAllocText(SourceLocation PragmaLocation,StringRef Section,const SmallVector<std::tuple<IdentifierInfo *,SourceLocation>> & Functions)789*12c85518Srobert void Sema::ActOnPragmaMSAllocText(
790*12c85518Srobert SourceLocation PragmaLocation, StringRef Section,
791*12c85518Srobert const SmallVector<std::tuple<IdentifierInfo *, SourceLocation>>
792*12c85518Srobert &Functions) {
793*12c85518Srobert if (!CurContext->getRedeclContext()->isFileContext()) {
794*12c85518Srobert Diag(PragmaLocation, diag::err_pragma_expected_file_scope) << "alloc_text";
795*12c85518Srobert return;
796*12c85518Srobert }
797*12c85518Srobert
798*12c85518Srobert for (auto &Function : Functions) {
799*12c85518Srobert IdentifierInfo *II;
800*12c85518Srobert SourceLocation Loc;
801*12c85518Srobert std::tie(II, Loc) = Function;
802*12c85518Srobert
803*12c85518Srobert DeclarationName DN(II);
804*12c85518Srobert NamedDecl *ND = LookupSingleName(TUScope, DN, Loc, LookupOrdinaryName);
805*12c85518Srobert if (!ND) {
806*12c85518Srobert Diag(Loc, diag::err_undeclared_use) << II->getName();
807*12c85518Srobert return;
808*12c85518Srobert }
809*12c85518Srobert
810*12c85518Srobert auto *FD = dyn_cast<FunctionDecl>(ND->getCanonicalDecl());
811*12c85518Srobert if (!FD) {
812*12c85518Srobert Diag(Loc, diag::err_pragma_alloc_text_not_function);
813*12c85518Srobert return;
814*12c85518Srobert }
815*12c85518Srobert
816*12c85518Srobert if (getLangOpts().CPlusPlus && !FD->isInExternCContext()) {
817*12c85518Srobert Diag(Loc, diag::err_pragma_alloc_text_c_linkage);
818*12c85518Srobert return;
819*12c85518Srobert }
820*12c85518Srobert
821*12c85518Srobert FunctionToSectionMap[II->getName()] = std::make_tuple(Section, Loc);
822*12c85518Srobert }
823*12c85518Srobert }
824*12c85518Srobert
ActOnPragmaUnused(const Token & IdTok,Scope * curScope,SourceLocation PragmaLoc)825e5dd7070Spatrick void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
826e5dd7070Spatrick SourceLocation PragmaLoc) {
827e5dd7070Spatrick
828e5dd7070Spatrick IdentifierInfo *Name = IdTok.getIdentifierInfo();
829e5dd7070Spatrick LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
830e5dd7070Spatrick LookupParsedName(Lookup, curScope, nullptr, true);
831e5dd7070Spatrick
832e5dd7070Spatrick if (Lookup.empty()) {
833e5dd7070Spatrick Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
834e5dd7070Spatrick << Name << SourceRange(IdTok.getLocation());
835e5dd7070Spatrick return;
836e5dd7070Spatrick }
837e5dd7070Spatrick
838e5dd7070Spatrick VarDecl *VD = Lookup.getAsSingle<VarDecl>();
839e5dd7070Spatrick if (!VD) {
840e5dd7070Spatrick Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
841e5dd7070Spatrick << Name << SourceRange(IdTok.getLocation());
842e5dd7070Spatrick return;
843e5dd7070Spatrick }
844e5dd7070Spatrick
845e5dd7070Spatrick // Warn if this was used before being marked unused.
846e5dd7070Spatrick if (VD->isUsed())
847e5dd7070Spatrick Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
848e5dd7070Spatrick
849e5dd7070Spatrick VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation(),
850e5dd7070Spatrick AttributeCommonInfo::AS_Pragma,
851e5dd7070Spatrick UnusedAttr::GNU_unused));
852e5dd7070Spatrick }
853e5dd7070Spatrick
AddCFAuditedAttribute(Decl * D)854e5dd7070Spatrick void Sema::AddCFAuditedAttribute(Decl *D) {
855e5dd7070Spatrick IdentifierInfo *Ident;
856e5dd7070Spatrick SourceLocation Loc;
857e5dd7070Spatrick std::tie(Ident, Loc) = PP.getPragmaARCCFCodeAuditedInfo();
858e5dd7070Spatrick if (!Loc.isValid()) return;
859e5dd7070Spatrick
860e5dd7070Spatrick // Don't add a redundant or conflicting attribute.
861e5dd7070Spatrick if (D->hasAttr<CFAuditedTransferAttr>() ||
862e5dd7070Spatrick D->hasAttr<CFUnknownTransferAttr>())
863e5dd7070Spatrick return;
864e5dd7070Spatrick
865e5dd7070Spatrick AttributeCommonInfo Info(Ident, SourceRange(Loc),
866e5dd7070Spatrick AttributeCommonInfo::AS_Pragma);
867e5dd7070Spatrick D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Info));
868e5dd7070Spatrick }
869e5dd7070Spatrick
870e5dd7070Spatrick namespace {
871e5dd7070Spatrick
872*12c85518Srobert std::optional<attr::SubjectMatchRule>
getParentAttrMatcherRule(attr::SubjectMatchRule Rule)873e5dd7070Spatrick getParentAttrMatcherRule(attr::SubjectMatchRule Rule) {
874e5dd7070Spatrick using namespace attr;
875e5dd7070Spatrick switch (Rule) {
876e5dd7070Spatrick default:
877*12c85518Srobert return std::nullopt;
878e5dd7070Spatrick #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
879e5dd7070Spatrick #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \
880e5dd7070Spatrick case Value: \
881e5dd7070Spatrick return Parent;
882e5dd7070Spatrick #include "clang/Basic/AttrSubMatchRulesList.inc"
883e5dd7070Spatrick }
884e5dd7070Spatrick }
885e5dd7070Spatrick
isNegatedAttrMatcherSubRule(attr::SubjectMatchRule Rule)886e5dd7070Spatrick bool isNegatedAttrMatcherSubRule(attr::SubjectMatchRule Rule) {
887e5dd7070Spatrick using namespace attr;
888e5dd7070Spatrick switch (Rule) {
889e5dd7070Spatrick default:
890e5dd7070Spatrick return false;
891e5dd7070Spatrick #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
892e5dd7070Spatrick #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \
893e5dd7070Spatrick case Value: \
894e5dd7070Spatrick return IsNegated;
895e5dd7070Spatrick #include "clang/Basic/AttrSubMatchRulesList.inc"
896e5dd7070Spatrick }
897e5dd7070Spatrick }
898e5dd7070Spatrick
replacementRangeForListElement(const Sema & S,SourceRange Range)899e5dd7070Spatrick CharSourceRange replacementRangeForListElement(const Sema &S,
900e5dd7070Spatrick SourceRange Range) {
901e5dd7070Spatrick // Make sure that the ',' is removed as well.
902e5dd7070Spatrick SourceLocation AfterCommaLoc = Lexer::findLocationAfterToken(
903e5dd7070Spatrick Range.getEnd(), tok::comma, S.getSourceManager(), S.getLangOpts(),
904e5dd7070Spatrick /*SkipTrailingWhitespaceAndNewLine=*/false);
905e5dd7070Spatrick if (AfterCommaLoc.isValid())
906e5dd7070Spatrick return CharSourceRange::getCharRange(Range.getBegin(), AfterCommaLoc);
907e5dd7070Spatrick else
908e5dd7070Spatrick return CharSourceRange::getTokenRange(Range);
909e5dd7070Spatrick }
910e5dd7070Spatrick
911e5dd7070Spatrick std::string
attrMatcherRuleListToString(ArrayRef<attr::SubjectMatchRule> Rules)912e5dd7070Spatrick attrMatcherRuleListToString(ArrayRef<attr::SubjectMatchRule> Rules) {
913e5dd7070Spatrick std::string Result;
914e5dd7070Spatrick llvm::raw_string_ostream OS(Result);
915e5dd7070Spatrick for (const auto &I : llvm::enumerate(Rules)) {
916e5dd7070Spatrick if (I.index())
917e5dd7070Spatrick OS << (I.index() == Rules.size() - 1 ? ", and " : ", ");
918e5dd7070Spatrick OS << "'" << attr::getSubjectMatchRuleSpelling(I.value()) << "'";
919e5dd7070Spatrick }
920*12c85518Srobert return Result;
921e5dd7070Spatrick }
922e5dd7070Spatrick
923e5dd7070Spatrick } // end anonymous namespace
924e5dd7070Spatrick
ActOnPragmaAttributeAttribute(ParsedAttr & Attribute,SourceLocation PragmaLoc,attr::ParsedSubjectMatchRuleSet Rules)925e5dd7070Spatrick void Sema::ActOnPragmaAttributeAttribute(
926e5dd7070Spatrick ParsedAttr &Attribute, SourceLocation PragmaLoc,
927e5dd7070Spatrick attr::ParsedSubjectMatchRuleSet Rules) {
928e5dd7070Spatrick Attribute.setIsPragmaClangAttribute();
929e5dd7070Spatrick SmallVector<attr::SubjectMatchRule, 4> SubjectMatchRules;
930e5dd7070Spatrick // Gather the subject match rules that are supported by the attribute.
931e5dd7070Spatrick SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4>
932e5dd7070Spatrick StrictSubjectMatchRuleSet;
933e5dd7070Spatrick Attribute.getMatchRules(LangOpts, StrictSubjectMatchRuleSet);
934e5dd7070Spatrick
935e5dd7070Spatrick // Figure out which subject matching rules are valid.
936e5dd7070Spatrick if (StrictSubjectMatchRuleSet.empty()) {
937e5dd7070Spatrick // Check for contradicting match rules. Contradicting match rules are
938e5dd7070Spatrick // either:
939e5dd7070Spatrick // - a top-level rule and one of its sub-rules. E.g. variable and
940e5dd7070Spatrick // variable(is_parameter).
941e5dd7070Spatrick // - a sub-rule and a sibling that's negated. E.g.
942e5dd7070Spatrick // variable(is_thread_local) and variable(unless(is_parameter))
943e5dd7070Spatrick llvm::SmallDenseMap<int, std::pair<int, SourceRange>, 2>
944e5dd7070Spatrick RulesToFirstSpecifiedNegatedSubRule;
945e5dd7070Spatrick for (const auto &Rule : Rules) {
946e5dd7070Spatrick attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
947*12c85518Srobert std::optional<attr::SubjectMatchRule> ParentRule =
948e5dd7070Spatrick getParentAttrMatcherRule(MatchRule);
949e5dd7070Spatrick if (!ParentRule)
950e5dd7070Spatrick continue;
951e5dd7070Spatrick auto It = Rules.find(*ParentRule);
952e5dd7070Spatrick if (It != Rules.end()) {
953e5dd7070Spatrick // A sub-rule contradicts a parent rule.
954e5dd7070Spatrick Diag(Rule.second.getBegin(),
955e5dd7070Spatrick diag::err_pragma_attribute_matcher_subrule_contradicts_rule)
956e5dd7070Spatrick << attr::getSubjectMatchRuleSpelling(MatchRule)
957e5dd7070Spatrick << attr::getSubjectMatchRuleSpelling(*ParentRule) << It->second
958e5dd7070Spatrick << FixItHint::CreateRemoval(
959e5dd7070Spatrick replacementRangeForListElement(*this, Rule.second));
960e5dd7070Spatrick // Keep going without removing this rule as it won't change the set of
961e5dd7070Spatrick // declarations that receive the attribute.
962e5dd7070Spatrick continue;
963e5dd7070Spatrick }
964e5dd7070Spatrick if (isNegatedAttrMatcherSubRule(MatchRule))
965e5dd7070Spatrick RulesToFirstSpecifiedNegatedSubRule.insert(
966e5dd7070Spatrick std::make_pair(*ParentRule, Rule));
967e5dd7070Spatrick }
968e5dd7070Spatrick bool IgnoreNegatedSubRules = false;
969e5dd7070Spatrick for (const auto &Rule : Rules) {
970e5dd7070Spatrick attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
971*12c85518Srobert std::optional<attr::SubjectMatchRule> ParentRule =
972e5dd7070Spatrick getParentAttrMatcherRule(MatchRule);
973e5dd7070Spatrick if (!ParentRule)
974e5dd7070Spatrick continue;
975e5dd7070Spatrick auto It = RulesToFirstSpecifiedNegatedSubRule.find(*ParentRule);
976e5dd7070Spatrick if (It != RulesToFirstSpecifiedNegatedSubRule.end() &&
977e5dd7070Spatrick It->second != Rule) {
978e5dd7070Spatrick // Negated sub-rule contradicts another sub-rule.
979e5dd7070Spatrick Diag(
980e5dd7070Spatrick It->second.second.getBegin(),
981e5dd7070Spatrick diag::
982e5dd7070Spatrick err_pragma_attribute_matcher_negated_subrule_contradicts_subrule)
983e5dd7070Spatrick << attr::getSubjectMatchRuleSpelling(
984e5dd7070Spatrick attr::SubjectMatchRule(It->second.first))
985e5dd7070Spatrick << attr::getSubjectMatchRuleSpelling(MatchRule) << Rule.second
986e5dd7070Spatrick << FixItHint::CreateRemoval(
987e5dd7070Spatrick replacementRangeForListElement(*this, It->second.second));
988e5dd7070Spatrick // Keep going but ignore all of the negated sub-rules.
989e5dd7070Spatrick IgnoreNegatedSubRules = true;
990e5dd7070Spatrick RulesToFirstSpecifiedNegatedSubRule.erase(It);
991e5dd7070Spatrick }
992e5dd7070Spatrick }
993e5dd7070Spatrick
994e5dd7070Spatrick if (!IgnoreNegatedSubRules) {
995e5dd7070Spatrick for (const auto &Rule : Rules)
996e5dd7070Spatrick SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
997e5dd7070Spatrick } else {
998e5dd7070Spatrick for (const auto &Rule : Rules) {
999e5dd7070Spatrick if (!isNegatedAttrMatcherSubRule(attr::SubjectMatchRule(Rule.first)))
1000e5dd7070Spatrick SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
1001e5dd7070Spatrick }
1002e5dd7070Spatrick }
1003e5dd7070Spatrick Rules.clear();
1004e5dd7070Spatrick } else {
1005a9ac8606Spatrick // Each rule in Rules must be a strict subset of the attribute's
1006a9ac8606Spatrick // SubjectMatch rules. I.e. we're allowed to use
1007a9ac8606Spatrick // `apply_to=variables(is_global)` on an attrubute with SubjectList<[Var]>,
1008a9ac8606Spatrick // but should not allow `apply_to=variables` on an attribute which has
1009a9ac8606Spatrick // `SubjectList<[GlobalVar]>`.
1010a9ac8606Spatrick for (const auto &StrictRule : StrictSubjectMatchRuleSet) {
1011a9ac8606Spatrick // First, check for exact match.
1012a9ac8606Spatrick if (Rules.erase(StrictRule.first)) {
1013e5dd7070Spatrick // Add the rule to the set of attribute receivers only if it's supported
1014e5dd7070Spatrick // in the current language mode.
1015a9ac8606Spatrick if (StrictRule.second)
1016a9ac8606Spatrick SubjectMatchRules.push_back(StrictRule.first);
1017a9ac8606Spatrick }
1018a9ac8606Spatrick }
1019a9ac8606Spatrick // Check remaining rules for subset matches.
1020a9ac8606Spatrick auto RulesToCheck = Rules;
1021a9ac8606Spatrick for (const auto &Rule : RulesToCheck) {
1022a9ac8606Spatrick attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
1023a9ac8606Spatrick if (auto ParentRule = getParentAttrMatcherRule(MatchRule)) {
1024a9ac8606Spatrick if (llvm::any_of(StrictSubjectMatchRuleSet,
1025a9ac8606Spatrick [ParentRule](const auto &StrictRule) {
1026a9ac8606Spatrick return StrictRule.first == *ParentRule &&
1027a9ac8606Spatrick StrictRule.second; // IsEnabled
1028a9ac8606Spatrick })) {
1029a9ac8606Spatrick SubjectMatchRules.push_back(MatchRule);
1030a9ac8606Spatrick Rules.erase(MatchRule);
1031a9ac8606Spatrick }
1032e5dd7070Spatrick }
1033e5dd7070Spatrick }
1034e5dd7070Spatrick }
1035e5dd7070Spatrick
1036e5dd7070Spatrick if (!Rules.empty()) {
1037e5dd7070Spatrick auto Diagnostic =
1038e5dd7070Spatrick Diag(PragmaLoc, diag::err_pragma_attribute_invalid_matchers)
1039e5dd7070Spatrick << Attribute;
1040e5dd7070Spatrick SmallVector<attr::SubjectMatchRule, 2> ExtraRules;
1041e5dd7070Spatrick for (const auto &Rule : Rules) {
1042e5dd7070Spatrick ExtraRules.push_back(attr::SubjectMatchRule(Rule.first));
1043e5dd7070Spatrick Diagnostic << FixItHint::CreateRemoval(
1044e5dd7070Spatrick replacementRangeForListElement(*this, Rule.second));
1045e5dd7070Spatrick }
1046e5dd7070Spatrick Diagnostic << attrMatcherRuleListToString(ExtraRules);
1047e5dd7070Spatrick }
1048e5dd7070Spatrick
1049e5dd7070Spatrick if (PragmaAttributeStack.empty()) {
1050e5dd7070Spatrick Diag(PragmaLoc, diag::err_pragma_attr_attr_no_push);
1051e5dd7070Spatrick return;
1052e5dd7070Spatrick }
1053e5dd7070Spatrick
1054e5dd7070Spatrick PragmaAttributeStack.back().Entries.push_back(
1055e5dd7070Spatrick {PragmaLoc, &Attribute, std::move(SubjectMatchRules), /*IsUsed=*/false});
1056e5dd7070Spatrick }
1057e5dd7070Spatrick
ActOnPragmaAttributeEmptyPush(SourceLocation PragmaLoc,const IdentifierInfo * Namespace)1058e5dd7070Spatrick void Sema::ActOnPragmaAttributeEmptyPush(SourceLocation PragmaLoc,
1059e5dd7070Spatrick const IdentifierInfo *Namespace) {
1060e5dd7070Spatrick PragmaAttributeStack.emplace_back();
1061e5dd7070Spatrick PragmaAttributeStack.back().Loc = PragmaLoc;
1062e5dd7070Spatrick PragmaAttributeStack.back().Namespace = Namespace;
1063e5dd7070Spatrick }
1064e5dd7070Spatrick
ActOnPragmaAttributePop(SourceLocation PragmaLoc,const IdentifierInfo * Namespace)1065e5dd7070Spatrick void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc,
1066e5dd7070Spatrick const IdentifierInfo *Namespace) {
1067e5dd7070Spatrick if (PragmaAttributeStack.empty()) {
1068e5dd7070Spatrick Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
1069e5dd7070Spatrick return;
1070e5dd7070Spatrick }
1071e5dd7070Spatrick
1072e5dd7070Spatrick // Dig back through the stack trying to find the most recently pushed group
1073e5dd7070Spatrick // that in Namespace. Note that this works fine if no namespace is present,
1074e5dd7070Spatrick // think of push/pops without namespaces as having an implicit "nullptr"
1075e5dd7070Spatrick // namespace.
1076e5dd7070Spatrick for (size_t Index = PragmaAttributeStack.size(); Index;) {
1077e5dd7070Spatrick --Index;
1078e5dd7070Spatrick if (PragmaAttributeStack[Index].Namespace == Namespace) {
1079e5dd7070Spatrick for (const PragmaAttributeEntry &Entry :
1080e5dd7070Spatrick PragmaAttributeStack[Index].Entries) {
1081e5dd7070Spatrick if (!Entry.IsUsed) {
1082e5dd7070Spatrick assert(Entry.Attribute && "Expected an attribute");
1083e5dd7070Spatrick Diag(Entry.Attribute->getLoc(), diag::warn_pragma_attribute_unused)
1084e5dd7070Spatrick << *Entry.Attribute;
1085e5dd7070Spatrick Diag(PragmaLoc, diag::note_pragma_attribute_region_ends_here);
1086e5dd7070Spatrick }
1087e5dd7070Spatrick }
1088e5dd7070Spatrick PragmaAttributeStack.erase(PragmaAttributeStack.begin() + Index);
1089e5dd7070Spatrick return;
1090e5dd7070Spatrick }
1091e5dd7070Spatrick }
1092e5dd7070Spatrick
1093e5dd7070Spatrick if (Namespace)
1094e5dd7070Spatrick Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch)
1095e5dd7070Spatrick << 0 << Namespace->getName();
1096e5dd7070Spatrick else
1097e5dd7070Spatrick Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
1098e5dd7070Spatrick }
1099e5dd7070Spatrick
AddPragmaAttributes(Scope * S,Decl * D)1100e5dd7070Spatrick void Sema::AddPragmaAttributes(Scope *S, Decl *D) {
1101e5dd7070Spatrick if (PragmaAttributeStack.empty())
1102e5dd7070Spatrick return;
1103e5dd7070Spatrick for (auto &Group : PragmaAttributeStack) {
1104e5dd7070Spatrick for (auto &Entry : Group.Entries) {
1105e5dd7070Spatrick ParsedAttr *Attribute = Entry.Attribute;
1106e5dd7070Spatrick assert(Attribute && "Expected an attribute");
1107e5dd7070Spatrick assert(Attribute->isPragmaClangAttribute() &&
1108e5dd7070Spatrick "expected #pragma clang attribute");
1109e5dd7070Spatrick
1110e5dd7070Spatrick // Ensure that the attribute can be applied to the given declaration.
1111e5dd7070Spatrick bool Applies = false;
1112e5dd7070Spatrick for (const auto &Rule : Entry.MatchRules) {
1113e5dd7070Spatrick if (Attribute->appliesToDecl(D, Rule)) {
1114e5dd7070Spatrick Applies = true;
1115e5dd7070Spatrick break;
1116e5dd7070Spatrick }
1117e5dd7070Spatrick }
1118e5dd7070Spatrick if (!Applies)
1119e5dd7070Spatrick continue;
1120e5dd7070Spatrick Entry.IsUsed = true;
1121e5dd7070Spatrick PragmaAttributeCurrentTargetDecl = D;
1122e5dd7070Spatrick ParsedAttributesView Attrs;
1123e5dd7070Spatrick Attrs.addAtEnd(Attribute);
1124e5dd7070Spatrick ProcessDeclAttributeList(S, D, Attrs);
1125e5dd7070Spatrick PragmaAttributeCurrentTargetDecl = nullptr;
1126e5dd7070Spatrick }
1127e5dd7070Spatrick }
1128e5dd7070Spatrick }
1129e5dd7070Spatrick
PrintPragmaAttributeInstantiationPoint()1130e5dd7070Spatrick void Sema::PrintPragmaAttributeInstantiationPoint() {
1131e5dd7070Spatrick assert(PragmaAttributeCurrentTargetDecl && "Expected an active declaration");
1132e5dd7070Spatrick Diags.Report(PragmaAttributeCurrentTargetDecl->getBeginLoc(),
1133e5dd7070Spatrick diag::note_pragma_attribute_applied_decl_here);
1134e5dd7070Spatrick }
1135e5dd7070Spatrick
DiagnoseUnterminatedPragmaAttribute()1136e5dd7070Spatrick void Sema::DiagnoseUnterminatedPragmaAttribute() {
1137e5dd7070Spatrick if (PragmaAttributeStack.empty())
1138e5dd7070Spatrick return;
1139e5dd7070Spatrick Diag(PragmaAttributeStack.back().Loc, diag::err_pragma_attribute_no_pop_eof);
1140e5dd7070Spatrick }
1141e5dd7070Spatrick
ActOnPragmaOptimize(bool On,SourceLocation PragmaLoc)1142e5dd7070Spatrick void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) {
1143e5dd7070Spatrick if(On)
1144e5dd7070Spatrick OptimizeOffPragmaLocation = SourceLocation();
1145e5dd7070Spatrick else
1146e5dd7070Spatrick OptimizeOffPragmaLocation = PragmaLoc;
1147e5dd7070Spatrick }
1148e5dd7070Spatrick
ActOnPragmaMSOptimize(SourceLocation Loc,bool IsOn)1149*12c85518Srobert void Sema::ActOnPragmaMSOptimize(SourceLocation Loc, bool IsOn) {
1150*12c85518Srobert if (!CurContext->getRedeclContext()->isFileContext()) {
1151*12c85518Srobert Diag(Loc, diag::err_pragma_expected_file_scope) << "optimize";
1152*12c85518Srobert return;
1153*12c85518Srobert }
1154*12c85518Srobert
1155*12c85518Srobert MSPragmaOptimizeIsOn = IsOn;
1156*12c85518Srobert }
1157*12c85518Srobert
ActOnPragmaMSFunction(SourceLocation Loc,const llvm::SmallVectorImpl<StringRef> & NoBuiltins)1158*12c85518Srobert void Sema::ActOnPragmaMSFunction(
1159*12c85518Srobert SourceLocation Loc, const llvm::SmallVectorImpl<StringRef> &NoBuiltins) {
1160*12c85518Srobert if (!CurContext->getRedeclContext()->isFileContext()) {
1161*12c85518Srobert Diag(Loc, diag::err_pragma_expected_file_scope) << "function";
1162*12c85518Srobert return;
1163*12c85518Srobert }
1164*12c85518Srobert
1165*12c85518Srobert MSFunctionNoBuiltins.insert(NoBuiltins.begin(), NoBuiltins.end());
1166*12c85518Srobert }
1167*12c85518Srobert
AddRangeBasedOptnone(FunctionDecl * FD)1168e5dd7070Spatrick void Sema::AddRangeBasedOptnone(FunctionDecl *FD) {
1169e5dd7070Spatrick // In the future, check other pragmas if they're implemented (e.g. pragma
1170e5dd7070Spatrick // optimize 0 will probably map to this functionality too).
1171e5dd7070Spatrick if(OptimizeOffPragmaLocation.isValid())
1172e5dd7070Spatrick AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation);
1173e5dd7070Spatrick }
1174e5dd7070Spatrick
AddSectionMSAllocText(FunctionDecl * FD)1175*12c85518Srobert void Sema::AddSectionMSAllocText(FunctionDecl *FD) {
1176*12c85518Srobert if (!FD->getIdentifier())
1177*12c85518Srobert return;
1178*12c85518Srobert
1179*12c85518Srobert StringRef Name = FD->getName();
1180*12c85518Srobert auto It = FunctionToSectionMap.find(Name);
1181*12c85518Srobert if (It != FunctionToSectionMap.end()) {
1182*12c85518Srobert StringRef Section;
1183*12c85518Srobert SourceLocation Loc;
1184*12c85518Srobert std::tie(Section, Loc) = It->second;
1185*12c85518Srobert
1186*12c85518Srobert if (!FD->hasAttr<SectionAttr>())
1187*12c85518Srobert FD->addAttr(SectionAttr::CreateImplicit(Context, Section));
1188*12c85518Srobert }
1189*12c85518Srobert }
1190*12c85518Srobert
ModifyFnAttributesMSPragmaOptimize(FunctionDecl * FD)1191*12c85518Srobert void Sema::ModifyFnAttributesMSPragmaOptimize(FunctionDecl *FD) {
1192*12c85518Srobert // Don't modify the function attributes if it's "on". "on" resets the
1193*12c85518Srobert // optimizations to the ones listed on the command line
1194*12c85518Srobert if (!MSPragmaOptimizeIsOn)
1195*12c85518Srobert AddOptnoneAttributeIfNoConflicts(FD, FD->getBeginLoc());
1196*12c85518Srobert }
1197*12c85518Srobert
AddOptnoneAttributeIfNoConflicts(FunctionDecl * FD,SourceLocation Loc)1198e5dd7070Spatrick void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD,
1199e5dd7070Spatrick SourceLocation Loc) {
1200e5dd7070Spatrick // Don't add a conflicting attribute. No diagnostic is needed.
1201e5dd7070Spatrick if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>())
1202e5dd7070Spatrick return;
1203e5dd7070Spatrick
1204e5dd7070Spatrick // Add attributes only if required. Optnone requires noinline as well, but if
1205e5dd7070Spatrick // either is already present then don't bother adding them.
1206e5dd7070Spatrick if (!FD->hasAttr<OptimizeNoneAttr>())
1207e5dd7070Spatrick FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc));
1208e5dd7070Spatrick if (!FD->hasAttr<NoInlineAttr>())
1209e5dd7070Spatrick FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc));
1210e5dd7070Spatrick }
1211e5dd7070Spatrick
AddImplicitMSFunctionNoBuiltinAttr(FunctionDecl * FD)1212*12c85518Srobert void Sema::AddImplicitMSFunctionNoBuiltinAttr(FunctionDecl *FD) {
1213*12c85518Srobert SmallVector<StringRef> V(MSFunctionNoBuiltins.begin(),
1214*12c85518Srobert MSFunctionNoBuiltins.end());
1215*12c85518Srobert if (!MSFunctionNoBuiltins.empty())
1216*12c85518Srobert FD->addAttr(NoBuiltinAttr::CreateImplicit(Context, V.data(), V.size()));
1217*12c85518Srobert }
1218*12c85518Srobert
1219e5dd7070Spatrick typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
1220e5dd7070Spatrick enum : unsigned { NoVisibility = ~0U };
1221e5dd7070Spatrick
AddPushedVisibilityAttribute(Decl * D)1222e5dd7070Spatrick void Sema::AddPushedVisibilityAttribute(Decl *D) {
1223e5dd7070Spatrick if (!VisContext)
1224e5dd7070Spatrick return;
1225e5dd7070Spatrick
1226e5dd7070Spatrick NamedDecl *ND = dyn_cast<NamedDecl>(D);
1227e5dd7070Spatrick if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
1228e5dd7070Spatrick return;
1229e5dd7070Spatrick
1230e5dd7070Spatrick VisStack *Stack = static_cast<VisStack*>(VisContext);
1231e5dd7070Spatrick unsigned rawType = Stack->back().first;
1232e5dd7070Spatrick if (rawType == NoVisibility) return;
1233e5dd7070Spatrick
1234e5dd7070Spatrick VisibilityAttr::VisibilityType type
1235e5dd7070Spatrick = (VisibilityAttr::VisibilityType) rawType;
1236e5dd7070Spatrick SourceLocation loc = Stack->back().second;
1237e5dd7070Spatrick
1238e5dd7070Spatrick D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
1239e5dd7070Spatrick }
1240e5dd7070Spatrick
1241e5dd7070Spatrick /// FreeVisContext - Deallocate and null out VisContext.
FreeVisContext()1242e5dd7070Spatrick void Sema::FreeVisContext() {
1243e5dd7070Spatrick delete static_cast<VisStack*>(VisContext);
1244e5dd7070Spatrick VisContext = nullptr;
1245e5dd7070Spatrick }
1246e5dd7070Spatrick
PushPragmaVisibility(Sema & S,unsigned type,SourceLocation loc)1247e5dd7070Spatrick static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
1248e5dd7070Spatrick // Put visibility on stack.
1249e5dd7070Spatrick if (!S.VisContext)
1250e5dd7070Spatrick S.VisContext = new VisStack;
1251e5dd7070Spatrick
1252e5dd7070Spatrick VisStack *Stack = static_cast<VisStack*>(S.VisContext);
1253e5dd7070Spatrick Stack->push_back(std::make_pair(type, loc));
1254e5dd7070Spatrick }
1255e5dd7070Spatrick
ActOnPragmaVisibility(const IdentifierInfo * VisType,SourceLocation PragmaLoc)1256e5dd7070Spatrick void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
1257e5dd7070Spatrick SourceLocation PragmaLoc) {
1258e5dd7070Spatrick if (VisType) {
1259e5dd7070Spatrick // Compute visibility to use.
1260e5dd7070Spatrick VisibilityAttr::VisibilityType T;
1261e5dd7070Spatrick if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
1262e5dd7070Spatrick Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
1263e5dd7070Spatrick return;
1264e5dd7070Spatrick }
1265e5dd7070Spatrick PushPragmaVisibility(*this, T, PragmaLoc);
1266e5dd7070Spatrick } else {
1267e5dd7070Spatrick PopPragmaVisibility(false, PragmaLoc);
1268e5dd7070Spatrick }
1269e5dd7070Spatrick }
1270e5dd7070Spatrick
ActOnPragmaFPContract(SourceLocation Loc,LangOptions::FPModeKind FPC)1271ec727ea7Spatrick void Sema::ActOnPragmaFPContract(SourceLocation Loc,
1272ec727ea7Spatrick LangOptions::FPModeKind FPC) {
1273a9ac8606Spatrick FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1274e5dd7070Spatrick switch (FPC) {
1275ec727ea7Spatrick case LangOptions::FPM_On:
1276ec727ea7Spatrick NewFPFeatures.setAllowFPContractWithinStatement();
1277e5dd7070Spatrick break;
1278ec727ea7Spatrick case LangOptions::FPM_Fast:
1279ec727ea7Spatrick NewFPFeatures.setAllowFPContractAcrossStatement();
1280e5dd7070Spatrick break;
1281ec727ea7Spatrick case LangOptions::FPM_Off:
1282ec727ea7Spatrick NewFPFeatures.setDisallowFPContract();
1283e5dd7070Spatrick break;
1284a9ac8606Spatrick case LangOptions::FPM_FastHonorPragmas:
1285a9ac8606Spatrick llvm_unreachable("Should not happen");
1286e5dd7070Spatrick }
1287a9ac8606Spatrick FpPragmaStack.Act(Loc, Sema::PSK_Set, StringRef(), NewFPFeatures);
1288ec727ea7Spatrick CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1289e5dd7070Spatrick }
1290e5dd7070Spatrick
ActOnPragmaFPReassociate(SourceLocation Loc,bool IsEnabled)1291ec727ea7Spatrick void Sema::ActOnPragmaFPReassociate(SourceLocation Loc, bool IsEnabled) {
1292*12c85518Srobert if (IsEnabled) {
1293*12c85518Srobert // For value unsafe context, combining this pragma with eval method
1294*12c85518Srobert // setting is not recommended. See comment in function FixupInvocation#506.
1295*12c85518Srobert int Reason = -1;
1296*12c85518Srobert if (getLangOpts().getFPEvalMethod() != LangOptions::FEM_UnsetOnCommandLine)
1297*12c85518Srobert // Eval method set using the option 'ffp-eval-method'.
1298*12c85518Srobert Reason = 1;
1299*12c85518Srobert if (PP.getLastFPEvalPragmaLocation().isValid())
1300*12c85518Srobert // Eval method set using the '#pragma clang fp eval_method'.
1301*12c85518Srobert // We could have both an option and a pragma used to the set the eval
1302*12c85518Srobert // method. The pragma overrides the option in the command line. The Reason
1303*12c85518Srobert // of the diagnostic is overriden too.
1304*12c85518Srobert Reason = 0;
1305*12c85518Srobert if (Reason != -1)
1306*12c85518Srobert Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context)
1307*12c85518Srobert << Reason << 4;
1308*12c85518Srobert }
1309a9ac8606Spatrick FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1310ec727ea7Spatrick NewFPFeatures.setAllowFPReassociateOverride(IsEnabled);
1311a9ac8606Spatrick FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1312a9ac8606Spatrick CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1313e5dd7070Spatrick }
1314e5dd7070Spatrick
ActOnPragmaFEnvRound(SourceLocation Loc,llvm::RoundingMode FPR)1315*12c85518Srobert void Sema::ActOnPragmaFEnvRound(SourceLocation Loc, llvm::RoundingMode FPR) {
1316a9ac8606Spatrick FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1317*12c85518Srobert NewFPFeatures.setConstRoundingModeOverride(FPR);
1318a9ac8606Spatrick FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1319a9ac8606Spatrick CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1320ec727ea7Spatrick }
1321ec727ea7Spatrick
setExceptionMode(SourceLocation Loc,LangOptions::FPExceptionModeKind FPE)1322ec727ea7Spatrick void Sema::setExceptionMode(SourceLocation Loc,
1323ec727ea7Spatrick LangOptions::FPExceptionModeKind FPE) {
1324a9ac8606Spatrick FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1325*12c85518Srobert NewFPFeatures.setSpecifiedExceptionModeOverride(FPE);
1326a9ac8606Spatrick FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1327a9ac8606Spatrick CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1328ec727ea7Spatrick }
1329ec727ea7Spatrick
ActOnPragmaFEnvAccess(SourceLocation Loc,bool IsEnabled)1330ec727ea7Spatrick void Sema::ActOnPragmaFEnvAccess(SourceLocation Loc, bool IsEnabled) {
1331a9ac8606Spatrick FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1332ec727ea7Spatrick if (IsEnabled) {
1333ec727ea7Spatrick // Verify Microsoft restriction:
1334ec727ea7Spatrick // You can't enable fenv_access unless precise semantics are enabled.
1335ec727ea7Spatrick // Precise semantics can be enabled either by the float_control
1336ec727ea7Spatrick // pragma, or by using the /fp:precise or /fp:strict compiler options
1337ec727ea7Spatrick if (!isPreciseFPEnabled())
1338ec727ea7Spatrick Diag(Loc, diag::err_pragma_fenv_requires_precise);
1339a9ac8606Spatrick }
1340*12c85518Srobert NewFPFeatures.setAllowFEnvAccessOverride(IsEnabled);
1341a9ac8606Spatrick FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1342*12c85518Srobert CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1343a9ac8606Spatrick }
1344a9ac8606Spatrick
ActOnPragmaFPExceptions(SourceLocation Loc,LangOptions::FPExceptionModeKind FPE)1345a9ac8606Spatrick void Sema::ActOnPragmaFPExceptions(SourceLocation Loc,
1346a9ac8606Spatrick LangOptions::FPExceptionModeKind FPE) {
1347a9ac8606Spatrick setExceptionMode(Loc, FPE);
1348ec727ea7Spatrick }
1349e5dd7070Spatrick
PushNamespaceVisibilityAttr(const VisibilityAttr * Attr,SourceLocation Loc)1350e5dd7070Spatrick void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
1351e5dd7070Spatrick SourceLocation Loc) {
1352e5dd7070Spatrick // Visibility calculations will consider the namespace's visibility.
1353e5dd7070Spatrick // Here we just want to note that we're in a visibility context
1354e5dd7070Spatrick // which overrides any enclosing #pragma context, but doesn't itself
1355e5dd7070Spatrick // contribute visibility.
1356e5dd7070Spatrick PushPragmaVisibility(*this, NoVisibility, Loc);
1357e5dd7070Spatrick }
1358e5dd7070Spatrick
PopPragmaVisibility(bool IsNamespaceEnd,SourceLocation EndLoc)1359e5dd7070Spatrick void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
1360e5dd7070Spatrick if (!VisContext) {
1361e5dd7070Spatrick Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
1362e5dd7070Spatrick return;
1363e5dd7070Spatrick }
1364e5dd7070Spatrick
1365e5dd7070Spatrick // Pop visibility from stack
1366e5dd7070Spatrick VisStack *Stack = static_cast<VisStack*>(VisContext);
1367e5dd7070Spatrick
1368e5dd7070Spatrick const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
1369e5dd7070Spatrick bool StartsWithPragma = Back->first != NoVisibility;
1370e5dd7070Spatrick if (StartsWithPragma && IsNamespaceEnd) {
1371e5dd7070Spatrick Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
1372e5dd7070Spatrick Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
1373e5dd7070Spatrick
1374e5dd7070Spatrick // For better error recovery, eat all pushes inside the namespace.
1375e5dd7070Spatrick do {
1376e5dd7070Spatrick Stack->pop_back();
1377e5dd7070Spatrick Back = &Stack->back();
1378e5dd7070Spatrick StartsWithPragma = Back->first != NoVisibility;
1379e5dd7070Spatrick } while (StartsWithPragma);
1380e5dd7070Spatrick } else if (!StartsWithPragma && !IsNamespaceEnd) {
1381e5dd7070Spatrick Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
1382e5dd7070Spatrick Diag(Back->second, diag::note_surrounding_namespace_starts_here);
1383e5dd7070Spatrick return;
1384e5dd7070Spatrick }
1385e5dd7070Spatrick
1386e5dd7070Spatrick Stack->pop_back();
1387e5dd7070Spatrick // To simplify the implementation, never keep around an empty stack.
1388e5dd7070Spatrick if (Stack->empty())
1389e5dd7070Spatrick FreeVisContext();
1390e5dd7070Spatrick }
1391a9ac8606Spatrick
1392a9ac8606Spatrick template <typename Ty>
checkCommonAttributeFeatures(Sema & S,const Ty * Node,const ParsedAttr & A,bool SkipArgCountCheck)1393a9ac8606Spatrick static bool checkCommonAttributeFeatures(Sema &S, const Ty *Node,
1394*12c85518Srobert const ParsedAttr &A,
1395*12c85518Srobert bool SkipArgCountCheck) {
1396a9ac8606Spatrick // Several attributes carry different semantics than the parsing requires, so
1397a9ac8606Spatrick // those are opted out of the common argument checks.
1398a9ac8606Spatrick //
1399a9ac8606Spatrick // We also bail on unknown and ignored attributes because those are handled
1400a9ac8606Spatrick // as part of the target-specific handling logic.
1401a9ac8606Spatrick if (A.getKind() == ParsedAttr::UnknownAttribute)
1402a9ac8606Spatrick return false;
1403a9ac8606Spatrick // Check whether the attribute requires specific language extensions to be
1404a9ac8606Spatrick // enabled.
1405a9ac8606Spatrick if (!A.diagnoseLangOpts(S))
1406a9ac8606Spatrick return true;
1407a9ac8606Spatrick // Check whether the attribute appertains to the given subject.
1408a9ac8606Spatrick if (!A.diagnoseAppertainsTo(S, Node))
1409a9ac8606Spatrick return true;
1410a9ac8606Spatrick // Check whether the attribute is mutually exclusive with other attributes
1411a9ac8606Spatrick // that have already been applied to the declaration.
1412a9ac8606Spatrick if (!A.diagnoseMutualExclusion(S, Node))
1413a9ac8606Spatrick return true;
1414a9ac8606Spatrick // Check whether the attribute exists in the target architecture.
1415a9ac8606Spatrick if (S.CheckAttrTarget(A))
1416a9ac8606Spatrick return true;
1417a9ac8606Spatrick
1418a9ac8606Spatrick if (A.hasCustomParsing())
1419a9ac8606Spatrick return false;
1420a9ac8606Spatrick
1421*12c85518Srobert if (!SkipArgCountCheck) {
1422a9ac8606Spatrick if (A.getMinArgs() == A.getMaxArgs()) {
1423*12c85518Srobert // If there are no optional arguments, then checking for the argument
1424*12c85518Srobert // count is trivial.
1425a9ac8606Spatrick if (!A.checkExactlyNumArgs(S, A.getMinArgs()))
1426a9ac8606Spatrick return true;
1427a9ac8606Spatrick } else {
1428a9ac8606Spatrick // There are optional arguments, so checking is slightly more involved.
1429a9ac8606Spatrick if (A.getMinArgs() && !A.checkAtLeastNumArgs(S, A.getMinArgs()))
1430a9ac8606Spatrick return true;
1431a9ac8606Spatrick else if (!A.hasVariadicArg() && A.getMaxArgs() &&
1432a9ac8606Spatrick !A.checkAtMostNumArgs(S, A.getMaxArgs()))
1433a9ac8606Spatrick return true;
1434a9ac8606Spatrick }
1435*12c85518Srobert }
1436a9ac8606Spatrick
1437a9ac8606Spatrick return false;
1438a9ac8606Spatrick }
1439a9ac8606Spatrick
checkCommonAttributeFeatures(const Decl * D,const ParsedAttr & A,bool SkipArgCountCheck)1440*12c85518Srobert bool Sema::checkCommonAttributeFeatures(const Decl *D, const ParsedAttr &A,
1441*12c85518Srobert bool SkipArgCountCheck) {
1442*12c85518Srobert return ::checkCommonAttributeFeatures(*this, D, A, SkipArgCountCheck);
1443a9ac8606Spatrick }
checkCommonAttributeFeatures(const Stmt * S,const ParsedAttr & A,bool SkipArgCountCheck)1444*12c85518Srobert bool Sema::checkCommonAttributeFeatures(const Stmt *S, const ParsedAttr &A,
1445*12c85518Srobert bool SkipArgCountCheck) {
1446*12c85518Srobert return ::checkCommonAttributeFeatures(*this, S, A, SkipArgCountCheck);
1447a9ac8606Spatrick }
1448