[cfe-dev] clang printf correspondence between conversion specifier and arguments

Cristian Draghici cristian.draghici at gmail.com
Wed Jan 20 01:26:36 CST 2010


Hi

I've started looking at printf correspondence between conversion specifier
and arguments.

1/ Am I on the right track with the patch below?

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).
I'm guessing defs are in include/clang/Basic/DiagnosticSemaKinds.inc and .td
and referred in DiagnosticGroups.inc and .td
A new diagnostic would be needed, along the lines of "conversion specifies
type 'X', but argument has type 'Y'"


Thanks for your time,
Cristi


cristi:clang diciu$ svn diff  ./lib/Sema/SemaChecking.cpp
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp (revision 93981)
+++ lib/Sema/SemaChecking.cpp (working copy)
@@ -1147,7 +1147,17 @@
     //
     // FIXME: additional checks will go into the following cases.
     case 'i':
-    case 'd':
+    case 'd': {
+      if(numDataArgs >= format_idx+numConversions+1) {
+        const Expr *E2 = TheCall->getArg(format_idx+numConversions+1);
+        const BuiltinType *BT = E2->getType()->getAs<BuiltinType>();
+        if(BT == NULL || BT->getKind() != BuiltinType::Int) {
+          SourceLocation Loc = getLocationOfStringLiteralByte(FExpr,
StrIdx);
+          Diag(Loc, diag::warn_printf_asterisk_width_wrong_type)
+            << E2->getType() << E2->getSourceRange();
+        }
+      }
+    }
     case 'o':
     case 'u':
     case 'x':
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cs.uiuc.edu/pipermail/cfe-dev/attachments/20100120/526bd271/attachment-0001.html 


More information about the cfe-dev mailing list