2012-12-21 03:22:21 +08:00
|
|
|
//===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
|
2009-02-17 08:57:29 +08:00
|
|
|
//
|
|
|
|
// 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"
|
2013-05-08 21:44:39 +08:00
|
|
|
#include "clang/AST/ASTConsumer.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"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Sema/Lookup.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);
|
2014-05-26 14:22:03 +08:00
|
|
|
PackContext = nullptr;
|
2009-02-17 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
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)
|
2014-01-16 21:03:14 +08:00
|
|
|
RD->addAttr(AlignMac68kAttr::CreateImplicit(Context));
|
2010-05-27 13:45:51 +08:00
|
|
|
else
|
2014-01-16 21:03:14 +08:00
|
|
|
RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context,
|
2010-08-19 07:23:40 +08:00
|
|
|
Alignment * 8));
|
2010-05-27 13:45:51 +08:00
|
|
|
}
|
2009-02-17 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
2011-04-27 01:54:40 +08:00
|
|
|
void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
|
2014-02-13 07:50:26 +08:00
|
|
|
if (MSStructPragmaOn)
|
2015-02-03 03:30:52 +08:00
|
|
|
RD->addAttr(MSStructAttr::CreateImplicit(Context));
|
2014-02-13 07:50:26 +08:00
|
|
|
|
|
|
|
// FIXME: We should merge AddAlignmentAttributesForRecord with
|
|
|
|
// AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes
|
|
|
|
// all active pragmas and applies them as attributes to class definitions.
|
|
|
|
if (VtorDispModeStack.back() != getLangOpts().VtorDispMode)
|
|
|
|
RD->addAttr(
|
|
|
|
MSVtorDispAttr::CreateImplicit(Context, VtorDispModeStack.back()));
|
2011-04-27 01:54:40 +08:00
|
|
|
}
|
|
|
|
|
2010-05-27 08:04:40 +08:00
|
|
|
void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
|
2012-10-04 10:36:51 +08:00
|
|
|
SourceLocation PragmaLoc) {
|
2014-05-26 14:22:03 +08:00
|
|
|
if (!PackContext)
|
2010-05-27 08:04:40 +08:00
|
|
|
PackContext = new PragmaPackStack();
|
|
|
|
|
|
|
|
PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
|
|
|
|
|
|
|
|
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:
|
2014-05-26 14:22:03 +08:00
|
|
|
Context->push(nullptr);
|
2010-05-29 03:43:33 +08:00
|
|
|
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:
|
2014-05-26 14:22:03 +08:00
|
|
|
Context->push(nullptr);
|
2010-05-28 02:42:17 +08:00
|
|
|
Context->setAlignment(1);
|
|
|
|
break;
|
|
|
|
|
2010-05-27 08:35:16 +08:00
|
|
|
case POAK_Mac68k:
|
|
|
|
// Check if the target supports this.
|
2014-05-03 11:45:55 +08:00
|
|
|
if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) {
|
2010-05-27 08:35:16 +08:00
|
|
|
Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
|
|
|
|
return;
|
|
|
|
}
|
2014-05-26 14:22:03 +08:00
|
|
|
Context->push(nullptr);
|
2010-05-27 13:45:51 +08:00
|
|
|
Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
|
2010-05-27 08:35:16 +08:00
|
|
|
break;
|
|
|
|
|
2012-10-04 10:36:51 +08:00
|
|
|
case POAK_Reset:
|
|
|
|
// Reset just pops the top of the stack, or resets the current alignment to
|
|
|
|
// default.
|
2014-05-26 14:22:03 +08:00
|
|
|
if (!Context->pop(nullptr, /*IsReset=*/true)) {
|
2012-10-04 10:36:51 +08:00
|
|
|
Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
|
|
|
|
<< "stack empty";
|
|
|
|
}
|
2010-05-27 08:04:40 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
|
2011-09-09 10:00:50 +08:00
|
|
|
Expr *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
|
|
|
|
2014-05-26 14:22:03 +08:00
|
|
|
if (!PackContext)
|
2009-02-17 09:09:29 +08:00
|
|
|
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.
|
2014-02-13 07:50:26 +08:00
|
|
|
Diag(PragmaLoc, diag::warn_pragma_pop_failed)
|
|
|
|
<< "pack" << (Name ? "no record matching name" : "stack empty");
|
2009-02-17 08:57:29 +08:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-26 02:49:15 +08:00
|
|
|
void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
|
|
|
|
MSStructPragmaOn = (Kind == PMSST_ON);
|
|
|
|
}
|
|
|
|
|
2016-03-03 01:28:48 +08:00
|
|
|
void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc,
|
|
|
|
PragmaMSCommentKind Kind, StringRef Arg) {
|
|
|
|
auto *PCD = PragmaCommentDecl::Create(
|
|
|
|
Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg);
|
|
|
|
Context.getTranslationUnitDecl()->addDecl(PCD);
|
|
|
|
Consumer.HandleTopLevelDecl(DeclGroupRef(PCD));
|
2013-05-08 21:44:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-10 21:29:01 +08:00
|
|
|
void Sema::ActOnPragmaDetectMismatch(StringRef Name, StringRef Value) {
|
2013-06-04 10:07:14 +08:00
|
|
|
// FIXME: Serialize this.
|
|
|
|
Consumer.HandleDetectMismatch(Name, Value);
|
|
|
|
}
|
|
|
|
|
2014-02-11 03:50:15 +08:00
|
|
|
void Sema::ActOnPragmaMSPointersToMembers(
|
2014-02-12 05:05:00 +08:00
|
|
|
LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,
|
2014-02-11 03:50:15 +08:00
|
|
|
SourceLocation PragmaLoc) {
|
|
|
|
MSPointerToMemberRepresentationMethod = RepresentationMethod;
|
|
|
|
ImplicitMSInheritanceAttrLoc = PragmaLoc;
|
|
|
|
}
|
|
|
|
|
2014-02-13 07:50:26 +08:00
|
|
|
void Sema::ActOnPragmaMSVtorDisp(PragmaVtorDispKind Kind,
|
|
|
|
SourceLocation PragmaLoc,
|
|
|
|
MSVtorDispAttr::Mode Mode) {
|
|
|
|
switch (Kind) {
|
|
|
|
case PVDK_Set:
|
|
|
|
VtorDispModeStack.back() = Mode;
|
|
|
|
break;
|
|
|
|
case PVDK_Push:
|
|
|
|
VtorDispModeStack.push_back(Mode);
|
|
|
|
break;
|
|
|
|
case PVDK_Reset:
|
|
|
|
VtorDispModeStack.clear();
|
|
|
|
VtorDispModeStack.push_back(MSVtorDispAttr::Mode(LangOpts.VtorDispMode));
|
|
|
|
break;
|
|
|
|
case PVDK_Pop:
|
|
|
|
VtorDispModeStack.pop_back();
|
|
|
|
if (VtorDispModeStack.empty()) {
|
|
|
|
Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
|
|
|
|
<< "stack empty";
|
|
|
|
VtorDispModeStack.push_back(MSVtorDispAttr::Mode(LangOpts.VtorDispMode));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 06:30:47 +08:00
|
|
|
template<typename ValueType>
|
|
|
|
void Sema::PragmaStack<ValueType>::Act(SourceLocation PragmaLocation,
|
|
|
|
PragmaMsStackAction Action,
|
|
|
|
llvm::StringRef StackSlotLabel,
|
|
|
|
ValueType Value) {
|
|
|
|
if (Action == PSK_Reset) {
|
|
|
|
CurrentValue = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Action & PSK_Push)
|
|
|
|
Stack.push_back(Slot(StackSlotLabel, CurrentValue, CurrentPragmaLocation));
|
|
|
|
else if (Action & PSK_Pop) {
|
|
|
|
if (!StackSlotLabel.empty()) {
|
|
|
|
// If we've got a label, try to find it and jump there.
|
|
|
|
auto I = std::find_if(Stack.rbegin(), Stack.rend(),
|
|
|
|
[&](const Slot &x) { return x.StackSlotLabel == StackSlotLabel; });
|
|
|
|
// If we found the label so pop from there.
|
|
|
|
if (I != Stack.rend()) {
|
|
|
|
CurrentValue = I->Value;
|
|
|
|
CurrentPragmaLocation = I->PragmaLocation;
|
|
|
|
Stack.erase(std::prev(I.base()), Stack.end());
|
|
|
|
}
|
|
|
|
} else if (!Stack.empty()) {
|
|
|
|
// We don't have a label, just pop the last entry.
|
|
|
|
CurrentValue = Stack.back().Value;
|
|
|
|
CurrentPragmaLocation = Stack.back().PragmaLocation;
|
|
|
|
Stack.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Action & PSK_Set) {
|
|
|
|
CurrentValue = Value;
|
|
|
|
CurrentPragmaLocation = PragmaLocation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-31 00:55:52 +08:00
|
|
|
bool Sema::UnifySection(StringRef SectionName,
|
2014-04-09 06:30:47 +08:00
|
|
|
int SectionFlags,
|
|
|
|
DeclaratorDecl *Decl) {
|
2014-10-17 04:52:46 +08:00
|
|
|
auto Section = Context.SectionInfos.find(SectionName);
|
|
|
|
if (Section == Context.SectionInfos.end()) {
|
|
|
|
Context.SectionInfos[SectionName] =
|
|
|
|
ASTContext::SectionInfo(Decl, SourceLocation(), SectionFlags);
|
2014-04-09 06:30:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// A pre-declared section takes precedence w/o diagnostic.
|
|
|
|
if (Section->second.SectionFlags == SectionFlags ||
|
2014-10-17 04:52:46 +08:00
|
|
|
!(Section->second.SectionFlags & ASTContext::PSF_Implicit))
|
2014-04-09 06:30:47 +08:00
|
|
|
return false;
|
|
|
|
auto OtherDecl = Section->second.Decl;
|
|
|
|
Diag(Decl->getLocation(), diag::err_section_conflict)
|
|
|
|
<< Decl << OtherDecl;
|
|
|
|
Diag(OtherDecl->getLocation(), diag::note_declared_at)
|
|
|
|
<< OtherDecl->getName();
|
|
|
|
if (auto A = Decl->getAttr<SectionAttr>())
|
|
|
|
if (A->isImplicit())
|
|
|
|
Diag(A->getLocation(), diag::note_pragma_entered_here);
|
|
|
|
if (auto A = OtherDecl->getAttr<SectionAttr>())
|
|
|
|
if (A->isImplicit())
|
|
|
|
Diag(A->getLocation(), diag::note_pragma_entered_here);
|
2014-09-23 03:46:39 +08:00
|
|
|
return true;
|
2014-04-09 06:30:47 +08:00
|
|
|
}
|
|
|
|
|
2014-08-31 00:55:52 +08:00
|
|
|
bool Sema::UnifySection(StringRef SectionName,
|
2014-04-09 06:30:47 +08:00
|
|
|
int SectionFlags,
|
|
|
|
SourceLocation PragmaSectionLocation) {
|
2014-10-17 04:52:46 +08:00
|
|
|
auto Section = Context.SectionInfos.find(SectionName);
|
|
|
|
if (Section != Context.SectionInfos.end()) {
|
2014-04-09 06:30:47 +08:00
|
|
|
if (Section->second.SectionFlags == SectionFlags)
|
|
|
|
return false;
|
2014-10-17 04:52:46 +08:00
|
|
|
if (!(Section->second.SectionFlags & ASTContext::PSF_Implicit)) {
|
2014-04-09 06:30:47 +08:00
|
|
|
Diag(PragmaSectionLocation, diag::err_section_conflict)
|
|
|
|
<< "this" << "a prior #pragma section";
|
|
|
|
Diag(Section->second.PragmaSectionLocation,
|
|
|
|
diag::note_pragma_entered_here);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-10-17 04:52:46 +08:00
|
|
|
Context.SectionInfos[SectionName] =
|
|
|
|
ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags);
|
2014-04-09 06:30:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Called on well formed \#pragma bss_seg().
|
|
|
|
void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation,
|
|
|
|
PragmaMsStackAction Action,
|
|
|
|
llvm::StringRef StackSlotLabel,
|
|
|
|
StringLiteral *SegmentName,
|
|
|
|
llvm::StringRef PragmaName) {
|
|
|
|
PragmaStack<StringLiteral *> *Stack =
|
|
|
|
llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName)
|
|
|
|
.Case("data_seg", &DataSegStack)
|
|
|
|
.Case("bss_seg", &BSSSegStack)
|
|
|
|
.Case("const_seg", &ConstSegStack)
|
|
|
|
.Case("code_seg", &CodeSegStack);
|
|
|
|
if (Action & PSK_Pop && Stack->Stack.empty())
|
|
|
|
Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName
|
|
|
|
<< "stack empty";
|
2015-03-05 07:39:17 +08:00
|
|
|
if (SegmentName &&
|
|
|
|
!checkSectionName(SegmentName->getLocStart(), SegmentName->getString()))
|
|
|
|
return;
|
2014-04-09 06:30:47 +08:00
|
|
|
Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Called on well formed \#pragma bss_seg().
|
|
|
|
void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation,
|
|
|
|
int SectionFlags, StringLiteral *SegmentName) {
|
|
|
|
UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation);
|
|
|
|
}
|
|
|
|
|
2014-07-22 08:53:05 +08:00
|
|
|
void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,
|
|
|
|
StringLiteral *SegmentName) {
|
|
|
|
// There's no stack to maintain, so we just have a current section. When we
|
|
|
|
// see the default section, reset our current section back to null so we stop
|
|
|
|
// tacking on unnecessary attributes.
|
|
|
|
CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName;
|
|
|
|
CurInitSegLoc = PragmaLocation;
|
|
|
|
}
|
|
|
|
|
2011-01-18 02:58:44 +08:00
|
|
|
void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
|
|
|
|
SourceLocation PragmaLoc) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-01-18 02:58:44 +08:00
|
|
|
IdentifierInfo *Name = IdTok.getIdentifierInfo();
|
|
|
|
LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
|
2014-05-26 14:22:03 +08:00
|
|
|
LookupParsedName(Lookup, curScope, nullptr, true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-01-18 02:58:44 +08:00
|
|
|
if (Lookup.empty()) {
|
|
|
|
Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
|
|
|
|
<< Name << SourceRange(IdTok.getLocation());
|
|
|
|
return;
|
|
|
|
}
|
2010-10-23 07:37:08 +08:00
|
|
|
|
2011-01-18 02:58:44 +08:00
|
|
|
VarDecl *VD = Lookup.getAsSingle<VarDecl>();
|
2011-01-28 02:16:48 +08:00
|
|
|
if (!VD) {
|
|
|
|
Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
|
2011-01-18 02:58:44 +08:00
|
|
|
<< Name << SourceRange(IdTok.getLocation());
|
|
|
|
return;
|
2009-03-24 06:28:25 +08:00
|
|
|
}
|
2011-01-18 02:58:44 +08:00
|
|
|
|
|
|
|
// Warn if this was used before being marked unused.
|
|
|
|
if (VD->isUsed())
|
|
|
|
Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
|
|
|
|
|
2014-01-16 21:03:14 +08:00
|
|
|
VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation()));
|
2009-03-24 06:28:25 +08:00
|
|
|
}
|
2010-08-05 14:57:20 +08:00
|
|
|
|
2011-09-30 13:12:12 +08:00
|
|
|
void Sema::AddCFAuditedAttribute(Decl *D) {
|
|
|
|
SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
|
|
|
|
if (!Loc.isValid()) return;
|
|
|
|
|
|
|
|
// Don't add a redundant or conflicting attribute.
|
|
|
|
if (D->hasAttr<CFAuditedTransferAttr>() ||
|
|
|
|
D->hasAttr<CFUnknownTransferAttr>())
|
|
|
|
return;
|
|
|
|
|
2014-01-16 21:03:14 +08:00
|
|
|
D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc));
|
2011-09-30 13:12:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-23 20:13:25 +08:00
|
|
|
void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) {
|
|
|
|
if(On)
|
|
|
|
OptimizeOffPragmaLocation = SourceLocation();
|
|
|
|
else
|
|
|
|
OptimizeOffPragmaLocation = PragmaLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::AddRangeBasedOptnone(FunctionDecl *FD) {
|
|
|
|
// In the future, check other pragmas if they're implemented (e.g. pragma
|
|
|
|
// optimize 0 will probably map to this functionality too).
|
|
|
|
if(OptimizeOffPragmaLocation.isValid())
|
|
|
|
AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
// Don't add a conflicting attribute. No diagnostic is needed.
|
|
|
|
if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Add attributes only if required. Optnone requires noinline as well, but if
|
|
|
|
// either is already present then don't bother adding them.
|
|
|
|
if (!FD->hasAttr<OptimizeNoneAttr>())
|
|
|
|
FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc));
|
|
|
|
if (!FD->hasAttr<NoInlineAttr>())
|
|
|
|
FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc));
|
|
|
|
}
|
|
|
|
|
2010-12-10 10:59:44 +08:00
|
|
|
typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
|
2014-03-02 11:20:16 +08:00
|
|
|
enum : unsigned { NoVisibility = ~0U };
|
2010-08-05 14:57:20 +08:00
|
|
|
|
|
|
|
void Sema::AddPushedVisibilityAttribute(Decl *D) {
|
|
|
|
if (!VisContext)
|
|
|
|
return;
|
|
|
|
|
2012-12-25 15:31:49 +08:00
|
|
|
NamedDecl *ND = dyn_cast<NamedDecl>(D);
|
2013-02-20 09:54:26 +08:00
|
|
|
if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
|
2010-08-05 14:57:20 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
VisStack *Stack = static_cast<VisStack*>(VisContext);
|
2010-12-10 10:59:44 +08:00
|
|
|
unsigned rawType = Stack->back().first;
|
|
|
|
if (rawType == NoVisibility) return;
|
|
|
|
|
|
|
|
VisibilityAttr::VisibilityType type
|
|
|
|
= (VisibilityAttr::VisibilityType) rawType;
|
2010-08-19 07:23:40 +08:00
|
|
|
SourceLocation loc = Stack->back().second;
|
2010-08-05 14:57:20 +08:00
|
|
|
|
2014-01-16 21:03:14 +08:00
|
|
|
D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
|
2010-08-05 14:57:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// FreeVisContext - Deallocate and null out VisContext.
|
|
|
|
void Sema::FreeVisContext() {
|
|
|
|
delete static_cast<VisStack*>(VisContext);
|
2014-05-26 14:22:03 +08:00
|
|
|
VisContext = nullptr;
|
2010-08-05 14:57:20 +08:00
|
|
|
}
|
|
|
|
|
2010-12-10 10:59:44 +08:00
|
|
|
static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
|
2010-08-26 17:15:37 +08:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
2012-01-21 13:43:40 +08:00
|
|
|
void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
|
2010-08-05 14:57:20 +08:00
|
|
|
SourceLocation PragmaLoc) {
|
2012-01-21 13:43:40 +08:00
|
|
|
if (VisType) {
|
2010-08-05 14:57:20 +08:00
|
|
|
// Compute visibility to use.
|
2013-09-12 03:47:58 +08:00
|
|
|
VisibilityAttr::VisibilityType T;
|
|
|
|
if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
|
|
|
|
Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
|
2010-08-05 14:57:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-12 03:47:58 +08:00
|
|
|
PushPragmaVisibility(*this, T, PragmaLoc);
|
2010-08-05 14:57:20 +08:00
|
|
|
} else {
|
2012-02-02 07:24:59 +08:00
|
|
|
PopPragmaVisibility(false, PragmaLoc);
|
2010-08-05 14:57:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 09:42:35 +08:00
|
|
|
void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
|
|
|
|
switch (OOS) {
|
|
|
|
case tok::OOS_ON:
|
|
|
|
FPFeatures.fp_contract = 1;
|
|
|
|
break;
|
|
|
|
case tok::OOS_OFF:
|
|
|
|
FPFeatures.fp_contract = 0;
|
|
|
|
break;
|
|
|
|
case tok::OOS_DEFAULT:
|
2012-03-11 15:00:24 +08:00
|
|
|
FPFeatures.fp_contract = getLangOpts().DefaultFPContract;
|
2011-02-14 09:42:35 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 07:24:59 +08:00
|
|
|
void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
|
|
|
|
SourceLocation Loc) {
|
2010-12-10 10:59:44 +08:00
|
|
|
// Visibility calculations will consider the namespace's visibility.
|
|
|
|
// Here we just want to note that we're in a visibility context
|
|
|
|
// which overrides any enclosing #pragma context, but doesn't itself
|
|
|
|
// contribute visibility.
|
2012-02-02 07:24:59 +08:00
|
|
|
PushPragmaVisibility(*this, NoVisibility, Loc);
|
2010-08-05 14:57:20 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 07:24:59 +08:00
|
|
|
void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
|
|
|
|
if (!VisContext) {
|
|
|
|
Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop visibility from stack
|
|
|
|
VisStack *Stack = static_cast<VisStack*>(VisContext);
|
2010-08-05 14:57:20 +08:00
|
|
|
|
2012-02-02 07:24:59 +08:00
|
|
|
const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
|
|
|
|
bool StartsWithPragma = Back->first != NoVisibility;
|
|
|
|
if (StartsWithPragma && IsNamespaceEnd) {
|
|
|
|
Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
|
|
|
|
Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
|
|
|
|
|
|
|
|
// For better error recovery, eat all pushes inside the namespace.
|
|
|
|
do {
|
|
|
|
Stack->pop_back();
|
|
|
|
Back = &Stack->back();
|
|
|
|
StartsWithPragma = Back->first != NoVisibility;
|
|
|
|
} while (StartsWithPragma);
|
|
|
|
} else if (!StartsWithPragma && !IsNamespaceEnd) {
|
|
|
|
Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
|
|
|
|
Diag(Back->second, diag::note_surrounding_namespace_starts_here);
|
|
|
|
return;
|
2010-08-05 14:57:20 +08:00
|
|
|
}
|
2012-02-02 07:24:59 +08:00
|
|
|
|
|
|
|
Stack->pop_back();
|
|
|
|
// To simplify the implementation, never keep around an empty stack.
|
|
|
|
if (Stack->empty())
|
|
|
|
FreeVisContext();
|
2010-08-05 14:57:20 +08:00
|
|
|
}
|