Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 8e07a07

Browse files
author
Denis Zobnin
committed
[MS] Make #pragma pack use PragmaStack<> class.
Make implementation of #pragma pack consistent with other "stack" pragmas. Use PragmaStack<> class instead of old representation of internal stack. Don't change compiler's behavior. TODO: 1. Introduce diagnostics on popping named slots from pragma stacks. Reviewer: rnk Differential revision: http://reviews.llvm.org/D19727 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@268085 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 9d575dd commit 8e07a07

File tree

4 files changed

+66
-194
lines changed

4 files changed

+66
-194
lines changed

include/clang/Sema/Sema.h

+8-18
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,6 @@ class Sema {
317317
/// This is used as part of a hack to omit that class from ADL results.
318318
DeclarationName VAListTagName;
319319

320-
/// PackContext - Manages the stack for \#pragma pack. An alignment
321-
/// of 0 indicates default alignment.
322-
void *PackContext; // Really a "PragmaPackStack*"
323-
324320
bool MSStructPragmaOn; // True when \#pragma ms_struct on
325321

326322
/// \brief Controls member pointer representation format under the MS ABI.
@@ -338,6 +334,7 @@ class Sema {
338334
PSK_Set = 0x1, // #pragma (value)
339335
PSK_Push = 0x2, // #pragma (push[, id])
340336
PSK_Pop = 0x4, // #pragma (pop[, id])
337+
PSK_Show = 0x8, // #pragma (show) -- only for "pack"!
341338
PSK_Push_Set = PSK_Push | PSK_Set, // #pragma (push[, id], value)
342339
PSK_Pop_Set = PSK_Pop | PSK_Set, // #pragma (pop[, id], value)
343340
};
@@ -400,11 +397,15 @@ class Sema {
400397
/// 2: Always insert vtordisps to support RTTI on partially constructed
401398
/// objects
402399
PragmaStack<MSVtorDispAttr::Mode> VtorDispStack;
400+
// #pragma pack.
401+
// Sentinel to represent when the stack is set to mac68k alignment.
402+
static const unsigned kMac68kAlignmentSentinel = ~0U;
403+
PragmaStack<unsigned> PackStack;
404+
// Segment #pragmas.
403405
PragmaStack<StringLiteral *> DataSegStack;
404406
PragmaStack<StringLiteral *> BSSSegStack;
405407
PragmaStack<StringLiteral *> ConstSegStack;
406408
PragmaStack<StringLiteral *> CodeSegStack;
407-
// TODO: Change implementation of #pragma pack to use PragmaStack<> approach.
408409

409410
// RAII object to push / pop sentinel slots for all MS #pragma stacks.
410411
// Actions should be performed only if we enter / exit a C++ method body.
@@ -7658,20 +7659,9 @@ class Sema {
76587659
void ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
76597660
SourceLocation PragmaLoc);
76607661

7661-
enum PragmaPackKind {
7662-
PPK_Default, // #pragma pack([n])
7663-
PPK_Show, // #pragma pack(show), only supported by MSVC.
7664-
PPK_Push, // #pragma pack(push, [identifier], [n])
7665-
PPK_Pop // #pragma pack(pop, [identifier], [n])
7666-
};
7667-
76687662
/// ActOnPragmaPack - Called on well formed \#pragma pack(...).
7669-
void ActOnPragmaPack(PragmaPackKind Kind,
7670-
IdentifierInfo *Name,
7671-
Expr *Alignment,
7672-
SourceLocation PragmaLoc,
7673-
SourceLocation LParenLoc,
7674-
SourceLocation RParenLoc);
7663+
void ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action,
7664+
StringRef SlotLabel, Expr *Alignment);
76757665

76767666
/// ActOnPragmaMSStruct - Called on well formed \#pragma ms_struct [on|off].
76777667
void ActOnPragmaMSStruct(PragmaMSStructKind Kind);

lib/Parse/ParsePragma.cpp

+17-20
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,9 @@ void Parser::HandlePragmaVisibility() {
337337

338338
namespace {
339339
struct PragmaPackInfo {
340-
Sema::PragmaPackKind Kind;
341-
IdentifierInfo *Name;
340+
Sema::PragmaMsStackAction Action;
341+
StringRef SlotLabel;
342342
Token Alignment;
343-
SourceLocation LParenLoc;
344-
SourceLocation RParenLoc;
345343
};
346344
} // end anonymous namespace
347345

@@ -356,8 +354,8 @@ void Parser::HandlePragmaPack() {
356354
if (Alignment.isInvalid())
357355
return;
358356
}
359-
Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
360-
Info->LParenLoc, Info->RParenLoc);
357+
Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
358+
Alignment.get());
361359
}
362360

363361
void Parser::HandlePragmaMSStruct() {
@@ -962,11 +960,10 @@ void PragmaPackHandler::HandlePragma(Preprocessor &PP,
962960
return;
963961
}
964962

965-
Sema::PragmaPackKind Kind = Sema::PPK_Default;
966-
IdentifierInfo *Name = nullptr;
963+
Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
964+
StringRef SlotLabel;
967965
Token Alignment;
968966
Alignment.startToken();
969-
SourceLocation LParenLoc = Tok.getLocation();
970967
PP.Lex(Tok);
971968
if (Tok.is(tok::numeric_constant)) {
972969
Alignment = Tok;
@@ -976,18 +973,18 @@ void PragmaPackHandler::HandlePragma(Preprocessor &PP,
976973
// In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
977974
// the push/pop stack.
978975
// In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
979-
if (PP.getLangOpts().ApplePragmaPack)
980-
Kind = Sema::PPK_Push;
976+
Action =
977+
PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
981978
} else if (Tok.is(tok::identifier)) {
982979
const IdentifierInfo *II = Tok.getIdentifierInfo();
983980
if (II->isStr("show")) {
984-
Kind = Sema::PPK_Show;
981+
Action = Sema::PSK_Show;
985982
PP.Lex(Tok);
986983
} else {
987984
if (II->isStr("push")) {
988-
Kind = Sema::PPK_Push;
985+
Action = Sema::PSK_Push;
989986
} else if (II->isStr("pop")) {
990-
Kind = Sema::PPK_Pop;
987+
Action = Sema::PSK_Pop;
991988
} else {
992989
PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
993990
return;
@@ -998,11 +995,12 @@ void PragmaPackHandler::HandlePragma(Preprocessor &PP,
998995
PP.Lex(Tok);
999996

1000997
if (Tok.is(tok::numeric_constant)) {
998+
Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
1001999
Alignment = Tok;
10021000

10031001
PP.Lex(Tok);
10041002
} else if (Tok.is(tok::identifier)) {
1005-
Name = Tok.getIdentifierInfo();
1003+
SlotLabel = Tok.getIdentifierInfo()->getName();
10061004
PP.Lex(Tok);
10071005

10081006
if (Tok.is(tok::comma)) {
@@ -1013,6 +1011,7 @@ void PragmaPackHandler::HandlePragma(Preprocessor &PP,
10131011
return;
10141012
}
10151013

1014+
Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
10161015
Alignment = Tok;
10171016

10181017
PP.Lex(Tok);
@@ -1027,7 +1026,7 @@ void PragmaPackHandler::HandlePragma(Preprocessor &PP,
10271026
// In MSVC/gcc, #pragma pack() resets the alignment without affecting
10281027
// the push/pop stack.
10291028
// In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1030-
Kind = Sema::PPK_Pop;
1029+
Action = Sema::PSK_Pop;
10311030
}
10321031

10331032
if (Tok.isNot(tok::r_paren)) {
@@ -1044,11 +1043,9 @@ void PragmaPackHandler::HandlePragma(Preprocessor &PP,
10441043

10451044
PragmaPackInfo *Info =
10461045
PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
1047-
Info->Kind = Kind;
1048-
Info->Name = Name;
1046+
Info->Action = Action;
1047+
Info->SlotLabel = SlotLabel;
10491048
Info->Alignment = Alignment;
1050-
Info->LParenLoc = LParenLoc;
1051-
Info->RParenLoc = RParenLoc;
10521049

10531050
MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
10541051
1);

lib/Sema/Sema.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,13 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
7979
Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
8080
CollectStats(false), CodeCompleter(CodeCompleter),
8181
CurContext(nullptr), OriginalLexicalContext(nullptr),
82-
PackContext(nullptr), MSStructPragmaOn(false),
82+
MSStructPragmaOn(false),
8383
MSPointerToMemberRepresentationMethod(
8484
LangOpts.getMSPointerToMemberRepresentationMethod()),
8585
VtorDispStack(MSVtorDispAttr::Mode(LangOpts.VtorDispMode)),
86-
DataSegStack(nullptr), BSSSegStack(nullptr), ConstSegStack(nullptr),
87-
CodeSegStack(nullptr), CurInitSeg(nullptr), VisContext(nullptr),
86+
PackStack(0), DataSegStack(nullptr), BSSSegStack(nullptr),
87+
ConstSegStack(nullptr), CodeSegStack(nullptr), CurInitSeg(nullptr),
88+
VisContext(nullptr),
8889
IsBuildingRecoveryCallExpr(false),
8990
ExprNeedsCleanups(false), LateTemplateParser(nullptr),
9091
LateTemplateParserCleanup(nullptr),
@@ -252,7 +253,6 @@ void Sema::Initialize() {
252253

253254
Sema::~Sema() {
254255
llvm::DeleteContainerSeconds(LateParsedTemplateMap);
255-
if (PackContext) FreePackedContext();
256256
if (VisContext) FreeVisContext();
257257
// Kill all the active scopes.
258258
for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I)

0 commit comments

Comments
 (0)