-Wpragma-pack: add an additional note and fixit when warning

about unterminated push directives that are followed by a reset
('#pragma pack()')

This has been suggested by Hans Wennborg.

llvm-svn: 309559
This commit is contained in:
Alex Lorenz 2017-07-31 13:37:50 +00:00
parent e841b214b1
commit a1479d7988
5 changed files with 26 additions and 3 deletions

View File

@ -723,6 +723,8 @@ def warn_pragma_pack_no_pop_eof : Warning<"unterminated "
"'#pragma pack (push, ...)' at end of file">, InGroup<PragmaPack>;
def note_pragma_pack_here : Note<
"previous '#pragma pack' directive that modifies alignment is here">;
def note_pragma_pack_pop_instead_reset : Note<
"did you intend to use '#pragma pack (pop)' instead of '#pragma pack()'?">;
// Follow the Microsoft implementation.
def warn_pragma_pack_show : Warning<"value of #pragma pack(show) == %0">;
def warn_pragma_pack_pop_identifer_and_alignment : Warning<

View File

@ -250,8 +250,22 @@ void Sema::DiagnoseNonDefaultPragmaPack(PragmaPackDiagnoseKind Kind,
void Sema::DiagnoseUnterminatedPragmaPack() {
if (PackStack.Stack.empty())
return;
for (const auto &StackSlot : llvm::reverse(PackStack.Stack))
bool IsInnermost = true;
for (const auto &StackSlot : llvm::reverse(PackStack.Stack)) {
Diag(StackSlot.PragmaPushLocation, diag::warn_pragma_pack_no_pop_eof);
// The user might have already reset the alignment, so suggest replacing
// the reset with a pop.
if (IsInnermost && PackStack.CurrentValue == PackStack.DefaultValue) {
DiagnosticBuilder DB = Diag(PackStack.CurrentPragmaLocation,
diag::note_pragma_pack_pop_instead_reset);
SourceLocation FixItLoc = Lexer::findLocationAfterToken(
PackStack.CurrentPragmaLocation, tok::l_paren, SourceMgr, LangOpts,
/*SkipTrailing=*/false);
if (FixItLoc.isValid())
DB << FixItHint::CreateInsertion(FixItLoc, "pop");
}
IsInnermost = false;
}
}
void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {

View File

@ -0,0 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
#pragma pack (push, 1)
#pragma pack()
// CHECK: fix-it:{{.*}}:{[[@LINE-1]]:14-[[@LINE-1]]:14}:"pop"

View File

@ -4,5 +4,7 @@
#ifndef HEADER
#define HEADER
#pragma pack (push, 1)
#else
#pragma pack (2)
#endif
// expected-warning@-2 {{unterminated '#pragma pack (push, ...)' at end of file}}
// expected-warning@-4 {{unterminated '#pragma pack (push, ...)' at end of file}}

View File

@ -29,4 +29,4 @@
// Warn about unbalanced pushes.
#pragma pack (push,4) // expected-warning {{unterminated '#pragma pack (push, ...)' at end of file}}
#pragma pack (push) // expected-warning {{unterminated '#pragma pack (push, ...)' at end of file}}
#pragma pack ()
#pragma pack () // expected-note {{did you intend to use '#pragma pack (pop)' instead of '#pragma pack()'?}}