<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On May 29, 2012, at 6:10 PM, Richard Smith wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Hi,<div><br></div><div><div class="gmail_quote">On Tue, May 29, 2012 at 5:34 PM, Anna Zaks <span dir="ltr"><<a href="mailto:ganna@apple.com" target="_blank">ganna@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: zaks<br>
Date: Tue May 29 19:34:21 2012<br>
New Revision: 157659<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=157659&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=157659&view=rev</a><br>
Log:<br>
Add fixits for memory access warnings.<br></blockquote><div><br></div><div>We have a rule that fix-its on warnings aren't supposed to change the code's semantics (-Werror -fixit shouldn't change the semantics of a valid program). Consequently, fix-its such as this are usually placed on an accompanying note.</div>
<div> </div></div></div></blockquote><br>We discussed this fixit internally, and reached a consensus that we should not issue a fixit in this case.</div><div>The warning only warns on the following code, where the user most likely forgot to both dereference AND multiply by the number of elements. </div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> memcpy(to, from, <span style="color: #921e67">sizeof</span>(to));</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><div style="font-family: Helvetica; font-size: medium; ">Note, we currently do not (but should) warn on the following, where the fixit would be more appropriate.</div><div style="font-family: Helvetica; font-size: medium; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> memcpy(to, from, <span style="color: rgb(146, 30, 103); ">sizeof</span>(to)*N);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "></div></div></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><div style="font-family: Helvetica; font-size: medium; ">I plan to make the warning message more user-friendly:</div><div style="font-family: Helvetica; font-size: medium; ">-  warning: argument to 'sizeof' in 'memcpy' call is the same expression as the destination; did you mean to dereference it?</div><div style="font-family: Helvetica; font-size: medium; "><div style="font-family: Helvetica; font-size: medium; ">-  memcpy(to, from, sizeof(to))</div><div><div style="font-family: Helvetica; font-size: medium; ">-                   ~~                      ~~</div></div><div><br></div></div><div style="font-family: Helvetica; font-size: medium; ">+  warning: 'memcpy' call operates on objects of type 'Blah' while the size is based on a different type 'Blah*'. </div><div style="font-family: Helvetica; font-size: medium; ">+  memcpy(to, from, sizeof(to))</div><div style="font-family: Helvetica; font-size: medium; ">+                   ~~                      ~~</div><div style="font-family: Helvetica; font-size: medium; ">+  note: did you mean to dereference the argument to 'sizeof' (and multiply it by the number of elements)?</div><div style="font-family: Helvetica; font-size: medium; "><br></div><div style="font-family: Helvetica; font-size: medium; ">Anna.</div></div><blockquote type="cite"><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; position: static; z-index: auto; ">
Also, do not display the builtin name and macro expansion when the<br>
function is a builtin.<br>
<br>
Added:<br>
    cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof-fixit.cpp<br>
Modified:<br>
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
    cfe/trunk/lib/Sema/SemaChecking.cpp<br>
<br>
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=157659&r1=157658&r2=157659&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=157659&r1=157658&r2=157659&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)<br>
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue May 29 19:34:21 2012<br>
@@ -331,7 +331,7 @@<br>
 def note_bad_memaccess_silence : Note<<br>
   "explicitly cast the pointer to silence this warning">;<br>
 def warn_sizeof_pointer_expr_memaccess : Warning<<br>
-  "argument to 'sizeof' in %0 call is the same expression as the "<br>
+  "argument to 'sizeof' in '%0' call is the same expression as the "<br>
   "%select{destination|source}1; did you mean to "<br>
   "%select{dereference it|remove the addressof|provide an explicit length}2?">,<br>
   InGroup<DiagGroup<"sizeof-pointer-memaccess">>;<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=157659&r1=157658&r2=157659&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=157659&r1=157658&r2=157659&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue May 29 19:34:21 2012<br>
@@ -2755,19 +2755,49 @@<br>
           // TODO: For strncpy() and friends, this could suggest sizeof(dst)<br>
           //       over sizeof(src) as well.<br>
           unsigned ActionIdx = 0; // Default is to suggest dereferencing.<br>
+          FixItHint Fixit = FixItHint(); // Default hint.<br>
+          StringRef ReadableName = FnName->getName();<br>
+<br>
+          if (isa<DeclRefExpr>(SizeOfArg))<br>
+            Fixit = FixItHint::CreateInsertion(SizeOfArg->getLocStart(), "*");<br>
+<br>
           if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))<br>
-            if (UnaryOp->getOpcode() == UO_AddrOf)<br>
+            if (UnaryOp->getOpcode() == UO_AddrOf) {<br>
+              Fixit = FixItHint::CreateRemoval(<br>
+                      CharSourceRange::getTokenRange(SizeOfArg->getLocStart(),<br>
+                                                     SizeOfArg->getLocStart()));<br>
               ActionIdx = 1; // If its an address-of operator, just remove it.<br>
+            }<br>
           if (Context.getTypeSize(PointeeTy) == Context.getCharWidth())<br>
             ActionIdx = 2; // If the pointee's size is sizeof(char),<br>
                            // suggest an explicit length.<br>
           unsigned DestSrcSelect =<br>
             (BId == Builtin::BIstrndup ? 1 : ArgIdx);<br>
-          DiagRuntimeBehavior(SizeOfArg->getExprLoc(), Dest,<br>
+<br>
+          // If the function is defined as a builtin macro, do not show macro<br>
+          // expansion.<br>
+          SourceLocation SL = SizeOfArg->getExprLoc();<br>
+          SourceRange DSR = Dest->getSourceRange();<br>
+          SourceRange SSR = SizeOfArg->getSourceRange();<br>
+          SourceManager &SM  = PP.getSourceManager();<br>
+<br>
+          if (SM.isMacroArgExpansion(SL)) {<br>
+            ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts);<br>
+            SL = SM.getSpellingLoc(SL);<br>
+            DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()),<br>
+                             SM.getSpellingLoc(DSR.getEnd()));<br>
+            SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()),<br>
+                             SM.getSpellingLoc(SSR.getEnd()));<br>
+          }<br>
+<br>
+          DiagRuntimeBehavior(SL, Dest,<br>
                               PDiag(diag::warn_sizeof_pointer_expr_memaccess)<br>
-                                << FnName << DestSrcSelect << ActionIdx<br>
-                                << Dest->getSourceRange()<br>
-                                << SizeOfArg->getSourceRange());<br>
+                                << ReadableName<br>
+                                << DestSrcSelect<br>
+                                << ActionIdx<br>
+                                << DSR<br>
+                                << SSR<br>
+                                << Fixit);<br>
           break;<br>
         }<br>
       }<br>
<br>
Added: cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof-fixit.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof-fixit.cpp?rev=157659&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof-fixit.cpp?rev=157659&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof-fixit.cpp (added)<br>
+++ cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof-fixit.cpp Tue May 29 19:34:21 2012<br>
@@ -0,0 +1,24 @@<br>
+// RUN: cp %s %t<br>
+// RUN: not %clang_cc1 -fixit -Werror -x c++ -std=c++98 %t<br>
+// RUN: %clang_cc1 -fsyntax-only -Werror -x c++ -std=c++98 %t<br>
+// RUN: cp %s %t<br>
+// RUN: not %clang_cc1 -DUSE_BUILTINS -fixit -Werror -x c++ -std=c++98 %t<br>
+// RUN: %clang_cc1 -DUSE_BUILTINS -fsyntax-only -Werror -x c++ -std=c++98 %t<br>
+<br>
+extern "C" void *memcpy(void *s1, const void *s2, unsigned n);<br>
+<br>
+#ifdef USE_BUILTINS<br>
+# define BUILTIN(f) __builtin_ ## f<br>
+#else<br>
+# define BUILTIN(f) f<br>
+#endif<br>
+<br>
+#define memcpy BUILTIN(memcpy)<br>
+<br>
+int testFixits(int *to, int *from) {<br>
+  memcpy(to, from, sizeof(to)); // \<br>
+         // expected-warning {{argument to 'sizeof' in 'memcpy' call is the same expression as the destination; did you mean to dereference it?}}<br>
+  memcpy(0, &from, sizeof(&from)); // \<br>
+         // expected-warning {{argument to 'sizeof' in 'memcpy' call is the same expression as the source; did you mean to remove the addressof?}}<br>
+  return 0;<br>
+}<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>
</blockquote></div><br></body></html>