SemaAttr.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. #include "clang/AST/ASTConsumer.h"
  14. #include "clang/AST/Attr.h"
  15. #include "clang/AST/Expr.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "clang/Lex/Preprocessor.h"
  18. #include "clang/Sema/Lookup.h"
  19. #include "clang/Sema/SemaInternal.h"
  20. using namespace clang;
  21. //===----------------------------------------------------------------------===//
  22. // Pragma 'pack' and 'options align'
  23. //===----------------------------------------------------------------------===//
  24. Sema::PragmaStackSentinelRAII::PragmaStackSentinelRAII(Sema &S,
  25. StringRef SlotLabel,
  26. bool ShouldAct)
  27. : S(S), SlotLabel(SlotLabel), ShouldAct(ShouldAct) {
  28. if (ShouldAct) {
  29. S.VtorDispStack.SentinelAction(PSK_Push, SlotLabel);
  30. S.DataSegStack.SentinelAction(PSK_Push, SlotLabel);
  31. S.BSSSegStack.SentinelAction(PSK_Push, SlotLabel);
  32. S.ConstSegStack.SentinelAction(PSK_Push, SlotLabel);
  33. S.CodeSegStack.SentinelAction(PSK_Push, SlotLabel);
  34. }
  35. }
  36. Sema::PragmaStackSentinelRAII::~PragmaStackSentinelRAII() {
  37. if (ShouldAct) {
  38. S.VtorDispStack.SentinelAction(PSK_Pop, SlotLabel);
  39. S.DataSegStack.SentinelAction(PSK_Pop, SlotLabel);
  40. S.BSSSegStack.SentinelAction(PSK_Pop, SlotLabel);
  41. S.ConstSegStack.SentinelAction(PSK_Pop, SlotLabel);
  42. S.CodeSegStack.SentinelAction(PSK_Pop, SlotLabel);
  43. }
  44. }
  45. void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
  46. // If there is no pack value, we don't need any attributes.
  47. if (!PackStack.CurrentValue)
  48. return;
  49. // Otherwise, check to see if we need a max field alignment attribute.
  50. if (unsigned Alignment = PackStack.CurrentValue) {
  51. if (Alignment == Sema::kMac68kAlignmentSentinel)
  52. RD->addAttr(AlignMac68kAttr::CreateImplicit(Context));
  53. else
  54. RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context,
  55. Alignment * 8));
  56. }
  57. if (PackIncludeStack.empty())
  58. return;
  59. // The #pragma pack affected a record in an included file, so Clang should
  60. // warn when that pragma was written in a file that included the included
  61. // file.
  62. for (auto &PackedInclude : llvm::reverse(PackIncludeStack)) {
  63. if (PackedInclude.CurrentPragmaLocation != PackStack.CurrentPragmaLocation)
  64. break;
  65. if (PackedInclude.HasNonDefaultValue)
  66. PackedInclude.ShouldWarnOnInclude = true;
  67. }
  68. }
  69. void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
  70. if (MSStructPragmaOn)
  71. RD->addAttr(MSStructAttr::CreateImplicit(Context));
  72. // FIXME: We should merge AddAlignmentAttributesForRecord with
  73. // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes
  74. // all active pragmas and applies them as attributes to class definitions.
  75. if (VtorDispStack.CurrentValue != getLangOpts().VtorDispMode)
  76. RD->addAttr(
  77. MSVtorDispAttr::CreateImplicit(Context, VtorDispStack.CurrentValue));
  78. }
  79. template <typename Attribute>
  80. static void addGslOwnerPointerAttributeIfNotExisting(ASTContext &Context,
  81. CXXRecordDecl *Record) {
  82. if (Record->hasAttr<OwnerAttr>() || Record->hasAttr<PointerAttr>())
  83. return;
  84. for (Decl *Redecl : Record->redecls())
  85. Redecl->addAttr(Attribute::CreateImplicit(Context, /*DerefType=*/nullptr));
  86. }
  87. void Sema::inferGslPointerAttribute(NamedDecl *ND,
  88. CXXRecordDecl *UnderlyingRecord) {
  89. if (!UnderlyingRecord)
  90. return;
  91. const auto *Parent = dyn_cast<CXXRecordDecl>(ND->getDeclContext());
  92. if (!Parent)
  93. return;
  94. static llvm::StringSet<> Containers{
  95. "array",
  96. "basic_string",
  97. "deque",
  98. "forward_list",
  99. "vector",
  100. "list",
  101. "map",
  102. "multiset",
  103. "multimap",
  104. "priority_queue",
  105. "queue",
  106. "set",
  107. "stack",
  108. "unordered_set",
  109. "unordered_map",
  110. "unordered_multiset",
  111. "unordered_multimap",
  112. };
  113. static llvm::StringSet<> Iterators{"iterator", "const_iterator",
  114. "reverse_iterator",
  115. "const_reverse_iterator"};
  116. if (Parent->isInStdNamespace() && Iterators.count(ND->getName()) &&
  117. Containers.count(Parent->getName()))
  118. addGslOwnerPointerAttributeIfNotExisting<PointerAttr>(Context,
  119. UnderlyingRecord);
  120. }
  121. void Sema::inferGslPointerAttribute(TypedefNameDecl *TD) {
  122. QualType Canonical = TD->getUnderlyingType().getCanonicalType();
  123. CXXRecordDecl *RD = Canonical->getAsCXXRecordDecl();
  124. if (!RD) {
  125. if (auto *TST =
  126. dyn_cast<TemplateSpecializationType>(Canonical.getTypePtr())) {
  127. RD = dyn_cast_or_null<CXXRecordDecl>(
  128. TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl());
  129. }
  130. }
  131. inferGslPointerAttribute(TD, RD);
  132. }
  133. void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) {
  134. static llvm::StringSet<> StdOwners{
  135. "any",
  136. "array",
  137. "basic_regex",
  138. "basic_string",
  139. "deque",
  140. "forward_list",
  141. "vector",
  142. "list",
  143. "map",
  144. "multiset",
  145. "multimap",
  146. "optional",
  147. "priority_queue",
  148. "queue",
  149. "set",
  150. "stack",
  151. "unique_ptr",
  152. "unordered_set",
  153. "unordered_map",
  154. "unordered_multiset",
  155. "unordered_multimap",
  156. "variant",
  157. };
  158. static llvm::StringSet<> StdPointers{
  159. "basic_string_view",
  160. "reference_wrapper",
  161. "regex_iterator",
  162. };
  163. if (!Record->getIdentifier())
  164. return;
  165. // Handle classes that directly appear in std namespace.
  166. if (Record->isInStdNamespace()) {
  167. if (Record->hasAttr<OwnerAttr>() || Record->hasAttr<PointerAttr>())
  168. return;
  169. if (StdOwners.count(Record->getName()))
  170. addGslOwnerPointerAttributeIfNotExisting<OwnerAttr>(Context, Record);
  171. else if (StdPointers.count(Record->getName()))
  172. addGslOwnerPointerAttributeIfNotExisting<PointerAttr>(Context, Record);
  173. return;
  174. }
  175. // Handle nested classes that could be a gsl::Pointer.
  176. inferGslPointerAttribute(Record, Record);
  177. }
  178. void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
  179. SourceLocation PragmaLoc) {
  180. PragmaMsStackAction Action = Sema::PSK_Reset;
  181. unsigned Alignment = 0;
  182. switch (Kind) {
  183. // For all targets we support native and natural are the same.
  184. //
  185. // FIXME: This is not true on Darwin/PPC.
  186. case POAK_Native:
  187. case POAK_Power:
  188. case POAK_Natural:
  189. Action = Sema::PSK_Push_Set;
  190. Alignment = 0;
  191. break;
  192. // Note that '#pragma options align=packed' is not equivalent to attribute
  193. // packed, it has a different precedence relative to attribute aligned.
  194. case POAK_Packed:
  195. Action = Sema::PSK_Push_Set;
  196. Alignment = 1;
  197. break;
  198. case POAK_Mac68k:
  199. // Check if the target supports this.
  200. if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) {
  201. Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
  202. return;
  203. }
  204. Action = Sema::PSK_Push_Set;
  205. Alignment = Sema::kMac68kAlignmentSentinel;
  206. break;
  207. case POAK_Reset:
  208. // Reset just pops the top of the stack, or resets the current alignment to
  209. // default.
  210. Action = Sema::PSK_Pop;
  211. if (PackStack.Stack.empty()) {
  212. if (PackStack.CurrentValue) {
  213. Action = Sema::PSK_Reset;
  214. } else {
  215. Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
  216. << "stack empty";
  217. return;
  218. }
  219. }
  220. break;
  221. }
  222. PackStack.Act(PragmaLoc, Action, StringRef(), Alignment);
  223. }
  224. void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc, PragmaClangSectionAction Action,
  225. PragmaClangSectionKind SecKind, StringRef SecName) {
  226. PragmaClangSection *CSec;
  227. switch (SecKind) {
  228. case PragmaClangSectionKind::PCSK_BSS:
  229. CSec = &PragmaClangBSSSection;
  230. break;
  231. case PragmaClangSectionKind::PCSK_Data:
  232. CSec = &PragmaClangDataSection;
  233. break;
  234. case PragmaClangSectionKind::PCSK_Rodata:
  235. CSec = &PragmaClangRodataSection;
  236. break;
  237. case PragmaClangSectionKind::PCSK_Relro:
  238. CSec = &PragmaClangRelroSection;
  239. break;
  240. case PragmaClangSectionKind::PCSK_Text:
  241. CSec = &PragmaClangTextSection;
  242. break;
  243. default:
  244. llvm_unreachable("invalid clang section kind");
  245. }
  246. if (Action == PragmaClangSectionAction::PCSA_Clear) {
  247. CSec->Valid = false;
  248. return;
  249. }
  250. CSec->Valid = true;
  251. CSec->SectionName = SecName;
  252. CSec->PragmaLocation = PragmaLoc;
  253. }
  254. void Sema::ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action,
  255. StringRef SlotLabel, Expr *alignment) {
  256. Expr *Alignment = static_cast<Expr *>(alignment);
  257. // If specified then alignment must be a "small" power of two.
  258. unsigned AlignmentVal = 0;
  259. if (Alignment) {
  260. llvm::APSInt Val;
  261. // pack(0) is like pack(), which just works out since that is what
  262. // we use 0 for in PackAttr.
  263. if (Alignment->isTypeDependent() ||
  264. Alignment->isValueDependent() ||
  265. !Alignment->isIntegerConstantExpr(Val, Context) ||
  266. !(Val == 0 || Val.isPowerOf2()) ||
  267. Val.getZExtValue() > 16) {
  268. Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
  269. return; // Ignore
  270. }
  271. AlignmentVal = (unsigned) Val.getZExtValue();
  272. }
  273. if (Action == Sema::PSK_Show) {
  274. // Show the current alignment, making sure to show the right value
  275. // for the default.
  276. // FIXME: This should come from the target.
  277. AlignmentVal = PackStack.CurrentValue;
  278. if (AlignmentVal == 0)
  279. AlignmentVal = 8;
  280. if (AlignmentVal == Sema::kMac68kAlignmentSentinel)
  281. Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
  282. else
  283. Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
  284. }
  285. // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
  286. // "#pragma pack(pop, identifier, n) is undefined"
  287. if (Action & Sema::PSK_Pop) {
  288. if (Alignment && !SlotLabel.empty())
  289. Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifier_and_alignment);
  290. if (PackStack.Stack.empty())
  291. Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "pack" << "stack empty";
  292. }
  293. PackStack.Act(PragmaLoc, Action, SlotLabel, AlignmentVal);
  294. }
  295. void Sema::DiagnoseNonDefaultPragmaPack(PragmaPackDiagnoseKind Kind,
  296. SourceLocation IncludeLoc) {
  297. if (Kind == PragmaPackDiagnoseKind::NonDefaultStateAtInclude) {
  298. SourceLocation PrevLocation = PackStack.CurrentPragmaLocation;
  299. // Warn about non-default alignment at #includes (without redundant
  300. // warnings for the same directive in nested includes).
  301. // The warning is delayed until the end of the file to avoid warnings
  302. // for files that don't have any records that are affected by the modified
  303. // alignment.
  304. bool HasNonDefaultValue =
  305. PackStack.hasValue() &&
  306. (PackIncludeStack.empty() ||
  307. PackIncludeStack.back().CurrentPragmaLocation != PrevLocation);
  308. PackIncludeStack.push_back(
  309. {PackStack.CurrentValue,
  310. PackStack.hasValue() ? PrevLocation : SourceLocation(),
  311. HasNonDefaultValue, /*ShouldWarnOnInclude*/ false});
  312. return;
  313. }
  314. assert(Kind == PragmaPackDiagnoseKind::ChangedStateAtExit && "invalid kind");
  315. PackIncludeState PrevPackState = PackIncludeStack.pop_back_val();
  316. if (PrevPackState.ShouldWarnOnInclude) {
  317. // Emit the delayed non-default alignment at #include warning.
  318. Diag(IncludeLoc, diag::warn_pragma_pack_non_default_at_include);
  319. Diag(PrevPackState.CurrentPragmaLocation, diag::note_pragma_pack_here);
  320. }
  321. // Warn about modified alignment after #includes.
  322. if (PrevPackState.CurrentValue != PackStack.CurrentValue) {
  323. Diag(IncludeLoc, diag::warn_pragma_pack_modified_after_include);
  324. Diag(PackStack.CurrentPragmaLocation, diag::note_pragma_pack_here);
  325. }
  326. }
  327. void Sema::DiagnoseUnterminatedPragmaPack() {
  328. if (PackStack.Stack.empty())
  329. return;
  330. bool IsInnermost = true;
  331. for (const auto &StackSlot : llvm::reverse(PackStack.Stack)) {
  332. Diag(StackSlot.PragmaPushLocation, diag::warn_pragma_pack_no_pop_eof);
  333. // The user might have already reset the alignment, so suggest replacing
  334. // the reset with a pop.
  335. if (IsInnermost && PackStack.CurrentValue == PackStack.DefaultValue) {
  336. DiagnosticBuilder DB = Diag(PackStack.CurrentPragmaLocation,
  337. diag::note_pragma_pack_pop_instead_reset);
  338. SourceLocation FixItLoc = Lexer::findLocationAfterToken(
  339. PackStack.CurrentPragmaLocation, tok::l_paren, SourceMgr, LangOpts,
  340. /*SkipTrailing=*/false);
  341. if (FixItLoc.isValid())
  342. DB << FixItHint::CreateInsertion(FixItLoc, "pop");
  343. }
  344. IsInnermost = false;
  345. }
  346. }
  347. void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
  348. MSStructPragmaOn = (Kind == PMSST_ON);
  349. }
  350. void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc,
  351. PragmaMSCommentKind Kind, StringRef Arg) {
  352. auto *PCD = PragmaCommentDecl::Create(
  353. Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg);
  354. Context.getTranslationUnitDecl()->addDecl(PCD);
  355. Consumer.HandleTopLevelDecl(DeclGroupRef(PCD));
  356. }
  357. void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name,
  358. StringRef Value) {
  359. auto *PDMD = PragmaDetectMismatchDecl::Create(
  360. Context, Context.getTranslationUnitDecl(), Loc, Name, Value);
  361. Context.getTranslationUnitDecl()->addDecl(PDMD);
  362. Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD));
  363. }
  364. void Sema::ActOnPragmaMSPointersToMembers(
  365. LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,
  366. SourceLocation PragmaLoc) {
  367. MSPointerToMemberRepresentationMethod = RepresentationMethod;
  368. ImplicitMSInheritanceAttrLoc = PragmaLoc;
  369. }
  370. void Sema::ActOnPragmaMSVtorDisp(PragmaMsStackAction Action,
  371. SourceLocation PragmaLoc,
  372. MSVtorDispAttr::Mode Mode) {
  373. if (Action & PSK_Pop && VtorDispStack.Stack.empty())
  374. Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
  375. << "stack empty";
  376. VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode);
  377. }
  378. template<typename ValueType>
  379. void Sema::PragmaStack<ValueType>::Act(SourceLocation PragmaLocation,
  380. PragmaMsStackAction Action,
  381. llvm::StringRef StackSlotLabel,
  382. ValueType Value) {
  383. if (Action == PSK_Reset) {
  384. CurrentValue = DefaultValue;
  385. CurrentPragmaLocation = PragmaLocation;
  386. return;
  387. }
  388. if (Action & PSK_Push)
  389. Stack.emplace_back(StackSlotLabel, CurrentValue, CurrentPragmaLocation,
  390. PragmaLocation);
  391. else if (Action & PSK_Pop) {
  392. if (!StackSlotLabel.empty()) {
  393. // If we've got a label, try to find it and jump there.
  394. auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) {
  395. return x.StackSlotLabel == StackSlotLabel;
  396. });
  397. // If we found the label so pop from there.
  398. if (I != Stack.rend()) {
  399. CurrentValue = I->Value;
  400. CurrentPragmaLocation = I->PragmaLocation;
  401. Stack.erase(std::prev(I.base()), Stack.end());
  402. }
  403. } else if (!Stack.empty()) {
  404. // We do not have a label, just pop the last entry.
  405. CurrentValue = Stack.back().Value;
  406. CurrentPragmaLocation = Stack.back().PragmaLocation;
  407. Stack.pop_back();
  408. }
  409. }
  410. if (Action & PSK_Set) {
  411. CurrentValue = Value;
  412. CurrentPragmaLocation = PragmaLocation;
  413. }
  414. }
  415. bool Sema::UnifySection(StringRef SectionName,
  416. int SectionFlags,
  417. DeclaratorDecl *Decl) {
  418. auto Section = Context.SectionInfos.find(SectionName);
  419. if (Section == Context.SectionInfos.end()) {
  420. Context.SectionInfos[SectionName] =
  421. ASTContext::SectionInfo(Decl, SourceLocation(), SectionFlags);
  422. return false;
  423. }
  424. // A pre-declared section takes precedence w/o diagnostic.
  425. if (Section->second.SectionFlags == SectionFlags ||
  426. !(Section->second.SectionFlags & ASTContext::PSF_Implicit))
  427. return false;
  428. auto OtherDecl = Section->second.Decl;
  429. Diag(Decl->getLocation(), diag::err_section_conflict)
  430. << Decl << OtherDecl;
  431. Diag(OtherDecl->getLocation(), diag::note_declared_at)
  432. << OtherDecl->getName();
  433. if (auto A = Decl->getAttr<SectionAttr>())
  434. if (A->isImplicit())
  435. Diag(A->getLocation(), diag::note_pragma_entered_here);
  436. if (auto A = OtherDecl->getAttr<SectionAttr>())
  437. if (A->isImplicit())
  438. Diag(A->getLocation(), diag::note_pragma_entered_here);
  439. return true;
  440. }
  441. bool Sema::UnifySection(StringRef SectionName,
  442. int SectionFlags,
  443. SourceLocation PragmaSectionLocation) {
  444. auto Section = Context.SectionInfos.find(SectionName);
  445. if (Section != Context.SectionInfos.end()) {
  446. if (Section->second.SectionFlags == SectionFlags)
  447. return false;
  448. if (!(Section->second.SectionFlags & ASTContext::PSF_Implicit)) {
  449. Diag(PragmaSectionLocation, diag::err_section_conflict)
  450. << "this" << "a prior #pragma section";
  451. Diag(Section->second.PragmaSectionLocation,
  452. diag::note_pragma_entered_here);
  453. return true;
  454. }
  455. }
  456. Context.SectionInfos[SectionName] =
  457. ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags);
  458. return false;
  459. }
  460. /// Called on well formed \#pragma bss_seg().
  461. void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation,
  462. PragmaMsStackAction Action,
  463. llvm::StringRef StackSlotLabel,
  464. StringLiteral *SegmentName,
  465. llvm::StringRef PragmaName) {
  466. PragmaStack<StringLiteral *> *Stack =
  467. llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName)
  468. .Case("data_seg", &DataSegStack)
  469. .Case("bss_seg", &BSSSegStack)
  470. .Case("const_seg", &ConstSegStack)
  471. .Case("code_seg", &CodeSegStack);
  472. if (Action & PSK_Pop && Stack->Stack.empty())
  473. Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName
  474. << "stack empty";
  475. if (SegmentName) {
  476. if (!checkSectionName(SegmentName->getBeginLoc(), SegmentName->getString()))
  477. return;
  478. if (SegmentName->getString() == ".drectve" &&
  479. Context.getTargetInfo().getCXXABI().isMicrosoft())
  480. Diag(PragmaLocation, diag::warn_attribute_section_drectve) << PragmaName;
  481. }
  482. Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName);
  483. }
  484. /// Called on well formed \#pragma bss_seg().
  485. void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation,
  486. int SectionFlags, StringLiteral *SegmentName) {
  487. UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation);
  488. }
  489. void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,
  490. StringLiteral *SegmentName) {
  491. // There's no stack to maintain, so we just have a current section. When we
  492. // see the default section, reset our current section back to null so we stop
  493. // tacking on unnecessary attributes.
  494. CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName;
  495. CurInitSegLoc = PragmaLocation;
  496. }
  497. void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
  498. SourceLocation PragmaLoc) {
  499. IdentifierInfo *Name = IdTok.getIdentifierInfo();
  500. LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
  501. LookupParsedName(Lookup, curScope, nullptr, true);
  502. if (Lookup.empty()) {
  503. Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
  504. << Name << SourceRange(IdTok.getLocation());
  505. return;
  506. }
  507. VarDecl *VD = Lookup.getAsSingle<VarDecl>();
  508. if (!VD) {
  509. Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
  510. << Name << SourceRange(IdTok.getLocation());
  511. return;
  512. }
  513. // Warn if this was used before being marked unused.
  514. if (VD->isUsed())
  515. Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
  516. VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation(),
  517. AttributeCommonInfo::AS_Pragma,
  518. UnusedAttr::GNU_unused));
  519. }
  520. void Sema::AddCFAuditedAttribute(Decl *D) {
  521. IdentifierInfo *Ident;
  522. SourceLocation Loc;
  523. std::tie(Ident, Loc) = PP.getPragmaARCCFCodeAuditedInfo();
  524. if (!Loc.isValid()) return;
  525. // Don't add a redundant or conflicting attribute.
  526. if (D->hasAttr<CFAuditedTransferAttr>() ||
  527. D->hasAttr<CFUnknownTransferAttr>())
  528. return;
  529. AttributeCommonInfo Info(Ident, SourceRange(Loc),
  530. AttributeCommonInfo::AS_Pragma);
  531. D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Info));
  532. }
  533. namespace {
  534. Optional<attr::SubjectMatchRule>
  535. getParentAttrMatcherRule(attr::SubjectMatchRule Rule) {
  536. using namespace attr;
  537. switch (Rule) {
  538. default:
  539. return None;
  540. #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
  541. #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \
  542. case Value: \
  543. return Parent;
  544. #include "clang/Basic/AttrSubMatchRulesList.inc"
  545. }
  546. }
  547. bool isNegatedAttrMatcherSubRule(attr::SubjectMatchRule Rule) {
  548. using namespace attr;
  549. switch (Rule) {
  550. default:
  551. return false;
  552. #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
  553. #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \
  554. case Value: \
  555. return IsNegated;
  556. #include "clang/Basic/AttrSubMatchRulesList.inc"
  557. }
  558. }
  559. CharSourceRange replacementRangeForListElement(const Sema &S,
  560. SourceRange Range) {
  561. // Make sure that the ',' is removed as well.
  562. SourceLocation AfterCommaLoc = Lexer::findLocationAfterToken(
  563. Range.getEnd(), tok::comma, S.getSourceManager(), S.getLangOpts(),
  564. /*SkipTrailingWhitespaceAndNewLine=*/false);
  565. if (AfterCommaLoc.isValid())
  566. return CharSourceRange::getCharRange(Range.getBegin(), AfterCommaLoc);
  567. else
  568. return CharSourceRange::getTokenRange(Range);
  569. }
  570. std::string
  571. attrMatcherRuleListToString(ArrayRef<attr::SubjectMatchRule> Rules) {
  572. std::string Result;
  573. llvm::raw_string_ostream OS(Result);
  574. for (const auto &I : llvm::enumerate(Rules)) {
  575. if (I.index())
  576. OS << (I.index() == Rules.size() - 1 ? ", and " : ", ");
  577. OS << "'" << attr::getSubjectMatchRuleSpelling(I.value()) << "'";
  578. }
  579. return OS.str();
  580. }
  581. } // end anonymous namespace
  582. void Sema::ActOnPragmaAttributeAttribute(
  583. ParsedAttr &Attribute, SourceLocation PragmaLoc,
  584. attr::ParsedSubjectMatchRuleSet Rules) {
  585. Attribute.setIsPragmaClangAttribute();
  586. SmallVector<attr::SubjectMatchRule, 4> SubjectMatchRules;
  587. // Gather the subject match rules that are supported by the attribute.
  588. SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4>
  589. StrictSubjectMatchRuleSet;
  590. Attribute.getMatchRules(LangOpts, StrictSubjectMatchRuleSet);
  591. // Figure out which subject matching rules are valid.
  592. if (StrictSubjectMatchRuleSet.empty()) {
  593. // Check for contradicting match rules. Contradicting match rules are
  594. // either:
  595. // - a top-level rule and one of its sub-rules. E.g. variable and
  596. // variable(is_parameter).
  597. // - a sub-rule and a sibling that's negated. E.g.
  598. // variable(is_thread_local) and variable(unless(is_parameter))
  599. llvm::SmallDenseMap<int, std::pair<int, SourceRange>, 2>
  600. RulesToFirstSpecifiedNegatedSubRule;
  601. for (const auto &Rule : Rules) {
  602. attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
  603. Optional<attr::SubjectMatchRule> ParentRule =
  604. getParentAttrMatcherRule(MatchRule);
  605. if (!ParentRule)
  606. continue;
  607. auto It = Rules.find(*ParentRule);
  608. if (It != Rules.end()) {
  609. // A sub-rule contradicts a parent rule.
  610. Diag(Rule.second.getBegin(),
  611. diag::err_pragma_attribute_matcher_subrule_contradicts_rule)
  612. << attr::getSubjectMatchRuleSpelling(MatchRule)
  613. << attr::getSubjectMatchRuleSpelling(*ParentRule) << It->second
  614. << FixItHint::CreateRemoval(
  615. replacementRangeForListElement(*this, Rule.second));
  616. // Keep going without removing this rule as it won't change the set of
  617. // declarations that receive the attribute.
  618. continue;
  619. }
  620. if (isNegatedAttrMatcherSubRule(MatchRule))
  621. RulesToFirstSpecifiedNegatedSubRule.insert(
  622. std::make_pair(*ParentRule, Rule));
  623. }
  624. bool IgnoreNegatedSubRules = false;
  625. for (const auto &Rule : Rules) {
  626. attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
  627. Optional<attr::SubjectMatchRule> ParentRule =
  628. getParentAttrMatcherRule(MatchRule);
  629. if (!ParentRule)
  630. continue;
  631. auto It = RulesToFirstSpecifiedNegatedSubRule.find(*ParentRule);
  632. if (It != RulesToFirstSpecifiedNegatedSubRule.end() &&
  633. It->second != Rule) {
  634. // Negated sub-rule contradicts another sub-rule.
  635. Diag(
  636. It->second.second.getBegin(),
  637. diag::
  638. err_pragma_attribute_matcher_negated_subrule_contradicts_subrule)
  639. << attr::getSubjectMatchRuleSpelling(
  640. attr::SubjectMatchRule(It->second.first))
  641. << attr::getSubjectMatchRuleSpelling(MatchRule) << Rule.second
  642. << FixItHint::CreateRemoval(
  643. replacementRangeForListElement(*this, It->second.second));
  644. // Keep going but ignore all of the negated sub-rules.
  645. IgnoreNegatedSubRules = true;
  646. RulesToFirstSpecifiedNegatedSubRule.erase(It);
  647. }
  648. }
  649. if (!IgnoreNegatedSubRules) {
  650. for (const auto &Rule : Rules)
  651. SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
  652. } else {
  653. for (const auto &Rule : Rules) {
  654. if (!isNegatedAttrMatcherSubRule(attr::SubjectMatchRule(Rule.first)))
  655. SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
  656. }
  657. }
  658. Rules.clear();
  659. } else {
  660. for (const auto &Rule : StrictSubjectMatchRuleSet) {
  661. if (Rules.erase(Rule.first)) {
  662. // Add the rule to the set of attribute receivers only if it's supported
  663. // in the current language mode.
  664. if (Rule.second)
  665. SubjectMatchRules.push_back(Rule.first);
  666. }
  667. }
  668. }
  669. if (!Rules.empty()) {
  670. auto Diagnostic =
  671. Diag(PragmaLoc, diag::err_pragma_attribute_invalid_matchers)
  672. << Attribute;
  673. SmallVector<attr::SubjectMatchRule, 2> ExtraRules;
  674. for (const auto &Rule : Rules) {
  675. ExtraRules.push_back(attr::SubjectMatchRule(Rule.first));
  676. Diagnostic << FixItHint::CreateRemoval(
  677. replacementRangeForListElement(*this, Rule.second));
  678. }
  679. Diagnostic << attrMatcherRuleListToString(ExtraRules);
  680. }
  681. if (PragmaAttributeStack.empty()) {
  682. Diag(PragmaLoc, diag::err_pragma_attr_attr_no_push);
  683. return;
  684. }
  685. PragmaAttributeStack.back().Entries.push_back(
  686. {PragmaLoc, &Attribute, std::move(SubjectMatchRules), /*IsUsed=*/false});
  687. }
  688. void Sema::ActOnPragmaAttributeEmptyPush(SourceLocation PragmaLoc,
  689. const IdentifierInfo *Namespace) {
  690. PragmaAttributeStack.emplace_back();
  691. PragmaAttributeStack.back().Loc = PragmaLoc;
  692. PragmaAttributeStack.back().Namespace = Namespace;
  693. }
  694. void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc,
  695. const IdentifierInfo *Namespace) {
  696. if (PragmaAttributeStack.empty()) {
  697. Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
  698. return;
  699. }
  700. // Dig back through the stack trying to find the most recently pushed group
  701. // that in Namespace. Note that this works fine if no namespace is present,
  702. // think of push/pops without namespaces as having an implicit "nullptr"
  703. // namespace.
  704. for (size_t Index = PragmaAttributeStack.size(); Index;) {
  705. --Index;
  706. if (PragmaAttributeStack[Index].Namespace == Namespace) {
  707. for (const PragmaAttributeEntry &Entry :
  708. PragmaAttributeStack[Index].Entries) {
  709. if (!Entry.IsUsed) {
  710. assert(Entry.Attribute && "Expected an attribute");
  711. Diag(Entry.Attribute->getLoc(), diag::warn_pragma_attribute_unused)
  712. << *Entry.Attribute;
  713. Diag(PragmaLoc, diag::note_pragma_attribute_region_ends_here);
  714. }
  715. }
  716. PragmaAttributeStack.erase(PragmaAttributeStack.begin() + Index);
  717. return;
  718. }
  719. }
  720. if (Namespace)
  721. Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch)
  722. << 0 << Namespace->getName();
  723. else
  724. Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
  725. }
  726. void Sema::AddPragmaAttributes(Scope *S, Decl *D) {
  727. if (PragmaAttributeStack.empty())
  728. return;
  729. for (auto &Group : PragmaAttributeStack) {
  730. for (auto &Entry : Group.Entries) {
  731. ParsedAttr *Attribute = Entry.Attribute;
  732. assert(Attribute && "Expected an attribute");
  733. assert(Attribute->isPragmaClangAttribute() &&
  734. "expected #pragma clang attribute");
  735. // Ensure that the attribute can be applied to the given declaration.
  736. bool Applies = false;
  737. for (const auto &Rule : Entry.MatchRules) {
  738. if (Attribute->appliesToDecl(D, Rule)) {
  739. Applies = true;
  740. break;
  741. }
  742. }
  743. if (!Applies)
  744. continue;
  745. Entry.IsUsed = true;
  746. PragmaAttributeCurrentTargetDecl = D;
  747. ParsedAttributesView Attrs;
  748. Attrs.addAtEnd(Attribute);
  749. ProcessDeclAttributeList(S, D, Attrs);
  750. PragmaAttributeCurrentTargetDecl = nullptr;
  751. }
  752. }
  753. }
  754. void Sema::PrintPragmaAttributeInstantiationPoint() {
  755. assert(PragmaAttributeCurrentTargetDecl && "Expected an active declaration");
  756. Diags.Report(PragmaAttributeCurrentTargetDecl->getBeginLoc(),
  757. diag::note_pragma_attribute_applied_decl_here);
  758. }
  759. void Sema::DiagnoseUnterminatedPragmaAttribute() {
  760. if (PragmaAttributeStack.empty())
  761. return;
  762. Diag(PragmaAttributeStack.back().Loc, diag::err_pragma_attribute_no_pop_eof);
  763. }
  764. void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) {
  765. if(On)
  766. OptimizeOffPragmaLocation = SourceLocation();
  767. else
  768. OptimizeOffPragmaLocation = PragmaLoc;
  769. }
  770. void Sema::AddRangeBasedOptnone(FunctionDecl *FD) {
  771. // In the future, check other pragmas if they're implemented (e.g. pragma
  772. // optimize 0 will probably map to this functionality too).
  773. if(OptimizeOffPragmaLocation.isValid())
  774. AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation);
  775. }
  776. void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD,
  777. SourceLocation Loc) {
  778. // Don't add a conflicting attribute. No diagnostic is needed.
  779. if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>())
  780. return;
  781. // Add attributes only if required. Optnone requires noinline as well, but if
  782. // either is already present then don't bother adding them.
  783. if (!FD->hasAttr<OptimizeNoneAttr>())
  784. FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc));
  785. if (!FD->hasAttr<NoInlineAttr>())
  786. FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc));
  787. }
  788. typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
  789. enum : unsigned { NoVisibility = ~0U };
  790. void Sema::AddPushedVisibilityAttribute(Decl *D) {
  791. if (!VisContext)
  792. return;
  793. NamedDecl *ND = dyn_cast<NamedDecl>(D);
  794. if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
  795. return;
  796. VisStack *Stack = static_cast<VisStack*>(VisContext);
  797. unsigned rawType = Stack->back().first;
  798. if (rawType == NoVisibility) return;
  799. VisibilityAttr::VisibilityType type
  800. = (VisibilityAttr::VisibilityType) rawType;
  801. SourceLocation loc = Stack->back().second;
  802. D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
  803. }
  804. /// FreeVisContext - Deallocate and null out VisContext.
  805. void Sema::FreeVisContext() {
  806. delete static_cast<VisStack*>(VisContext);
  807. VisContext = nullptr;
  808. }
  809. static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
  810. // Put visibility on stack.
  811. if (!S.VisContext)
  812. S.VisContext = new VisStack;
  813. VisStack *Stack = static_cast<VisStack*>(S.VisContext);
  814. Stack->push_back(std::make_pair(type, loc));
  815. }
  816. void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
  817. SourceLocation PragmaLoc) {
  818. if (VisType) {
  819. // Compute visibility to use.
  820. VisibilityAttr::VisibilityType T;
  821. if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
  822. Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
  823. return;
  824. }
  825. PushPragmaVisibility(*this, T, PragmaLoc);
  826. } else {
  827. PopPragmaVisibility(false, PragmaLoc);
  828. }
  829. }
  830. void Sema::ActOnPragmaFPContract(LangOptions::FPContractModeKind FPC) {
  831. switch (FPC) {
  832. case LangOptions::FPC_On:
  833. FPFeatures.setAllowFPContractWithinStatement();
  834. break;
  835. case LangOptions::FPC_Fast:
  836. FPFeatures.setAllowFPContractAcrossStatement();
  837. break;
  838. case LangOptions::FPC_Off:
  839. FPFeatures.setDisallowFPContract();
  840. break;
  841. }
  842. }
  843. void Sema::ActOnPragmaFEnvAccess(LangOptions::FEnvAccessModeKind FPC) {
  844. switch (FPC) {
  845. case LangOptions::FEA_On:
  846. FPFeatures.setAllowFEnvAccess();
  847. break;
  848. case LangOptions::FEA_Off:
  849. FPFeatures.setDisallowFEnvAccess();
  850. break;
  851. }
  852. }
  853. void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
  854. SourceLocation Loc) {
  855. // Visibility calculations will consider the namespace's visibility.
  856. // Here we just want to note that we're in a visibility context
  857. // which overrides any enclosing #pragma context, but doesn't itself
  858. // contribute visibility.
  859. PushPragmaVisibility(*this, NoVisibility, Loc);
  860. }
  861. void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
  862. if (!VisContext) {
  863. Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
  864. return;
  865. }
  866. // Pop visibility from stack
  867. VisStack *Stack = static_cast<VisStack*>(VisContext);
  868. const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
  869. bool StartsWithPragma = Back->first != NoVisibility;
  870. if (StartsWithPragma && IsNamespaceEnd) {
  871. Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
  872. Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
  873. // For better error recovery, eat all pushes inside the namespace.
  874. do {
  875. Stack->pop_back();
  876. Back = &Stack->back();
  877. StartsWithPragma = Back->first != NoVisibility;
  878. } while (StartsWithPragma);
  879. } else if (!StartsWithPragma && !IsNamespaceEnd) {
  880. Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
  881. Diag(Back->second, diag::note_surrounding_namespace_starts_here);
  882. return;
  883. }
  884. Stack->pop_back();
  885. // To simplify the implementation, never keep around an empty stack.
  886. if (Stack->empty())
  887. FreeVisContext();
  888. }