How To Get The Name of the type the popinter is pointing to

I want to know the type of pointer .
for example

if we have

int *p,
struct node *w

char *q

then i want int for p,char for q , struct node for w.

any help would be appreciated.

amitjai <amitjaiswal.knit@gmail.com> writes:

I want to know the type of pointer .
for example

if we have

int *p,
struct node *w

char *q

then i want int for p,char for q , struct node for w.

Is your question related to Clang? If yes, you'll better ask on Clang's
mailing list ( cfe-dev, see http://clang.llvm.org/ )

If your question is about LLVM, let's suppose `v' is a pointer to a
llvm::Value instance or one of its derivatives, then

llvm::outs() << *v << '\n';

will print on the console a reprentation of the Value, including its
Type, while

llvm::outs() << *v->getType() << '\n';

will print a representation of its Type.

HTH.

[resending message. somehow the address of llvmdev was corrupted when it
reached some intermediate server]

amitjai <amitjaiswal.knit@gmail.com> writes:

I want to know the type of pointer .
for example

if we have

int *p,
struct node *w

char *q

then i want int for p,char for q , struct node for w.

Is your question related to Clang? If yes, you'll better ask on Clang's
mailing list ( cfe-dev, see http://clang.llvm.org/ )

If your question is about LLVM, let's suppose `v' is a pointer to a
llvm::Value instance or one of its derivatives, then

llvm::outs() << *v << '\n';

will print on the console a reprentation of the Value, including its
Type, while

llvm::outs() << *v->getType() << '\n';

will print a representation of its Type.

HTH.

i am using llvm ,
i did this

                        if ( AllocaInst *allocInst =
dyn_cast<AllocaInst>(&*bbit)){
                                PointerType *p = allocInst->getType();
                                if ( p->getElementType()->isPointerTy()){
                                        StringRef name =
allocInst->getName();
                                        Type *type =
allocInst->getOperand(0)->getType();;
                                        errs() << type->getName() << " "
<< name << "\n";
                                }
                        }

but i am getting the output in the following format

i32 line
i32 temp
i32 p
i32 filename
i32 arrays
i32 label
i32 fp
i32 head
i32 b

here line is char* ,temp is char* , p is int pointer,fp is file pointer
,head is pointer to list node structure and so on

but why i am getting i32 for all the pointers .

please help me out.

amitjai wrote

i am using llvm ,
i did this

                        if ( AllocaInst *allocInst = dyn_cast
<AllocaInst>
(&*bbit)){
                                PointerType *p = allocInst->getType();
                                if ( p->getElementType()->isPointerTy()){
                                        StringRef name =
allocInst->getName();
                                        Type *type =
allocInst->getOperand(0)->getType();;
                                        errs() << type->getName() << " "
<< name << "\n";
                                }
                        }

but i am getting the output in the following format

i32 line
i32 temp
i32 p
i32 filename
i32 arrays
i32 label
i32 fp
i32 head
i32 b

here line is char* ,temp is char* , p is int pointer,fp is file pointer
,head is pointer to list node structure and so on

but why i am getting i32 for all the pointers .

please help me out.

Hi ,
I am using the following code to get the Type of Pointer element allocated-

if ( (AllocaInst *AI = dyn_cast<AllocaInst> (Inst)) )
    if ( AI->getType()->getElementType()->isPointerTy() )
      errs() << "\nType :- " << *AI->getType()->getElementType()
                                     << "\tName:- " << AI->getName();

"allocInst->getOperand(0)" contains the number of elements of that type to
be allocated , so its type is always i32..

(AllocaInst)getOperand(0) is "array size". It must be
(ConstantInt)(i32 1) in most case.
Please try AllocaInst::getAllocatedType().

...Takumi