title: How to use the GCC attribute 'format'?
body: <p>Here is a little piece of code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdarg.h&gt;
  
  void MyPrintf(char const* format, va_list args);
  void MyVariadicPrintf(char const* format, ...);
  
  void MyPrintf(char const* format, va_list args)
  {
      vprintf(format, args);
  }
  
  void MyVariadicPrintf(char const* format, ...)
  {
      va_list args;
      va_start(args, format);
      MyPrintf(format, args);
      va_end(args);
  }
  
  int main(int, char*)
  {
      MyVariadicPrintf("%s" /* missing 2nd argument */);
  
      return 0;
  }
  </code></pre>
  
  <p>I'm compiling it with GCC 4.0, running Xcode on Mac OS X Leopard.<br>
  -Wformat and -Wmissing-format-attribute are enabled.<br>
  This code gives a warning on line 9 (call to <code>vprintf</code>), suggesting that <code>MyPrintf</code> could use the 'format' attribute:</p>
  
  <blockquote>
    <p>function might be possible candidate for 'printf' format attribute</p>
  </blockquote>
  
  <p>So I add the attribute this way (not sure if this is right):</p>
  
  <pre><code>void MyPrintf(char const* format, va_list args) __attribute__((format(printf, 1, 0)));
  </code></pre>
  
  <p>The previous warning disappears and the same warning now appears on line 16 (call to <code>MyPrintf</code>), suggesting that <code>MyVariadicPrintf</code> could use the 'format' attribute.<br>
  So I add the attribute this way (pretty sure this is right this time):</p>
  
  <pre><code>void MyVariadicPrintf(char const* format, ...) __attribute__((format(printf, 1, 2)));
  </code></pre>
  
  <p>And now I get the expected warning on line 22 (call to <code>MyVariadicPrintf</code>):</p>
  
  <blockquote>
    <p>too few arguments for format</p>
  </blockquote>
  
  <ol>
  <li>Did I do this right?  </li>
  <li>I noticed that on <code>MyPrintf</code> declaration, if I delete the attribute part, I'll still get the wanted warning on line 22. I also noticed that in this attribute part, changing the index from 1 to 2 won't give any warning or error. Which one is right and what is the goal of the attribute on this function?  </li>
  <li><p>If I add the following function <code>MyVariadicPrintfT</code> and call it (specialized with <code>char</code>), I'll get the warning suggesting to use the 'format' attribute on this function. I think it's impossible because the <code>format</code> argument is dependent on the templated type. Am I right?</p>
  
  <pre><code>template&lt;typename Type&gt;
  void MyVariadicPrintfT(Type const* format, ...)
  {
      va_list args;
      va_start(args, format);
      MyPrintf(format, args);
      va_end(args);
  }
  </code></pre></li>
  </ol>
  
  <p>The latest gnu documentation can be found at <a href="http://gcc.gnu.org/onlinedocs/gcc/" rel="noreferrer">gnu.org</a>.<br>
  Warning Options are in <a href="http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html" rel="noreferrer">section 3.8</a> (look for "-Wmissing-format-attribute").<br>
  Function Attributes are in <a href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#Function-Attributes" rel="noreferrer">section 6.30</a> (look for "format (archetype, string-index, first-to-check)").</p>
  
  <p>Thanks.</p>