[PATCH] D35564: Suppress -pedantic warnings about GNU extension StmtExpr in glibc's assert macro

Stephan Bergmann via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 18 08:04:59 PDT 2017

sberg created this revision.

See the mail thread starting at http://lists.llvm.org/pipermail/cfe-dev/2017-July/054666.html "[cfe-dev] -pedantic warnings in system headers?":

The assert.h in glibc 2.25 defines assert as a GNU-extension "statement expression", see https://sourceware.org/git/?p=glibc.git;a=commit;h=e077349ce589466eecd47213db4fae6b80ec18c4 "assert.h: allow gcc to detect assert(a = 1) errors". That caused Clang -pedantic to emit -Wgnu-statement-expression warnings (unlike GCC).

This patch extends the heuristic when to suppress warnings in system headers, to also suppress warnings about extensions used in the body of a function-like macro defined in a system header.  (The "function-like" part is there so that the warning from the body of __LONG_LONG_MAX__ in clang/test/CXX/drs/dr4xx.cpp is still emitted.)

I ran into this when trying to build CoinMP (which wants to be built with -pedantic-errors) as part of building LibreOffice master with Clang trunk against Fedora 26 glibc.  I'm not sure what the best solution is, though: Either extend the heuristic when to suppress warnings as done here (which has the benefit of aligning the behavior with GCC), or change the CoinMP build system to not insist on -pedantic-error.



Index: clang/test/SemaCXX/warn-sysheader-macro.cpp
--- clang/test/SemaCXX/warn-sysheader-macro.cpp
+++ clang/test/SemaCXX/warn-sysheader-macro.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify -fsyntax-only -Wshadow -Wold-style-cast %s
+// RUN: %clang_cc1 -verify -fsyntax-only -Wshadow -Wold-style-cast -pedantic %s
 // Test that macro expansions from system headers don't trigger 'syntactic'
 // warnings that are not actionable.
@@ -12,6 +12,8 @@
 #define OLD_STYLE_CAST(a) ((int) (a))
+#define ASSERT(a) ({ if (a) {} else {} })
@@ -32,4 +34,9 @@
   int i = OLD_STYLE_CAST(0);
+void testExtension() {
+  ASSERT(true);
+  ASSERT(({ true; })); // expected-warning {{use of GNU statement expression extension}}
Index: clang/lib/Basic/DiagnosticIDs.cpp
--- clang/lib/Basic/DiagnosticIDs.cpp
+++ clang/lib/Basic/DiagnosticIDs.cpp
@@ -396,6 +396,14 @@
   return toLevel(getDiagnosticSeverity(DiagID, Loc, Diag));
+static bool isSystemFunctionMacroBodyExpansion(SourceLocation Loc,
+                                               const SourceManager &SM) {
+  if (!(SM.isInSystemMacro(Loc) && SM.isMacroBodyExpansion(Loc)))
+    return false;
+  auto Range = SM.getImmediateExpansionRange(Loc);
+  return Range.first != Range.second;
 /// \brief Based on the way the client configured the Diagnostic
 /// object, classify the specified diagnostic ID into a Level, consumable by
 /// the DiagnosticClient.
@@ -469,8 +477,10 @@
   // because we also want to ignore extensions and warnings in -Werror and
   // -pedantic-errors modes, which *map* warnings/extensions to errors.
   if (State->SuppressSystemWarnings && !ShowInSystemHeader && Loc.isValid() &&
-      Diag.getSourceManager().isInSystemHeader(
-          Diag.getSourceManager().getExpansionLoc(Loc)))
+      (Diag.getSourceManager().isInSystemHeader(
+          Diag.getSourceManager().getExpansionLoc(Loc)) ||
+       (IsExtensionDiag &&
+        isSystemFunctionMacroBodyExpansion(Loc, Diag.getSourceManager()))))
     return diag::Severity::Ignored;
   return Result;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35564.107099.patch
Type: text/x-patch
Size: 2231 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170718/27c26413/attachment.bin>

More information about the cfe-commits mailing list