<div style="font-family: arial, helvetica, sans-serif"><font size="2">Attached is my latest attempt.<br><br><div class="gmail_quote">On Wed, Jun 13, 2012 at 6:46 PM, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Wed, Jun 13, 2012 at 5:57 PM, Sam Panzer <<a href="mailto:panzer@google.com">panzer@google.com</a>> wrote:<br>

> Thanks for taking the time to review! Here is another update that addresses<br>
> Richard's suggestions.<br>
><br>
</div><div><div class="h5">>> diff --git a/include/clang/Basic/DiagnosticSemaKinds.td<br>
>> b/include/clang/Basic/DiagnosticSemaKinds.td<br>
>> index 3dc03fa..c51302c 100644<br>
>> --- a/include/clang/Basic/DiagnosticSemaKinds.td<br>
>> +++ b/include/clang/Basic/DiagnosticSemaKinds.td<br>
>> @@ -4681,6 +4681,11 @@ def err_cannot_pass_objc_interface_to_vararg :<br>
>> Error<<br>
>>    "cannot pass object with interface type %0 by-value through variadic "<br>
>>    "%select{function|block|method}1">;<br>
>><br>
>> +def warn_non_pod_vararg_with_format_string : Warning<<br>
>> +  "cannot pass non-POD object of type %0 to variadic function; "<br>
>><br>
>> In C++11, this should be 'non-trivial' not 'non-POD'.<br>
><br>
> I added an extra %select to print "non-trivial" for C++11.<br>
>><br>
>> +  "expected type from format string was %1">,<br>
>> +  InGroup<DiagGroup<"non-pod-varargs">>, DefaultError;<br>
>> +<br>
>>  def warn_cannot_pass_non_pod_arg_to_vararg : Warning<<br>
>>    "cannot pass object of %select{non-POD|non-trivial}0 type %1 through<br>
>> variadic"<br>
>>    " %select{function|block|method|constructor}2; call will abort at<br>
>> runtime">,<br>
>> @@ -5161,6 +5166,7 @@ def warn_scanf_scanlist_incomplete : Warning<<br>
>>    "no closing ']' for '%%[' in scanf format string">,<br>
>>    InGroup<Format>;<br>
>>  def note_format_string_defined : Note<"format string is defined here">;<br>
>> +def note_printf_c_str: Note<"did you mean to call the %0 method?">;<br>
>><br>
>>  def warn_null_arg : Warning<<br>
>>    "null passed to a callee which requires a non-null argument">,<br>
>> @@ -5617,4 +5623,3 @@ def err_module_private_definition : Error<<br>
>>  }<br>
>><br>
>>  } // end of sema component.<br>
>> -<br>
>> diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h<br>
>> index f0d3213..c1d2933 100644<br>
>> --- a/include/clang/Sema/Sema.h<br>
>> +++ b/include/clang/Sema/Sema.h<br>
>> @@ -6395,6 +6395,17 @@ public:<br>
>>      VariadicDoesNotApply<br>
>>    };<br>
>><br>
>> +  // Used for determining in which context a type is considered POD<br>
>> +  enum PODKind {<br>
>> +    PK_POD,<br>
>> +    PK_CXX11_POD,<br>
>> +    PK_ObjC_Lifetime_POD,<br>
>> +    PK_NON_POD<br>
>> +  };<br>
>> +<br>
>> +  // Determines which PODKind fits an expression.<br>
>> +  PODKind levelOfTriviality(const Expr *E);<br>
>><br>
>> This seems to be specific to vararg arguments, so I'd prefer for it to be<br>
>> phrased in those terms. Possibly VarArgKind with values VAK_Invalid,<br>
>> VAK_Valid, and VAK_ValidInCXX11 (for the C++98 compat warning), and<br>
>> isValidVarArgType?<br>
>><br>
>> I would also suggest changing the argument type from const Expr * to<br>
>> QualType, since only the type is relevant.<br>
><br>
><br>
> Good points! Done.<br>
<br>
</div></div>The comments around there need updating.<br></blockquote><div><br></div><div>Done.  </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5"><br>
>> +<br>
>>    /// GatherArgumentsForCall - Collector argument expressions for various<br>
>>    /// form of call prototypes.<br>
>>    bool GatherArgumentsForCall(SourceLocation CallLoc,<br>
>> @@ -6409,7 +6420,8 @@ public:<br>
>>    // DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion,<br>
>> but<br>
>>    // will warn if the resulting type is not a POD type.<br>
>>    ExprResult DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType<br>
>> CT,<br>
>> -                                              FunctionDecl *FDecl);<br>
>> +                                              FunctionDecl *FDecl,<br>
>> +                                              bool HasFormatString =<br>
>> false);<br>
>><br>
>>    // UsualArithmeticConversions - performs the UsualUnaryConversions on<br>
>> it's<br>
>>    // operands and then handles various conversions that are common to<br>
>> binary<br>
>> @@ -6985,6 +6997,19 @@ private:<br>
>>                          const ArraySubscriptExpr *ASE=0,<br>
>>                          bool AllowOnePastEnd=true, bool<br>
>> IndexNegated=false);<br>
>>    void CheckArrayAccess(const Expr *E);<br>
>> +  bool hasFormatStringArgument(FunctionDecl *FDecl, Expr **Args,<br>
>> +                               unsigned NumArgs);<br>
>> +  // Used to grab the relevant information from a FormatAttr and a<br>
>> +  // FunctionDeclaration.<br>
>> +  struct FormatStringInfo {<br>
>> +    unsigned format_idx;<br>
>> +    unsigned firstDataArg;<br>
>> +    bool hasVAListArg;<br>
>><br>
>> Even though these are just being factored out from elsewhere, please fix<br>
>> the names to conform to the coding standard: FormatIdx, FirstDataArg,<br>
>> HasVAListArg.<br>
<br>
</div></div>I think you missed this?<br></blockquote><div><br></div><div>I did. For some reason Gmail didn't format it differently...</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div><div class="h5"><br>
>> @@ -557,37 +584,25 @@ ExprResult<br>
>> Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT,<br>
>>                            << E->getType() << CT))<br>
>>      return ExprError();<br>
>><br>
>> +  PODKind PT = levelOfTriviality(E);<br>
>> +<br>
>>    // Complain about passing non-POD types through varargs. However, don't<br>
>>    // perform this check for incomplete types, which we can get here when<br>
>> we're<br>
>>    // in an unevaluated context.<br>
>> -  if (!E->getType()->isIncompleteType() &&<br>
>> -      !E->getType().isCXX98PODType(Context)) {<br>
>> -    // C++0x [expr.call]p7:<br>
>> -    //   Passing a potentially-evaluated argument of class type (Clause<br>
>> 9)<br>
>> -    //   having a non-trivial copy constructor, a non-trivial move<br>
>> constructor,<br>
>> -    //   or a non-trivial destructor, with no corresponding parameter,<br>
>> -    //   is conditionally-supported with implementation-defined<br>
>> semantics.<br>
>> -    bool TrivialEnough = false;<br>
>> -    if (getLangOpts().CPlusPlus0x && !E->getType()->isDependentType())  {<br>
>> -      if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) {<br>
>> -        if (Record->hasTrivialCopyConstructor() &&<br>
>> -            Record->hasTrivialMoveConstructor() &&<br>
>> -            Record->hasTrivialDestructor()) {<br>
>> +  switch (PT) {<br>
>> +  case PK_POD: // Perfectly fine.<br>
>> +  case PK_ObjC_Lifetime_POD: // Nothing to diagnose. This is okay.<br>
>> +    break;<br>
>> +  case PK_CXX11_POD:<br>
>>            DiagRuntimeBehavior(E->getLocStart(), 0,<br>
>>              PDiag(diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg)<br>
>>                << E->getType() << CT);<br>
>><br>
>> Indentation seems to have gone a bit weird here.<br>
><br>
><br>
> Fixed. Though the line-wrap gets a bit difficult for<br>
> diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg.<br>
<br>
</div></div>You don't need to indent 'case' labels within a switch. You can also<br>
dedent the PDiag line to get it within 80 columns.<br></blockquote><div><br></div><div>The indenting should be better now.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div><div class="h5"><br>
>> -          TrivialEnough = true;<br>
>> -        }<br>
>> -      }<br>
>> -    }<br>
>> -<br>
>> -    if (!TrivialEnough &&<br>
>> -        getLangOpts().ObjCAutoRefCount &&<br>
>> -        E->getType()->isObjCLifetimeType())<br>
>> -      TrivialEnough = true;<br>
>> -<br>
>> -    if (TrivialEnough) {<br>
>> -      // Nothing to diagnose. This is okay.<br>
>> +    break;<br>
>> +  case PK_NON_POD:<br>
>> +    if (HasFormatString) {<br>
>> +      // If there is a format string argument, non-POD argument checking<br>
>> is<br>
>> +      // handled along with format string checking.<br>
>><br>
>> Does this do the right thing for arguments which aren't format arguments,<br>
>> for instance sprintf(my_cstr1, "%d", 1); ? Do we reach the check in the<br>
>> -Wformat code if the format string is not a constant?<br>
><br>
><br>
> Yes, the sprintf() case is handled correctly - that's what<br>
> getFormatStringInfo() is responsible for. The -Wformat check wasn't reached<br>
> if the format string wasn't const-qualified, though. This is fixed in the<br>
> new patch.<br>
> I added test cases for these two situations to printf-cstr.cpp.<br>
<br>
</div></div>I would imagine you still won't get a diagnostic about the non-pod<br>
argument in a case like this:<br>
<br>
extern const char str[30];<br>
printf(str, my_std_string);<br>
<br>
I think you need essentially the same set of checks which are<br>
performed by SemaCheckStringLiteral in order to get this exactly<br>
right. Perhaps the easiest approach would be to defer all POD checking<br>
of vararg types to CheckFunctionArguments (after the call to<br>
SemaCheckStringLiteral) if the callee has a FormatAttr.<br></blockquote><div><br></div><div>I factored out the checks from DefaultVariadicArgumentPromotion so that they are reused right after SemaCheckStringLiteral in CheckFormatArguments as Richeard suggested. Now any format string arguments that the old warning would have caught but the format string matching misses should now be caught, and there's a few extra test to catch this sort of thing.</div>
</div><br></font></div>