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