using clang - getting type for C variable declaration

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

using clang - getting type for C variable declaration

Rajendra
Hi,

Using clang API, I want to get type for a declaration.

e.g. declaration is: int x; I am getting variable name as x but I am
not able to get type of x.

If I can get clang::QualType then I can see type, but I am not getting
how to get clang::QualType from clang::Decl

   clang::DeclGroupRef::iterator it;

   for (it = declGroupRef.begin(); it != declGroupRef.end(); it++)
   {

     clang::Decl* decl = *it;
     std::cerr << "\tdecl statement: ";
     decl->dump();        // --> this gives `int x'

     const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);

     if (namedDecl)
     {
       std::cerr << "\t\tidentifier name = "
         << namedDecl->getNameAsString() << "\n";        // --> this
gives `x'
     }

   }

Any pointers?

Rajendra
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: using clang - getting type for C variable declaration

João Matos
You can use ValueDecl::getType.

http://clang.llvm.org/doxygen/classclang_1_1ValueDecl.html

On Thu, Nov 8, 2012 at 4:35 PM, Rajendra <[hidden email]> wrote:
Hi,

Using clang API, I want to get type for a declaration.

e.g. declaration is: int x; I am getting variable name as x but I am not able to get type of x.

If I can get clang::QualType then I can see type, but I am not getting how to get clang::QualType from clang::Decl

  clang::DeclGroupRef::iterator it;

  for (it = declGroupRef.begin(); it != declGroupRef.end(); it++)
  {

    clang::Decl* decl = *it;
    std::cerr << "\tdecl statement: ";
    decl->dump();        // --> this gives `int x'

    const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);

    if (namedDecl)
    {
      std::cerr << "\t\tidentifier name = "
        << namedDecl->getNameAsString() << "\n";        // --> this gives `x'
    }

  }

Any pointers?

Rajendra
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



--
João Matos

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: using clang - getting type for C variable declaration

Rajendra
In reply to this post by Rajendra
Hi,

1) I need to get type as string from Expr object. Any pointers?

   Expr* lhs = E->getLHS();

   if (strcmp(lhs->getStmtClassName(), "DeclRefExpr") == 0)
   {
     // get name and type for Expr *lhs from DeclRefExpr
     const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr>(lhs);

     const ValueDecl *valueDecl = declRefExpr->getDecl();

     if (valueDecl)
     {
       std::cerr << "\tLHS identifier = " <<
valueDecl->getNameAsString() << "\n"; // this gives x

       QualType declQT = valueDecl->getType();
       std::cerr << "\tqual type: ";
       declQT.dump();  // this prints  : int identifier

       // now call QualType.getAsString(PrintingPolicy &Policy)
       // ---> how to get compiler instance and AST context? <---- //
       clang::ASTContext &context =
compilerInstance_ptr->getASTContext();
       std::cerr << "\ttype: " <<
declQT.getAsString(context.getPrintingPolicy()) << "\n";
     }
   }

2) Here is how I got things right for Decl :) this will be helpful for
others as well.

   clang::DeclGroupRef::iterator it;

   for (it = declGroupRef.begin(); it != declGroupRef.end(); it++)
   {
     clang::Decl* decl = *it;

     const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);

     if (namedDecl)
     {
       std::cerr << "\tidentifier name = "
         << namedDecl->getNameAsString() << "\n";
     }

     const ValueDecl *valueDecl = dyn_cast<ValueDecl>(decl);

     if (valueDecl)
     {
       QualType declQT = valueDecl->getType();

       // now call QualType.getAsString(PrintingPolicy &Policy)
       clang::ASTContext &context = decl->getASTContext();
       std::cerr << "\ttype = " <<
declQT.getAsString(context.getPrintingPolicy()) << "\n";
     }
   }


Rajendra
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: using clang - getting type for C variable declaration

David Blaikie
On Fri, Nov 9, 2012 at 2:12 AM, Rajendra <[hidden email]> wrote:
> Hi,
>
> 1) I need to get type as string from Expr object. Any pointers?

Presumably Expr::getType():
http://clang.llvm.org/doxygen/classclang_1_1Expr.html#a3dd8850a4ad8a5b5f595dd9e9446187b

>
>   Expr* lhs = E->getLHS();
>
>   if (strcmp(lhs->getStmtClassName(), "DeclRefExpr") == 0)
>   {
>     // get name and type for Expr *lhs from DeclRefExpr
>     const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr>(lhs);

The usual way we write these last two lines is:

if (const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr(lhs))

no string comparison required
no double-check (the string check and then the check in the dyn_cast)

For more details on LLVM's casting machinery:
http://llvm.org/docs/ProgrammersManual.html#isa

>
>     const ValueDecl *valueDecl = declRefExpr->getDecl();
>
>     if (valueDecl)

Just a stylistic point related to my first (in case you're planning to
be a Clang/LLVM contributor): we usually collapse the variable
declaration into the condition.

>     {
>       std::cerr << "\tLHS identifier = " << valueDecl->getNameAsString() <<
> "\n"; // this gives x
>
>       QualType declQT = valueDecl->getType();
>       std::cerr << "\tqual type: ";
>       declQT.dump();  // this prints  : int identifier
>
>       // now call QualType.getAsString(PrintingPolicy &Policy)
>       // ---> how to get compiler instance and AST context? <---- //
>       clang::ASTContext &context = compilerInstance_ptr->getASTContext();
>       std::cerr << "\ttype: " <<
> declQT.getAsString(context.getPrintingPolicy()) << "\n";
>     }
>   }
>
> 2) Here is how I got things right for Decl :) this will be helpful for
> others as well.
>
>
>   clang::DeclGroupRef::iterator it;
>
>   for (it = declGroupRef.begin(); it != declGroupRef.end(); it++)
>   {
>     clang::Decl* decl = *it;
>
>     const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
>
>     if (namedDecl)
>     {
>       std::cerr << "\tidentifier name = "
>         << namedDecl->getNameAsString() << "\n";
>     }
>
>     const ValueDecl *valueDecl = dyn_cast<ValueDecl>(decl);
>
>     if (valueDecl)
>     {
>       QualType declQT = valueDecl->getType();
>
>       // now call QualType.getAsString(PrintingPolicy &Policy)
>       clang::ASTContext &context = decl->getASTContext();
>       std::cerr << "\ttype = " <<
> declQT.getAsString(context.getPrintingPolicy()) << "\n";
>
>     }
>   }
>
>
> Rajendra
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: using clang - getting type for C variable declaration

Rajendra
Hi,

Actually, my question is more about getting type as string from Expr or
QualType.

Say I have,
       QualType declQT = valueDecl->getType();
or
       QualType lhsQT = lhs->getType();  // lhs is on Expr* type

Now, I want to call QualType.getAsString(PrintingPolicy &Policy) and I
need compiler instance or AST context to get printing policy, how to get
compiler instance or AST context here?
       //compilerInstance_ptr?
       clang::ASTContext &context =
compilerInstance_ptr->getASTContext();
       std::cerr << "\ttype: " <<
declQT.getAsString(context.getPrintingPolicy()) << "\n";

Using Decl, I could do this like decl->getASTContext(); - but same I
cannot do here!

I am overriding method bool VisitBinAssign(BinaryOperator *E);

Rajendra

On 09-11-2012 09:48 PM, David Blaikie wrote:

> On Fri, Nov 9, 2012 at 2:12 AM, Rajendra <[hidden email]> wrote:
>> Hi,
>>
>> 1) I need to get type as string from Expr object. Any pointers?
>
> Presumably Expr::getType():
>
> http://clang.llvm.org/doxygen/classclang_1_1Expr.html#a3dd8850a4ad8a5b5f595dd9e9446187b
>
>>
>>   Expr* lhs = E->getLHS();
>>
>>   if (strcmp(lhs->getStmtClassName(), "DeclRefExpr") == 0)
>>   {
>>     // get name and type for Expr *lhs from DeclRefExpr
>>     const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr>(lhs);
>
> The usual way we write these last two lines is:
>
> if (const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr(lhs))
>
> no string comparison required
> no double-check (the string check and then the check in the dyn_cast)
>
> For more details on LLVM's casting machinery:
> http://llvm.org/docs/ProgrammersManual.html#isa
>
>>
>>     const ValueDecl *valueDecl = declRefExpr->getDecl();
>>
>>     if (valueDecl)
>
> Just a stylistic point related to my first (in case you're planning
> to
> be a Clang/LLVM contributor): we usually collapse the variable
> declaration into the condition.
>
>>     {
>>       std::cerr << "\tLHS identifier = " <<
>> valueDecl->getNameAsString() <<
>> "\n"; // this gives x
>>
>>       QualType declQT = valueDecl->getType();
>>       std::cerr << "\tqual type: ";
>>       declQT.dump();  // this prints  : int identifier
>>
>>       // now call QualType.getAsString(PrintingPolicy &Policy)
>>       // ---> how to get compiler instance and AST context? <---- //
>>       clang::ASTContext &context =
>> compilerInstance_ptr->getASTContext();
>>       std::cerr << "\ttype: " <<
>> declQT.getAsString(context.getPrintingPolicy()) << "\n";
>>     }
>>   }
>>
>> 2) Here is how I got things right for Decl :) this will be helpful
>> for
>> others as well.
>>
>>
>>   clang::DeclGroupRef::iterator it;
>>
>>   for (it = declGroupRef.begin(); it != declGroupRef.end(); it++)
>>   {
>>     clang::Decl* decl = *it;
>>
>>     const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
>>
>>     if (namedDecl)
>>     {
>>       std::cerr << "\tidentifier name = "
>>         << namedDecl->getNameAsString() << "\n";
>>     }
>>
>>     const ValueDecl *valueDecl = dyn_cast<ValueDecl>(decl);
>>
>>     if (valueDecl)
>>     {
>>       QualType declQT = valueDecl->getType();
>>
>>       // now call QualType.getAsString(PrintingPolicy &Policy)
>>       clang::ASTContext &context = decl->getASTContext();
>>       std::cerr << "\ttype = " <<
>> declQT.getAsString(context.getPrintingPolicy()) << "\n";
>>
>>     }
>>   }
>>
>>
>> Rajendra
>> _______________________________________________
>> cfe-dev mailing list
>> [hidden email]
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: using clang - getting type for C variable declaration

David Blaikie
On Fri, Nov 9, 2012 at 8:47 PM, Rajendra <[hidden email]> wrote:

> Hi,
>
> Actually, my question is more about getting type as string from Expr or
> QualType.
>
> Say I have,
>
>       QualType declQT = valueDecl->getType();
> or
>       QualType lhsQT = lhs->getType();  // lhs is on Expr* type
>
> Now, I want to call QualType.getAsString(PrintingPolicy &Policy) and I need
> compiler instance or AST context to get printing policy, how to get compiler
> instance or AST context here?
>       //compilerInstance_ptr?
>
>       clang::ASTContext &context = compilerInstance_ptr->getASTContext();
>       std::cerr << "\ttype: " <<
> declQT.getAsString(context.getPrintingPolicy()) << "\n";
>
> Using Decl, I could do this like decl->getASTContext(); - but same I cannot
> do here!

Off-hand, the simplest way I could find to do this was to look at how
QualType's dump() member is implemented:

It simply create a PrintingPolicy directly with a default LangOptions
( http://clang.llvm.org/doxygen/classclang_1_1QualType.html#a83734a19d59252c9527473a32cb44a12
). This may suffice for your needs. Otherwise I assume there's some
way to get the current ASTContext in your visitor, or perhaps track it
separately.

>
> I am overriding method bool VisitBinAssign(BinaryOperator *E);
>
> Rajendra
>
>
> On 09-11-2012 09:48 PM, David Blaikie wrote:
>>
>> On Fri, Nov 9, 2012 at 2:12 AM, Rajendra <[hidden email]> wrote:
>>>
>>> Hi,
>>>
>>> 1) I need to get type as string from Expr object. Any pointers?
>>
>>
>> Presumably Expr::getType():
>>
>>
>> http://clang.llvm.org/doxygen/classclang_1_1Expr.html#a3dd8850a4ad8a5b5f595dd9e9446187b
>>
>>>
>>>   Expr* lhs = E->getLHS();
>>>
>>>   if (strcmp(lhs->getStmtClassName(), "DeclRefExpr") == 0)
>>>   {
>>>     // get name and type for Expr *lhs from DeclRefExpr
>>>     const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr>(lhs);
>>
>>
>> The usual way we write these last two lines is:
>>
>> if (const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr(lhs))
>>
>> no string comparison required
>> no double-check (the string check and then the check in the dyn_cast)
>>
>> For more details on LLVM's casting machinery:
>> http://llvm.org/docs/ProgrammersManual.html#isa
>>
>>>
>>>     const ValueDecl *valueDecl = declRefExpr->getDecl();
>>>
>>>     if (valueDecl)
>>
>>
>> Just a stylistic point related to my first (in case you're planning to
>> be a Clang/LLVM contributor): we usually collapse the variable
>> declaration into the condition.
>>
>>>     {
>>>       std::cerr << "\tLHS identifier = " << valueDecl->getNameAsString()
>>> <<
>>> "\n"; // this gives x
>>>
>>>       QualType declQT = valueDecl->getType();
>>>       std::cerr << "\tqual type: ";
>>>       declQT.dump();  // this prints  : int identifier
>>>
>>>       // now call QualType.getAsString(PrintingPolicy &Policy)
>>>       // ---> how to get compiler instance and AST context? <---- //
>>>       clang::ASTContext &context = compilerInstance_ptr->getASTContext();
>>>       std::cerr << "\ttype: " <<
>>> declQT.getAsString(context.getPrintingPolicy()) << "\n";
>>>     }
>>>   }
>>>
>>> 2) Here is how I got things right for Decl :) this will be helpful for
>>> others as well.
>>>
>>>
>>>   clang::DeclGroupRef::iterator it;
>>>
>>>   for (it = declGroupRef.begin(); it != declGroupRef.end(); it++)
>>>   {
>>>     clang::Decl* decl = *it;
>>>
>>>     const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
>>>
>>>     if (namedDecl)
>>>     {
>>>       std::cerr << "\tidentifier name = "
>>>         << namedDecl->getNameAsString() << "\n";
>>>     }
>>>
>>>     const ValueDecl *valueDecl = dyn_cast<ValueDecl>(decl);
>>>
>>>     if (valueDecl)
>>>     {
>>>       QualType declQT = valueDecl->getType();
>>>
>>>       // now call QualType.getAsString(PrintingPolicy &Policy)
>>>       clang::ASTContext &context = decl->getASTContext();
>>>       std::cerr << "\ttype = " <<
>>> declQT.getAsString(context.getPrintingPolicy()) << "\n";
>>>
>>>     }
>>>   }
>>>
>>>
>>> Rajendra
>>> _______________________________________________
>>> cfe-dev mailing list
>>> [hidden email]
>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev