Advertisement
Guest User

attempt2.cpp

a guest
Aug 19th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. //-------------------------------------------------------------------------
  2. //
  3. // main.cpp: Source-to-source transformatio sample with Clang,
  4. // using Rewriter - the code rewriting interface.
  5. //
  6. // This code is in the public domain
  7. //
  8. #include <cstdio>
  9. #include <string>
  10. #include <sstream>
  11. #include <iostream>
  12. #include <fstream>
  13. #include <string.h>
  14.  
  15. #include "clang/AST/ASTConsumer.h"
  16. #include "clang/AST/RecursiveASTVisitor.h"
  17. #include "clang/Basic/Diagnostic.h"
  18. #include "clang/Frontend/TextDiagnosticPrinter.h"
  19. #include "clang/Basic/FileManager.h"
  20. #include "clang/Basic/SourceManager.h"
  21. #include "clang/Basic/TargetOptions.h"
  22. #include "clang/Basic/TargetInfo.h"
  23. #include "clang/Frontend/CompilerInstance.h"
  24. #include "clang/Lex/Preprocessor.h"
  25. #include "clang/Parse/ParseAST.h"
  26. //#include "clang/Rewrite/Core/Rewriter.h"
  27. #include "clang/Rewrite/Rewriter.h"
  28. //#include "clang/Rewrite/Frontend/Rewriters.h"
  29. #include "llvm/Support/Host.h"
  30. #include "llvm/Support/raw_ostream.h"
  31.  
  32. using namespace clang;
  33. using namespace std;
  34.  
  35. CompilerInstance* pTheCompInst_g;
  36. std::set<int> unexecutedlines_g;
  37. std::set<int> uselesslines_g;
  38. std::set<int> usefullines_g;
  39. std::map<string,int> funcdeclloc_g;
  40. std::map<string,string> functionToFile_g;
  41. string currFileName_g;
  42.  
  43. // By implementing RecursiveASTVisitor, we can specify which AST nodes
  44. // we're interested in by overriding relevant methods.
  45. class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor>
  46. {
  47. public:
  48.     MyASTVisitor()
  49.     {}
  50.  
  51.     bool VisitCallExpr(CallExpr *ce) {
  52.  
  53.         // Function name
  54.             //DeclarationName DeclName = f->getNameInfo().getName();
  55.             //string FuncName = DeclName.getAsString();
  56.  
  57.         return true;
  58.     }
  59.     bool VisitStmt(Stmt *s) {
  60.         // Only care about If statements.
  61.         static int divid = 0;
  62.  
  63.         return true;
  64.     }
  65.  
  66.     bool VisitFunctionDecl(FunctionDecl *f) {
  67.         // Only function definitions (with bodies), not declarations.
  68.         return true;
  69.     }
  70.  
  71. private:
  72.     void AddBraces(Stmt *s);
  73.  
  74. };
  75.  
  76.  
  77. // Implementation of the ASTConsumer interface for reading an AST produced
  78. // by the Clang parser.
  79. class MyASTConsumer : public ASTConsumer
  80. {
  81. public:
  82.     MyASTConsumer()
  83.         : Visitor()
  84.     {}
  85.  
  86. private:
  87.     MyASTVisitor Visitor;
  88. };
  89.  
  90.  
  91. int main(int argc, char *argv[])
  92. {
  93.    
  94.     string sourcefile;
  95.     string arrayorig[] = {"/usr/include/","/usr/include/i386-linux-gnu/","/usr/include/i386-linux-gnu/gnu/","/usr/include/i386-linux-gnu/sys/","/usr/lib/gcc/i686-linux-gnu/4.6/include/", "/usr/include", "/usr/include/linux", "/usr/lib/gcc/i686-linux-gnu/4.6/include-fixed/","/usr/local/include/"};
  96.  
  97.  
  98.     int arrayoriglen = sizeof (arrayorig)/sizeof (string);
  99.     vector<string> array(arrayorig, arrayorig+arrayoriglen);
  100.  
  101.     /*First get all command line args first*/
  102.     for (int i=1;i<argc;i++) {
  103.         string arg (argv[i]);
  104.         const char* x = arg.c_str();
  105.         cout << x << "----" << endl;
  106.         if ((x[0] == '-') && (x[1] == 'I')) {
  107.             cout << "argv[i] is " << x+2 << endl;
  108.             string newy (x+2);
  109.             array.push_back (newy);
  110.         }
  111.         int len = strlen (x);
  112.         if ((len >= 2) && (x[len-2]=='.') && ((x[len-1]=='c') || (x[len-1]=='C'))) {
  113.             sourcefile = x;//==argv[i];
  114.         }
  115.     }
  116.     if (sourcefile.length() == 0) {
  117.         llvm::errs() << "No source specified?\n";
  118.         llvm::errs() << "Usage: rewritersample <filename> <gcov-file>\n";
  119.         llvm::errs() << "Or you could just specify a complete gcc compilation command line and we will handle it\n";
  120.         exit (-1);
  121.     }
  122.  
  123.     cout << "Size of array is " << array.size() << endl;
  124.  
  125.     // CompilerInstance will hold the instance of the Clang compiler for us,
  126.     // managing the various objects needed to run the compiler.
  127.     CompilerInstance TheCompInst;
  128.     pTheCompInst_g = &TheCompInst;
  129.  
  130.     /*
  131.     LangOptions& Opts = TheCompInst.getLangOpts();
  132.     TheCompInst.getInvocation().setLangDefaults (Opts, IK_CXX);
  133.     */
  134.  
  135.     //TheCompInst.createDiagnostics(0, 0);
  136.     TheCompInst.createDiagnostics(NULL, false);
  137.  
  138.     // Create an invocation that passes any flags to preprocessor
  139.     CompilerInvocation *Invocation = new CompilerInvocation;
  140.     CompilerInvocation::CreateFromArgs(*Invocation, argv + 1, argv + argc,
  141.                                       TheCompInst.getDiagnostics());
  142.      TheCompInst.setInvocation(Invocation);
  143.  
  144.  
  145.     // Initialize target info with the default triple for our platform.
  146.     TargetOptions TO;
  147.     TO.Triple = llvm::sys::getDefaultTargetTriple();
  148.     TargetInfo *TI = TargetInfo::CreateTargetInfo(
  149.         TheCompInst.getDiagnostics(), &TO);
  150.     TheCompInst.setTarget(TI);
  151.  
  152.     TheCompInst.createFileManager();
  153.     FileManager &FileMgr = TheCompInst.getFileManager();
  154.     TheCompInst.createSourceManager(FileMgr);
  155.     SourceManager &SourceMgr = TheCompInst.getSourceManager();
  156.  
  157.  
  158.     HeaderSearchOptions &headerSearchOptions = TheCompInst.getHeaderSearchOpts();
  159.  
  160.     for (int i=0;i<array.size();i++) {
  161.         string x = array[i];
  162.         cout << "Adding " << x << " to search path\n";
  163.         headerSearchOptions.AddPath(array[i],
  164.                 clang::frontend::Angled,
  165.                 false,
  166.                 false);
  167.     }
  168.  
  169.  
  170.  
  171.     TheCompInst.createPreprocessor();
  172.     TheCompInst.createASTContext();
  173.  
  174.     //Parse the gcov file.
  175.     std::ifstream infile (argv[2]);
  176.     string line;
  177.     int linenum = 0;
  178.  
  179.     // Set the main file handled by the source manager to the input file.
  180.     currFileName_g = sourcefile;
  181.     const FileEntry *FileIn = FileMgr.getFile(sourcefile);
  182.     SourceMgr.createMainFileID(FileIn);
  183.     TheCompInst.getDiagnosticClient().BeginSourceFile(
  184.         TheCompInst.getLangOpts(),
  185.         &TheCompInst.getPreprocessor());
  186.  
  187.     // Create an AST consumer instance which is going to get called by
  188.     // ParseAST.
  189.     MyASTConsumer TheConsumer;
  190.  
  191.     // Parse the file to AST, registering our consumer as the AST consumer.
  192.     ParseAST(TheCompInst.getPreprocessor(), &TheConsumer,
  193.              TheCompInst.getASTContext());
  194.  
  195.  
  196.     // At this point the rewriter's buffer should be full with the rewritten
  197.     // file contents.
  198.     exit (-1);
  199.     return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement