Hi<div><br></div><div>I am attaching a new version of the patch that takes into account length modifiers for ints.</div><div>The previous version emits wrong warnings when running on ints preceded by length modifiers.</div>
<div><br></div><div>I'm not sure this is the best way to handle them - I'm consuming modifiers at conversion specifier time by walking backwards into the format string until I hit the '%' conversion opening char.</div>
<div><br></div><div>I also left out modifiers j,t,z,q (intmax_t, ptrdiff_t, etc) - I'm not sure how to handle these.</div><div><br></div><div>This new version breaks an existing test from Clang::Sema/format-strings.c because it traps the use of "ld" on an int.</div>
<div><br></div><div>======</div><div><div>  sprintf(buf,"%%%%%ld%d%d", 1, 2, 3); // no-warning</div><div>  snprintf(buf, 2, "%%%%%ld%;%d", 1, 2, 3); // expected-warning {{sion '%;'}}</div><div>
======</div><div><br></div><div>Thanks,</div><div>Cristi</div><div><br></div><div><br></div></div><div><br><div class="gmail_quote">On Thu, Jan 21, 2010 at 12:17 PM, Cristian Draghici <span dir="ltr"><<a href="mailto:cristian.draghici@gmail.com">cristian.draghici@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi<div><br></div><div>I am attaching a patch for "%i" and "%d".</div><div><br></div><div>I've not carried on with the rest of the conversion specifiers because they should probably be handled by a single block of code (vs having one common block per type of argument - i.e. one for int, unsigned int, etc).</div>

<div><br></div><div>i.e.</div><div><br></div><div><font face="'courier new', monospace">builtinTypeKind conversionSpecifierType;</font></div><div><font face="'courier new', monospace">case 'i':</font></div>

<div><font face="'courier new', monospace">case 'd':</font></div><div><font face="'courier new', monospace">    conversionSpecifierType = BuiltinType::Int;</font></div>
<div><font face="'courier new', monospace">case 'o':</font></div><div><div><font face="'courier new', monospace">case 'u':</font></div><div>
<div><font face="'courier new', monospace">case 'X':</font></div><div><div><font face="'courier new', monospace">case 'x':</font></div><div>
<font face="'courier new', monospace">   conversionSpecifierType = BuiltinType::UInt;</font></div></div></div></div><div><font face="'courier new', monospace">[..]</font></div>
<div><font face="'courier new', monospace">case 'p':</font></div><div><font face="'courier new', monospace">     /* do diagnostics here based on the conversionSpecifierType and the current argument */</font></div>

<div><br></div><div><br></div><div>Thanks,</div><div>Cristi   </div><div><div>PS Is there a way to get the string representation of the built-in type? (i.e. 'int' out of '<span style="font-family:'courier new', monospace">BuiltinType::Int')</span></div>

<div><font face="'courier new', monospace"><br></font></div><br></div><div><div></div><div class="h5"><div><br></div><div><br><div class="gmail_quote">On Thu, Jan 21, 2010 at 8:24 AM, Ted Kremenek <span dir="ltr"><<a href="mailto:kremenek@apple.com" target="_blank">kremenek@apple.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Cristi,<br>
<br>
This is definitely on the right track.<br>
<br>
To add a warning, you only need to modify the TableGen files (.td), as the .inc files are generated from the .td files during the build.  Specifically, you only need to modify DiagnosticSemaKinds.td and add one warning to the Format group.  Your warning declaration would look something like:<br>


<br>
 def my_warning : Warning<<br>
  "conversion specifies type '%0' but the argument has type '%1'">, InGroup<Format>;<br>
<br>
You would then use it similarly to the warn_printf_asterisk_precision_wrong_type warning (in SemaChecking.cpp), except you specify two QualType arguments instead of one.<br>
<br>
Please also include a couple test cases that show when the warning is both reported and *not* reported.  The test cases should also include the use of typedefs (which I believe your logic already handles).<br>
<br>
- Ted<br>
<div><div></div><div><br>
On Jan 19, 2010, at 11:26 PM, Cristian Draghici wrote:<br>
<br>
> Hi<br>
><br>
> I've started looking at printf correspondence between conversion specifier and arguments.<br>
><br>
> 1/ Am I on the right track with the patch below?<br>
><br>
> 2/ Assuming an affirmative answer on (1), I'm not sure how to define a new warning (I used warn_printf_asterisk_width_wrong_type which is wrong in this case).<br>
> I'm guessing defs are in include/clang/Basic/DiagnosticSemaKinds.inc and .td and referred in DiagnosticGroups.inc and .td<br>
> A new diagnostic would be needed, along the lines of "conversion specifies type 'X', but argument has type 'Y'"<br>
><br>
><br>
> Thanks for your time,<br>
> Cristi<br>
><br>
><br>
> cristi:clang diciu$ svn diff  ./lib/Sema/SemaChecking.cpp<br>
> Index: lib/Sema/SemaChecking.cpp<br>
> ===================================================================<br>
> --- lib/Sema/SemaChecking.cpp (revision 93981)<br>
> +++ lib/Sema/SemaChecking.cpp (working copy)<br>
> @@ -1147,7 +1147,17 @@<br>
>      //<br>
>      // FIXME: additional checks will go into the following cases.<br>
>      case 'i':<br>
> -    case 'd':<br>
> +    case 'd': {<br>
> +      if(numDataArgs >= format_idx+numConversions+1) {<br>
> +        const Expr *E2 = TheCall->getArg(format_idx+numConversions+1);<br>
> +        const BuiltinType *BT = E2->getType()->getAs<BuiltinType>();<br>
> +        if(BT == NULL || BT->getKind() != BuiltinType::Int) {<br>
> +          SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx);<br>
> +          Diag(Loc, diag::warn_printf_asterisk_width_wrong_type)<br>
> +            << E2->getType() << E2->getSourceRange();<br>
> +        }<br>
> +      }<br>
> +    }<br>
>      case 'o':<br>
>      case 'u':<br>
>      case 'x':<br>
><br>
><br>
><br>
</div></div>> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br>
</blockquote></div><br><br clear="all"><br>
</div>
</div></div></blockquote></div><br><br clear="all"><br>
</div>