[cfe-commits] r62117 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaChecking.cpp test/Sema/format-strings.c

Ted Kremenek kremenek at apple.com
Mon Jan 12 15:09:10 PST 2009


Author: kremenek
Date: Mon Jan 12 17:09:09 2009
New Revision: 62117

URL: http://llvm.org/viewvc/llvm-project?rev=62117&view=rev
Log:
Patch by Roman Divacky:

Extend string-literal checking for printf() format string to handle conditional
ternary operators where both sides are literals.

This fixes PR 3319: http://llvm.org/bugs/show_bug.cgi?id=3319

Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/format-strings.c

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=62117&r1=62116&r2=62117&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Mon Jan 12 17:09:09 2009
@@ -1516,6 +1516,11 @@
   Action::ExprResult SemaBuiltinShuffleVector(CallExpr *TheCall);
   bool SemaBuiltinPrefetch(CallExpr *TheCall); 
   bool SemaBuiltinObjectSize(CallExpr *TheCall); 
+  bool SemaCheckStringLiteral(Expr *E, CallExpr *TheCall, bool HasVAListArg,
+                              unsigned format_idx);
+  void CheckPrintfString(StringLiteral *FExpr, Expr *OrigFormatExpr,
+                         CallExpr *TheCall, bool HasVAListArg,
+                         unsigned format_idx);
   void CheckPrintfArguments(CallExpr *TheCall,
                             bool HasVAListArg, unsigned format_idx);
   void CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=62117&r1=62116&r2=62117&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Jan 12 17:09:09 2009
@@ -369,6 +369,51 @@
   return false;
 }
 
+// Handle i > 1 ? "x" : "y", recursivelly
+bool Sema::SemaCheckStringLiteral(Expr *E, CallExpr *TheCall, bool HasVAListArg,
+                                  unsigned format_idx) {
+
+  switch (E->getStmtClass()) {
+  case Stmt::ConditionalOperatorClass: {
+    ConditionalOperator *C = cast<ConditionalOperator>(E);
+    return SemaCheckStringLiteral(C->getLHS(), TheCall,
+                                  HasVAListArg, format_idx)
+        && SemaCheckStringLiteral(C->getRHS(), TheCall,
+                                  HasVAListArg, format_idx);
+  }
+
+  case Stmt::ImplicitCastExprClass: {
+    ImplicitCastExpr *Expr = dyn_cast<ImplicitCastExpr>(E);
+    return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg,
+                                  format_idx);
+  }
+
+  case Stmt::ParenExprClass: {
+    ParenExpr *Expr = dyn_cast<ParenExpr>(E);
+    return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg,
+                                  format_idx);
+  }
+
+  default: {
+    ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E);
+    StringLiteral *StrE = NULL;
+
+    if (ObjCFExpr)
+      StrE = ObjCFExpr->getString();
+    else
+      StrE = dyn_cast<StringLiteral>(E);
+
+    if (StrE) {
+      CheckPrintfString(StrE, E, TheCall, HasVAListArg, format_idx);
+      return true;
+    }
+    
+    return false;
+  }
+  }
+}
+
+
 /// CheckPrintfArguments - Check calls to printf (and similar functions) for
 /// correct use of format strings.  
 ///
@@ -444,15 +489,9 @@
   // C string (e.g. "%d")
   // ObjC string uses the same format specifiers as C string, so we can use 
   // the same format string checking logic for both ObjC and C strings.
-  ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(OrigFormatExpr);
-  StringLiteral *FExpr = NULL;
+  bool isFExpr = SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx);
 
-  if(ObjCFExpr != NULL) 
-    FExpr = ObjCFExpr->getString();
-  else
-    FExpr = dyn_cast<StringLiteral>(OrigFormatExpr);
-
-  if (FExpr == NULL) {
+  if (!isFExpr) {
     // For vprintf* functions (i.e., HasVAListArg==true), we add a
     // special check to see if the format string is a function parameter
     // of the function calling the printf function.  If the function
@@ -475,13 +514,18 @@
       if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(OrigFormatExpr))
         if (isa<ParmVarDecl>(DR->getDecl()))
           return;
-    
+
     Diag(TheCall->getArg(format_idx)->getLocStart(), 
          diag::warn_printf_not_string_constant)
       << OrigFormatExpr->getSourceRange();
     return;
   }
+}
+
+void Sema::CheckPrintfString(StringLiteral *FExpr, Expr *OrigFormatExpr,
+      CallExpr *TheCall, bool HasVAListArg, unsigned format_idx) {
 
+  ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(OrigFormatExpr);
   // CHECK: is the format string a wide literal?
   if (FExpr->isWide()) {
     Diag(FExpr->getLocStart(),

Modified: cfe/trunk/test/Sema/format-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=62117&r1=62116&r2=62117&view=diff

==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Mon Jan 12 17:09:09 2009
@@ -31,6 +31,12 @@
   __builtin___vsnprintf_chk(buf,2,0,-1,global_fmt,ap); // expected-warning {{format string is not a string literal}}
 }
 
+void check_conditional_literal(const char* s, int i) {
+  printf(i == 1 ? "yes" : "no"); // no-warning
+  printf(i == 0 ? (i == 1 ? "yes" : "no") : "dont know"); // no-warning
+  printf(i == 0 ? (i == 1 ? s : "no") : "dont know"); // expected-warning
+}
+
 void check_writeback_specifier()
 {
   int x;





More information about the cfe-commits mailing list