SemaAttr.cpp revision 638e7cf3a09436dce7f3150ff8e4f27d190bd2ed
15a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner//===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
25a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner//
35a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner//                     The LLVM Compiler Infrastructure
45a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner//
55a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner// This file is distributed under the University of Illinois Open Source
65a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner// License. See LICENSE.TXT for details.
75a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner//
85a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner//===----------------------------------------------------------------------===//
95a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner//
10574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner// This file implements semantic analysis for non-trivial attributes and
11574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner// pragmas.
125a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner//
135a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner//===----------------------------------------------------------------------===//
145a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
155a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner#include "Sema.h"
167d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall#include "Lookup.h"
175a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner#include "clang/AST/Expr.h"
18613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar#include "clang/Basic/TargetInfo.h"
19613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar#include "clang/Lex/Preprocessor.h"
205a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattnerusing namespace clang;
215a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
22574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner//===----------------------------------------------------------------------===//
23ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar// Pragma 'pack' and 'options align'
24574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner//===----------------------------------------------------------------------===//
25574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
26574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattnernamespace {
27ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar  struct PackStackEntry {
28c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    // We just use a sentinel to represent when the stack is set to mac68k
29c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    // alignment.
30c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    static const unsigned kMac68kAlignmentSentinel = ~0U;
31c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar
32ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar    unsigned Alignment;
33ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar    IdentifierInfo *Name;
34ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar  };
35ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar
36574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  /// PragmaPackStack - Simple class to wrap the stack used by #pragma
37574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  /// pack.
38574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  class PragmaPackStack {
39ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar    typedef std::vector<PackStackEntry> stack_ty;
40574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
41574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// Alignment - The current user specified alignment.
42574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    unsigned Alignment;
43574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
44574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// Stack - Entries in the #pragma pack stack, consisting of saved
45574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// alignments and optional names.
46574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    stack_ty Stack;
471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  public:
49574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    PragmaPackStack() : Alignment(0) {}
50574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
51574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    void setAlignment(unsigned A) { Alignment = A; }
52574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    unsigned getAlignment() { return Alignment; }
53574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
54574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// push - Push the current alignment onto the stack, optionally
55574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// using the given \arg Name for the record, if non-zero.
56574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    void push(IdentifierInfo *Name) {
57ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar      PackStackEntry PSE = { Alignment, Name };
58ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar      Stack.push_back(PSE);
59574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    }
60574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
61574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// pop - Pop a record from the stack and restore the current
62574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// alignment to the previous value. If \arg Name is non-zero then
63574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// the first such named record is popped, otherwise the top record
64574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// is popped. Returns true if the pop succeeded.
65574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    bool pop(IdentifierInfo *Name);
66574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  };
67574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner}  // end anonymous namespace.
68574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
69574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattnerbool PragmaPackStack::pop(IdentifierInfo *Name) {
70574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  if (Stack.empty())
71574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    return false;
721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
73574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  // If name is empty just pop top.
74574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  if (!Name) {
75ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar    Alignment = Stack.back().Alignment;
76574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    Stack.pop_back();
77574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    return true;
781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  // Otherwise, find the named record.
81574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  for (unsigned i = Stack.size(); i != 0; ) {
82574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    --i;
83ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar    if (Stack[i].Name == Name) {
84574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner      // Found it, pop up to and including this record.
85ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar      Alignment = Stack[i].Alignment;
86574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner      Stack.erase(Stack.begin() + i, Stack.end());
87574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner      return true;
88574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    }
89574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  }
901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
91574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  return false;
92574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner}
93574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
94574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
95574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner/// FreePackedContext - Deallocate and null out PackContext.
96574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattnervoid Sema::FreePackedContext() {
97574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  delete static_cast<PragmaPackStack*>(PackContext);
98574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  PackContext = 0;
99574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner}
100574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
1019f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbarvoid Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
1029f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar  // If there is no pack context, we don't need any attributes.
1039f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar  if (!PackContext)
1049f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar    return;
1059f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar
1069f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar  PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext);
1079f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar
1089f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar  // Otherwise, check to see if we need a max field alignment attribute.
109c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar  if (unsigned Alignment = Stack->getAlignment()) {
110c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
111c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar      RD->addAttr(::new (Context) AlignMac68kAttr());
112c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    else
113c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar      RD->addAttr(::new (Context) MaxFieldAlignmentAttr(Alignment * 8));
114c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar  }
115574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner}
116574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
117ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbarvoid Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
118ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar                                   SourceLocation PragmaLoc,
119ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar                                   SourceLocation KindLoc) {
120ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  if (PackContext == 0)
121ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    PackContext = new PragmaPackStack();
122ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar
123ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
124ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar
125ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  // Reset just pops the top of the stack.
126ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  if (Kind == Action::POAK_Reset) {
127ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    // Do the pop.
128ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    if (!Context->pop(0)) {
129ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar      // If a name was specified then failure indicates the name
130ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar      // wasn't found. Otherwise failure indicates the stack was
131ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar      // empty.
132ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar      Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
133ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar        << "stack empty";
134ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    }
135ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    return;
136ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  }
137ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar
138ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  // We don't support #pragma options align=power.
139ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  switch (Kind) {
140638e7cf3a09436dce7f3150ff8e4f27d190bd2edDaniel Dunbar    // For all targets we support native and natural are the same.
141638e7cf3a09436dce7f3150ff8e4f27d190bd2edDaniel Dunbar    //
142638e7cf3a09436dce7f3150ff8e4f27d190bd2edDaniel Dunbar    // FIXME: This is not true on Darwin/PPC.
143638e7cf3a09436dce7f3150ff8e4f27d190bd2edDaniel Dunbar  case POAK_Native:
144ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  case POAK_Natural:
145ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    Context->push(0);
146ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    Context->setAlignment(0);
147ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    break;
148ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar
149613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar  case POAK_Mac68k:
150613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar    // Check if the target supports this.
151613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar    if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
152613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar      Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
153613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar      return;
154613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar    }
155c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    Context->push(0);
156c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
157613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar    break;
158613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar
159ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  default:
160ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option)
161ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar      << KindLoc;
162ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    break;
163ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  }
164ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar}
165ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar
1661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                           ExprTy *alignment, SourceLocation PragmaLoc,
1685a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner                           SourceLocation LParenLoc, SourceLocation RParenLoc) {
1695a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  Expr *Alignment = static_cast<Expr *>(alignment);
1705a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
1715a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  // If specified then alignment must be a "small" power of two.
1725a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  unsigned AlignmentVal = 0;
1735a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  if (Alignment) {
1745a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    llvm::APSInt Val;
1751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17679cd11620c6f05dbf7bb706744eba354574e8b54Daniel Dunbar    // pack(0) is like pack(), which just works out since that is what
17779cd11620c6f05dbf7bb706744eba354574e8b54Daniel Dunbar    // we use 0 for in PackAttr.
178ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor    if (Alignment->isTypeDependent() ||
179ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor        Alignment->isValueDependent() ||
180ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor        !Alignment->isIntegerConstantExpr(Val, Context) ||
18179cd11620c6f05dbf7bb706744eba354574e8b54Daniel Dunbar        !(Val == 0 || Val.isPowerOf2()) ||
1825a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner        Val.getZExtValue() > 16) {
1835a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
1845a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      Alignment->Destroy(Context);
1855a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      return; // Ignore
1865a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    }
1875a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
1885a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    AlignmentVal = (unsigned) Val.getZExtValue();
1895a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  }
1901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
191574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  if (PackContext == 0)
192574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    PackContext = new PragmaPackStack();
1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
194574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1965a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  switch (Kind) {
1975a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  case Action::PPK_Default: // pack([n])
198574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    Context->setAlignment(AlignmentVal);
1995a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    break;
2005a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
2015a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  case Action::PPK_Show: // pack(show)
2025a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // Show the current alignment, making sure to show the right value
2035a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // for the default.
204574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    AlignmentVal = Context->getAlignment();
2055a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // FIXME: This should come from the target.
2065a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    if (AlignmentVal == 0)
2075a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      AlignmentVal = 8;
208c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
209c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar      Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
210c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    else
211c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar      Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
2125a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    break;
2135a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
2145a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  case Action::PPK_Push: // pack(push [, id] [, [n])
215574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    Context->push(Name);
2165a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // Set the new alignment if specified.
2175a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    if (Alignment)
2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Context->setAlignment(AlignmentVal);
2195a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    break;
2205a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
2215a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  case Action::PPK_Pop: // pack(pop [, id] [,  n])
2225a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
2235a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // "#pragma pack(pop, identifier, n) is undefined"
2245a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    if (Alignment && Name)
2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2275a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // Do the pop.
228574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    if (!Context->pop(Name)) {
2295a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      // If a name was specified then failure indicates the name
2305a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      // wasn't found. Otherwise failure indicates the stack was
2315a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      // empty.
2325a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
2335a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner        << (Name ? "no record matching name" : "stack empty");
2345a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
2355a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      // FIXME: Warn about popping named records as MSVC does.
2365a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    } else {
2375a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      // Pop succeeded, set the new alignment if specified.
2385a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      if (Alignment)
239574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner        Context->setAlignment(AlignmentVal);
2405a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    }
2415a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    break;
2425a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
2435a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  default:
2445a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    assert(0 && "Invalid #pragma pack kind.");
2455a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  }
2465a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner}
2475a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
2487a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenekvoid Sema::ActOnPragmaUnused(const Token *Identifiers, unsigned NumIdentifiers,
2497a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek                             Scope *curScope,
2504726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek                             SourceLocation PragmaLoc,
2514726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek                             SourceLocation LParenLoc,
2524726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek                             SourceLocation RParenLoc) {
2534726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek
2547a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek  for (unsigned i = 0; i < NumIdentifiers; ++i) {
2557a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek    const Token &Tok = Identifiers[i];
2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    IdentifierInfo *Name = Tok.getIdentifierInfo();
257a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    LookupResult Lookup(*this, Name, Tok.getLocation(), LookupOrdinaryName);
258a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall    LookupParsedName(Lookup, curScope, NULL, true);
2597a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek
2601bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall    if (Lookup.empty()) {
2617a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek      Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
2627a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek        << Name << SourceRange(Tok.getLocation());
2637a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek      continue;
2644726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek    }
2651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2661bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall    VarDecl *VD = Lookup.getAsSingle<VarDecl>();
2671bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall    if (!VD || !VD->hasLocalStorage()) {
2687a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek      Diag(PragmaLoc, diag::warn_pragma_unused_expected_localvar)
2697a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek        << Name << SourceRange(Tok.getLocation());
2707a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek      continue;
2717a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek    }
2721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2731bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall    VD->addAttr(::new (Context) UnusedAttr());
2744726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  }
2754726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek}
276