2009-02-17 08:57:29 +08:00
|
|
|
//===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-02-17 09:09:29 +08:00
|
|
|
// This file implements semantic analysis for non-trivial attributes and
|
|
|
|
// pragmas.
|
2009-02-17 08:57:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-26 06:03:47 +08:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2010-08-13 04:07:10 +08:00
|
|
|
#include "clang/Sema/Lookup.h"
|
2010-08-19 07:23:40 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2009-02-17 08:57:29 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2010-05-27 08:35:16 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-02-17 08:57:29 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-02-17 09:09:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-05-27 08:04:40 +08:00
|
|
|
// Pragma 'pack' and 'options align'
|
2009-02-17 09:09:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2010-05-27 10:25:27 +08:00
|
|
|
struct PackStackEntry {
|
2010-05-27 13:45:51 +08:00
|
|
|
// We just use a sentinel to represent when the stack is set to mac68k
|
|
|
|
// alignment.
|
|
|
|
static const unsigned kMac68kAlignmentSentinel = ~0U;
|
|
|
|
|
2010-05-27 10:25:27 +08:00
|
|
|
unsigned Alignment;
|
|
|
|
IdentifierInfo *Name;
|
|
|
|
};
|
|
|
|
|
2009-02-17 09:09:29 +08:00
|
|
|
/// PragmaPackStack - Simple class to wrap the stack used by #pragma
|
|
|
|
/// pack.
|
|
|
|
class PragmaPackStack {
|
2010-05-27 10:25:27 +08:00
|
|
|
typedef std::vector<PackStackEntry> stack_ty;
|
2009-02-17 09:09:29 +08:00
|
|
|
|
|
|
|
/// Alignment - The current user specified alignment.
|
|
|
|
unsigned Alignment;
|
|
|
|
|
|
|
|
/// Stack - Entries in the #pragma pack stack, consisting of saved
|
|
|
|
/// alignments and optional names.
|
|
|
|
stack_ty Stack;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
public:
|
2009-02-17 09:09:29 +08:00
|
|
|
PragmaPackStack() : Alignment(0) {}
|
|
|
|
|
|
|
|
void setAlignment(unsigned A) { Alignment = A; }
|
|
|
|
unsigned getAlignment() { return Alignment; }
|
|
|
|
|
|
|
|
/// push - Push the current alignment onto the stack, optionally
|
|
|
|
/// using the given \arg Name for the record, if non-zero.
|
|
|
|
void push(IdentifierInfo *Name) {
|
2010-05-27 10:25:27 +08:00
|
|
|
PackStackEntry PSE = { Alignment, Name };
|
|
|
|
Stack.push_back(PSE);
|
2009-02-17 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// pop - Pop a record from the stack and restore the current
|
|
|
|
/// alignment to the previous value. If \arg Name is non-zero then
|
|
|
|
/// the first such named record is popped, otherwise the top record
|
|
|
|
/// is popped. Returns true if the pop succeeded.
|
2010-07-16 12:54:16 +08:00
|
|
|
bool pop(IdentifierInfo *Name, bool IsReset);
|
2009-02-17 09:09:29 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2010-07-16 12:54:16 +08:00
|
|
|
bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
|
2009-02-17 09:09:29 +08:00
|
|
|
// If name is empty just pop top.
|
|
|
|
if (!Name) {
|
2010-07-16 12:54:16 +08:00
|
|
|
// An empty stack is a special case...
|
|
|
|
if (Stack.empty()) {
|
|
|
|
// If this isn't a reset, it is always an error.
|
|
|
|
if (!IsReset)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, it is an error only if some alignment has been set.
|
|
|
|
if (!Alignment)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, reset to the default alignment.
|
|
|
|
Alignment = 0;
|
|
|
|
} else {
|
|
|
|
Alignment = Stack.back().Alignment;
|
|
|
|
Stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2009-02-17 09:09:29 +08:00
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2009-02-17 09:09:29 +08:00
|
|
|
// Otherwise, find the named record.
|
|
|
|
for (unsigned i = Stack.size(); i != 0; ) {
|
|
|
|
--i;
|
2010-05-27 10:25:27 +08:00
|
|
|
if (Stack[i].Name == Name) {
|
2009-02-17 09:09:29 +08:00
|
|
|
// Found it, pop up to and including this record.
|
2010-05-27 10:25:27 +08:00
|
|
|
Alignment = Stack[i].Alignment;
|
2009-02-17 09:09:29 +08:00
|
|
|
Stack.erase(Stack.begin() + i, Stack.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-17 09:09:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// FreePackedContext - Deallocate and null out PackContext.
|
|
|
|
void Sema::FreePackedContext() {
|
|
|
|
delete static_cast<PragmaPackStack*>(PackContext);
|
|
|
|
PackContext = 0;
|
|
|
|
}
|
|
|
|
|
2010-05-27 09:53:40 +08:00
|
|
|
void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
|
|
|
|
// If there is no pack context, we don't need any attributes.
|
|
|
|
if (!PackContext)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext);
|
|
|
|
|
|
|
|
// Otherwise, check to see if we need a max field alignment attribute.
|
2010-05-27 13:45:51 +08:00
|
|
|
if (unsigned Alignment = Stack->getAlignment()) {
|
|
|
|
if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
|
2010-08-19 07:23:40 +08:00
|
|
|
RD->addAttr(::new (Context) AlignMac68kAttr(SourceLocation(), Context));
|
2010-05-27 13:45:51 +08:00
|
|
|
else
|
2010-08-19 07:23:40 +08:00
|
|
|
RD->addAttr(::new (Context) MaxFieldAlignmentAttr(SourceLocation(),
|
|
|
|
Context,
|
|
|
|
Alignment * 8));
|
2010-05-27 13:45:51 +08:00
|
|
|
}
|
2009-02-17 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
2010-05-27 08:04:40 +08:00
|
|
|
void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
|
|
|
|
SourceLocation PragmaLoc,
|
|
|
|
SourceLocation KindLoc) {
|
|
|
|
if (PackContext == 0)
|
|
|
|
PackContext = new PragmaPackStack();
|
|
|
|
|
|
|
|
PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
|
|
|
|
|
2010-07-16 12:54:16 +08:00
|
|
|
// Reset just pops the top of the stack, or resets the current alignment to
|
|
|
|
// default.
|
2010-08-27 07:41:50 +08:00
|
|
|
if (Kind == Sema::POAK_Reset) {
|
2010-07-16 12:54:16 +08:00
|
|
|
if (!Context->pop(0, /*IsReset=*/true)) {
|
2010-05-27 08:04:40 +08:00
|
|
|
Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
|
|
|
|
<< "stack empty";
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Kind) {
|
2010-05-28 02:42:09 +08:00
|
|
|
// For all targets we support native and natural are the same.
|
|
|
|
//
|
|
|
|
// FIXME: This is not true on Darwin/PPC.
|
|
|
|
case POAK_Native:
|
2010-05-29 03:43:33 +08:00
|
|
|
case POAK_Power:
|
2010-05-29 04:08:00 +08:00
|
|
|
case POAK_Natural:
|
2010-05-29 03:43:33 +08:00
|
|
|
Context->push(0);
|
|
|
|
Context->setAlignment(0);
|
|
|
|
break;
|
|
|
|
|
2010-05-28 02:42:17 +08:00
|
|
|
// Note that '#pragma options align=packed' is not equivalent to attribute
|
|
|
|
// packed, it has a different precedence relative to attribute aligned.
|
|
|
|
case POAK_Packed:
|
|
|
|
Context->push(0);
|
|
|
|
Context->setAlignment(1);
|
|
|
|
break;
|
|
|
|
|
2010-05-27 08:35:16 +08:00
|
|
|
case POAK_Mac68k:
|
|
|
|
// Check if the target supports this.
|
|
|
|
if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
|
|
|
|
Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
|
|
|
|
return;
|
|
|
|
}
|
2010-05-27 13:45:51 +08:00
|
|
|
Context->push(0);
|
|
|
|
Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
|
2010-05-27 08:35:16 +08:00
|
|
|
break;
|
|
|
|
|
2010-05-27 08:04:40 +08:00
|
|
|
default:
|
|
|
|
Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option)
|
|
|
|
<< KindLoc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
|
|
|
|
ExprTy *alignment, SourceLocation PragmaLoc,
|
2009-02-17 08:57:29 +08:00
|
|
|
SourceLocation LParenLoc, SourceLocation RParenLoc) {
|
|
|
|
Expr *Alignment = static_cast<Expr *>(alignment);
|
|
|
|
|
|
|
|
// If specified then alignment must be a "small" power of two.
|
|
|
|
unsigned AlignmentVal = 0;
|
|
|
|
if (Alignment) {
|
|
|
|
llvm::APSInt Val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-07 04:45:54 +08:00
|
|
|
// pack(0) is like pack(), which just works out since that is what
|
|
|
|
// we use 0 for in PackAttr.
|
2010-05-19 07:01:22 +08:00
|
|
|
if (Alignment->isTypeDependent() ||
|
|
|
|
Alignment->isValueDependent() ||
|
|
|
|
!Alignment->isIntegerConstantExpr(Val, Context) ||
|
2009-03-07 04:45:54 +08:00
|
|
|
!(Val == 0 || Val.isPowerOf2()) ||
|
2009-02-17 08:57:29 +08:00
|
|
|
Val.getZExtValue() > 16) {
|
|
|
|
Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
|
|
|
|
return; // Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
AlignmentVal = (unsigned) Val.getZExtValue();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-17 09:09:29 +08:00
|
|
|
if (PackContext == 0)
|
|
|
|
PackContext = new PragmaPackStack();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-17 09:09:29 +08:00
|
|
|
PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-17 08:57:29 +08:00
|
|
|
switch (Kind) {
|
2010-08-27 07:41:50 +08:00
|
|
|
case Sema::PPK_Default: // pack([n])
|
2009-02-17 09:09:29 +08:00
|
|
|
Context->setAlignment(AlignmentVal);
|
2009-02-17 08:57:29 +08:00
|
|
|
break;
|
|
|
|
|
2010-08-27 07:41:50 +08:00
|
|
|
case Sema::PPK_Show: // pack(show)
|
2009-02-17 08:57:29 +08:00
|
|
|
// Show the current alignment, making sure to show the right value
|
|
|
|
// for the default.
|
2009-02-17 09:09:29 +08:00
|
|
|
AlignmentVal = Context->getAlignment();
|
2009-02-17 08:57:29 +08:00
|
|
|
// FIXME: This should come from the target.
|
|
|
|
if (AlignmentVal == 0)
|
|
|
|
AlignmentVal = 8;
|
2010-05-27 13:45:51 +08:00
|
|
|
if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
|
|
|
|
Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
|
|
|
|
else
|
|
|
|
Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
|
2009-02-17 08:57:29 +08:00
|
|
|
break;
|
|
|
|
|
2010-08-27 07:41:50 +08:00
|
|
|
case Sema::PPK_Push: // pack(push [, id] [, [n])
|
2009-02-17 09:09:29 +08:00
|
|
|
Context->push(Name);
|
2009-02-17 08:57:29 +08:00
|
|
|
// Set the new alignment if specified.
|
|
|
|
if (Alignment)
|
2009-09-09 23:08:12 +08:00
|
|
|
Context->setAlignment(AlignmentVal);
|
2009-02-17 08:57:29 +08:00
|
|
|
break;
|
|
|
|
|
2010-08-27 07:41:50 +08:00
|
|
|
case Sema::PPK_Pop: // pack(pop [, id] [, n])
|
2009-02-17 08:57:29 +08:00
|
|
|
// MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
|
|
|
|
// "#pragma pack(pop, identifier, n) is undefined"
|
|
|
|
if (Alignment && Name)
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
|
|
|
|
|
2009-02-17 08:57:29 +08:00
|
|
|
// Do the pop.
|
2010-07-16 12:54:16 +08:00
|
|
|
if (!Context->pop(Name, /*IsReset=*/false)) {
|
2009-02-17 08:57:29 +08:00
|
|
|
// If a name was specified then failure indicates the name
|
|
|
|
// wasn't found. Otherwise failure indicates the stack was
|
|
|
|
// empty.
|
|
|
|
Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
|
|
|
|
<< (Name ? "no record matching name" : "stack empty");
|
|
|
|
|
|
|
|
// FIXME: Warn about popping named records as MSVC does.
|
|
|
|
} else {
|
|
|
|
// Pop succeeded, set the new alignment if specified.
|
|
|
|
if (Alignment)
|
2009-02-17 09:09:29 +08:00
|
|
|
Context->setAlignment(AlignmentVal);
|
2009-02-17 08:57:29 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0 && "Invalid #pragma pack kind.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-04 07:24:57 +08:00
|
|
|
void Sema::ActOnPragmaUnused(const Token *Identifiers, unsigned NumIdentifiers,
|
|
|
|
Scope *curScope,
|
2009-03-24 06:28:25 +08:00
|
|
|
SourceLocation PragmaLoc,
|
|
|
|
SourceLocation LParenLoc,
|
|
|
|
SourceLocation RParenLoc) {
|
|
|
|
|
2009-08-04 07:24:57 +08:00
|
|
|
for (unsigned i = 0; i < NumIdentifiers; ++i) {
|
|
|
|
const Token &Tok = Identifiers[i];
|
2009-09-09 23:08:12 +08:00
|
|
|
IdentifierInfo *Name = Tok.getIdentifierInfo();
|
2009-11-17 10:14:36 +08:00
|
|
|
LookupResult Lookup(*this, Name, Tok.getLocation(), LookupOrdinaryName);
|
|
|
|
LookupParsedName(Lookup, curScope, NULL, true);
|
2009-08-04 07:24:57 +08:00
|
|
|
|
2009-12-02 16:25:40 +08:00
|
|
|
if (Lookup.empty()) {
|
2009-08-04 07:24:57 +08:00
|
|
|
Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
|
|
|
|
<< Name << SourceRange(Tok.getLocation());
|
|
|
|
continue;
|
2009-03-24 06:28:25 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-02 16:25:40 +08:00
|
|
|
VarDecl *VD = Lookup.getAsSingle<VarDecl>();
|
|
|
|
if (!VD || !VD->hasLocalStorage()) {
|
2009-08-04 07:24:57 +08:00
|
|
|
Diag(PragmaLoc, diag::warn_pragma_unused_expected_localvar)
|
|
|
|
<< Name << SourceRange(Tok.getLocation());
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
VD->addAttr(::new (Context) UnusedAttr(Tok.getLocation(), Context));
|
2009-03-24 06:28:25 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-05 14:57:20 +08:00
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
typedef std::vector<std::pair<VisibilityAttr::VisibilityType,
|
|
|
|
SourceLocation> > VisStack;
|
2010-08-05 14:57:20 +08:00
|
|
|
|
|
|
|
void Sema::AddPushedVisibilityAttribute(Decl *D) {
|
|
|
|
if (!VisContext)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (D->hasAttr<VisibilityAttr>())
|
|
|
|
return;
|
|
|
|
|
|
|
|
VisStack *Stack = static_cast<VisStack*>(VisContext);
|
2010-08-19 07:23:40 +08:00
|
|
|
VisibilityAttr::VisibilityType type = Stack->back().first;
|
|
|
|
SourceLocation loc = Stack->back().second;
|
2010-08-05 14:57:20 +08:00
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
D->addAttr(::new (Context) VisibilityAttr(loc, Context, type));
|
2010-08-05 14:57:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// FreeVisContext - Deallocate and null out VisContext.
|
|
|
|
void Sema::FreeVisContext() {
|
|
|
|
delete static_cast<VisStack*>(VisContext);
|
|
|
|
VisContext = 0;
|
|
|
|
}
|
|
|
|
|
2010-08-26 17:15:37 +08:00
|
|
|
static void PushPragmaVisibility(Sema &S, VisibilityAttr::VisibilityType type,
|
|
|
|
SourceLocation loc) {
|
|
|
|
// Put visibility on stack.
|
|
|
|
if (!S.VisContext)
|
|
|
|
S.VisContext = new VisStack;
|
|
|
|
|
|
|
|
VisStack *Stack = static_cast<VisStack*>(S.VisContext);
|
|
|
|
Stack->push_back(std::make_pair(type, loc));
|
|
|
|
}
|
|
|
|
|
2010-08-05 14:57:20 +08:00
|
|
|
void Sema::ActOnPragmaVisibility(bool IsPush, const IdentifierInfo* VisType,
|
|
|
|
SourceLocation PragmaLoc) {
|
|
|
|
if (IsPush) {
|
|
|
|
// Compute visibility to use.
|
2010-08-19 07:23:40 +08:00
|
|
|
VisibilityAttr::VisibilityType type;
|
2010-08-05 14:57:20 +08:00
|
|
|
if (VisType->isStr("default"))
|
2010-08-19 07:23:40 +08:00
|
|
|
type = VisibilityAttr::Default;
|
2010-08-05 14:57:20 +08:00
|
|
|
else if (VisType->isStr("hidden"))
|
2010-08-19 07:23:40 +08:00
|
|
|
type = VisibilityAttr::Hidden;
|
2010-08-05 14:57:20 +08:00
|
|
|
else if (VisType->isStr("internal"))
|
2010-08-19 07:23:40 +08:00
|
|
|
type = VisibilityAttr::Hidden; // FIXME
|
2010-08-05 14:57:20 +08:00
|
|
|
else if (VisType->isStr("protected"))
|
2010-08-19 07:23:40 +08:00
|
|
|
type = VisibilityAttr::Protected;
|
2010-08-05 14:57:20 +08:00
|
|
|
else {
|
|
|
|
Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) <<
|
|
|
|
VisType->getName();
|
|
|
|
return;
|
|
|
|
}
|
2010-08-26 17:15:37 +08:00
|
|
|
PushPragmaVisibility(*this, type, PragmaLoc);
|
2010-08-05 14:57:20 +08:00
|
|
|
} else {
|
|
|
|
PopPragmaVisibility();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-26 17:15:37 +08:00
|
|
|
void Sema::PushVisibilityAttr(const VisibilityAttr *Attr) {
|
|
|
|
PushPragmaVisibility(*this, Attr->getVisibility(), Attr->getLocation());
|
2010-08-05 14:57:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::PopPragmaVisibility() {
|
|
|
|
// Pop visibility from stack, if there is one on the stack.
|
|
|
|
if (VisContext) {
|
|
|
|
VisStack *Stack = static_cast<VisStack*>(VisContext);
|
|
|
|
|
|
|
|
Stack->pop_back();
|
|
|
|
// To simplify the implementation, never keep around an empty stack.
|
|
|
|
if (Stack->empty())
|
|
|
|
FreeVisContext();
|
|
|
|
}
|
|
|
|
// FIXME: Add diag for pop without push.
|
|
|
|
}
|