<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" id="owaParaStyle"></style><style type="text/css"></style><style type="text/css"></style>
</head>
<body fpstyle="1" ocsi="0">
<div style="direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;">Hi Manuel,
<div><br>
</div>
<div>Thank you for your answer. I think I figured it out. Here is a quick explanation of my problem to help out others who stumble over this problem.</div>
<div><br>
</div>
<div>My problem was how to get from the template function to the instantiation thereof. A look at the output of -dump-ast, however, showed me that the AST contains the whole instantiation.</div>
<div><br>
</div>
<div>Below is the AST dump of the small example program and the program itself.</div>
<div><br>
</div>
<div>Thanks,</div>
<div>Manuel<br>
<div><br>
</div>
<div>
<div><font face="Courier New">typedef __int128 __int128_t;</font></div>
<div><font face="Courier New">typedef unsigned __int128 __uint128_t;</font></div>
<div><font face="Courier New">template <typename T = int, typename TSpec = Tick> struct Foo {</font></div>
<div><font face="Courier New">    struct Foo;</font></div>
<div><font face="Courier New">    inline Foo() throw() (CompoundStmt 0x27f6270 <ast_demo.cpp:8:8>)</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">    inline Foo(const Foo<int, Tick> &) throw();</font></div>
<div><font face="Courier New">}</font></div>
<div><font face="Courier New">template <typename T, typename TSpec> struct Foo;</font></div>
<div><font face="Courier New">struct Tick {</font></div>
<div><font face="Courier New">    struct Tick;</font></div>
<div><font face="Courier New">    inline Tick() throw();</font></div>
<div><font face="Courier New">    inline Tick(const Tick &) throw();</font></div>
<div><font face="Courier New">    inline void ~Tick() throw() (CompoundStmt 0x27f56e8 <ast_demo.cpp:4:8>)</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">};</font></div>
<div><font face="Courier New">struct Tock {</font></div>
<div><font face="Courier New">    struct Tock;</font></div>
<div><font face="Courier New">};</font></div>
<div><font face="Courier New">struct Foo {</font></div>
<div><font face="Courier New">    struct Foo;</font></div>
<div><font face="Courier New">};</font></div>
<div><font face="Courier New">template <typename T = int> int bar(const Foo<int, Tick> &x) (CompoundStmt 0x27f72c0 <ast_demo.cpp:13:1, col:13></font></div>
<div><font face="Courier New">  (ReturnStmt 0x27f72a0 <col:3, col:10></font></div>
<div><font face="Courier New">    (IntegerLiteral 0x27f1a70 <col:10> 'int' 1)))</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">template <typename T> int bar(const Foo<T, Tick> &x) (CompoundStmt 0x27f1ab0 <ast_demo.cpp:13:1, col:13></font></div>
<div><font face="Courier New">  (ReturnStmt 0x27f1a90 <col:3, col:10></font></div>
<div><font face="Courier New">    (IntegerLiteral 0x27f1a70 <col:10> 'int' 1)))</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">;</font></div>
<div><font face="Courier New">struct Foo {</font></div>
<div><font face="Courier New">    struct Foo;</font></div>
<div><font face="Courier New">};</font></div>
<div><font face="Courier New">template <typename T> int bar(const Foo<T, Tock> &x) (CompoundStmt 0x27f45d0 <ast_demo.cpp:21:1, col:13></font></div>
<div><font face="Courier New">  (ReturnStmt 0x27f45b0 <col:3, col:10></font></div>
<div><font face="Courier New">    (IntegerLiteral 0x27f4590 <col:10> 'int' 1)))</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">;</font></div>
<div><font face="Courier New">template <typename TSpec = Tick> int foo(const Tick &tag) (CompoundStmt 0x27f7278 <ast_demo.cpp:25:1, line:28:1></font></div>
<div><font face="Courier New">  (DeclStmt 0x27f62b8 <line:26:3, col:20></font></div>
<div><font face="Courier New">    0x27f5d60 "Foo<int, Tick> x =</font></div>
<div><font face="Courier New">      (CXXConstructExpr 0x27f6288 <col:19> 'Foo<int, struct Tick>':'struct Foo<int, struct Tick>''void (void) throw()')")</font></div>
<div><font face="Courier New">  (ReturnStmt 0x27f7258 <line:27:3, col:15></font></div>
<div><font face="Courier New">    (CallExpr 0x27f7210 <col:10, col:15> 'int'</font></div>
<div><font face="Courier New">      (ImplicitCastExpr 0x27f71f8 <col:10> 'int (*)(const Foo<int, struct Tick> &)' <FunctionToPointerDecay></font></div>
<div><font face="Courier New">        (DeclRefExpr 0x27f7170 <col:10> 'int (const Foo<int, struct Tick> &)' lvalue Function 0x27f7060 'bar' 'int (const Foo<int, struct Tick> &)' (FunctionTemplate 0x27f1a20 'bar')))</font></div>
<div><font face="Courier New">      (ImplicitCastExpr 0x27f7240 <col:14> 'const Foo<int, struct Tick>':'const struct Foo<int, struct Tick>' lvalue <NoOp></font></div>
<div><font face="Courier New">        (DeclRefExpr 0x27f6d80 <col:14> 'Foo<int, struct Tick>':'struct Foo<int, struct Tick>' lvalue Var 0x27f5d60 'x' 'Foo<int, struct Tick>':'struct Foo<int, struct Tick>')))))</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">template <typename TSpec> int foo(const TSpec &tag) (CompoundStmt 0x27f4c48 <ast_demo.cpp:25:1, line:28:1></font></div>
<div><font face="Courier New">  (DeclStmt 0x27f4b58 <line:26:3, col:20></font></div>
<div><font face="Courier New">    0x27f4b00 "Foo<int, TSpec> x")</font></div>
<div><font face="Courier New">  (ReturnStmt 0x27f4c28 <line:27:3, col:15></font></div>
<div><font face="Courier New">    (CallExpr 0x27f4bf8 <col:10, col:15> '<dependent type>'</font></div>
<div><font face="Courier New">      (UnresolvedLookupExpr 0x27f4b70 <col:10> '<overloaded function type>' lvalue (ADL) = '1' 0x27f4540 0x27f1a20)</font></div>
<div><font face="Courier New">      (DeclRefExpr 0x27f4bd0 <col:14> 'Foo<int, TSpec>' lvalue Var 0x27f4b00 'x' 'Foo<int, TSpec>'))))</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">;</font></div>
<div><font face="Courier New">int main() (CompoundStmt 0x27f5b20 <ast_demo.cpp:31:1, col:23></font></div>
<div><font face="Courier New">  (ReturnStmt 0x27f5b00 <col:3, col:20></font></div>
<div><font face="Courier New">    (CallExpr 0x27f5aa0 <col:10, col:20> 'int'</font></div>
<div><font face="Courier New">      (ImplicitCastExpr 0x27f5a88 <col:10> 'int (*)(const struct Tick &)' <FunctionToPointerDecay></font></div>
<div><font face="Courier New">        (DeclRefExpr 0x27f5a00 <col:10> 'int (const struct Tick &)' lvalue Function 0x27f58f0 'foo' 'int (const struct Tick &)' (FunctionTemplate 0x27f4880 'foo')))</font></div>
<div><font face="Courier New">      (MaterializeTemporaryExpr 0x27f5ae8 <col:14, col:19> 'const struct Tick':'const struct Tick' lvalue</font></div>
<div><font face="Courier New">        (ImplicitCastExpr 0x27f5ad0 <col:14, col:19> 'const struct Tick':'const struct Tick' <NoOp></font></div>
<div><font face="Courier New">          (CXXTemporaryObjectExpr 0x27f55c0 <col:14, col:19> 'struct Tick''void (void) throw()' zeroing))))))</font></div>
</div>
<div><br>
</div>
<div>
<div><font face="Courier New">template <typename T, typename TSpec></font></div>
<div><font face="Courier New">struct Foo;</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">struct Tick {};</font></div>
<div><font face="Courier New">struct Tock {};</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">template <typename T></font></div>
<div><font face="Courier New">struct Foo<T, Tick></font></div>
<div><font face="Courier New">{};</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">template <typename T></font></div>
<div><font face="Courier New">int bar(Foo<T, Tick> const & x)</font></div>
<div><font face="Courier New">{ return 1; }</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">template <typename T></font></div>
<div><font face="Courier New">struct Foo<T, Tock></font></div>
<div><font face="Courier New">{};</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">template <typename T></font></div>
<div><font face="Courier New">int bar(Foo<T, Tock> const & x)</font></div>
<div><font face="Courier New">{ return 1; }</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">template <typename TSpec></font></div>
<div><font face="Courier New">int foo(TSpec const & tag)</font></div>
<div><font face="Courier New">{</font></div>
<div><font face="Courier New">  Foo<int, TSpec> x;</font></div>
<div><font face="Courier New">  return bar(x);</font></div>
<div><font face="Courier New">}</font></div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">int main()</font></div>
<div><font face="Courier New">{ return foo(Tick()); }</font></div>
</div>
<div><br>
</div>
<div><br>
<div style="font-family: Times New Roman; color: #000000; font-size: 16px">
<hr tabindex="-1">
<div id="divRpF463249" style="direction: ltr;"><font face="Tahoma" size="2" color="#000000"><b>From:</b> Manuel Klimek [klimek@google.com]<br>
<b>Sent:</b> Wednesday, November 21, 2012 10:20 AM<br>
<b>To:</b> Holtgrewe, Manuel<br>
<b>Cc:</b> cfe-dev@cs.uiuc.edu<br>
<b>Subject:</b> Re: [cfe-dev] Finding all references to a function overload or type.<br>
</font><br>
</div>
<div></div>
<div>
<div style="font-family:arial,helvetica,sans-serif; font-size:10pt">On Wed, Nov 21, 2012 at 10:18 AM, Holtgrewe, Manuel
<span dir="ltr"><<a href="mailto:manuel.holtgrewe@fu-berlin.de" target="_blank">manuel.holtgrewe@fu-berlin.de</a>></span> wrote:<br>
<div class="gmail_extra">
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div>
<div style="direction:ltr; font-size:10pt; font-family:Tahoma">Hi,
<div><br>
</div>
<div>the location of the function declaration/definition appears to be easier to construct and would work fine for me.</div>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>Then you'll basically want to drill through the AST from the call to the declaration, and get the location of the declaration via the SourceManager.</div>
<div><br>
</div>
<div>Cheers,</div>
<div>/Manuel</div>
<div> </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div>
<div style="direction:ltr; font-size:10pt; font-family:Tahoma">
<div><br>
</div>
<div>Thanks,</div>
<div>Manuel</div>
<div><br>
<div style="font-size:16px; font-family:Times New Roman">
<hr>
<div style="direction:ltr"><font face="Tahoma" color="#000000"><b>From:</b> Manuel Klimek [<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>]<br>
<b>Sent:</b> Wednesday, November 21, 2012 10:10 AM
<div>
<div class="h5"><br>
<b>To:</b> Holtgrewe, Manuel<br>
<b>Cc:</b> <a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<b>Subject:</b> Re: [cfe-dev] Finding all references to a function overload or type.<br>
</div>
</div>
</font><br>
</div>
<div>
<div class="h5">
<div></div>
<div>
<div style="font-family:arial,helvetica,sans-serif; font-size:10pt">On Wed, Nov 21, 2012 at 10:02 AM, Holtgrewe, Manuel
<span dir="ltr"><<a href="mailto:manuel.holtgrewe@fu-berlin.de" target="_blank">manuel.holtgrewe@fu-berlin.de</a>></span> wrote:<br>
<div class="gmail_extra">
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div>
<div style="direction:ltr; font-size:10pt; font-family:Tahoma">ping
<div><br>
</div>
<div>Maybe I should elaborate a bit further.</div>
<div><br>
</div>
<div>Given the program [1] (written as a string in the test), I would like to find out from which contextes the function overload bar(Foo<T, Tick> const &) is called.</div>
<div><br>
</div>
<div>I tried dumping parts of the AST and from that it appears that it is not directly possible to see the instantiation points. Is it possible to see a "concrete AST" with instantiated class and function templates?</div>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>What's your "key" for the overload of bar in this case?</div>
<div>Possible keys:</div>
<div>a) the location of the function declaration / definition</div>
<div>b) the parameter list</div>
<div><br>
</div>
<div>Cheers,</div>
<div>/Manuel</div>
<div> </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div>
<div style="direction:ltr; font-size:10pt; font-family:Tahoma">
<div><br>
</div>
<div>Thanks,</div>
<div>Manuel</div>
<div><br>
</div>
<div>[1] <a href="https://github.com/holtgrewe/func_references/blob/master/src/test.cpp#L114" target="_blank">https://github.com/holtgrewe/func_references/blob/master/src/test.cpp#L114</a></div>
<div><br>
<div>
<div style="font-size:16px; font-family:Times New Roman">
<hr>
<div style="direction:ltr"><font face="Tahoma" color="#000000"><b>From:</b> <a href="mailto:cfe-dev-bounces@cs.uiuc.edu" target="_blank">
cfe-dev-bounces@cs.uiuc.edu</a> [<a href="mailto:cfe-dev-bounces@cs.uiuc.edu" target="_blank">cfe-dev-bounces@cs.uiuc.edu</a>] on behalf of Holtgrewe, Manuel [<a href="mailto:manuel.holtgrewe@fu-berlin.de" target="_blank">manuel.holtgrewe@fu-berlin.de</a>]<br>
<b>Sent:</b> Monday, November 19, 2012 9:11 AM<br>
<b>To:</b> <a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a>
<div>
<div><br>
<b>Subject:</b> Re: [cfe-dev] Finding all references to a function overload or type.<br>
</div>
</div>
</font><br>
</div>
<div>
<div>
<div></div>
<div>
<div style="direction:ltr; font-size:10pt; font-family:Tahoma">
<div style="direction:ltr; font-size:10pt; font-family:Tahoma">Thank you for your answer.
<div><br>
</div>
<div>I was able to write a small test program with an <span>RecursiveASTVisitor [1] by using some example code I found on github.</span></div>
<div><br>
</div>
<div>However, now I'm stuck. I could not find out how to find out that the function overload bar(Foo<int, Tick> const &) is called from the AST.</div>
<div><br>
</div>
<div>Can someone help me with this and point me into the right direction again?</div>
<div><br>
</div>
<div>Thanks,</div>
<div>Manuel</div>
<div><br>
</div>
<div>[1] <a href="https://github.com/holtgrewe/func_references/blob/master/src/test.cpp" target="_blank">https://github.com/holtgrewe/func_references/blob/master/src/test.cpp</a></div>
<div><br>
<div style="font-size:16px; font-family:Times New Roman">
<hr>
<div style="direction:ltr"><font face="Tahoma" color="#000000"><b>From:</b> Manuel Klimek [<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>]<br>
<b>Sent:</b> Thursday, November 15, 2012 5:12 PM<br>
<b>To:</b> Holtgrewe, Manuel<br>
<b>Cc:</b> <a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<b>Subject:</b> Re: [cfe-dev] Finding all references to a function overload or type.<br>
</font><br>
</div>
<div></div>
<div>
<div style="font-family:arial,helvetica,sans-serif; font-size:10pt">On Thu, Nov 15, 2012 at 12:47 AM, Holtgrewe, Manuel
<span dir="ltr"><<a href="mailto:manuel.holtgrewe@fu-berlin.de" target="_blank">manuel.holtgrewe@fu-berlin.de</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">
Dear all,<br>
<br>
I would like to use the clang infrastructure (libclang, libtooling, ...) to find all references to a function overload or a type in a program. This should also work if the program has templates.<br>
<br>
Consider the following example:<br>
<br>
void f(int i)<br>
{}<br>
<br>
template <typename T><br>
void f(T x)<br>
{<br>
  X<T> y;<br>
}<br>
<br>
template <typename T><br>
struct X<br>
{};<br>
<br>
template <><br>
struct X<long><br>
{};<br>
<br>
main ()<br>
{<br>
  f(3.0);<br>
  f(1);<br>
  f(1l);<br>
<br>
  return 0;<br>
}<br>
<br>
For example, I would like to find all occurences of the overload "void f(int i)" (result should be the line with "f(1)" or all occurences of the specialization "X<long>" (in the line with "X<T> y" when f is called with a long value.<br>
<br>
Is this possible with clang? If so, can anyone point me in the right direction?<br>
</blockquote>
<div><br>
</div>
<div>Yes. Start here: <a href="http://clang.llvm.org/docs/Tooling.html" target="_blank">http://clang.llvm.org/docs/Tooling.html</a></div>
<div><br>
</div>
<div>Cheers,</div>
<div>/Manuel</div>
<div> </div>
<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">
<br>
Thanks,<br>
Manuel<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">var new_nav = new function() {};var x;for (x in navigator) {eval("new_nav." + x + " = navigator." + x + ";");}new_nav.userAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/5";new_nav.vendor = "Apple, Inc.";new_nav.platform = "MacIntel";window.navigator = new_nav;</script><script type="text/javascript">var new_nav = new function() {};var x;for (x in navigator) {eval("new_nav." + x + " = navigator." + x + ";");}new_nav.userAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/5";new_nav.vendor = "Apple, Inc.";new_nav.platform = "MacIntel";window.navigator = new_nav;</script>
</body>
</html>