SemaAttr.cpp revision c6082fe347a414a2e19f2ad8fe41720f10733296
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) { 140ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar case POAK_Natural: 141ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar Context->push(0); 142ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar Context->setAlignment(0); 143ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar break; 144ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar 145613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar case POAK_Mac68k: 146613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar // Check if the target supports this. 147613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar if (!PP.getTargetInfo().hasAlignMac68kSupport()) { 148613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); 149613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar return; 150613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar } 151c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar Context->push(0); 152c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel); 153613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar break; 154613fd67e575ff1c038535b18dafebca070f3ed91Daniel Dunbar 155ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar default: 156ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option) 157ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar << KindLoc; 158ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar break; 159ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar } 160ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar} 161ea75a8286fb87ce7549e08d9dcb597f91479f54dDaniel Dunbar 1621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, 1631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ExprTy *alignment, SourceLocation PragmaLoc, 1645a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner SourceLocation LParenLoc, SourceLocation RParenLoc) { 1655a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner Expr *Alignment = static_cast<Expr *>(alignment); 1665a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner 1675a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // If specified then alignment must be a "small" power of two. 1685a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner unsigned AlignmentVal = 0; 1695a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner if (Alignment) { 1705a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner llvm::APSInt Val; 1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17279cd11620c6f05dbf7bb706744eba354574e8b54Daniel Dunbar // pack(0) is like pack(), which just works out since that is what 17379cd11620c6f05dbf7bb706744eba354574e8b54Daniel Dunbar // we use 0 for in PackAttr. 174ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor if (Alignment->isTypeDependent() || 175ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor Alignment->isValueDependent() || 176ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor !Alignment->isIntegerConstantExpr(Val, Context) || 17779cd11620c6f05dbf7bb706744eba354574e8b54Daniel Dunbar !(Val == 0 || Val.isPowerOf2()) || 1785a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner Val.getZExtValue() > 16) { 1795a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); 1805a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner Alignment->Destroy(Context); 1815a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner return; // Ignore 1825a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner } 1835a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner 1845a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner AlignmentVal = (unsigned) Val.getZExtValue(); 1855a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner } 1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 187574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner if (PackContext == 0) 188574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner PackContext = new PragmaPackStack(); 1891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 190574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); 1911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1925a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner switch (Kind) { 1935a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner case Action::PPK_Default: // pack([n]) 194574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner Context->setAlignment(AlignmentVal); 1955a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner break; 1965a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner 1975a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner case Action::PPK_Show: // pack(show) 1985a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // Show the current alignment, making sure to show the right value 1995a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // for the default. 200574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner AlignmentVal = Context->getAlignment(); 2015a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // FIXME: This should come from the target. 2025a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner if (AlignmentVal == 0) 2035a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner AlignmentVal = 8; 204c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel) 205c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; 206c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar else 207c6082fe347a414a2e19f2ad8fe41720f10733296Daniel Dunbar Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; 2085a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner break; 2095a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner 2105a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner case Action::PPK_Push: // pack(push [, id] [, [n]) 211574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner Context->push(Name); 2125a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // Set the new alignment if specified. 2135a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner if (Alignment) 2141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Context->setAlignment(AlignmentVal); 2155a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner break; 2165a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner 2175a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner case Action::PPK_Pop: // pack(pop [, id] [, n]) 2185a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: 2195a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // "#pragma pack(pop, identifier, n) is undefined" 2205a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner if (Alignment && Name) 2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); 2221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2235a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // Do the pop. 224574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner if (!Context->pop(Name)) { 2255a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // If a name was specified then failure indicates the name 2265a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // wasn't found. Otherwise failure indicates the stack was 2275a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // empty. 2285a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed) 2295a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner << (Name ? "no record matching name" : "stack empty"); 2305a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner 2315a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // FIXME: Warn about popping named records as MSVC does. 2325a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner } else { 2335a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner // Pop succeeded, set the new alignment if specified. 2345a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner if (Alignment) 235574aa40703ffb2fddad5b076cec1c2fc27f0b2d3Chris Lattner Context->setAlignment(AlignmentVal); 2365a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner } 2375a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner break; 2385a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner 2395a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner default: 2405a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner assert(0 && "Invalid #pragma pack kind."); 2415a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner } 2425a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner} 2435a0c35150102f95ab270f741cdab5907c9bd9017Chris Lattner 2447a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenekvoid Sema::ActOnPragmaUnused(const Token *Identifiers, unsigned NumIdentifiers, 2457a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek Scope *curScope, 2464726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek SourceLocation PragmaLoc, 2474726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek SourceLocation LParenLoc, 2484726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek SourceLocation RParenLoc) { 2494726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek 2507a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek for (unsigned i = 0; i < NumIdentifiers; ++i) { 2517a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek const Token &Tok = Identifiers[i]; 2521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump IdentifierInfo *Name = Tok.getIdentifierInfo(); 253a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall LookupResult Lookup(*this, Name, Tok.getLocation(), LookupOrdinaryName); 254a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall LookupParsedName(Lookup, curScope, NULL, true); 2557a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek 2561bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall if (Lookup.empty()) { 2577a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) 2587a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek << Name << SourceRange(Tok.getLocation()); 2597a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek continue; 2604726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek } 2611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2621bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall VarDecl *VD = Lookup.getAsSingle<VarDecl>(); 2631bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall if (!VD || !VD->hasLocalStorage()) { 2647a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek Diag(PragmaLoc, diag::warn_pragma_unused_expected_localvar) 2657a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek << Name << SourceRange(Tok.getLocation()); 2667a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek continue; 2677a02a3733cdd2ca672902d869fda4ef2e3f05052Ted Kremenek } 2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2691bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall VD->addAttr(::new (Context) UnusedAttr()); 2704726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek } 2714726d03ab3abce41911c31d1354a18f1258cae4dTed Kremenek} 272