Modified format-string checking to not emit a warning when all of the
following hold:

(1) A vprintf-like function is called that takes the argument list via a
    via_list argument.

(2) The format string is a non-literal that is the parameter value of
    the enclosing function, e.g:
    
    void logmessage(const char *fmt,...) { 
      va_list ap;
      va_start(ap,fmt);
      fprintf(fmt,ap);  // Do not emit a warning.
    }
    
In the future this special case will be enhanced to consult the "format"
attribute attached to a function declaration instead of just allowing a blank
check for all function parameters to be used as format strings to vprintf-like
functions. This will happen when more support for attributes becomes
available.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45114 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Sema/SemaChecking.cpp b/Sema/SemaChecking.cpp
index 38b93ab..b366a2b 100644
--- a/Sema/SemaChecking.cpp
+++ b/Sema/SemaChecking.cpp
@@ -237,8 +237,32 @@
   StringLiteral *FExpr = dyn_cast<StringLiteral>(OrigFormatExpr);
   
   if (FExpr == NULL) {
-    Diag(Args[format_idx]->getLocStart(), 
-         diag::warn_printf_not_string_constant, Fn->getSourceRange());
+    // 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
+    // has an attribute indicating it is a printf-like function, then we
+    // should suppress warnings concerning non-literals being used in a call
+    // to a vprintf function.  For example:
+    //
+    // void
+    // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...) {
+    //      va_list ap;
+    //      va_start(ap, fmt);
+    //      vprintf(fmt, ap);  // Do NOT emit a warning about "fmt".
+    //      ...
+    //
+    //
+    //  FIXME: We don't have full attribute support yet, so just check to see
+    //    if the argument is a DeclRefExpr that references a parameter.  We'll
+    //    add proper support for checking the attribute later.
+    if (HasVAListArg)
+      if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(IgnoreParen(OrigFormatExpr)))
+          if (isa<ParmVarDecl>(DR->getDecl()))
+          return;
+    
+    Diag(Args[format_idx]->getLocStart(), diag::warn_printf_not_string_constant,
+         Fn->getSourceRange());
+          
     return;
   }