1 //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements semantic analysis for non-trivial attributes and
13 //===----------------------------------------------------------------------===//
15 #include "clang/Sema/SemaInternal.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/Basic/TargetInfo.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Sema/Lookup.h"
21 using namespace clang;
23 //===----------------------------------------------------------------------===//
24 // Pragma 'pack' and 'options align'
25 //===----------------------------------------------------------------------===//
28 struct PackStackEntry {
29 // We just use a sentinel to represent when the stack is set to mac68k
31 static const unsigned kMac68kAlignmentSentinel = ~0U;
37 /// PragmaPackStack - Simple class to wrap the stack used by #pragma
39 class PragmaPackStack {
40 typedef std::vector<PackStackEntry> stack_ty;
42 /// Alignment - The current user specified alignment.
45 /// Stack - Entries in the #pragma pack stack, consisting of saved
46 /// alignments and optional names.
50 PragmaPackStack() : Alignment(0) {}
52 void setAlignment(unsigned A) { Alignment = A; }
53 unsigned getAlignment() { return Alignment; }
55 /// push - Push the current alignment onto the stack, optionally
56 /// using the given \arg Name for the record, if non-zero.
57 void push(IdentifierInfo *Name) {
58 PackStackEntry PSE = { Alignment, Name };
62 /// pop - Pop a record from the stack and restore the current
63 /// alignment to the previous value. If \arg Name is non-zero then
64 /// the first such named record is popped, otherwise the top record
65 /// is popped. Returns true if the pop succeeded.
66 bool pop(IdentifierInfo *Name, bool IsReset);
68 } // end anonymous namespace.
70 bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
71 // If name is empty just pop top.
73 // An empty stack is a special case...
75 // If this isn't a reset, it is always an error.
79 // Otherwise, it is an error only if some alignment has been set.
83 // Otherwise, reset to the default alignment.
86 Alignment = Stack.back().Alignment;
93 // Otherwise, find the named record.
94 for (unsigned i = Stack.size(); i != 0; ) {
96 if (Stack[i].Name == Name) {
97 // Found it, pop up to and including this record.
98 Alignment = Stack[i].Alignment;
99 Stack.erase(Stack.begin() + i, Stack.end());
108 /// FreePackedContext - Deallocate and null out PackContext.
109 void Sema::FreePackedContext() {
110 delete static_cast<PragmaPackStack*>(PackContext);
114 void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
115 // If there is no pack context, we don't need any attributes.
119 PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext);
121 // Otherwise, check to see if we need a max field alignment attribute.
122 if (unsigned Alignment = Stack->getAlignment()) {
123 if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
124 RD->addAttr(::new (Context) AlignMac68kAttr(SourceLocation(), Context));
126 RD->addAttr(::new (Context) MaxFieldAlignmentAttr(SourceLocation(),
132 void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
133 if (!MSStructPragmaOn)
135 RD->addAttr(::new (Context) MsStructAttr(SourceLocation(), Context));
138 void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
139 SourceLocation PragmaLoc) {
140 if (PackContext == 0)
141 PackContext = new PragmaPackStack();
143 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
146 // For all targets we support native and natural are the same.
148 // FIXME: This is not true on Darwin/PPC.
153 Context->setAlignment(0);
156 // Note that '#pragma options align=packed' is not equivalent to attribute
157 // packed, it has a different precedence relative to attribute aligned.
160 Context->setAlignment(1);
164 // Check if the target supports this.
165 if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
166 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
170 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
174 // Reset just pops the top of the stack, or resets the current alignment to
176 if (!Context->pop(0, /*IsReset=*/true)) {
177 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
184 void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
185 Expr *alignment, SourceLocation PragmaLoc,
186 SourceLocation LParenLoc, SourceLocation RParenLoc) {
187 Expr *Alignment = static_cast<Expr *>(alignment);
189 // If specified then alignment must be a "small" power of two.
190 unsigned AlignmentVal = 0;
194 // pack(0) is like pack(), which just works out since that is what
195 // we use 0 for in PackAttr.
196 if (Alignment->isTypeDependent() ||
197 Alignment->isValueDependent() ||
198 !Alignment->isIntegerConstantExpr(Val, Context) ||
199 !(Val == 0 || Val.isPowerOf2()) ||
200 Val.getZExtValue() > 16) {
201 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
205 AlignmentVal = (unsigned) Val.getZExtValue();
208 if (PackContext == 0)
209 PackContext = new PragmaPackStack();
211 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
214 case Sema::PPK_Default: // pack([n])
215 Context->setAlignment(AlignmentVal);
218 case Sema::PPK_Show: // pack(show)
219 // Show the current alignment, making sure to show the right value
221 AlignmentVal = Context->getAlignment();
222 // FIXME: This should come from the target.
223 if (AlignmentVal == 0)
225 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
226 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
228 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
231 case Sema::PPK_Push: // pack(push [, id] [, [n])
233 // Set the new alignment if specified.
235 Context->setAlignment(AlignmentVal);
238 case Sema::PPK_Pop: // pack(pop [, id] [, n])
239 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
240 // "#pragma pack(pop, identifier, n) is undefined"
241 if (Alignment && Name)
242 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
245 if (!Context->pop(Name, /*IsReset=*/false)) {
246 // If a name was specified then failure indicates the name
247 // wasn't found. Otherwise failure indicates the stack was
249 Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
250 << (Name ? "no record matching name" : "stack empty");
252 // FIXME: Warn about popping named records as MSVC does.
254 // Pop succeeded, set the new alignment if specified.
256 Context->setAlignment(AlignmentVal);
262 void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
263 MSStructPragmaOn = (Kind == PMSST_ON);
266 void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
267 SourceLocation PragmaLoc) {
269 IdentifierInfo *Name = IdTok.getIdentifierInfo();
270 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
271 LookupParsedName(Lookup, curScope, NULL, true);
273 if (Lookup.empty()) {
274 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
275 << Name << SourceRange(IdTok.getLocation());
279 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
281 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
282 << Name << SourceRange(IdTok.getLocation());
286 // Warn if this was used before being marked unused.
288 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
290 VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context));
293 void Sema::AddCFAuditedAttribute(Decl *D) {
294 SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
295 if (!Loc.isValid()) return;
297 // Don't add a redundant or conflicting attribute.
298 if (D->hasAttr<CFAuditedTransferAttr>() ||
299 D->hasAttr<CFUnknownTransferAttr>())
302 D->addAttr(::new (Context) CFAuditedTransferAttr(Loc, Context));
305 typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
306 enum { NoVisibility = (unsigned) -1 };
308 void Sema::AddPushedVisibilityAttribute(Decl *D) {
312 NamedDecl *ND = dyn_cast<NamedDecl>(D);
313 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
316 VisStack *Stack = static_cast<VisStack*>(VisContext);
317 unsigned rawType = Stack->back().first;
318 if (rawType == NoVisibility) return;
320 VisibilityAttr::VisibilityType type
321 = (VisibilityAttr::VisibilityType) rawType;
322 SourceLocation loc = Stack->back().second;
324 D->addAttr(::new (Context) VisibilityAttr(loc, Context, type));
327 /// FreeVisContext - Deallocate and null out VisContext.
328 void Sema::FreeVisContext() {
329 delete static_cast<VisStack*>(VisContext);
333 static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
334 // Put visibility on stack.
336 S.VisContext = new VisStack;
338 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
339 Stack->push_back(std::make_pair(type, loc));
342 void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
343 SourceLocation PragmaLoc) {
345 // Compute visibility to use.
346 VisibilityAttr::VisibilityType type;
347 if (VisType->isStr("default"))
348 type = VisibilityAttr::Default;
349 else if (VisType->isStr("hidden"))
350 type = VisibilityAttr::Hidden;
351 else if (VisType->isStr("internal"))
352 type = VisibilityAttr::Hidden; // FIXME
353 else if (VisType->isStr("protected"))
354 type = VisibilityAttr::Protected;
356 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) <<
360 PushPragmaVisibility(*this, type, PragmaLoc);
362 PopPragmaVisibility(false, PragmaLoc);
366 void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
369 FPFeatures.fp_contract = 1;
372 FPFeatures.fp_contract = 0;
374 case tok::OOS_DEFAULT:
375 FPFeatures.fp_contract = getLangOpts().DefaultFPContract;
380 void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
381 SourceLocation Loc) {
382 // Visibility calculations will consider the namespace's visibility.
383 // Here we just want to note that we're in a visibility context
384 // which overrides any enclosing #pragma context, but doesn't itself
385 // contribute visibility.
386 PushPragmaVisibility(*this, NoVisibility, Loc);
389 void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
391 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
395 // Pop visibility from stack
396 VisStack *Stack = static_cast<VisStack*>(VisContext);
398 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
399 bool StartsWithPragma = Back->first != NoVisibility;
400 if (StartsWithPragma && IsNamespaceEnd) {
401 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
402 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
404 // For better error recovery, eat all pushes inside the namespace.
407 Back = &Stack->back();
408 StartsWithPragma = Back->first != NoVisibility;
409 } while (StartsWithPragma);
410 } else if (!StartsWithPragma && !IsNamespaceEnd) {
411 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
412 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
417 // To simplify the implementation, never keep around an empty stack.