1 //===--- 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 //
10 // This file implements semantic analysis for non-trivial attributes and
11 // pragmas.
12 //
13 //===----------------------------------------------------------------------===//
14 
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;
23 
24 //===----------------------------------------------------------------------===//
25 // Pragma 'pack' and 'options align'
26 //===----------------------------------------------------------------------===//
27 
28 namespace {
29   struct PackStackEntry {
30     // We just use a sentinel to represent when the stack is set to mac68k
31     // alignment.
32     static const unsigned kMac68kAlignmentSentinel = ~0U;
33 
34     unsigned Alignment;
35     IdentifierInfo *Name;
36   };
37 
38   /// PragmaPackStack - Simple class to wrap the stack used by #pragma
39   /// pack.
40   class PragmaPackStack {
41     typedef std::vector<PackStackEntry> stack_ty;
42 
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;
49 
50   public:
51     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) {
59       PackStackEntry PSE = { Alignment, Name };
60       Stack.push_back(PSE);
61     }
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.
67     bool pop(IdentifierInfo *Name, bool IsReset);
68   };
69 }  // end anonymous namespace.
70 
71 bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
72   // If name is empty just pop top.
73   if (!Name) {
74     // 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 
91     return true;
92   }
93 
94   // Otherwise, find the named record.
95   for (unsigned i = Stack.size(); i != 0; ) {
96     --i;
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());
101       return true;
102     }
103   }
104 
105   return false;
106 }
107 
108 
109 /// FreePackedContext - Deallocate and null out PackContext.
110 void Sema::FreePackedContext() {
111   delete static_cast<PragmaPackStack*>(PackContext);
112   PackContext = 0;
113 }
114 
115 void 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.
123   if (unsigned Alignment = Stack->getAlignment()) {
124     if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
125       RD->addAttr(AlignMac68kAttr::CreateImplicit(Context));
126     else
127       RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context,
128                                                         Alignment * 8));
129   }
130 }
131 
132 void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
133   if (MSStructPragmaOn)
134     RD->addAttr(MsStructAttr::CreateImplicit(Context));
135 
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()));
142 }
143 
144 void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
145                                    SourceLocation PragmaLoc) {
146   if (PackContext == 0)
147     PackContext = new PragmaPackStack();
148 
149   PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
150 
151   switch (Kind) {
152     // 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:
156   case POAK_Power:
157   case POAK_Natural:
158     Context->push(0);
159     Context->setAlignment(0);
160     break;
161 
162     // 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:
165     Context->push(0);
166     Context->setAlignment(1);
167     break;
168 
169   case POAK_Mac68k:
170     // Check if the target supports this.
171     if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
172       Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
173       return;
174     }
175     Context->push(0);
176     Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
177     break;
178 
179   case POAK_Reset:
180     // Reset just pops the top of the stack, or resets the current alignment to
181     // default.
182     if (!Context->pop(0, /*IsReset=*/true)) {
183       Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
184         << "stack empty";
185     }
186     break;
187   }
188 }
189 
190 void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
191                            Expr *alignment, SourceLocation PragmaLoc,
192                            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;
199 
200     // pack(0) is like pack(), which just works out since that is what
201     // we use 0 for in PackAttr.
202     if (Alignment->isTypeDependent() ||
203         Alignment->isValueDependent() ||
204         !Alignment->isIntegerConstantExpr(Val, Context) ||
205         !(Val == 0 || Val.isPowerOf2()) ||
206         Val.getZExtValue() > 16) {
207       Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
208       return; // Ignore
209     }
210 
211     AlignmentVal = (unsigned) Val.getZExtValue();
212   }
213 
214   if (PackContext == 0)
215     PackContext = new PragmaPackStack();
216 
217   PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
218 
219   switch (Kind) {
220   case Sema::PPK_Default: // pack([n])
221     Context->setAlignment(AlignmentVal);
222     break;
223 
224   case Sema::PPK_Show: // pack(show)
225     // Show the current alignment, making sure to show the right value
226     // for the default.
227     AlignmentVal = Context->getAlignment();
228     // FIXME: This should come from the target.
229     if (AlignmentVal == 0)
230       AlignmentVal = 8;
231     if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
232       Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
233     else
234       Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
235     break;
236 
237   case Sema::PPK_Push: // pack(push [, id] [, [n])
238     Context->push(Name);
239     // Set the new alignment if specified.
240     if (Alignment)
241       Context->setAlignment(AlignmentVal);
242     break;
243 
244   case Sema::PPK_Pop: // pack(pop [, id] [,  n])
245     // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
246     // "#pragma pack(pop, identifier, n) is undefined"
247     if (Alignment && Name)
248       Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
249 
250     // Do the pop.
251     if (!Context->pop(Name, /*IsReset=*/false)) {
252       // If a name was specified then failure indicates the name
253       // wasn't found. Otherwise failure indicates the stack was
254       // empty.
255       Diag(PragmaLoc, diag::warn_pragma_pop_failed)
256           << "pack" << (Name ? "no record matching name" : "stack empty");
257 
258       // FIXME: Warn about popping named records as MSVC does.
259     } else {
260       // Pop succeeded, set the new alignment if specified.
261       if (Alignment)
262         Context->setAlignment(AlignmentVal);
263     }
264     break;
265   }
266 }
267 
268 void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
269   MSStructPragmaOn = (Kind == PMSST_ON);
270 }
271 
272 void Sema::ActOnPragmaMSComment(PragmaMSCommentKind Kind, StringRef Arg) {
273   // FIXME: Serialize this.
274   switch (Kind) {
275   case PCK_Unknown:
276     llvm_unreachable("unexpected pragma comment kind");
277   case PCK_Linker:
278     Consumer.HandleLinkerOptionPragma(Arg);
279     return;
280   case PCK_Lib:
281     Consumer.HandleDependentLibrary(Arg);
282     return;
283   case PCK_Compiler:
284   case PCK_ExeStr:
285   case PCK_User:
286     return;  // We ignore all of these.
287   }
288   llvm_unreachable("invalid pragma comment kind");
289 }
290 
291 void Sema::ActOnPragmaDetectMismatch(StringRef Name, StringRef Value) {
292   // FIXME: Serialize this.
293   Consumer.HandleDetectMismatch(Name, Value);
294 }
295 
296 void Sema::ActOnPragmaMSPointersToMembers(
297     LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,
298     SourceLocation PragmaLoc) {
299   MSPointerToMemberRepresentationMethod = RepresentationMethod;
300   ImplicitMSInheritanceAttrLoc = PragmaLoc;
301 }
302 
303 void Sema::ActOnPragmaMSVtorDisp(PragmaVtorDispKind Kind,
304                                  SourceLocation PragmaLoc,
305                                  MSVtorDispAttr::Mode Mode) {
306   switch (Kind) {
307   case PVDK_Set:
308     VtorDispModeStack.back() = Mode;
309     break;
310   case PVDK_Push:
311     VtorDispModeStack.push_back(Mode);
312     break;
313   case PVDK_Reset:
314     VtorDispModeStack.clear();
315     VtorDispModeStack.push_back(MSVtorDispAttr::Mode(LangOpts.VtorDispMode));
316     break;
317   case PVDK_Pop:
318     VtorDispModeStack.pop_back();
319     if (VtorDispModeStack.empty()) {
320       Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
321                                                     << "stack empty";
322       VtorDispModeStack.push_back(MSVtorDispAttr::Mode(LangOpts.VtorDispMode));
323     }
324     break;
325   }
326 }
327 
328 void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
329                              SourceLocation PragmaLoc) {
330 
331   IdentifierInfo *Name = IdTok.getIdentifierInfo();
332   LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
333   LookupParsedName(Lookup, curScope, NULL, true);
334 
335   if (Lookup.empty()) {
336     Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
337       << Name << SourceRange(IdTok.getLocation());
338     return;
339   }
340 
341   VarDecl *VD = Lookup.getAsSingle<VarDecl>();
342   if (!VD) {
343     Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
344       << Name << SourceRange(IdTok.getLocation());
345     return;
346   }
347 
348   // Warn if this was used before being marked unused.
349   if (VD->isUsed())
350     Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
351 
352   VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation()));
353 }
354 
355 void Sema::AddCFAuditedAttribute(Decl *D) {
356   SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
357   if (!Loc.isValid()) return;
358 
359   // Don't add a redundant or conflicting attribute.
360   if (D->hasAttr<CFAuditedTransferAttr>() ||
361       D->hasAttr<CFUnknownTransferAttr>())
362     return;
363 
364   D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc));
365 }
366 
367 typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
368 enum LLVM_ENUM_INT_TYPE(unsigned) { NoVisibility = ~0U };
369 
370 void Sema::AddPushedVisibilityAttribute(Decl *D) {
371   if (!VisContext)
372     return;
373 
374   NamedDecl *ND = dyn_cast<NamedDecl>(D);
375   if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
376     return;
377 
378   VisStack *Stack = static_cast<VisStack*>(VisContext);
379   unsigned rawType = Stack->back().first;
380   if (rawType == NoVisibility) return;
381 
382   VisibilityAttr::VisibilityType type
383     = (VisibilityAttr::VisibilityType) rawType;
384   SourceLocation loc = Stack->back().second;
385 
386   D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
387 }
388 
389 /// FreeVisContext - Deallocate and null out VisContext.
390 void Sema::FreeVisContext() {
391   delete static_cast<VisStack*>(VisContext);
392   VisContext = 0;
393 }
394 
395 static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
396   // Put visibility on stack.
397   if (!S.VisContext)
398     S.VisContext = new VisStack;
399 
400   VisStack *Stack = static_cast<VisStack*>(S.VisContext);
401   Stack->push_back(std::make_pair(type, loc));
402 }
403 
404 void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
405                                  SourceLocation PragmaLoc) {
406   if (VisType) {
407     // Compute visibility to use.
408     VisibilityAttr::VisibilityType T;
409     if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
410       Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
411       return;
412     }
413     PushPragmaVisibility(*this, T, PragmaLoc);
414   } else {
415     PopPragmaVisibility(false, PragmaLoc);
416   }
417 }
418 
419 void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
420   switch (OOS) {
421   case tok::OOS_ON:
422     FPFeatures.fp_contract = 1;
423     break;
424   case tok::OOS_OFF:
425     FPFeatures.fp_contract = 0;
426     break;
427   case tok::OOS_DEFAULT:
428     FPFeatures.fp_contract = getLangOpts().DefaultFPContract;
429     break;
430   }
431 }
432 
433 void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
434                                        SourceLocation Loc) {
435   // Visibility calculations will consider the namespace's visibility.
436   // Here we just want to note that we're in a visibility context
437   // which overrides any enclosing #pragma context, but doesn't itself
438   // contribute visibility.
439   PushPragmaVisibility(*this, NoVisibility, Loc);
440 }
441 
442 void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
443   if (!VisContext) {
444     Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
445     return;
446   }
447 
448   // Pop visibility from stack
449   VisStack *Stack = static_cast<VisStack*>(VisContext);
450 
451   const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
452   bool StartsWithPragma = Back->first != NoVisibility;
453   if (StartsWithPragma && IsNamespaceEnd) {
454     Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
455     Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
456 
457     // For better error recovery, eat all pushes inside the namespace.
458     do {
459       Stack->pop_back();
460       Back = &Stack->back();
461       StartsWithPragma = Back->first != NoVisibility;
462     } while (StartsWithPragma);
463   } else if (!StartsWithPragma && !IsNamespaceEnd) {
464     Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
465     Diag(Back->second, diag::note_surrounding_namespace_starts_here);
466     return;
467   }
468 
469   Stack->pop_back();
470   // To simplify the implementation, never keep around an empty stack.
471   if (Stack->empty())
472     FreeVisContext();
473 }
474