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

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 31 06:37:50 PDT 2017


Author: arphaman
Date: Mon Jul 31 06:37:50 2017
New Revision: 309559

URL: http://llvm.org/viewvc/llvm-project?rev=309559&view=rev
Log:
-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.

Added:
    cfe/trunk/test/FixIt/fixit-pragma-pack.c
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaAttr.cpp
    cfe/trunk/test/PCH/suspicious-pragma-pack.c
    cfe/trunk/test/Sema/pragma-pack.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=309559&r1=309558&r2=309559&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Jul 31 06:37:50 2017
@@ -723,6 +723,8 @@ def warn_pragma_pack_no_pop_eof : Warnin
   "'#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<

Modified: cfe/trunk/lib/Sema/SemaAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaAttr.cpp?rev=309559&r1=309558&r2=309559&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaAttr.cpp Mon Jul 31 06:37:50 2017
@@ -250,8 +250,22 @@ void Sema::DiagnoseNonDefaultPragmaPack(
 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) { 

Added: cfe/trunk/test/FixIt/fixit-pragma-pack.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/FixIt/fixit-pragma-pack.c?rev=309559&view=auto
==============================================================================
--- cfe/trunk/test/FixIt/fixit-pragma-pack.c (added)
+++ cfe/trunk/test/FixIt/fixit-pragma-pack.c Mon Jul 31 06:37:50 2017
@@ -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"

Modified: cfe/trunk/test/PCH/suspicious-pragma-pack.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/suspicious-pragma-pack.c?rev=309559&r1=309558&r2=309559&view=diff
==============================================================================
--- cfe/trunk/test/PCH/suspicious-pragma-pack.c (original)
+++ cfe/trunk/test/PCH/suspicious-pragma-pack.c Mon Jul 31 06:37:50 2017
@@ -4,5 +4,7 @@
 #ifndef HEADER
 #define HEADER
 #pragma pack (push, 1)
+#else
+#pragma pack (2)
 #endif
-// expected-warning at -2 {{unterminated '#pragma pack (push, ...)' at end of file}}
+// expected-warning at -4 {{unterminated '#pragma pack (push, ...)' at end of file}}

Modified: cfe/trunk/test/Sema/pragma-pack.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/pragma-pack.c?rev=309559&r1=309558&r2=309559&view=diff
==============================================================================
--- cfe/trunk/test/Sema/pragma-pack.c (original)
+++ cfe/trunk/test/Sema/pragma-pack.c Mon Jul 31 06:37:50 2017
@@ -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()'?}}




More information about the cfe-commits mailing list