<div dir="ltr">OK, here's another go at it. This fixes the two test cases discussed, and doesn't break anything else that I can see. <div><br></div>Because callees do not always match the canonical declaration, I swap FunctionDecls for their canonical counterparts when they are visited. <br><br>This differs from what we discussed by not using the canonical declaration for ObjCMethodDecls. This is because hasBody and getBody work differently for ObjCMethodDecls than FunctionDecls. Using the canonical declaration breaks the call graph. There isn't a problem with the call graph created for ObjCMethodDecls, so I have left them alone. Also for ObjCMethodDecls, isThisDeclarationADefinition is just { return hasBody(); }, so that includeInGraph check is redundant.<div><br></div><div><div>Thoughts?<br></div><div><div><br></div><div>---<br> include/clang/Analysis/CallGraph.h | 1 +<br> lib/Analysis/CallGraph.cpp | 10 ++--------<br> test/Analysis/debug-CallGraph.c | 14 +++++++++++++-<br> 3 files changed, 16 insertions(+), 9 deletions(-)<br><br>diff --git a/include/clang/Analysis/CallGraph.h b/include/clang/Analysis/CallGraph.h<br>index eda22a5..49aad7a 100644<br>--- a/include/clang/Analysis/CallGraph.h<br>+++ b/include/clang/Analysis/CallGraph.h<br>@@ -95,6 +95,7 @@ public:<br> /// Part of recursive declaration visitation. We recursively visit all the<br> /// declarations to collect the root functions.<br> bool VisitFunctionDecl(FunctionDecl *FD) {<br>+ FD = FD->getCanonicalDecl();<br> // We skip function template definitions, as their semantics is<br> // only determined when they are instantiated.<br> if (includeInGraph(FD)) {<br>diff --git a/lib/Analysis/CallGraph.cpp b/lib/Analysis/CallGraph.cpp<br>index f41a96d..5ea2dc3 100644<br>--- a/lib/Analysis/CallGraph.cpp<br>+++ b/lib/Analysis/CallGraph.cpp<br>@@ -110,14 +110,13 @@ CallGraph::~CallGraph() {<br> <br> bool CallGraph::includeInGraph(const Decl *D) {<br> assert(D);<br>- if (!D->getBody())<br>+ if (!D->hasBody())<br> return false;<br> <br> if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {<br> // We skip function template definitions, as their semantics is<br> // only determined when they are instantiated.<br>- if (!FD->isThisDeclarationADefinition() ||<br>- FD->isDependentContext())<br>+ if (FD->isDependentContext())<br> return false;<br> <br> IdentifierInfo *II = FD->getIdentifier();<br>@@ -125,11 +124,6 @@ bool CallGraph::includeInGraph(const Decl *D) {<br> return false;<br> }<br> <br>- if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) {<br>- if (!ID->isThisDeclarationADefinition())<br>- return false;<br>- }<br>-<br> return true;<br> }<br> <br>diff --git a/test/Analysis/debug-CallGraph.c b/test/Analysis/debug-CallGraph.c<br>index 4523c78..ea7ea70 100644<br>--- a/test/Analysis/debug-CallGraph.c<br>+++ b/test/Analysis/debug-CallGraph.c<br>@@ -24,8 +24,20 @@ void bbb(int y) {<br> }();<br> }<br> <br>+void ccc();<br>+void ddd() { ccc(); }<br>+void ccc() {}<br>+<br>+void eee();<br>+void eee() {}<br>+void fff() { eee(); }<br>+<br> // CHECK:--- Call graph Dump ---<br>-// CHECK: Function: < root > calls: mmm foo aaa < > bbb<br>+// CHECK: Function: < root > calls: mmm foo aaa < > bbb ccc ddd eee fff<br>+// CHECK: {{Function: fff calls: eee $}}<br>+// CHECK: {{Function: eee calls: $}}<br>+// CHECK: {{Function: ddd calls: ccc $}}<br>+// CHECK: {{Function: ccc calls: $}}<br> // CHECK: Function: bbb calls: < ><br> // CHECK: Function: < > calls: foo<br> // CHECK: Function: aaa calls: foo<br>-- <br>1.9.1<br><br><br><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Nov 22, 2014 at 2:23 AM, 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"><div style="word-wrap:break-word"><br><div><div><div class="h5"><blockquote type="cite"><div>On Nov 21, 2014, at 5:21 PM, Daniel DeFreez <<a href="mailto:dcdefreez@ucdavis.edu" target="_blank">dcdefreez@ucdavis.edu</a>> wrote:</div><br><div><div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote">On Sat, Nov 22, 2014 at 2:11 AM, 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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div><br><div><blockquote type="cite"><div>On Nov 21, 2014, at 5:01 PM, Daniel DeFreez <<a href="mailto:dcdefreez@ucdavis.edu" target="_blank">dcdefreez@ucdavis.edu</a>> wrote:</div><br><div><br><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">On Sat, Nov 22, 2014 at 12:48 AM, Anna Zaks<span> </span></span><span dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><<a href="mailto:ganna@apple.com" target="_blank">ganna@apple.com</a>></span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important"> </span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">wrote:</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><br><div><span><blockquote type="cite"><div>On Nov 21, 2014, at 3:35 PM, Daniel DeFreez <<a href="mailto:dcdefreez@ucdavis.edu" target="_blank">dcdefreez@ucdavis.edu</a>> wrote:</div><br><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><span><blockquote type="cite"><div><div dir="ltr"><div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div> I believe with your patch, the node will get added, but the definition used will not have the body attached to it, so the analyzer will not process the body. </div></div></blockquote><div><br></div><div>Hmm, doesn't the analyzer itself use getBody() to access the body, thereby walking the list of redeclarations? It seems like it is still processing the body of the function, but maybe I don't know what to look for.</div></div></div></div></div></div></blockquote></span><div>This is a good point. We might not be gaining anything by replacing the decl with the one that has a body since getBody is guaranteed to search the redeclarations regardless.</div><div><br></div><div>How about just removing both calls to isThisDeclarationADefinition() (from includeInGraph) and replacing the call to getBody with hasBody (to avoid the unnecessary AST de-serialization of the body)? That should work and would simplify the function greatly. What do you think?</div></div></div></blockquote><div><br></div><div>I think replacing getBody() with hasBody() is a good idea regardless of the approach taken to fix the missing edges. And avoiding node replacement would make it cleaner.<br></div><div> <br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div>I am not sure why some of the checks in your first patch were necessary (like this one).</div><span><div>+ // Skip definitions with previous declarations</div><div>+ if (FD->getPreviousDecl())<br>+ return false;<br>+<br></div></span><br></div></div></blockquote><div> </div></div></div><div class="gmail_extra">The reason I put that in was to avoid inserting redundant nodes. Just removing isThisDeclarationADefinition() yields:<br><br></div></div></div></blockquote><div><br></div></span>Oh, I see! getOrInsertNode() would not insert an existing declaration, but it does not check for re-declarations.</div><div><br></div><div>But would your approach work for this set?</div><div>void a();</div><div><div>void a() { }</div>void b() { a(); } // Here, a call to "a()" might not be added since "a()" has a previous declaration.<br></div></div></blockquote><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"> <br>Alas, yes, you're right. I can't believe I didn't try that. Not going to work.<br><br></div><blockquote class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div></div><div>Sounds like we would want to store canonical declarations, which brings us back to the suggestion of using "hasBody(Definition)" and only store the definitions that have bodies. Changing the signature of "includeInGraph" so that it mutates the Decl is fine; though, you might want to rename it as well.<br><br></div></div></blockquote><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"> </div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Yep. The problem isn't with includeInGraph, though. It's with hasBody(Definition). Since it takes a reference to a pointer-to-const, using it get the canonical declaration means the canonical declaration itself must be const. So pretty much all of the Decl* in CallGraph would have to get changed to const.<br><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">I only know of two ways around it:<br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">1) Change the FunctionDecl class<br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">2) Walk redecls in some fashion similar to my second patch, which essentially just re-implemented hasBody without forcing the declaration to be const.<br><br>Any other way?</div></div></blockquote></div><div><br></div></div></div>Looks like we can use getCanonicalDecl() instead (in both FunctionDecl and ObjCMethodDecl case).<br><div><div style="margin:0px;font-size:14px;font-family:Menlo"><br></div></div></div></blockquote><div> </div><div>Ah! Excellent. How aptly named :)<br><br></div></div></div></div></div></blockquote><br></div></div>:)</div><div><br></div><div>I'll be on vacation next week. </div><div><br></div><div>Seems like we have a solution here. I might be able to approve the patch, but won't be able to apply it. You should send the patch to cfe-dev and CC me.</div><div><br></div><div>Thank you!</div><div>Anna.</div><div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Many thanks.<br></div></div><br></div></div>
</div></blockquote></div><br></div></blockquote></div><br></div>