[llvm-dev] [cfe-dev] Scope differentiate.

Richard Smith via llvm-dev llvm-dev at lists.llvm.org
Fri May 26 11:11:29 PDT 2017


On 26 May 2017 at 09:54, Umesh Kalappa via cfe-dev <cfe-dev at lists.llvm.org>
wrote:

> Hi All ,
>
> We added the below code to differentiate Local  and Global Variable in
> the "NamedDecl *Sema::HandleDeclarator" function.
>
> if(S->getFlags() == Scope::DeclScope)
>  its global
> else
>  its local
>
> Where S  is a pointer parameter to the  HandleDeclarator of type Scope.
>
> At first place ,we decided  to use
> "D.getDeclSpec().getStorageClassSpec()" ,but has no luck ,since its
> always "SCS_unspecified " .
>
> Question is that , do using the  "Scope::DeclScope" to differentiate
> the variable scope is the  right approach, if so
>
> we have a problem for some instance like a nested  local declaration,
> we are not able to differentiate.
>
> if the "Scope::DeclScope" is not the right approach, then  please
> anybody here , help us with the right approach and would like to
> inform you all that, we need this info before any Act call like
> "ActOnVariableDeclarator"


HandleDeclarator computes the DeclContext for the declarator; you can check
DC->getRedeclContext()->isFileContext() to find if that's global scope
(translation unit or namespace scope, or extern "C" context within them,
that kind of thing).

But that just tells you where the variable was declared, which is not the
same as whether it's local or global. You'll also need to check for
SCS_extern to handle local extern declarations, and possibly also for
SCS_static if you want to treat static local variables as global rather
than local. If you also care about C++, you'd also need to decide how you
want to handle static class members (for which DC would be a CXXRecordDecl).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170526/46be9c86/attachment.html>


More information about the llvm-dev mailing list