Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace clang_utilsA
- {
- struct VariableDeclaration
- {
- std::string name;
- std::string type;
- std::string fullType;
- clang::SourceRange completeRange;
- clang::SourceRange initValueRange;
- clang::SourceLocation nameLocation;
- bool isArray;
- int arraySize;
- bool isPointer;
- bool isPointerPointer;
- bool isStruct;
- bool hasInit;
- bool isStringLiteral;
- void print(llvm::raw_ostream &stream);
- VariableDeclaration(const clang::ValueDecl *decl);
- };
- VariableDeclaration::VariableDeclaration(const clang::ValueDecl *decl)
- {
- using namespace clang;
- name = decl->getNameAsString();
- type = decl->getType().getCanonicalType().getAsString(); // TODO: long int -> just long
- fullType = decl->getType().getAsString();
- isPointerPointer = hasInit = isStringLiteral = false;
- isArray = decl->getType()->isArrayType();
- isStruct = decl->getType()->isStructureType();
- isPointer = decl->getType()->isPointerType();
- if(isStruct)
- type = decl->getType().getUnqualifiedType().getAsString(); // to get possible typedef type
- if(isPointer)
- {
- type = decl->getType()->getPointeeType().getCanonicalType().getAsString();
- isStruct = decl->getType()->getPointeeType().getUnqualifiedType()->isStructureType();
- if(isStruct)
- type = decl->getType()->getPointeeType().getUnqualifiedType().getAsString(); // to get possible typedef type
- // TODO: start to loop to get any number of pointers
- if(decl->getType()->getPointeeType().getUnqualifiedType()->isPointerType())
- {
- isPointer = false;
- isPointerPointer = true;
- type = decl->getType()->getPointeeType().getCanonicalType()->getPointeeType().getUnqualifiedType().getAsString();
- isStruct = decl->getType()->getPointeeType().getUnqualifiedType()->getPointeeType().getUnqualifiedType()->isStructureType();
- if(isStruct)
- type = decl->getType()->getPointeeType().getUnqualifiedType()->getPointeeType().getUnqualifiedType().getAsString(); // to get possible typedef type
- }
- }
- if(isArray)
- {
- type = decl->getType()->getArrayElementTypeNoTypeQual()->getCanonicalTypeInternal().getCanonicalType().getAsString();
- arraySize = 0; // TODO: just works with init-list (see below)
- }
- if(llvm::isa<VarDecl>(decl))
- {
- // TODO: dont like this here
- const VarDecl *varDecl = llvm::cast<VarDecl>(decl);
- if(varDecl->hasInit() && varDecl->getInitStyle() == VarDecl::CInit)
- {
- const Expr *expr = varDecl->getInit();
- while(const ImplicitCastExpr *ice = llvm::dyn_cast<ImplicitCastExpr>(expr))
- expr = ice->getSubExpr();
- initValueRange = expr->getSourceRange();
- hasInit = true;
- if(isArray && llvm::isa<StringLiteral>(expr))
- arraySize = llvm::cast<StringLiteral>(expr)->getLength() + 1; // +1 for null-terminator
- else if(isArray && llvm::isa<InitListExpr>(expr))
- arraySize = llvm::cast<InitListExpr>(expr)->getNumInits();
- else if(llvm::isa<StringLiteral>(expr))
- isStringLiteral = true;
- }
- }
- completeRange = clang::SourceRange(decl->getLocStart(), decl->getLocEnd());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement