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
152d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
16e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Lookup.h"
17cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt#include "clang/AST/Attr.h"
185a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner#include "clang/AST/Expr.h"
19613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar#include "clang/Basic/TargetInfo.h"
20613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar#include "clang/Lex/Preprocessor.h"
215a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattnerusing namespace clang;
225a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
23574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner//===----------------------------------------------------------------------===//
24ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar// Pragma 'pack' and 'options align'
25574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner//===----------------------------------------------------------------------===//
26574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
27574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattnernamespace {
28ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar  struct PackStackEntry {
29c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    // We just use a sentinel to represent when the stack is set to mac68k
30c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    // alignment.
31c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    static const unsigned kMac68kAlignmentSentinel = ~0U;
32c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar
33ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar    unsigned Alignment;
34ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar    IdentifierInfo *Name;
35ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar  };
36ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar
37574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  /// PragmaPackStack - Simple class to wrap the stack used by #pragma
38574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  /// pack.
39574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  class PragmaPackStack {
40ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar    typedef std::vector<PackStackEntry> stack_ty;
41574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
42574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// Alignment - The current user specified alignment.
43574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    unsigned Alignment;
44574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
45574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// Stack - Entries in the #pragma pack stack, consisting of saved
46574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// alignments and optional names.
47574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    stack_ty Stack;
481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  public:
50574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    PragmaPackStack() : Alignment(0) {}
51574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
52574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    void setAlignment(unsigned A) { Alignment = A; }
53574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    unsigned getAlignment() { return Alignment; }
54574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
55574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// push - Push the current alignment onto the stack, optionally
56574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// using the given \arg Name for the record, if non-zero.
57574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    void push(IdentifierInfo *Name) {
58ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar      PackStackEntry PSE = { Alignment, Name };
59ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar      Stack.push_back(PSE);
60574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    }
61574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
62574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// pop - Pop a record from the stack and restore the current
63574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// alignment to the previous value. If \arg Name is non-zero then
64574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// the first such named record is popped, otherwise the top record
65574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    /// is popped. Returns true if the pop succeeded.
66ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar    bool pop(IdentifierInfo *Name, bool IsReset);
67574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  };
68574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner}  // end anonymous namespace.
69574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
70ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbarbool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
71574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  // If name is empty just pop top.
72574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  if (!Name) {
73ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar    // An empty stack is a special case...
74ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar    if (Stack.empty()) {
75ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar      // If this isn't a reset, it is always an error.
76ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar      if (!IsReset)
77ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar        return false;
78ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar
79ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar      // Otherwise, it is an error only if some alignment has been set.
80ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar      if (!Alignment)
81ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar        return false;
82ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar
83ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar      // Otherwise, reset to the default alignment.
84ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar      Alignment = 0;
85ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar    } else {
86ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar      Alignment = Stack.back().Alignment;
87ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar      Stack.pop_back();
88ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar    }
89ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar
90574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    return true;
911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  // Otherwise, find the named record.
94574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  for (unsigned i = Stack.size(); i != 0; ) {
95574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    --i;
96ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar    if (Stack[i].Name == Name) {
97574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner      // Found it, pop up to and including this record.
98ae2232b9957302374ee8d8626aeedcb4e4c0441eDaniel Dunbar      Alignment = Stack[i].Alignment;
99574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner      Stack.erase(Stack.begin() + i, Stack.end());
100574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner      return true;
101574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    }
102574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  }
1031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
104574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  return false;
105574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner}
106574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
107574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
108574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner/// FreePackedContext - Deallocate and null out PackContext.
109574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattnervoid Sema::FreePackedContext() {
110574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  delete static_cast<PragmaPackStack*>(PackContext);
111574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  PackContext = 0;
112574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner}
113574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
1149f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbarvoid Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
1159f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar  // If there is no pack context, we don't need any attributes.
1169f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar  if (!PackContext)
1179f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar    return;
1189f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar
1199f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar  PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext);
1209f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar
1219f21f89c323ccf32f6b27acd2e739f6535440df0Daniel Dunbar  // Otherwise, check to see if we need a max field alignment attribute.
122c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar  if (unsigned Alignment = Stack->getAlignment()) {
123c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
124cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      RD->addAttr(::new (Context) AlignMac68kAttr(SourceLocation(), Context));
125c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    else
126cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      RD->addAttr(::new (Context) MaxFieldAlignmentAttr(SourceLocation(),
127cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                                        Context,
128cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                                        Alignment * 8));
129c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar  }
130574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner}
131574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner
132c1a0a73c1fad684dd23e9aade02c4e00dbaeaee6Fariborz Jahanianvoid Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
133c1a0a73c1fad684dd23e9aade02c4e00dbaeaee6Fariborz Jahanian  if (!MSStructPragmaOn)
134c1a0a73c1fad684dd23e9aade02c4e00dbaeaee6Fariborz Jahanian    return;
135c1a0a73c1fad684dd23e9aade02c4e00dbaeaee6Fariborz Jahanian  RD->addAttr(::new (Context) MsStructAttr(SourceLocation(), Context));
136c1a0a73c1fad684dd23e9aade02c4e00dbaeaee6Fariborz Jahanian}
137c1a0a73c1fad684dd23e9aade02c4e00dbaeaee6Fariborz Jahanian
138ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbarvoid Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
139ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar                                   SourceLocation PragmaLoc,
140ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar                                   SourceLocation KindLoc) {
141ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  if (PackContext == 0)
142ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    PackContext = new PragmaPackStack();
143ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar
144ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
145ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar
146ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar  // Reset just pops the top of the stack, or resets the current alignment to
147ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar  // default.
148f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (Kind == Sema::POAK_Reset) {
149ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar    if (!Context->pop(0, /*IsReset=*/true)) {
150ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar      Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
151ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar        << "stack empty";
152ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    }
153ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    return;
154ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  }
155ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar
156ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  switch (Kind) {
157638e7cf3a09436dce7f3150ff8e4f27d190bd2edDaniel Dunbar    // For all targets we support native and natural are the same.
158638e7cf3a09436dce7f3150ff8e4f27d190bd2edDaniel Dunbar    //
159638e7cf3a09436dce7f3150ff8e4f27d190bd2edDaniel Dunbar    // FIXME: This is not true on Darwin/PPC.
160638e7cf3a09436dce7f3150ff8e4f27d190bd2edDaniel Dunbar  case POAK_Native:
161450f79340808e54cd4b39738f943217f6fc0c2beDaniel Dunbar  case POAK_Power:
162d6b305dcba95fc4928a2baa6bbeb15d22db9d061Daniel Dunbar  case POAK_Natural:
163450f79340808e54cd4b39738f943217f6fc0c2beDaniel Dunbar    Context->push(0);
164450f79340808e54cd4b39738f943217f6fc0c2beDaniel Dunbar    Context->setAlignment(0);
165450f79340808e54cd4b39738f943217f6fc0c2beDaniel Dunbar    break;
166450f79340808e54cd4b39738f943217f6fc0c2beDaniel Dunbar
1676f739145b94ede1ca98b5a5e0e179c817c405d7bDaniel Dunbar    // Note that '#pragma options align=packed' is not equivalent to attribute
1686f739145b94ede1ca98b5a5e0e179c817c405d7bDaniel Dunbar    // packed, it has a different precedence relative to attribute aligned.
1696f739145b94ede1ca98b5a5e0e179c817c405d7bDaniel Dunbar  case POAK_Packed:
1706f739145b94ede1ca98b5a5e0e179c817c405d7bDaniel Dunbar    Context->push(0);
1716f739145b94ede1ca98b5a5e0e179c817c405d7bDaniel Dunbar    Context->setAlignment(1);
1726f739145b94ede1ca98b5a5e0e179c817c405d7bDaniel Dunbar    break;
1736f739145b94ede1ca98b5a5e0e179c817c405d7bDaniel Dunbar
174613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar  case POAK_Mac68k:
175613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar    // Check if the target supports this.
176613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar    if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
177613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar      Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
178613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar      return;
179613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar    }
180c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    Context->push(0);
181c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
182613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar    break;
183613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar
184ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  default:
185ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option)
186ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar      << KindLoc;
187ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar    break;
188ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar  }
189ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar}
190ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar
1911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
192f81e5a9e3f3ff80c56e4afb4fe6311a8735f36e8Richard Trieu                           Expr *alignment, SourceLocation PragmaLoc,
1935a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner                           SourceLocation LParenLoc, SourceLocation RParenLoc) {
1945a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  Expr *Alignment = static_cast<Expr *>(alignment);
1955a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
1965a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  // If specified then alignment must be a "small" power of two.
1975a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  unsigned AlignmentVal = 0;
1985a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  if (Alignment) {
1995a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    llvm::APSInt Val;
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20179cd11620c6f05dbf7bb706744eba354574e8b54Daniel Dunbar    // pack(0) is like pack(), which just works out since that is what
20279cd11620c6f05dbf7bb706744eba354574e8b54Daniel Dunbar    // we use 0 for in PackAttr.
203ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor    if (Alignment->isTypeDependent() ||
204ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor        Alignment->isValueDependent() ||
205ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor        !Alignment->isIntegerConstantExpr(Val, Context) ||
20679cd11620c6f05dbf7bb706744eba354574e8b54Daniel Dunbar        !(Val == 0 || Val.isPowerOf2()) ||
2075a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner        Val.getZExtValue() > 16) {
2085a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
2095a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      return; // Ignore
2105a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    }
2115a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
2125a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    AlignmentVal = (unsigned) Val.getZExtValue();
2135a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  }
2141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
215574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  if (PackContext == 0)
216574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    PackContext = new PragmaPackStack();
2171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
218574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner  PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
2191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2205a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  switch (Kind) {
221f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  case Sema::PPK_Default: // pack([n])
222574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    Context->setAlignment(AlignmentVal);
2235a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    break;
2245a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
225f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  case Sema::PPK_Show: // pack(show)
2265a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // Show the current alignment, making sure to show the right value
2275a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // for the default.
228574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    AlignmentVal = Context->getAlignment();
2295a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // FIXME: This should come from the target.
2305a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    if (AlignmentVal == 0)
2315a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      AlignmentVal = 8;
232c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
233c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar      Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
234c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar    else
235c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar      Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
2365a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    break;
2375a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
238f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  case Sema::PPK_Push: // pack(push [, id] [, [n])
239574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner    Context->push(Name);
2405a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // Set the new alignment if specified.
2415a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    if (Alignment)
2421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Context->setAlignment(AlignmentVal);
2435a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    break;
2445a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
245f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  case Sema::PPK_Pop: // pack(pop [, id] [,  n])
2465a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
2475a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // "#pragma pack(pop, identifier, n) is undefined"
2485a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    if (Alignment && Name)
2491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
2501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2515a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    // Do the pop.
252ddc6ff6b9aff656504c1e84ee7dc9f617a20f866Daniel Dunbar    if (!Context->pop(Name, /*IsReset=*/false)) {
2535a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      // If a name was specified then failure indicates the name
2545a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      // wasn't found. Otherwise failure indicates the stack was
2555a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      // empty.
2565a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
2575a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner        << (Name ? "no record matching name" : "stack empty");
2585a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
2595a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      // FIXME: Warn about popping named records as MSVC does.
2605a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    } else {
2615a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      // Pop succeeded, set the new alignment if specified.
2625a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner      if (Alignment)
263574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner        Context->setAlignment(AlignmentVal);
2645a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    }
2655a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner    break;
2665a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner  }
2675a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner}
2685a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner
26962c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanianvoid Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
27062c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructPragmaOn = (Kind == PMSST_ON);
27162c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian}
27262c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian
273b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidisvoid Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
274b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis                             SourceLocation PragmaLoc) {
2751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
276b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  IdentifierInfo *Name = IdTok.getIdentifierInfo();
277b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
278b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  LookupParsedName(Lookup, curScope, NULL, true);
2791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
280b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  if (Lookup.empty()) {
281b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis    Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
282b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis      << Name << SourceRange(IdTok.getLocation());
283b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis    return;
284b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  }
2852127eccbe15fd3b1b29aa53ccedd2e0f55ad27f9Anders Carlsson
286b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  VarDecl *VD = Lookup.getAsSingle<VarDecl>();
2872a5c45b1ae4406459fbb39cb477951987c59cb0fArgyrios Kyrtzidis  if (!VD) {
2882a5c45b1ae4406459fbb39cb477951987c59cb0fArgyrios Kyrtzidis    Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
289b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis      << Name << SourceRange(IdTok.getLocation());
290b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis    return;
2914726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek  }
292b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
293b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  // Warn if this was used before being marked unused.
294b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  if (VD->isUsed())
295b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis    Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
296b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis
297b918d0f5d8f147e1e26c34e6cf42a79af2d2ec41Argyrios Kyrtzidis  VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context));
2984726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek}
299aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
3008dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCallvoid Sema::AddCFAuditedAttribute(Decl *D) {
3018dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
3028dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  if (!Loc.isValid()) return;
3038dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
3048dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  // Don't add a redundant or conflicting attribute.
3058dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  if (D->hasAttr<CFAuditedTransferAttr>() ||
3068dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall      D->hasAttr<CFUnknownTransferAttr>())
3078dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall    return;
3088dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
3098dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  D->addAttr(::new (Context) CFAuditedTransferAttr(Loc, Context));
3108dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall}
3118dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
31290f1450c109fbbd333001165bbd986061f7c4513John McCalltypedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
31390f1450c109fbbd333001165bbd986061f7c4513John McCallenum { NoVisibility = (unsigned) -1 };
314aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
315aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedmanvoid Sema::AddPushedVisibilityAttribute(Decl *D) {
316aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  if (!VisContext)
317aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    return;
318aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
3194421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (isa<NamedDecl>(D) && cast<NamedDecl>(D)->getExplicitVisibility())
320aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    return;
321aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
322aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  VisStack *Stack = static_cast<VisStack*>(VisContext);
32390f1450c109fbbd333001165bbd986061f7c4513John McCall  unsigned rawType = Stack->back().first;
32490f1450c109fbbd333001165bbd986061f7c4513John McCall  if (rawType == NoVisibility) return;
32590f1450c109fbbd333001165bbd986061f7c4513John McCall
32690f1450c109fbbd333001165bbd986061f7c4513John McCall  VisibilityAttr::VisibilityType type
32790f1450c109fbbd333001165bbd986061f7c4513John McCall    = (VisibilityAttr::VisibilityType) rawType;
328cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  SourceLocation loc = Stack->back().second;
329aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
330cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  D->addAttr(::new (Context) VisibilityAttr(loc, Context, type));
331aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman}
332aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
333aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman/// FreeVisContext - Deallocate and null out VisContext.
334aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedmanvoid Sema::FreeVisContext() {
335aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  delete static_cast<VisStack*>(VisContext);
336aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  VisContext = 0;
337aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman}
338aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
33990f1450c109fbbd333001165bbd986061f7c4513John McCallstatic void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
340ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall  // Put visibility on stack.
341ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall  if (!S.VisContext)
342ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall    S.VisContext = new VisStack;
343ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall
344ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall  VisStack *Stack = static_cast<VisStack*>(S.VisContext);
345ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall  Stack->push_back(std::make_pair(type, loc));
346ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall}
347ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall
3483fc809de96f9b0a4fcf7bc936399194d0b7198c8Rafael Espindolavoid Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
349aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman                                 SourceLocation PragmaLoc) {
3503fc809de96f9b0a4fcf7bc936399194d0b7198c8Rafael Espindola  if (VisType) {
351aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    // Compute visibility to use.
352cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    VisibilityAttr::VisibilityType type;
353aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    if (VisType->isStr("default"))
354cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      type = VisibilityAttr::Default;
355aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    else if (VisType->isStr("hidden"))
356cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      type = VisibilityAttr::Hidden;
357aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    else if (VisType->isStr("internal"))
358cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      type = VisibilityAttr::Hidden; // FIXME
359aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    else if (VisType->isStr("protected"))
360cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      type = VisibilityAttr::Protected;
361aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    else {
362aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman      Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) <<
363aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman        VisType->getName();
364aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman      return;
365aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    }
366ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall    PushPragmaVisibility(*this, type, PragmaLoc);
367aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  } else {
36820039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    PopPragmaVisibility(false, PragmaLoc);
369aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  }
370aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman}
371aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
372321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbournevoid Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
373321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  switch (OOS) {
374321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  case tok::OOS_ON:
375321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne    FPFeatures.fp_contract = 1;
376321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne    break;
377321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  case tok::OOS_OFF:
378321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne    FPFeatures.fp_contract = 0;
379321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne    break;
380321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  case tok::OOS_DEFAULT:
3814e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    FPFeatures.fp_contract = getLangOpts().DefaultFPContract;
382321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne    break;
383321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  }
384321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne}
385321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne
38620039ae1d9f520d8395899d807473b638fb48688Rafael Espindolavoid Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
38720039ae1d9f520d8395899d807473b638fb48688Rafael Espindola                                       SourceLocation Loc) {
38890f1450c109fbbd333001165bbd986061f7c4513John McCall  // Visibility calculations will consider the namespace's visibility.
38990f1450c109fbbd333001165bbd986061f7c4513John McCall  // Here we just want to note that we're in a visibility context
39090f1450c109fbbd333001165bbd986061f7c4513John McCall  // which overrides any enclosing #pragma context, but doesn't itself
39190f1450c109fbbd333001165bbd986061f7c4513John McCall  // contribute visibility.
39220039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  PushPragmaVisibility(*this, NoVisibility, Loc);
393aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman}
394aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
39520039ae1d9f520d8395899d807473b638fb48688Rafael Espindolavoid Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
39620039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  if (!VisContext) {
39720039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
39820039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    return;
39920039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  }
40020039ae1d9f520d8395899d807473b638fb48688Rafael Espindola
40120039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  // Pop visibility from stack
40220039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  VisStack *Stack = static_cast<VisStack*>(VisContext);
403aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman
40420039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
40520039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  bool StartsWithPragma = Back->first != NoVisibility;
40620039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  if (StartsWithPragma && IsNamespaceEnd) {
40720039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
40820039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
40920039ae1d9f520d8395899d807473b638fb48688Rafael Espindola
41020039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    // For better error recovery, eat all pushes inside the namespace.
41120039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    do {
41220039ae1d9f520d8395899d807473b638fb48688Rafael Espindola      Stack->pop_back();
41320039ae1d9f520d8395899d807473b638fb48688Rafael Espindola      Back = &Stack->back();
41420039ae1d9f520d8395899d807473b638fb48688Rafael Espindola      StartsWithPragma = Back->first != NoVisibility;
41520039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    } while (StartsWithPragma);
41620039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  } else if (!StartsWithPragma && !IsNamespaceEnd) {
41720039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
41820039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    Diag(Back->second, diag::note_surrounding_namespace_starts_here);
41920039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    return;
420aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  }
42120039ae1d9f520d8395899d807473b638fb48688Rafael Espindola
42220039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  Stack->pop_back();
42320039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  // To simplify the implementation, never keep around an empty stack.
42420039ae1d9f520d8395899d807473b638fb48688Rafael Espindola  if (Stack->empty())
42520039ae1d9f520d8395899d807473b638fb48688Rafael Espindola    FreeVisContext();
426aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman}
427