<div dir="ltr">On Mon, Dec 2, 2013 at 2:35 PM, Aaron Ballman <span dir="ltr"><<a href="mailto:aaron@aaronballman.com" target="_blank">aaron@aaronballman.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im">On Mon, Dec 2, 2013 at 5:25 PM, Richard Smith <<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>> wrote:<br>

</div><div><div class="h5">> On Mon, Dec 2, 2013 at 1:28 PM, Aaron Ballman <<a href="mailto:aaron@aaronballman.com">aaron@aaronballman.com</a>><br>
> wrote:<br>
>><br>
>> The nothrow and const attributes have a curious construct that does<br>
>> not appear with any other attributes. Both of them check whether the<br>
>> attribute has already been applied, and if it has, it silently fails<br>
>> to reapply the attribute.  Eg)<br>
>><br>
>>   if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {<br>
>>     if (Existing->getLocation().isInvalid())<br>
>>       Existing->setRange(Attr.getRange());<br>
>>   } else {<br>
>>     D->addAttr(::new (S.Context)<br>
>>                NoThrowAttr(Attr.getRange(), S.Context,<br>
>>                            Attr.getAttributeSpellingListIndex()));<br>
>>   }<br>
>><br>
>> While this makes some sense, two things in particular are worrisome:<br>
>><br>
>> 1) We lose syntactic information because we are losing the as-written<br>
>> attribute information. This happens relatively frequently, but any<br>
>> time we can cut that down would be good.<br>
>> 2) This is not consistently applied across attributes.<br>
>><br>
>> I am wondering whether it is reasonable to remove the check for the<br>
>> existing attribute from the nothrow and const attributes, and simply<br>
>> apply multiple times. Since neither of these attributes accept<br>
>> arguments, it seems like this would be a harmless action. However, I<br>
>> may not have the full picture; this is the original commit message:<br>
>><br>
>> dgregor  6/15/2011 1:45:11 AM<br>
>> Don't add redundant FormatAttr, ConstAttr, or NoThrowAttr attributes,<br>
>> either imlicitly (for builtins) or explicitly (due to multiple<br>
>> specification of the same attributes). Fixes <rdar://problem/9612060>.<br>
>><br>
>> (Note, I am not suggesting any modifications for FormatAttr.)<br>
>><br>
>> Thanks!<br>
><br>
><br>
> Here's the testcase from that change:<br>
><br>
> +int printf(const char * restrict, ...) __attribute__((__format__<br>
> (__printf__, 1, 2)));<br>
> +<br>
> +void rdar9612060(void) {<br>
> +  printf("%s", 2); // expected-warning{{conversion specifies type 'char *'<br>
> but the argument has type 'int'}}<br>
> +}<br>
><br>
> It seems the problem that this change was trying to avoid was where an<br>
> attribute is implicitly added because the function is a builtin, and then<br>
> explicitly added. I suspect this is redundant when initially building the<br>
> attribute -- mergeDeclAttributes will not propagate the attribute from the<br>
> implicitly-declared builtin onto the explicitly-declared function.<br>
<br>
</div></div>That makes sense - you would want to avoid duplicate diagnostics in<br>
that case. I believe you are correct that it is redundant. Quick<br>
testing by removing the code from const and nothrow handlers and<br>
testing demonstrates no regressions. Do you have a better way for me<br>
to test this?</blockquote><div><br></div><div>You could look at the output of -ast-dump and check that there are no duplicate attributes.</div><div><br></div><div>I wonder what we should do about a case like this:</div><div>
<br></div><div>  void f(const char*, ...) __attribute__((format(printf, 1, 2))) __attribute__((format(printf, 1, 2)));</div><div>  void g() { f("%s"); }</div><div>  void f(const char*, ...);</div><div><div>  void h() { f("%s"); }</div>
</div><div><br></div><div>I think removing the deduplication will result in g() producing two warnings, but h() only producing one (because mergeDeclAttributes will only propagate one of them from the earlier declaration to the later one).</div>
</div></div></div>