SHARE
TWEET

VariableDeclaration

a guest Dec 15th, 2015 18 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace clang_utilsA
  2. {
  3.     struct VariableDeclaration
  4.     {
  5.         std::string name;
  6.         std::string type;
  7.         std::string fullType;
  8.  
  9.         clang::SourceRange completeRange;
  10.         clang::SourceRange initValueRange;
  11.         clang::SourceLocation nameLocation;
  12.  
  13.         bool isArray;
  14.         int arraySize;
  15.  
  16.         bool isPointer;
  17.         bool isPointerPointer;
  18.  
  19.         bool isStruct;
  20.         bool hasInit;
  21.  
  22.         bool isStringLiteral;
  23.         void print(llvm::raw_ostream &stream);
  24.  
  25.         VariableDeclaration(const clang::ValueDecl *decl);
  26.     };
  27.  
  28.     VariableDeclaration::VariableDeclaration(const clang::ValueDecl *decl)
  29.     {
  30.         using namespace clang;
  31.  
  32.         name = decl->getNameAsString();
  33.  
  34.         type = decl->getType().getCanonicalType().getAsString();            // TODO: long int -> just long
  35.         fullType = decl->getType().getAsString();
  36.  
  37.         isPointerPointer = hasInit = isStringLiteral = false;
  38.  
  39.         isArray = decl->getType()->isArrayType();
  40.         isStruct = decl->getType()->isStructureType();
  41.         isPointer = decl->getType()->isPointerType();
  42.  
  43.         if(isStruct)
  44.             type = decl->getType().getUnqualifiedType().getAsString();  // to get possible typedef type
  45.  
  46.         if(isPointer)
  47.         {
  48.             type = decl->getType()->getPointeeType().getCanonicalType().getAsString();
  49.             isStruct = decl->getType()->getPointeeType().getUnqualifiedType()->isStructureType();
  50.  
  51.             if(isStruct)
  52.                 type = decl->getType()->getPointeeType().getUnqualifiedType().getAsString();  // to get possible typedef type
  53.  
  54.             // TODO: start to loop to get any number of pointers
  55.             if(decl->getType()->getPointeeType().getUnqualifiedType()->isPointerType())
  56.             {
  57.                 isPointer = false;
  58.                 isPointerPointer = true;
  59.  
  60.                 type = decl->getType()->getPointeeType().getCanonicalType()->getPointeeType().getUnqualifiedType().getAsString();
  61.                 isStruct = decl->getType()->getPointeeType().getUnqualifiedType()->getPointeeType().getUnqualifiedType()->isStructureType();
  62.  
  63.                 if(isStruct)
  64.                     type = decl->getType()->getPointeeType().getUnqualifiedType()->getPointeeType().getUnqualifiedType().getAsString(); // to get possible typedef type
  65.             }
  66.         }
  67.  
  68.         if(isArray)
  69.         {
  70.             type = decl->getType()->getArrayElementTypeNoTypeQual()->getCanonicalTypeInternal().getCanonicalType().getAsString();
  71.  
  72.             arraySize = 0;  // TODO: just works with init-list (see below)
  73.         }
  74.  
  75.         if(llvm::isa<VarDecl>(decl))
  76.         {
  77.             // TODO: dont like this here
  78.             const VarDecl *varDecl = llvm::cast<VarDecl>(decl);
  79.  
  80.             if(varDecl->hasInit() && varDecl->getInitStyle() == VarDecl::CInit)
  81.             {
  82.                 const Expr *expr = varDecl->getInit();
  83.  
  84.                 while(const ImplicitCastExpr *ice = llvm::dyn_cast<ImplicitCastExpr>(expr))
  85.                     expr = ice->getSubExpr();
  86.  
  87.                 initValueRange = expr->getSourceRange();
  88.                 hasInit = true;
  89.  
  90.                 if(isArray && llvm::isa<StringLiteral>(expr))
  91.                     arraySize = llvm::cast<StringLiteral>(expr)->getLength() + 1;   // +1 for null-terminator
  92.                 else if(isArray && llvm::isa<InitListExpr>(expr))
  93.                     arraySize = llvm::cast<InitListExpr>(expr)->getNumInits();
  94.                 else if(llvm::isa<StringLiteral>(expr))
  95.                     isStringLiteral = true;
  96.             }
  97.         }
  98.  
  99.         completeRange = clang::SourceRange(decl->getLocStart(), decl->getLocEnd());
  100.     }
  101.  
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top