blob: 82978c9560535428f77410dd600ab64c6651446d [file] [log] [blame]
Chris Lattner5a0c3512009-02-17 00:57:29 +00001//===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
2//
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 Lattner574aa402009-02-17 01:09:29 +000010// This file implements semantic analysis for non-trivial attributes and
11// pragmas.
Chris Lattner5a0c3512009-02-17 00:57:29 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "Sema.h"
John McCall7d384dd2009-11-18 07:57:50 +000016#include "Lookup.h"
Chris Lattner5a0c3512009-02-17 00:57:29 +000017#include "clang/AST/Expr.h"
Daniel Dunbar613fd672010-05-27 00:35:16 +000018#include "clang/Basic/TargetInfo.h"
19#include "clang/Lex/Preprocessor.h"
Chris Lattner5a0c3512009-02-17 00:57:29 +000020using namespace clang;
21
Chris Lattner574aa402009-02-17 01:09:29 +000022//===----------------------------------------------------------------------===//
Daniel Dunbarea75a822010-05-27 00:04:40 +000023// Pragma 'pack' and 'options align'
Chris Lattner574aa402009-02-17 01:09:29 +000024//===----------------------------------------------------------------------===//
25
26namespace {
Daniel Dunbarae2232b2010-05-27 02:25:27 +000027 struct PackStackEntry {
Daniel Dunbarc6082fe2010-05-27 05:45:51 +000028 // We just use a sentinel to represent when the stack is set to mac68k
29 // alignment.
30 static const unsigned kMac68kAlignmentSentinel = ~0U;
31
Daniel Dunbarae2232b2010-05-27 02:25:27 +000032 unsigned Alignment;
33 IdentifierInfo *Name;
34 };
35
Chris Lattner574aa402009-02-17 01:09:29 +000036 /// PragmaPackStack - Simple class to wrap the stack used by #pragma
37 /// pack.
38 class PragmaPackStack {
Daniel Dunbarae2232b2010-05-27 02:25:27 +000039 typedef std::vector<PackStackEntry> stack_ty;
Chris Lattner574aa402009-02-17 01:09:29 +000040
41 /// Alignment - The current user specified alignment.
42 unsigned Alignment;
43
44 /// Stack - Entries in the #pragma pack stack, consisting of saved
45 /// alignments and optional names.
46 stack_ty Stack;
Mike Stump1eb44332009-09-09 15:08:12 +000047
48 public:
Chris Lattner574aa402009-02-17 01:09:29 +000049 PragmaPackStack() : Alignment(0) {}
50
51 void setAlignment(unsigned A) { Alignment = A; }
52 unsigned getAlignment() { return Alignment; }
53
54 /// push - Push the current alignment onto the stack, optionally
55 /// using the given \arg Name for the record, if non-zero.
56 void push(IdentifierInfo *Name) {
Daniel Dunbarae2232b2010-05-27 02:25:27 +000057 PackStackEntry PSE = { Alignment, Name };
58 Stack.push_back(PSE);
Chris Lattner574aa402009-02-17 01:09:29 +000059 }
60
61 /// pop - Pop a record from the stack and restore the current
62 /// alignment to the previous value. If \arg Name is non-zero then
63 /// the first such named record is popped, otherwise the top record
64 /// is popped. Returns true if the pop succeeded.
65 bool pop(IdentifierInfo *Name);
66 };
67} // end anonymous namespace.
68
69bool PragmaPackStack::pop(IdentifierInfo *Name) {
70 if (Stack.empty())
71 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000072
Chris Lattner574aa402009-02-17 01:09:29 +000073 // If name is empty just pop top.
74 if (!Name) {
Daniel Dunbarae2232b2010-05-27 02:25:27 +000075 Alignment = Stack.back().Alignment;
Chris Lattner574aa402009-02-17 01:09:29 +000076 Stack.pop_back();
77 return true;
Mike Stump1eb44332009-09-09 15:08:12 +000078 }
79
Chris Lattner574aa402009-02-17 01:09:29 +000080 // Otherwise, find the named record.
81 for (unsigned i = Stack.size(); i != 0; ) {
82 --i;
Daniel Dunbarae2232b2010-05-27 02:25:27 +000083 if (Stack[i].Name == Name) {
Chris Lattner574aa402009-02-17 01:09:29 +000084 // Found it, pop up to and including this record.
Daniel Dunbarae2232b2010-05-27 02:25:27 +000085 Alignment = Stack[i].Alignment;
Chris Lattner574aa402009-02-17 01:09:29 +000086 Stack.erase(Stack.begin() + i, Stack.end());
87 return true;
88 }
89 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattner574aa402009-02-17 01:09:29 +000091 return false;
92}
93
94
95/// FreePackedContext - Deallocate and null out PackContext.
96void Sema::FreePackedContext() {
97 delete static_cast<PragmaPackStack*>(PackContext);
98 PackContext = 0;
99}
100
Daniel Dunbar9f21f892010-05-27 01:53:40 +0000101void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
102 // If there is no pack context, we don't need any attributes.
103 if (!PackContext)
104 return;
105
106 PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext);
107
108 // Otherwise, check to see if we need a max field alignment attribute.
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000109 if (unsigned Alignment = Stack->getAlignment()) {
110 if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
111 RD->addAttr(::new (Context) AlignMac68kAttr());
112 else
113 RD->addAttr(::new (Context) MaxFieldAlignmentAttr(Alignment * 8));
114 }
Chris Lattner574aa402009-02-17 01:09:29 +0000115}
116
Daniel Dunbarea75a822010-05-27 00:04:40 +0000117void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
118 SourceLocation PragmaLoc,
119 SourceLocation KindLoc) {
120 if (PackContext == 0)
121 PackContext = new PragmaPackStack();
122
123 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
124
125 // Reset just pops the top of the stack.
126 if (Kind == Action::POAK_Reset) {
127 // Do the pop.
128 if (!Context->pop(0)) {
129 // If a name was specified then failure indicates the name
130 // wasn't found. Otherwise failure indicates the stack was
131 // empty.
132 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
133 << "stack empty";
134 }
135 return;
136 }
137
138 // We don't support #pragma options align=power.
139 switch (Kind) {
140 case POAK_Natural:
141 Context->push(0);
142 Context->setAlignment(0);
143 break;
144
Daniel Dunbar613fd672010-05-27 00:35:16 +0000145 case POAK_Mac68k:
146 // Check if the target supports this.
147 if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
148 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
149 return;
Daniel Dunbar613fd672010-05-27 00:35:16 +0000150 }
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000151 Context->push(0);
152 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
Daniel Dunbar613fd672010-05-27 00:35:16 +0000153 break;
154
Daniel Dunbarea75a822010-05-27 00:04:40 +0000155 default:
156 Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option)
157 << KindLoc;
158 break;
159 }
160}
161
Mike Stump1eb44332009-09-09 15:08:12 +0000162void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
163 ExprTy *alignment, SourceLocation PragmaLoc,
Chris Lattner5a0c3512009-02-17 00:57:29 +0000164 SourceLocation LParenLoc, SourceLocation RParenLoc) {
165 Expr *Alignment = static_cast<Expr *>(alignment);
166
167 // If specified then alignment must be a "small" power of two.
168 unsigned AlignmentVal = 0;
169 if (Alignment) {
170 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000172 // pack(0) is like pack(), which just works out since that is what
173 // we use 0 for in PackAttr.
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000174 if (Alignment->isTypeDependent() ||
175 Alignment->isValueDependent() ||
176 !Alignment->isIntegerConstantExpr(Val, Context) ||
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000177 !(Val == 0 || Val.isPowerOf2()) ||
Chris Lattner5a0c3512009-02-17 00:57:29 +0000178 Val.getZExtValue() > 16) {
179 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
180 Alignment->Destroy(Context);
181 return; // Ignore
182 }
183
184 AlignmentVal = (unsigned) Val.getZExtValue();
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattner574aa402009-02-17 01:09:29 +0000187 if (PackContext == 0)
188 PackContext = new PragmaPackStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Chris Lattner574aa402009-02-17 01:09:29 +0000190 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Chris Lattner5a0c3512009-02-17 00:57:29 +0000192 switch (Kind) {
193 case Action::PPK_Default: // pack([n])
Chris Lattner574aa402009-02-17 01:09:29 +0000194 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000195 break;
196
197 case Action::PPK_Show: // pack(show)
198 // Show the current alignment, making sure to show the right value
199 // for the default.
Chris Lattner574aa402009-02-17 01:09:29 +0000200 AlignmentVal = Context->getAlignment();
Chris Lattner5a0c3512009-02-17 00:57:29 +0000201 // FIXME: This should come from the target.
202 if (AlignmentVal == 0)
203 AlignmentVal = 8;
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000204 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
205 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
206 else
207 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
Chris Lattner5a0c3512009-02-17 00:57:29 +0000208 break;
209
210 case Action::PPK_Push: // pack(push [, id] [, [n])
Chris Lattner574aa402009-02-17 01:09:29 +0000211 Context->push(Name);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000212 // Set the new alignment if specified.
213 if (Alignment)
Mike Stump1eb44332009-09-09 15:08:12 +0000214 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000215 break;
216
217 case Action::PPK_Pop: // pack(pop [, id] [, n])
218 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
219 // "#pragma pack(pop, identifier, n) is undefined"
220 if (Alignment && Name)
Mike Stump1eb44332009-09-09 15:08:12 +0000221 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
222
Chris Lattner5a0c3512009-02-17 00:57:29 +0000223 // Do the pop.
Chris Lattner574aa402009-02-17 01:09:29 +0000224 if (!Context->pop(Name)) {
Chris Lattner5a0c3512009-02-17 00:57:29 +0000225 // If a name was specified then failure indicates the name
226 // wasn't found. Otherwise failure indicates the stack was
227 // empty.
228 Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
229 << (Name ? "no record matching name" : "stack empty");
230
231 // FIXME: Warn about popping named records as MSVC does.
232 } else {
233 // Pop succeeded, set the new alignment if specified.
234 if (Alignment)
Chris Lattner574aa402009-02-17 01:09:29 +0000235 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000236 }
237 break;
238
239 default:
240 assert(0 && "Invalid #pragma pack kind.");
241 }
242}
243
Ted Kremenek7a02a372009-08-03 23:24:57 +0000244void Sema::ActOnPragmaUnused(const Token *Identifiers, unsigned NumIdentifiers,
245 Scope *curScope,
Ted Kremenek4726d032009-03-23 22:28:25 +0000246 SourceLocation PragmaLoc,
247 SourceLocation LParenLoc,
248 SourceLocation RParenLoc) {
Ted Kremenek4726d032009-03-23 22:28:25 +0000249
Ted Kremenek7a02a372009-08-03 23:24:57 +0000250 for (unsigned i = 0; i < NumIdentifiers; ++i) {
251 const Token &Tok = Identifiers[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000252 IdentifierInfo *Name = Tok.getIdentifierInfo();
John McCalla24dc2e2009-11-17 02:14:36 +0000253 LookupResult Lookup(*this, Name, Tok.getLocation(), LookupOrdinaryName);
254 LookupParsedName(Lookup, curScope, NULL, true);
Ted Kremenek4726d032009-03-23 22:28:25 +0000255
John McCall1bcee0a2009-12-02 08:25:40 +0000256 if (Lookup.empty()) {
Ted Kremenek7a02a372009-08-03 23:24:57 +0000257 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
258 << Name << SourceRange(Tok.getLocation());
259 continue;
Ted Kremenek4726d032009-03-23 22:28:25 +0000260 }
Mike Stump1eb44332009-09-09 15:08:12 +0000261
John McCall1bcee0a2009-12-02 08:25:40 +0000262 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
263 if (!VD || !VD->hasLocalStorage()) {
Ted Kremenek7a02a372009-08-03 23:24:57 +0000264 Diag(PragmaLoc, diag::warn_pragma_unused_expected_localvar)
265 << Name << SourceRange(Tok.getLocation());
266 continue;
267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
John McCall1bcee0a2009-12-02 08:25:40 +0000269 VD->addAttr(::new (Context) UnusedAttr());
Ted Kremenek4726d032009-03-23 22:28:25 +0000270 }
271}