blob: 540ee7a2af123cdf45ff7e2dc506284c85169aeb [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.
Daniel Dunbarddc6ff62010-07-16 04:54:16 +000065 bool pop(IdentifierInfo *Name, bool IsReset);
Chris Lattner574aa402009-02-17 01:09:29 +000066 };
67} // end anonymous namespace.
68
Daniel Dunbarddc6ff62010-07-16 04:54:16 +000069bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
Chris Lattner574aa402009-02-17 01:09:29 +000070 // If name is empty just pop top.
71 if (!Name) {
Daniel Dunbarddc6ff62010-07-16 04:54:16 +000072 // An empty stack is a special case...
73 if (Stack.empty()) {
74 // If this isn't a reset, it is always an error.
75 if (!IsReset)
76 return false;
77
78 // Otherwise, it is an error only if some alignment has been set.
79 if (!Alignment)
80 return false;
81
82 // Otherwise, reset to the default alignment.
83 Alignment = 0;
84 } else {
85 Alignment = Stack.back().Alignment;
86 Stack.pop_back();
87 }
88
Chris Lattner574aa402009-02-17 01:09:29 +000089 return true;
Mike Stump1eb44332009-09-09 15:08:12 +000090 }
91
Chris Lattner574aa402009-02-17 01:09:29 +000092 // Otherwise, find the named record.
93 for (unsigned i = Stack.size(); i != 0; ) {
94 --i;
Daniel Dunbarae2232b2010-05-27 02:25:27 +000095 if (Stack[i].Name == Name) {
Chris Lattner574aa402009-02-17 01:09:29 +000096 // Found it, pop up to and including this record.
Daniel Dunbarae2232b2010-05-27 02:25:27 +000097 Alignment = Stack[i].Alignment;
Chris Lattner574aa402009-02-17 01:09:29 +000098 Stack.erase(Stack.begin() + i, Stack.end());
99 return true;
100 }
101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner574aa402009-02-17 01:09:29 +0000103 return false;
104}
105
106
107/// FreePackedContext - Deallocate and null out PackContext.
108void Sema::FreePackedContext() {
109 delete static_cast<PragmaPackStack*>(PackContext);
110 PackContext = 0;
111}
112
Daniel Dunbar9f21f892010-05-27 01:53:40 +0000113void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
114 // If there is no pack context, we don't need any attributes.
115 if (!PackContext)
116 return;
117
118 PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext);
119
120 // Otherwise, check to see if we need a max field alignment attribute.
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000121 if (unsigned Alignment = Stack->getAlignment()) {
122 if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
123 RD->addAttr(::new (Context) AlignMac68kAttr());
124 else
125 RD->addAttr(::new (Context) MaxFieldAlignmentAttr(Alignment * 8));
126 }
Chris Lattner574aa402009-02-17 01:09:29 +0000127}
128
Daniel Dunbarea75a822010-05-27 00:04:40 +0000129void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
130 SourceLocation PragmaLoc,
131 SourceLocation KindLoc) {
132 if (PackContext == 0)
133 PackContext = new PragmaPackStack();
134
135 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
136
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000137 // Reset just pops the top of the stack, or resets the current alignment to
138 // default.
Daniel Dunbarea75a822010-05-27 00:04:40 +0000139 if (Kind == Action::POAK_Reset) {
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000140 if (!Context->pop(0, /*IsReset=*/true)) {
Daniel Dunbarea75a822010-05-27 00:04:40 +0000141 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
142 << "stack empty";
143 }
144 return;
145 }
146
Daniel Dunbarea75a822010-05-27 00:04:40 +0000147 switch (Kind) {
Daniel Dunbar638e7cf2010-05-27 18:42:09 +0000148 // For all targets we support native and natural are the same.
149 //
150 // FIXME: This is not true on Darwin/PPC.
151 case POAK_Native:
Daniel Dunbar450f7932010-05-28 19:43:33 +0000152 case POAK_Power:
Daniel Dunbard6b305d2010-05-28 20:08:00 +0000153 case POAK_Natural:
Daniel Dunbar450f7932010-05-28 19:43:33 +0000154 Context->push(0);
155 Context->setAlignment(0);
156 break;
157
Daniel Dunbar6f739142010-05-27 18:42:17 +0000158 // Note that '#pragma options align=packed' is not equivalent to attribute
159 // packed, it has a different precedence relative to attribute aligned.
160 case POAK_Packed:
161 Context->push(0);
162 Context->setAlignment(1);
163 break;
164
Daniel Dunbar613fd672010-05-27 00:35:16 +0000165 case POAK_Mac68k:
166 // Check if the target supports this.
167 if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
168 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
169 return;
Daniel Dunbar613fd672010-05-27 00:35:16 +0000170 }
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000171 Context->push(0);
172 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
Daniel Dunbar613fd672010-05-27 00:35:16 +0000173 break;
174
Daniel Dunbarea75a822010-05-27 00:04:40 +0000175 default:
176 Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option)
177 << KindLoc;
178 break;
179 }
180}
181
Mike Stump1eb44332009-09-09 15:08:12 +0000182void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
183 ExprTy *alignment, SourceLocation PragmaLoc,
Chris Lattner5a0c3512009-02-17 00:57:29 +0000184 SourceLocation LParenLoc, SourceLocation RParenLoc) {
185 Expr *Alignment = static_cast<Expr *>(alignment);
186
187 // If specified then alignment must be a "small" power of two.
188 unsigned AlignmentVal = 0;
189 if (Alignment) {
190 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000192 // pack(0) is like pack(), which just works out since that is what
193 // we use 0 for in PackAttr.
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000194 if (Alignment->isTypeDependent() ||
195 Alignment->isValueDependent() ||
196 !Alignment->isIntegerConstantExpr(Val, Context) ||
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000197 !(Val == 0 || Val.isPowerOf2()) ||
Chris Lattner5a0c3512009-02-17 00:57:29 +0000198 Val.getZExtValue() > 16) {
199 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
200 Alignment->Destroy(Context);
201 return; // Ignore
202 }
203
204 AlignmentVal = (unsigned) Val.getZExtValue();
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Chris Lattner574aa402009-02-17 01:09:29 +0000207 if (PackContext == 0)
208 PackContext = new PragmaPackStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Chris Lattner574aa402009-02-17 01:09:29 +0000210 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattner5a0c3512009-02-17 00:57:29 +0000212 switch (Kind) {
213 case Action::PPK_Default: // pack([n])
Chris Lattner574aa402009-02-17 01:09:29 +0000214 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000215 break;
216
217 case Action::PPK_Show: // pack(show)
218 // Show the current alignment, making sure to show the right value
219 // for the default.
Chris Lattner574aa402009-02-17 01:09:29 +0000220 AlignmentVal = Context->getAlignment();
Chris Lattner5a0c3512009-02-17 00:57:29 +0000221 // FIXME: This should come from the target.
222 if (AlignmentVal == 0)
223 AlignmentVal = 8;
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000224 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
225 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
226 else
227 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
Chris Lattner5a0c3512009-02-17 00:57:29 +0000228 break;
229
230 case Action::PPK_Push: // pack(push [, id] [, [n])
Chris Lattner574aa402009-02-17 01:09:29 +0000231 Context->push(Name);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000232 // Set the new alignment if specified.
233 if (Alignment)
Mike Stump1eb44332009-09-09 15:08:12 +0000234 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000235 break;
236
237 case Action::PPK_Pop: // pack(pop [, id] [, n])
238 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
239 // "#pragma pack(pop, identifier, n) is undefined"
240 if (Alignment && Name)
Mike Stump1eb44332009-09-09 15:08:12 +0000241 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
242
Chris Lattner5a0c3512009-02-17 00:57:29 +0000243 // Do the pop.
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000244 if (!Context->pop(Name, /*IsReset=*/false)) {
Chris Lattner5a0c3512009-02-17 00:57:29 +0000245 // If a name was specified then failure indicates the name
246 // wasn't found. Otherwise failure indicates the stack was
247 // empty.
248 Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
249 << (Name ? "no record matching name" : "stack empty");
250
251 // FIXME: Warn about popping named records as MSVC does.
252 } else {
253 // Pop succeeded, set the new alignment if specified.
254 if (Alignment)
Chris Lattner574aa402009-02-17 01:09:29 +0000255 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000256 }
257 break;
258
259 default:
260 assert(0 && "Invalid #pragma pack kind.");
261 }
262}
263
Ted Kremenek7a02a372009-08-03 23:24:57 +0000264void Sema::ActOnPragmaUnused(const Token *Identifiers, unsigned NumIdentifiers,
265 Scope *curScope,
Ted Kremenek4726d032009-03-23 22:28:25 +0000266 SourceLocation PragmaLoc,
267 SourceLocation LParenLoc,
268 SourceLocation RParenLoc) {
Ted Kremenek4726d032009-03-23 22:28:25 +0000269
Ted Kremenek7a02a372009-08-03 23:24:57 +0000270 for (unsigned i = 0; i < NumIdentifiers; ++i) {
271 const Token &Tok = Identifiers[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000272 IdentifierInfo *Name = Tok.getIdentifierInfo();
John McCalla24dc2e2009-11-17 02:14:36 +0000273 LookupResult Lookup(*this, Name, Tok.getLocation(), LookupOrdinaryName);
274 LookupParsedName(Lookup, curScope, NULL, true);
Ted Kremenek4726d032009-03-23 22:28:25 +0000275
John McCall1bcee0a2009-12-02 08:25:40 +0000276 if (Lookup.empty()) {
Ted Kremenek7a02a372009-08-03 23:24:57 +0000277 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
278 << Name << SourceRange(Tok.getLocation());
279 continue;
Ted Kremenek4726d032009-03-23 22:28:25 +0000280 }
Mike Stump1eb44332009-09-09 15:08:12 +0000281
John McCall1bcee0a2009-12-02 08:25:40 +0000282 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
283 if (!VD || !VD->hasLocalStorage()) {
Ted Kremenek7a02a372009-08-03 23:24:57 +0000284 Diag(PragmaLoc, diag::warn_pragma_unused_expected_localvar)
285 << Name << SourceRange(Tok.getLocation());
286 continue;
287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
John McCall1bcee0a2009-12-02 08:25:40 +0000289 VD->addAttr(::new (Context) UnusedAttr());
Ted Kremenek4726d032009-03-23 22:28:25 +0000290 }
291}