blob: 7f523c465aff8a3146defd2fe8c63b1e5c7cf26d [file] [log] [blame]
Bill Wendling44426052012-12-20 19:22:21 +00001//===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
Chris Lattner2eccbc12009-02-17 00:57:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner31180bb2009-02-17 01:09:29 +000010// This file implements semantic analysis for non-trivial attributes and
11// pragmas.
Chris Lattner2eccbc12009-02-17 00:57:29 +000012//
13//===----------------------------------------------------------------------===//
14
John McCall83024632010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Reid Klecknere43f0fe2013-05-08 13:44:39 +000016#include "clang/AST/ASTConsumer.h"
Alexis Huntdcfba7b2010-08-18 23:23:40 +000017#include "clang/AST/Attr.h"
Chris Lattner2eccbc12009-02-17 00:57:29 +000018#include "clang/AST/Expr.h"
Daniel Dunbarbd606522010-05-27 00:35:16 +000019#include "clang/Basic/TargetInfo.h"
20#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Sema/Lookup.h"
Chris Lattner2eccbc12009-02-17 00:57:29 +000022using namespace clang;
23
Chris Lattner31180bb2009-02-17 01:09:29 +000024//===----------------------------------------------------------------------===//
Daniel Dunbar69dac582010-05-27 00:04:40 +000025// Pragma 'pack' and 'options align'
Chris Lattner31180bb2009-02-17 01:09:29 +000026//===----------------------------------------------------------------------===//
27
28namespace {
Daniel Dunbar4dbe15d2010-05-27 02:25:27 +000029 struct PackStackEntry {
Daniel Dunbar6da10982010-05-27 05:45:51 +000030 // We just use a sentinel to represent when the stack is set to mac68k
31 // alignment.
32 static const unsigned kMac68kAlignmentSentinel = ~0U;
33
Daniel Dunbar4dbe15d2010-05-27 02:25:27 +000034 unsigned Alignment;
35 IdentifierInfo *Name;
36 };
37
Chris Lattner31180bb2009-02-17 01:09:29 +000038 /// PragmaPackStack - Simple class to wrap the stack used by #pragma
39 /// pack.
40 class PragmaPackStack {
Daniel Dunbar4dbe15d2010-05-27 02:25:27 +000041 typedef std::vector<PackStackEntry> stack_ty;
Chris Lattner31180bb2009-02-17 01:09:29 +000042
43 /// Alignment - The current user specified alignment.
44 unsigned Alignment;
45
46 /// Stack - Entries in the #pragma pack stack, consisting of saved
47 /// alignments and optional names.
48 stack_ty Stack;
Mike Stump11289f42009-09-09 15:08:12 +000049
50 public:
Chris Lattner31180bb2009-02-17 01:09:29 +000051 PragmaPackStack() : Alignment(0) {}
52
53 void setAlignment(unsigned A) { Alignment = A; }
54 unsigned getAlignment() { return Alignment; }
55
56 /// push - Push the current alignment onto the stack, optionally
57 /// using the given \arg Name for the record, if non-zero.
58 void push(IdentifierInfo *Name) {
Daniel Dunbar4dbe15d2010-05-27 02:25:27 +000059 PackStackEntry PSE = { Alignment, Name };
60 Stack.push_back(PSE);
Chris Lattner31180bb2009-02-17 01:09:29 +000061 }
62
63 /// pop - Pop a record from the stack and restore the current
64 /// alignment to the previous value. If \arg Name is non-zero then
65 /// the first such named record is popped, otherwise the top record
66 /// is popped. Returns true if the pop succeeded.
Daniel Dunbar84336ba2010-07-16 04:54:16 +000067 bool pop(IdentifierInfo *Name, bool IsReset);
Chris Lattner31180bb2009-02-17 01:09:29 +000068 };
69} // end anonymous namespace.
70
Daniel Dunbar84336ba2010-07-16 04:54:16 +000071bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
Chris Lattner31180bb2009-02-17 01:09:29 +000072 // If name is empty just pop top.
73 if (!Name) {
Daniel Dunbar84336ba2010-07-16 04:54:16 +000074 // An empty stack is a special case...
75 if (Stack.empty()) {
76 // If this isn't a reset, it is always an error.
77 if (!IsReset)
78 return false;
79
80 // Otherwise, it is an error only if some alignment has been set.
81 if (!Alignment)
82 return false;
83
84 // Otherwise, reset to the default alignment.
85 Alignment = 0;
86 } else {
87 Alignment = Stack.back().Alignment;
88 Stack.pop_back();
89 }
90
Chris Lattner31180bb2009-02-17 01:09:29 +000091 return true;
Mike Stump11289f42009-09-09 15:08:12 +000092 }
93
Chris Lattner31180bb2009-02-17 01:09:29 +000094 // Otherwise, find the named record.
95 for (unsigned i = Stack.size(); i != 0; ) {
96 --i;
Daniel Dunbar4dbe15d2010-05-27 02:25:27 +000097 if (Stack[i].Name == Name) {
Chris Lattner31180bb2009-02-17 01:09:29 +000098 // Found it, pop up to and including this record.
Daniel Dunbar4dbe15d2010-05-27 02:25:27 +000099 Alignment = Stack[i].Alignment;
Chris Lattner31180bb2009-02-17 01:09:29 +0000100 Stack.erase(Stack.begin() + i, Stack.end());
101 return true;
102 }
103 }
Mike Stump11289f42009-09-09 15:08:12 +0000104
Chris Lattner31180bb2009-02-17 01:09:29 +0000105 return false;
106}
107
108
109/// FreePackedContext - Deallocate and null out PackContext.
110void Sema::FreePackedContext() {
111 delete static_cast<PragmaPackStack*>(PackContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000112 PackContext = nullptr;
Chris Lattner31180bb2009-02-17 01:09:29 +0000113}
114
Daniel Dunbar8804f2e2010-05-27 01:53:40 +0000115void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
116 // If there is no pack context, we don't need any attributes.
117 if (!PackContext)
118 return;
119
120 PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext);
121
122 // Otherwise, check to see if we need a max field alignment attribute.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000123 if (unsigned Alignment = Stack->getAlignment()) {
124 if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
Aaron Ballman36a53502014-01-16 13:03:14 +0000125 RD->addAttr(AlignMac68kAttr::CreateImplicit(Context));
Daniel Dunbar6da10982010-05-27 05:45:51 +0000126 else
Aaron Ballman36a53502014-01-16 13:03:14 +0000127 RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000128 Alignment * 8));
Daniel Dunbar6da10982010-05-27 05:45:51 +0000129 }
Chris Lattner31180bb2009-02-17 01:09:29 +0000130}
131
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000132void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000133 if (MSStructPragmaOn)
David Majnemer8ab003a2015-02-02 19:30:52 +0000134 RD->addAttr(MSStructAttr::CreateImplicit(Context));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000135
136 // FIXME: We should merge AddAlignmentAttributesForRecord with
137 // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes
138 // all active pragmas and applies them as attributes to class definitions.
139 if (VtorDispModeStack.back() != getLangOpts().VtorDispMode)
140 RD->addAttr(
141 MSVtorDispAttr::CreateImplicit(Context, VtorDispModeStack.back()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000142}
143
Daniel Dunbar69dac582010-05-27 00:04:40 +0000144void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
Eli Friedman68be1642012-10-04 02:36:51 +0000145 SourceLocation PragmaLoc) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000146 if (!PackContext)
Daniel Dunbar69dac582010-05-27 00:04:40 +0000147 PackContext = new PragmaPackStack();
148
149 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
150
Daniel Dunbar69dac582010-05-27 00:04:40 +0000151 switch (Kind) {
Daniel Dunbar663e8092010-05-27 18:42:09 +0000152 // For all targets we support native and natural are the same.
153 //
154 // FIXME: This is not true on Darwin/PPC.
155 case POAK_Native:
Daniel Dunbar5794c6f2010-05-28 19:43:33 +0000156 case POAK_Power:
Daniel Dunbara6885662010-05-28 20:08:00 +0000157 case POAK_Natural:
Craig Topperc3ec1492014-05-26 06:22:03 +0000158 Context->push(nullptr);
Daniel Dunbar5794c6f2010-05-28 19:43:33 +0000159 Context->setAlignment(0);
160 break;
161
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +0000162 // Note that '#pragma options align=packed' is not equivalent to attribute
163 // packed, it has a different precedence relative to attribute aligned.
164 case POAK_Packed:
Craig Topperc3ec1492014-05-26 06:22:03 +0000165 Context->push(nullptr);
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +0000166 Context->setAlignment(1);
167 break;
168
Daniel Dunbarbd606522010-05-27 00:35:16 +0000169 case POAK_Mac68k:
170 // Check if the target supports this.
Alp Tokerb6cc5922014-05-03 03:45:55 +0000171 if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) {
Daniel Dunbarbd606522010-05-27 00:35:16 +0000172 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
173 return;
Daniel Dunbarbd606522010-05-27 00:35:16 +0000174 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000175 Context->push(nullptr);
Daniel Dunbar6da10982010-05-27 05:45:51 +0000176 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
Daniel Dunbarbd606522010-05-27 00:35:16 +0000177 break;
178
Eli Friedman68be1642012-10-04 02:36:51 +0000179 case POAK_Reset:
180 // Reset just pops the top of the stack, or resets the current alignment to
181 // default.
Craig Topperc3ec1492014-05-26 06:22:03 +0000182 if (!Context->pop(nullptr, /*IsReset=*/true)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000183 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
184 << "stack empty";
185 }
Daniel Dunbar69dac582010-05-27 00:04:40 +0000186 break;
187 }
188}
189
Mike Stump11289f42009-09-09 15:08:12 +0000190void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
Richard Trieu2bd04012011-09-09 02:00:50 +0000191 Expr *alignment, SourceLocation PragmaLoc,
Chris Lattner2eccbc12009-02-17 00:57:29 +0000192 SourceLocation LParenLoc, SourceLocation RParenLoc) {
193 Expr *Alignment = static_cast<Expr *>(alignment);
194
195 // If specified then alignment must be a "small" power of two.
196 unsigned AlignmentVal = 0;
197 if (Alignment) {
198 llvm::APSInt Val;
Mike Stump11289f42009-09-09 15:08:12 +0000199
Daniel Dunbare03c6102009-03-06 20:45:54 +0000200 // pack(0) is like pack(), which just works out since that is what
201 // we use 0 for in PackAttr.
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000202 if (Alignment->isTypeDependent() ||
203 Alignment->isValueDependent() ||
204 !Alignment->isIntegerConstantExpr(Val, Context) ||
Daniel Dunbare03c6102009-03-06 20:45:54 +0000205 !(Val == 0 || Val.isPowerOf2()) ||
Chris Lattner2eccbc12009-02-17 00:57:29 +0000206 Val.getZExtValue() > 16) {
207 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000208 return; // Ignore
209 }
210
211 AlignmentVal = (unsigned) Val.getZExtValue();
212 }
Mike Stump11289f42009-09-09 15:08:12 +0000213
Craig Topperc3ec1492014-05-26 06:22:03 +0000214 if (!PackContext)
Chris Lattner31180bb2009-02-17 01:09:29 +0000215 PackContext = new PragmaPackStack();
Mike Stump11289f42009-09-09 15:08:12 +0000216
Chris Lattner31180bb2009-02-17 01:09:29 +0000217 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
Mike Stump11289f42009-09-09 15:08:12 +0000218
Chris Lattner2eccbc12009-02-17 00:57:29 +0000219 switch (Kind) {
John McCallfaf5fb42010-08-26 23:41:50 +0000220 case Sema::PPK_Default: // pack([n])
Chris Lattner31180bb2009-02-17 01:09:29 +0000221 Context->setAlignment(AlignmentVal);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000222 break;
223
John McCallfaf5fb42010-08-26 23:41:50 +0000224 case Sema::PPK_Show: // pack(show)
Chris Lattner2eccbc12009-02-17 00:57:29 +0000225 // Show the current alignment, making sure to show the right value
226 // for the default.
Chris Lattner31180bb2009-02-17 01:09:29 +0000227 AlignmentVal = Context->getAlignment();
Chris Lattner2eccbc12009-02-17 00:57:29 +0000228 // FIXME: This should come from the target.
229 if (AlignmentVal == 0)
230 AlignmentVal = 8;
Daniel Dunbar6da10982010-05-27 05:45:51 +0000231 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
232 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
233 else
234 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
Chris Lattner2eccbc12009-02-17 00:57:29 +0000235 break;
236
John McCallfaf5fb42010-08-26 23:41:50 +0000237 case Sema::PPK_Push: // pack(push [, id] [, [n])
Chris Lattner31180bb2009-02-17 01:09:29 +0000238 Context->push(Name);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000239 // Set the new alignment if specified.
240 if (Alignment)
Mike Stump11289f42009-09-09 15:08:12 +0000241 Context->setAlignment(AlignmentVal);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000242 break;
243
John McCallfaf5fb42010-08-26 23:41:50 +0000244 case Sema::PPK_Pop: // pack(pop [, id] [, n])
Chris Lattner2eccbc12009-02-17 00:57:29 +0000245 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
246 // "#pragma pack(pop, identifier, n) is undefined"
247 if (Alignment && Name)
Mike Stump11289f42009-09-09 15:08:12 +0000248 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
249
Chris Lattner2eccbc12009-02-17 00:57:29 +0000250 // Do the pop.
Daniel Dunbar84336ba2010-07-16 04:54:16 +0000251 if (!Context->pop(Name, /*IsReset=*/false)) {
Chris Lattner2eccbc12009-02-17 00:57:29 +0000252 // If a name was specified then failure indicates the name
253 // wasn't found. Otherwise failure indicates the stack was
254 // empty.
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000255 Diag(PragmaLoc, diag::warn_pragma_pop_failed)
256 << "pack" << (Name ? "no record matching name" : "stack empty");
Chris Lattner2eccbc12009-02-17 00:57:29 +0000257
258 // FIXME: Warn about popping named records as MSVC does.
259 } else {
260 // Pop succeeded, set the new alignment if specified.
261 if (Alignment)
Chris Lattner31180bb2009-02-17 01:09:29 +0000262 Context->setAlignment(AlignmentVal);
Chris Lattner2eccbc12009-02-17 00:57:29 +0000263 }
264 break;
Chris Lattner2eccbc12009-02-17 00:57:29 +0000265 }
266}
267
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000268void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
269 MSStructPragmaOn = (Kind == PMSST_ON);
270}
271
Nico Weber66220292016-03-02 17:28:48 +0000272void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc,
273 PragmaMSCommentKind Kind, StringRef Arg) {
274 auto *PCD = PragmaCommentDecl::Create(
275 Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg);
276 Context.getTranslationUnitDecl()->addDecl(PCD);
277 Consumer.HandleTopLevelDecl(DeclGroupRef(PCD));
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000278}
279
Nico Webercbbaeb12016-03-02 19:28:54 +0000280void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name,
281 StringRef Value) {
282 auto *PDMD = PragmaDetectMismatchDecl::Create(
283 Context, Context.getTranslationUnitDecl(), Loc, Name, Value);
284 Context.getTranslationUnitDecl()->addDecl(PDMD);
285 Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD));
Aaron Ballman5d041be2013-06-04 02:07:14 +0000286}
287
David Majnemer4bb09802014-02-10 19:50:15 +0000288void Sema::ActOnPragmaMSPointersToMembers(
David Majnemer86c318f2014-02-11 21:05:00 +0000289 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,
David Majnemer4bb09802014-02-10 19:50:15 +0000290 SourceLocation PragmaLoc) {
291 MSPointerToMemberRepresentationMethod = RepresentationMethod;
292 ImplicitMSInheritanceAttrLoc = PragmaLoc;
293}
294
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000295void Sema::ActOnPragmaMSVtorDisp(PragmaVtorDispKind Kind,
296 SourceLocation PragmaLoc,
297 MSVtorDispAttr::Mode Mode) {
298 switch (Kind) {
299 case PVDK_Set:
300 VtorDispModeStack.back() = Mode;
301 break;
302 case PVDK_Push:
303 VtorDispModeStack.push_back(Mode);
304 break;
305 case PVDK_Reset:
306 VtorDispModeStack.clear();
307 VtorDispModeStack.push_back(MSVtorDispAttr::Mode(LangOpts.VtorDispMode));
308 break;
309 case PVDK_Pop:
310 VtorDispModeStack.pop_back();
311 if (VtorDispModeStack.empty()) {
312 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
313 << "stack empty";
314 VtorDispModeStack.push_back(MSVtorDispAttr::Mode(LangOpts.VtorDispMode));
315 }
316 break;
317 }
318}
319
Warren Huntc3b18962014-04-08 22:30:47 +0000320template<typename ValueType>
321void Sema::PragmaStack<ValueType>::Act(SourceLocation PragmaLocation,
322 PragmaMsStackAction Action,
323 llvm::StringRef StackSlotLabel,
324 ValueType Value) {
325 if (Action == PSK_Reset) {
326 CurrentValue = nullptr;
327 return;
328 }
329 if (Action & PSK_Push)
330 Stack.push_back(Slot(StackSlotLabel, CurrentValue, CurrentPragmaLocation));
331 else if (Action & PSK_Pop) {
332 if (!StackSlotLabel.empty()) {
333 // If we've got a label, try to find it and jump there.
334 auto I = std::find_if(Stack.rbegin(), Stack.rend(),
335 [&](const Slot &x) { return x.StackSlotLabel == StackSlotLabel; });
336 // If we found the label so pop from there.
337 if (I != Stack.rend()) {
338 CurrentValue = I->Value;
339 CurrentPragmaLocation = I->PragmaLocation;
340 Stack.erase(std::prev(I.base()), Stack.end());
341 }
342 } else if (!Stack.empty()) {
343 // We don't have a label, just pop the last entry.
344 CurrentValue = Stack.back().Value;
345 CurrentPragmaLocation = Stack.back().PragmaLocation;
346 Stack.pop_back();
347 }
348 }
349 if (Action & PSK_Set) {
350 CurrentValue = Value;
351 CurrentPragmaLocation = PragmaLocation;
352 }
353}
354
Craig Topperbf3e3272014-08-30 16:55:52 +0000355bool Sema::UnifySection(StringRef SectionName,
Warren Huntc3b18962014-04-08 22:30:47 +0000356 int SectionFlags,
357 DeclaratorDecl *Decl) {
Hans Wennborg899ded92014-10-16 20:52:46 +0000358 auto Section = Context.SectionInfos.find(SectionName);
359 if (Section == Context.SectionInfos.end()) {
360 Context.SectionInfos[SectionName] =
361 ASTContext::SectionInfo(Decl, SourceLocation(), SectionFlags);
Warren Huntc3b18962014-04-08 22:30:47 +0000362 return false;
363 }
364 // A pre-declared section takes precedence w/o diagnostic.
365 if (Section->second.SectionFlags == SectionFlags ||
Hans Wennborg899ded92014-10-16 20:52:46 +0000366 !(Section->second.SectionFlags & ASTContext::PSF_Implicit))
Warren Huntc3b18962014-04-08 22:30:47 +0000367 return false;
368 auto OtherDecl = Section->second.Decl;
369 Diag(Decl->getLocation(), diag::err_section_conflict)
370 << Decl << OtherDecl;
371 Diag(OtherDecl->getLocation(), diag::note_declared_at)
372 << OtherDecl->getName();
373 if (auto A = Decl->getAttr<SectionAttr>())
374 if (A->isImplicit())
375 Diag(A->getLocation(), diag::note_pragma_entered_here);
376 if (auto A = OtherDecl->getAttr<SectionAttr>())
377 if (A->isImplicit())
378 Diag(A->getLocation(), diag::note_pragma_entered_here);
Ehsan Akhgari0b510602014-09-22 19:46:39 +0000379 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000380}
381
Craig Topperbf3e3272014-08-30 16:55:52 +0000382bool Sema::UnifySection(StringRef SectionName,
Warren Huntc3b18962014-04-08 22:30:47 +0000383 int SectionFlags,
384 SourceLocation PragmaSectionLocation) {
Hans Wennborg899ded92014-10-16 20:52:46 +0000385 auto Section = Context.SectionInfos.find(SectionName);
386 if (Section != Context.SectionInfos.end()) {
Warren Huntc3b18962014-04-08 22:30:47 +0000387 if (Section->second.SectionFlags == SectionFlags)
388 return false;
Hans Wennborg899ded92014-10-16 20:52:46 +0000389 if (!(Section->second.SectionFlags & ASTContext::PSF_Implicit)) {
Warren Huntc3b18962014-04-08 22:30:47 +0000390 Diag(PragmaSectionLocation, diag::err_section_conflict)
391 << "this" << "a prior #pragma section";
392 Diag(Section->second.PragmaSectionLocation,
393 diag::note_pragma_entered_here);
394 return true;
395 }
396 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000397 Context.SectionInfos[SectionName] =
398 ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags);
Warren Huntc3b18962014-04-08 22:30:47 +0000399 return false;
400}
401
402/// \brief Called on well formed \#pragma bss_seg().
403void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation,
404 PragmaMsStackAction Action,
405 llvm::StringRef StackSlotLabel,
406 StringLiteral *SegmentName,
407 llvm::StringRef PragmaName) {
408 PragmaStack<StringLiteral *> *Stack =
409 llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName)
410 .Case("data_seg", &DataSegStack)
411 .Case("bss_seg", &BSSSegStack)
412 .Case("const_seg", &ConstSegStack)
413 .Case("code_seg", &CodeSegStack);
414 if (Action & PSK_Pop && Stack->Stack.empty())
415 Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName
416 << "stack empty";
Reid Kleckner2a133222015-03-04 23:39:17 +0000417 if (SegmentName &&
418 !checkSectionName(SegmentName->getLocStart(), SegmentName->getString()))
419 return;
Warren Huntc3b18962014-04-08 22:30:47 +0000420 Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName);
421}
422
423/// \brief Called on well formed \#pragma bss_seg().
424void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation,
425 int SectionFlags, StringLiteral *SegmentName) {
426 UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation);
427}
428
Reid Kleckner1a711b12014-07-22 00:53:05 +0000429void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,
430 StringLiteral *SegmentName) {
431 // There's no stack to maintain, so we just have a current section. When we
432 // see the default section, reset our current section back to null so we stop
433 // tacking on unnecessary attributes.
434 CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName;
435 CurInitSegLoc = PragmaLocation;
436}
437
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000438void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
439 SourceLocation PragmaLoc) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000440
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000441 IdentifierInfo *Name = IdTok.getIdentifierInfo();
442 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
Craig Topperc3ec1492014-05-26 06:22:03 +0000443 LookupParsedName(Lookup, curScope, nullptr, true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000444
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000445 if (Lookup.empty()) {
446 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
447 << Name << SourceRange(IdTok.getLocation());
448 return;
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000449 }
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000450
451 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
Argyrios Kyrtzidisff115a22011-01-27 18:16:48 +0000452 if (!VD) {
453 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000454 << Name << SourceRange(IdTok.getLocation());
455 return;
456 }
457
458 // Warn if this was used before being marked unused.
459 if (VD->isUsed())
460 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
461
Aaron Ballman0bcd6c12016-03-09 16:48:08 +0000462 VD->addAttr(UnusedAttr::CreateImplicit(Context, UnusedAttr::GNU_unused,
463 IdTok.getLocation()));
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000464}
Eli Friedman570024a2010-08-05 06:57:20 +0000465
John McCall32f5fe12011-09-30 05:12:12 +0000466void Sema::AddCFAuditedAttribute(Decl *D) {
467 SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
468 if (!Loc.isValid()) return;
469
470 // Don't add a redundant or conflicting attribute.
471 if (D->hasAttr<CFAuditedTransferAttr>() ||
472 D->hasAttr<CFUnknownTransferAttr>())
473 return;
474
Aaron Ballman36a53502014-01-16 13:03:14 +0000475 D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc));
John McCall32f5fe12011-09-30 05:12:12 +0000476}
477
Dario Domizioli13a0a382014-05-23 12:13:25 +0000478void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) {
479 if(On)
480 OptimizeOffPragmaLocation = SourceLocation();
481 else
482 OptimizeOffPragmaLocation = PragmaLoc;
483}
484
485void Sema::AddRangeBasedOptnone(FunctionDecl *FD) {
486 // In the future, check other pragmas if they're implemented (e.g. pragma
487 // optimize 0 will probably map to this functionality too).
488 if(OptimizeOffPragmaLocation.isValid())
489 AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation);
490}
491
492void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD,
493 SourceLocation Loc) {
494 // Don't add a conflicting attribute. No diagnostic is needed.
495 if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>())
496 return;
497
498 // Add attributes only if required. Optnone requires noinline as well, but if
499 // either is already present then don't bother adding them.
500 if (!FD->hasAttr<OptimizeNoneAttr>())
501 FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc));
502 if (!FD->hasAttr<NoInlineAttr>())
503 FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc));
504}
505
John McCall2faf32c2010-12-10 02:59:44 +0000506typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
Alp Tokerceb95c42014-03-02 03:20:16 +0000507enum : unsigned { NoVisibility = ~0U };
Eli Friedman570024a2010-08-05 06:57:20 +0000508
509void Sema::AddPushedVisibilityAttribute(Decl *D) {
510 if (!VisContext)
511 return;
512
Rafael Espindola54606d52012-12-25 07:31:49 +0000513 NamedDecl *ND = dyn_cast<NamedDecl>(D);
John McCalld041a9b2013-02-20 01:54:26 +0000514 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
Eli Friedman570024a2010-08-05 06:57:20 +0000515 return;
516
517 VisStack *Stack = static_cast<VisStack*>(VisContext);
John McCall2faf32c2010-12-10 02:59:44 +0000518 unsigned rawType = Stack->back().first;
519 if (rawType == NoVisibility) return;
520
521 VisibilityAttr::VisibilityType type
522 = (VisibilityAttr::VisibilityType) rawType;
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000523 SourceLocation loc = Stack->back().second;
Eli Friedman570024a2010-08-05 06:57:20 +0000524
Aaron Ballman36a53502014-01-16 13:03:14 +0000525 D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
Eli Friedman570024a2010-08-05 06:57:20 +0000526}
527
528/// FreeVisContext - Deallocate and null out VisContext.
529void Sema::FreeVisContext() {
530 delete static_cast<VisStack*>(VisContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000531 VisContext = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000532}
533
John McCall2faf32c2010-12-10 02:59:44 +0000534static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
John McCallb1be5232010-08-26 09:15:37 +0000535 // Put visibility on stack.
536 if (!S.VisContext)
537 S.VisContext = new VisStack;
538
539 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
540 Stack->push_back(std::make_pair(type, loc));
541}
542
Rafael Espindolade15baf2012-01-21 05:43:40 +0000543void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
Eli Friedman570024a2010-08-05 06:57:20 +0000544 SourceLocation PragmaLoc) {
Rafael Espindolade15baf2012-01-21 05:43:40 +0000545 if (VisType) {
Eli Friedman570024a2010-08-05 06:57:20 +0000546 // Compute visibility to use.
Aaron Ballman682ee422013-09-11 19:47:58 +0000547 VisibilityAttr::VisibilityType T;
548 if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
549 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
Eli Friedman570024a2010-08-05 06:57:20 +0000550 return;
551 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000552 PushPragmaVisibility(*this, T, PragmaLoc);
Eli Friedman570024a2010-08-05 06:57:20 +0000553 } else {
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000554 PopPragmaVisibility(false, PragmaLoc);
Eli Friedman570024a2010-08-05 06:57:20 +0000555 }
556}
557
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000558void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
559 switch (OOS) {
560 case tok::OOS_ON:
561 FPFeatures.fp_contract = 1;
562 break;
563 case tok::OOS_OFF:
564 FPFeatures.fp_contract = 0;
565 break;
566 case tok::OOS_DEFAULT:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000567 FPFeatures.fp_contract = getLangOpts().DefaultFPContract;
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000568 break;
569 }
570}
571
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000572void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
573 SourceLocation Loc) {
John McCall2faf32c2010-12-10 02:59:44 +0000574 // Visibility calculations will consider the namespace's visibility.
575 // Here we just want to note that we're in a visibility context
576 // which overrides any enclosing #pragma context, but doesn't itself
577 // contribute visibility.
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000578 PushPragmaVisibility(*this, NoVisibility, Loc);
Eli Friedman570024a2010-08-05 06:57:20 +0000579}
580
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000581void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
582 if (!VisContext) {
583 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
584 return;
Eli Friedman570024a2010-08-05 06:57:20 +0000585 }
Rafael Espindola6d65d7b2012-02-01 23:24:59 +0000586
587 // Pop visibility from stack
588 VisStack *Stack = static_cast<VisStack*>(VisContext);
589
590 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
591 bool StartsWithPragma = Back->first != NoVisibility;
592 if (StartsWithPragma && IsNamespaceEnd) {
593 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
594 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
595
596 // For better error recovery, eat all pushes inside the namespace.
597 do {
598 Stack->pop_back();
599 Back = &Stack->back();
600 StartsWithPragma = Back->first != NoVisibility;
601 } while (StartsWithPragma);
602 } else if (!StartsWithPragma && !IsNamespaceEnd) {
603 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
604 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
605 return;
606 }
607
608 Stack->pop_back();
609 // To simplify the implementation, never keep around an empty stack.
610 if (Stack->empty())
611 FreeVisContext();
Eli Friedman570024a2010-08-05 06:57:20 +0000612}