title: GCC does not honor 'pragma GCC diagnostic' to silence warnings
body: <p>We recently enabled <code>-Wall</code> for a project. Its enabled when GCC is at 4.7 or above (or Clang) because we can use <a href="https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html" rel="noreferrer"><code>GCC diagnostic</code></a> to manage the output from the elevated warnings. We want to manage them from the source code, and <strong><em>not</em></strong> via command line arguments. (We don't want to pollute the command line, or ask library users to rediscover what is needed).</p>
  
  <p>Under GCC 4.8 and 5.1, we are catching warnings that were disabled in a GCC diagnostic block for <code>-Wunused-variable</code>, <code>-Wunused-value</code>, <code>-Wunused-function</code> and <code>-Wunknown-pragmas</code>. Both GCCs accept <code>-fopenmp</code>, and both define <code>_OPENMP</code> in response to it, so I'm fairly certain it we should never see a <code>-Wunknown-pragmas</code> in response to <code>#prgam omp ...</code> (it <em>is</em> disabled, but it <em>is not</em> unknown).</p>
  
  <pre class="lang-none prettyprint-override"><code>g++ -DNDEBUG -g2 -O3 -Wall -march=native -pipe -c nbtheory.cpp
  nbtheory.cpp:655:0: warning: ignoring #pragma omp parallel [-Wunknown-pragmas]
    #pragma omp parallel
   ^
  nbtheory.cpp:656:0: warning: ignoring #pragma omp sections [-Wunknown-pragmas]
     #pragma omp sections
   ^
  ...
  </code></pre>
  
  <p>In this particular case, the <a href="https://github.com/weidai11/cryptopp/blob/master/nbtheory.cpp" rel="noreferrer">file <code>nbtheroy.cpp</code></a> has the following guard in place to help manage that warning (only relevant parts are shown, but you can see everything from <a href="https://github.com/weidai11/cryptopp/blob/master/nbtheory.cpp" rel="noreferrer">the GitHub link</a>):</p>
  
  <pre class="lang-c++ prettyprint-override"><code>// Defines GCC_DIAGNOSTIC_AWARE if GCC 4.7 or above.
  #include &lt;misc.h&gt;
  ...
  
  #if GCC_DIAGNOSTIC_AWARE
  # pragma GCC diagnostic ignored "-Wunknown-pragmas"
  #endif
  
  ...
  Integer ModularRoot(const Integer &amp;a, const Integer &amp;dp, const Integer &amp;dq,
                      const Integer &amp;p, const Integer &amp;q, const Integer &amp;u)
  {
      Integer p2, q2;
      #pragma omp parallel
          #pragma omp sections
          {
              #pragma omp section
                  p2 = ModularExponentiation((a % p), dp, p);
              #pragma omp section
                  q2 = ModularExponentiation((a % q), dq, q);
          }
      return CRT(p2, p, q2, q, u);
  }
  ...
  </code></pre>
  
  <p>Because the file is <code>*.cpp</code> (its effectively <em>the</em> translation unit), we <strong><em>do not</em></strong> perform a <code>#pragma GCC diagnostic push</code> at the beginning and <code>#pragma GCC diagnostic pop</code> at the end. (We do that for header files that are included, however). (We also tried doing it, but it did not help).</p>
  
  <p>And here is <code>GCC_DIAGNOSTIC_AWARE</code> (from <a href="https://github.com/weidai11/cryptopp/blob/master/misc.h" rel="noreferrer"><code>misc.h</code></a>):</p>
  
  <pre class="lang-c++ prettyprint-override"><code>// Used to suppress some warnings in some header and implementation files.
  //   Some platforms, like CentOS and OpenBSD, use old compilers that don't understand -Wno-unknown-pragma.
  #define GCC_DIAGNOSTIC_AWARE ((__GNUC__ &gt; 4 || (__GNUC__ == 4 &amp;&amp; __GNUC_MINOR__ &gt;= 7)) || defined(__clang__))
  </code></pre>
  
  <p>I know the guard is working because adding a <code>#error</code> in the block causes an error. Also, commenting out the guard and calling out <code>#pragma GCC diagnostic ignored "-Wunknown-pragmas"</code> does not help. Finally, it works fine under Clang.</p>
  
  <p>I'm also experiencing it for other warnings, like <code>-Wunused-variable</code>, <code>-Wunused-value</code> and <code>-Wunused-function</code>. I <strong><em>really</em></strong> don't want to pollute the command line as suggested with the potential duplicate.</p>
  
  <p>How do I get the <code>GCC pragma diagnostic</code> mechanism to work as expected to silence warnings under GCC when using <code>-Wall</code>?</p>
  
  <hr>
  
  <p>Related, if you want to reproduce it (its GNUmakefile based, and does not require configurations or autotools):</p>
  
  <pre class="lang-none prettyprint-override"><code>git clone https://github.com/weidai11/cryptopp.git cryptopp-warn
  cd cryptopp-warn
  make
  </code></pre>
  
  <p><strong>EDIT</strong>: we checked-in a patch that disables <code>-Wall</code> except for Clang. If you want to reproduce the old behavior, then:</p>
  
  <pre class="lang-none prettyprint-override"><code>git clone https://github.com/weidai11/cryptopp.git cryptopp-warn
  cd cryptopp-warn
  export CXXFLAGS="-g2 -O3 -DNDEBUG -Wall"
  make
  </code></pre>