[cfe-dev] How do i check the type of a variable?

Douglas Gregor dgregor at apple.com
Wed Jun 22 14:06:53 PDT 2011


On Jun 22, 2011, at 1:31 PM, Adam Smalin wrote:

> I spent the last 2.5hr on this and failed.
> How do i do something simple like check the type of a variable?
> 
> I have a VarDecl. I tried checking for 1) the type (int vs my own struct), 2) if it is static 3) if it is volatile.
> isStaticDataMember() and isStaticLocal() both failed on "static int a=1;",

That could be a static global, depending on context.

> i did not see anything for volatile including when i tried to find qualifier flags to do an & IsVolatile. I messed around with get context, definition, getCanonicalDecl, getType(), etc. Some of them seem to give me the very same object back.

The context is the parent in the AST; in your example, the variable "a" is also the definition and, if that's the only declaration of that variable, also the canonical declaration.

getType() returns a QualType with complete type information. Look at that class to find out how to extract the information you need, e.g., isVolatileQualified().

> I can check if something is a "Var" or "Record" but simply getting the name of something is a challenge. This is different from every reflector and AST i have worked with in the past.

getDeclName() or getIdentifier() gets the name of a NamedDecl. 

> Is there some documentation or manual i can read? I dont think i'll understand this by looking at definitions and headers.

There is some information here:

	http://clang.llvm.org/docs/InternalsManual.html


	- Doug



More information about the cfe-dev mailing list