<div dir="ltr"><div><div><div><div><div>It really depends on what you are actually trying to achieve, but `nm` will give you a list of function & variable names that are declared or external to a particular file. <br></div>If I do `nm` on a simple test-file here, I get:<br><br> U fflush<br>0000000000000000 T main<br>0000000000000000 D max_source<br> U printf<br> U puts<br> U stdout<br><br></div>This tells me that this file declares a function called `main` (T = Text = Code = Function) and a variable (D = Data = Variable) called `max_source`. It also uses `fflush`, `printf`, `puts` and `stdout` from somewhere else (in this case, I happen to know these are all in the C library).<br><br></div>Let's say my file also had a call to `func_1` with U in front, it would mean that a (non-standard library function, since `func_1` is not the name of any standard library function) is in a different file. You may then have to do `nm func.o` to find that this function is declared in `func.c` (assuming that's what produced `func.o` of course).<br><br></div>To get C++ names into human readable form, you probably want to run the output from `nm` into `c++filt` (it demangles the C++ mangled name into something like `func_1(int)` instead of `_Z6func_1i` or some whatever the mangled form of func_1 would be.<br><br></div><div>Putting this together to show which source produces which functions and variables, you'd want a bit of Python code, using a dictionary (or write it in C++ and use std::map if you prefer - my Python skills are low, so I may well have opted for that method).<br><br></div><div>The point about this is that you can put this together with about 15-20 lines of code, rather than a rather complex ASTVisitor that does about the same thing (of course, if you want to build a proper cross referencing tool, that lists that `func_1` is declared on line 4231 in `func.c`, and that it references the global variable `x` 13 times, 5 of which are writes, 8 of which are reads), then you can't do that using `nm`)<br></div><div><br>--<br></div>Mats<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 19 September 2017 at 18:09, Siddharth Shankar Swain via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Chris,<div>Thanks for the suggestion i was trying to use the patch <a href="https://reviews.llvm.org/D30691" target="_blank">https://reviews.llvm.<wbr>org/D30691</a> and ASTimporter concept for cross file analysis. Can u explain in details the approach u suggested ? What is <span style="font-size:12.8px">`nm` on the object files ? Can u suggest some approach to start on this cross file analysis tool. </span></div><div><span style="font-size:12.8px">Thanks,</span></div><div><span style="font-size:12.8px">Siddharth</span></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 19, 2017 at 9:42 PM, Chris Bieneman <span dir="ltr"><<a href="mailto:beanz@apple.com" target="_blank">beanz@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">(Apologies for the re-send Siddharth, I failed to cc the list)<br>
<span class="m_-3513806448846743659im m_-3513806448846743659HOEnZb"><br>
There is no existing tool that I'm aware of which performs this analysis on the AST. It is possible to do on an AST. You would just need to write an AST Visitor that finds declarations, definitions, and uses of functions.<br>
<br>
Is there a reason you need to do this at the AST level? With C code this analysis can be trivially performed with `nm` on the object files, so if you don't have a strong reason for needing to do this on the AST I'd just write a script to wrap `nm` rather than writing an AST Visitor.<br>
<br>
-Chris<br>
<br>
> On Sep 14, 2017, at 6:57 AM, Siddharth Shankar Swain via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
</span><div class="m_-3513806448846743659HOEnZb"><div class="m_-3513806448846743659h5">> Hi,<br>
> Can anyone help if for example we have multiple c files and we want to analyze their ASTs to find out any dependency between them ( like a function defined in one c file is used in other, any external variable etc ). How can we do it at the AST level? I mean is there any automated tool or flag for it in LLVM. If anyone has any idea please tell.<br>
> sincerely,<br>
> Siddharth<br>
</div></div><div class="m_-3513806448846743659HOEnZb"><div class="m_-3513806448846743659h5">> ______________________________<wbr>_________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</div></div></blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>