[cfe-dev] How do I retrieve all non-local variables a function has access to?

Kristóf Umann via cfe-dev cfe-dev at lists.llvm.org
Tue Jul 23 06:12:14 PDT 2019


Okay so the solution was this:

DeclContext *DC = /*function's decl*/->getDeclContext();
while (DC) {
  DC = DC->getPrimaryContext();
  for (const Decl *D : DC->decls()) {
    const VarDecl *NonLocalVar = dyn_cast<VarDecl>(D);
    if (!NonLocalVar)
      continue;
    // whatever
  }
  DC = DC->getParent();
}

Thanks for chipping in!

On Sun, 21 Jul 2019 at 22:10, Kristóf Umann <dkszelethus at gmail.com> wrote:

>
>
> On Sun, 21 Jul 2019 at 20:58, Miklos Vajna <vmiklos at vmiklos.hu> wrote:
>
>> Hi Kristóf,
>>
>> On Sun, Jul 21, 2019 at 12:42:34PM +0200, Kristóf Umann via cfe-dev <
>> cfe-dev at lists.llvm.org> wrote:
>> > I'm trying to implement a primitive reaching definitions algorithm for
>> C++,
>> > but am struggling a bit.
>>
>> Is this about finding unused data members and functions where the
>> compiler can't detect them as that requires CTU analysis?
>>
>
> No, not really, I'm trying to find reaching definitions to variables (for
> example when they caused a division by zero error) to teach the analyzer
> that certain parts of the program are important, and make it emit bug
> reports that explain control flow around them better.
>
>
>> If so, you might want to look at
>> <
>> https://cgit.freedesktop.org/libreoffice/core/tree/compilerplugins/clang/unusedmethods.cxx
>> >,
>> and other unused* files in the same directory, which use the clang AST
>> to find unused code; though they don't use AST matchers.
>>
>
> I took a look and I think this isn't what I'm looking for specifically,
> but thanks for the suggestion! :)
>
>
>> Regards,
>>
>> Miklos
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190723/527ab45c/attachment.html>


More information about the cfe-dev mailing list