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/ASTConsumer.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/Basic/TargetInfo.h"
20 #include "clang/Lex/Preprocessor.h"
21 #include "clang/Sema/Lookup.h"
22 using namespace clang;
24 //===----------------------------------------------------------------------===//
25 // Pragma 'pack' and 'options align'
26 //===----------------------------------------------------------------------===//
29 struct PackStackEntry {
30 // We just use a sentinel to represent when the stack is set to mac68k
32 static const unsigned kMac68kAlignmentSentinel = ~0U;
38 /// PragmaPackStack - Simple class to wrap the stack used by #pragma
40 class PragmaPackStack {
41 typedef std::vector<PackStackEntry> stack_ty;
43 /// Alignment - The current user specified alignment.
46 /// Stack - Entries in the #pragma pack stack, consisting of saved
47 /// alignments and optional names.
51 PragmaPackStack() : Alignment(0) {}
53 void setAlignment(unsigned A) { Alignment = A; }
54 unsigned getAlignment() { return Alignment; }
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) {
59 PackStackEntry PSE = { Alignment, Name };
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.
67 bool pop(IdentifierInfo *Name, bool IsReset);
69 } // end anonymous namespace.
71 bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
72 // If name is empty just pop top.
74 // An empty stack is a special case...
76 // If this isn't a reset, it is always an error.
80 // Otherwise, it is an error only if some alignment has been set.
84 // Otherwise, reset to the default alignment.
87 Alignment = Stack.back().Alignment;
94 // Otherwise, find the named record.
95 for (unsigned i = Stack.size(); i != 0; ) {
97 if (Stack[i].Name == Name) {
98 // Found it, pop up to and including this record.
99 Alignment = Stack[i].Alignment;
100 Stack.erase(Stack.begin() + i, Stack.end());
109 /// FreePackedContext - Deallocate and null out PackContext.
110 void Sema::FreePackedContext() {
111 delete static_cast<PragmaPackStack*>(PackContext);
115 void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
116 // If there is no pack context, we don't need any attributes.
120 PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext);
122 // Otherwise, check to see if we need a max field alignment attribute.
123 if (unsigned Alignment = Stack->getAlignment()) {
124 if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
125 RD->addAttr(::new (Context) AlignMac68kAttr(SourceLocation(), Context));
127 RD->addAttr(::new (Context) MaxFieldAlignmentAttr(SourceLocation(),
133 void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
134 if (!MSStructPragmaOn)
136 RD->addAttr(::new (Context) MsStructAttr(SourceLocation(), Context));
139 void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
140 SourceLocation PragmaLoc) {
141 if (PackContext == 0)
142 PackContext = new PragmaPackStack();
144 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
147 // For all targets we support native and natural are the same.
149 // FIXME: This is not true on Darwin/PPC.
154 Context->setAlignment(0);
157 // Note that '#pragma options align=packed' is not equivalent to attribute
158 // packed, it has a different precedence relative to attribute aligned.
161 Context->setAlignment(1);
165 // Check if the target supports this.
166 if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
167 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
171 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
175 // Reset just pops the top of the stack, or resets the current alignment to
177 if (!Context->pop(0, /*IsReset=*/true)) {
178 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
185 void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
186 Expr *alignment, SourceLocation PragmaLoc,
187 SourceLocation LParenLoc, SourceLocation RParenLoc) {
188 Expr *Alignment = static_cast<Expr *>(alignment);
190 // If specified then alignment must be a "small" power of two.
191 unsigned AlignmentVal = 0;
195 // pack(0) is like pack(), which just works out since that is what
196 // we use 0 for in PackAttr.
197 if (Alignment->isTypeDependent() ||
198 Alignment->isValueDependent() ||
199 !Alignment->isIntegerConstantExpr(Val, Context) ||
200 !(Val == 0 || Val.isPowerOf2()) ||
201 Val.getZExtValue() > 16) {
202 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
206 AlignmentVal = (unsigned) Val.getZExtValue();
209 if (PackContext == 0)
210 PackContext = new PragmaPackStack();
212 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
215 case Sema::PPK_Default: // pack([n])
216 Context->setAlignment(AlignmentVal);
219 case Sema::PPK_Show: // pack(show)
220 // Show the current alignment, making sure to show the right value
222 AlignmentVal = Context->getAlignment();
223 // FIXME: This should come from the target.
224 if (AlignmentVal == 0)
226 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
227 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
229 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
232 case Sema::PPK_Push: // pack(push [, id] [, [n])
234 // Set the new alignment if specified.
236 Context->setAlignment(AlignmentVal);
239 case Sema::PPK_Pop: // pack(pop [, id] [, n])
240 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
241 // "#pragma pack(pop, identifier, n) is undefined"
242 if (Alignment && Name)
243 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
246 if (!Context->pop(Name, /*IsReset=*/false)) {
247 // If a name was specified then failure indicates the name
248 // wasn't found. Otherwise failure indicates the stack was
250 Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
251 << (Name ? "no record matching name" : "stack empty");
253 // FIXME: Warn about popping named records as MSVC does.
255 // Pop succeeded, set the new alignment if specified.
257 Context->setAlignment(AlignmentVal);
263 void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
264 MSStructPragmaOn = (Kind == PMSST_ON);
267 void Sema::ActOnPragmaMSComment(PragmaMSCommentKind Kind, StringRef Arg) {
268 // FIXME: Serialize this.
271 llvm_unreachable("unexpected pragma comment kind");
273 Consumer.HandleLinkerOptionPragma(Arg);
276 Consumer.HandleDependentLibrary(Arg);
281 return; // We ignore all of these.
283 llvm_unreachable("invalid pragma comment kind");
286 void Sema::ActOnPragmaDetectMismatch(StringRef Name, StringRef Value) {
287 // FIXME: Serialize this.
288 Consumer.HandleDetectMismatch(Name, Value);
291 void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
292 SourceLocation PragmaLoc) {
294 IdentifierInfo *Name = IdTok.getIdentifierInfo();
295 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
296 LookupParsedName(Lookup, curScope, NULL, true);
298 if (Lookup.empty()) {
299 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
300 << Name << SourceRange(IdTok.getLocation());
304 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
306 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
307 << Name << SourceRange(IdTok.getLocation());
311 // Warn if this was used before being marked unused.
313 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
315 VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context));
318 void Sema::AddCFAuditedAttribute(Decl *D) {
319 SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
320 if (!Loc.isValid()) return;
322 // Don't add a redundant or conflicting attribute.
323 if (D->hasAttr<CFAuditedTransferAttr>() ||
324 D->hasAttr<CFUnknownTransferAttr>())
327 D->addAttr(::new (Context) CFAuditedTransferAttr(Loc, Context));
330 typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
331 enum { NoVisibility = (unsigned) -1 };
333 void Sema::AddPushedVisibilityAttribute(Decl *D) {
337 NamedDecl *ND = dyn_cast<NamedDecl>(D);
338 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
341 VisStack *Stack = static_cast<VisStack*>(VisContext);
342 unsigned rawType = Stack->back().first;
343 if (rawType == NoVisibility) return;
345 VisibilityAttr::VisibilityType type
346 = (VisibilityAttr::VisibilityType) rawType;
347 SourceLocation loc = Stack->back().second;
349 D->addAttr(::new (Context) VisibilityAttr(loc, Context, type));
352 /// FreeVisContext - Deallocate and null out VisContext.
353 void Sema::FreeVisContext() {
354 delete static_cast<VisStack*>(VisContext);
358 static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
359 // Put visibility on stack.
361 S.VisContext = new VisStack;
363 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
364 Stack->push_back(std::make_pair(type, loc));
367 void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
368 SourceLocation PragmaLoc) {
370 // Compute visibility to use.
371 VisibilityAttr::VisibilityType T;
372 if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
373 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
376 PushPragmaVisibility(*this, T, PragmaLoc);
378 PopPragmaVisibility(false, PragmaLoc);
382 void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
385 FPFeatures.fp_contract = 1;
388 FPFeatures.fp_contract = 0;
390 case tok::OOS_DEFAULT:
391 FPFeatures.fp_contract = getLangOpts().DefaultFPContract;
396 void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
397 SourceLocation Loc) {
398 // Visibility calculations will consider the namespace's visibility.
399 // Here we just want to note that we're in a visibility context
400 // which overrides any enclosing #pragma context, but doesn't itself
401 // contribute visibility.
402 PushPragmaVisibility(*this, NoVisibility, Loc);
405 void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
407 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
411 // Pop visibility from stack
412 VisStack *Stack = static_cast<VisStack*>(VisContext);
414 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
415 bool StartsWithPragma = Back->first != NoVisibility;
416 if (StartsWithPragma && IsNamespaceEnd) {
417 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
418 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
420 // For better error recovery, eat all pushes inside the namespace.
423 Back = &Stack->back();
424 StartsWithPragma = Back->first != NoVisibility;
425 } while (StartsWithPragma);
426 } else if (!StartsWithPragma && !IsNamespaceEnd) {
427 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
428 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
433 // To simplify the implementation, never keep around an empty stack.