SHARE
TWEET

DeclarationTransformer

a guest Dec 15th, 2015 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class DeclarationTransformer : public clang::ast_matchers::MatchFinder::MatchCallback
  2. {
  3.     private:
  4.  
  5.         clang::ASTContext &context;
  6.         clang::Rewriter &rewriter;
  7.  
  8.     public:
  9.  
  10.         explicit DeclarationTransformer(clang::ASTContext &context, clang::Rewriter &rewriter);
  11.  
  12.         void doTransformation();
  13.         virtual void run(const clang::ast_matchers::MatchFinder::MatchResult &result) override;
  14. };
  15.  
  16. DeclarationTransformer::DeclarationTransformer(clang::ASTContext &context, clang::Rewriter &rewriter)
  17.         : context(context), rewriter(rewriter)
  18. {}
  19.  
  20. void DeclarationTransformer::doTransformation()
  21. {
  22.     using namespace clang::ast_matchers;
  23.  
  24.     MatchFinder declgroupFinder;
  25.  
  26.     auto declStmtExpr = declStmt().bind("declstmt");
  27.     declgroupFinder.addMatcher(declStmtExpr, this);
  28.  
  29.     declgroupFinder.matchAST(context);
  30. }
  31.  
  32. void DeclarationTransformer::run(const clang::ast_matchers::MatchFinder::MatchResult &result)
  33. {
  34.     using namespace clang;
  35.     using namespace clang_utilsA;
  36.  
  37.     if(const DeclStmt *declStmt = result.Nodes.getNodeAs<DeclStmt>("declstmt"))
  38.     {
  39.         if(declStmt->isSingleDecl() == false)
  40.         {
  41.             std::string transformed = "";
  42.  
  43.             for(auto it = declStmt->getDeclGroup().begin(); it != declStmt->getDeclGroup().end(); ++it)
  44.             {
  45.                 if(const VarDecl *var = llvm::dyn_cast<VarDecl>(*it))
  46.                 {
  47.                     VariableDeclaration variableDeclaration(var);
  48.  
  49.                     if(variableDeclaration.isPointer)
  50.                     {
  51.                         transformed += variableDeclaration.type + " *" + variableDeclaration.name;
  52.                     }
  53.                     else if(variableDeclaration.isPointerPointer)
  54.                     {
  55.                         transformed += variableDeclaration.type + " **" + variableDeclaration.name;
  56.                     }
  57.                     else if(variableDeclaration.isArray)
  58.                     {
  59.                         std::string typeWithName = variableDeclaration.fullType;
  60.  
  61.                         auto pos = typeWithName.rfind(" ");
  62.                         typeWithName.insert(pos+1, variableDeclaration.name);
  63.  
  64.                         transformed += typeWithName;
  65.  
  66.                         // TODO: dont like the find-insert part. try to use arraySize.
  67.                         // variableDeclaration.name + "[" + std::to_string(variableDeclaration.arraySize) + "]";
  68.                     }
  69.                     else
  70.                     {
  71.                         transformed += variableDeclaration.type + " " + variableDeclaration.name;
  72.                     }
  73.  
  74.                     if(variableDeclaration.hasInit)
  75.                     {
  76.                         auto initStmt = rewriter.getRewrittenText(variableDeclaration.initValueRange);
  77.                         transformed += " = " + initStmt;
  78.                     }
  79.  
  80.                     transformed +=  ";\n    ";
  81.                 }
  82.             }
  83.  
  84.             //llvm::outs() << transformed;
  85.             rewriter.ReplaceText(declStmt->getSourceRange(), transformed);
  86.         }
  87.     }
  88. }
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