Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------
- //
- // main.cpp: Source-to-source transformatio sample with Clang,
- // using Rewriter - the code rewriting interface.
- //
- // This code is in the public domain
- //
- #include <cstdio>
- #include <string>
- #include <sstream>
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include "clang/AST/ASTConsumer.h"
- #include "clang/AST/RecursiveASTVisitor.h"
- #include "clang/Basic/Diagnostic.h"
- #include "clang/Frontend/TextDiagnosticPrinter.h"
- #include "clang/Basic/FileManager.h"
- #include "clang/Basic/SourceManager.h"
- #include "clang/Basic/TargetOptions.h"
- #include "clang/Basic/TargetInfo.h"
- #include "clang/Frontend/CompilerInstance.h"
- #include "clang/Lex/Preprocessor.h"
- #include "clang/Parse/ParseAST.h"
- //#include "clang/Rewrite/Core/Rewriter.h"
- #include "clang/Rewrite/Rewriter.h"
- //#include "clang/Rewrite/Frontend/Rewriters.h"
- #include "llvm/Support/Host.h"
- #include "llvm/Support/raw_ostream.h"
- using namespace clang;
- using namespace std;
- CompilerInstance* pTheCompInst_g;
- std::set<int> unexecutedlines_g;
- std::set<int> uselesslines_g;
- std::set<int> usefullines_g;
- std::map<string,int> funcdeclloc_g;
- std::map<string,string> functionToFile_g;
- string currFileName_g;
- // By implementing RecursiveASTVisitor, we can specify which AST nodes
- // we're interested in by overriding relevant methods.
- class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor>
- {
- public:
- MyASTVisitor()
- {}
- bool VisitCallExpr(CallExpr *ce) {
- // Function name
- //DeclarationName DeclName = f->getNameInfo().getName();
- //string FuncName = DeclName.getAsString();
- return true;
- }
- bool VisitStmt(Stmt *s) {
- // Only care about If statements.
- static int divid = 0;
- return true;
- }
- bool VisitFunctionDecl(FunctionDecl *f) {
- // Only function definitions (with bodies), not declarations.
- return true;
- }
- private:
- void AddBraces(Stmt *s);
- };
- // Implementation of the ASTConsumer interface for reading an AST produced
- // by the Clang parser.
- class MyASTConsumer : public ASTConsumer
- {
- public:
- MyASTConsumer()
- : Visitor()
- {}
- private:
- MyASTVisitor Visitor;
- };
- int main(int argc, char *argv[])
- {
- string sourcefile;
- 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/"};
- int arrayoriglen = sizeof (arrayorig)/sizeof (string);
- vector<string> array(arrayorig, arrayorig+arrayoriglen);
- /*First get all command line args first*/
- for (int i=1;i<argc;i++) {
- string arg (argv[i]);
- const char* x = arg.c_str();
- cout << x << "----" << endl;
- if ((x[0] == '-') && (x[1] == 'I')) {
- cout << "argv[i] is " << x+2 << endl;
- string newy (x+2);
- array.push_back (newy);
- }
- int len = strlen (x);
- if ((len >= 2) && (x[len-2]=='.') && ((x[len-1]=='c') || (x[len-1]=='C'))) {
- sourcefile = x;//==argv[i];
- }
- }
- if (sourcefile.length() == 0) {
- llvm::errs() << "No source specified?\n";
- llvm::errs() << "Usage: rewritersample <filename> <gcov-file>\n";
- llvm::errs() << "Or you could just specify a complete gcc compilation command line and we will handle it\n";
- exit (-1);
- }
- cout << "Size of array is " << array.size() << endl;
- // CompilerInstance will hold the instance of the Clang compiler for us,
- // managing the various objects needed to run the compiler.
- CompilerInstance TheCompInst;
- pTheCompInst_g = &TheCompInst;
- /*
- LangOptions& Opts = TheCompInst.getLangOpts();
- TheCompInst.getInvocation().setLangDefaults (Opts, IK_CXX);
- */
- //TheCompInst.createDiagnostics(0, 0);
- TheCompInst.createDiagnostics(NULL, false);
- // Create an invocation that passes any flags to preprocessor
- CompilerInvocation *Invocation = new CompilerInvocation;
- CompilerInvocation::CreateFromArgs(*Invocation, argv + 1, argv + argc,
- TheCompInst.getDiagnostics());
- TheCompInst.setInvocation(Invocation);
- // Initialize target info with the default triple for our platform.
- TargetOptions TO;
- TO.Triple = llvm::sys::getDefaultTargetTriple();
- TargetInfo *TI = TargetInfo::CreateTargetInfo(
- TheCompInst.getDiagnostics(), &TO);
- TheCompInst.setTarget(TI);
- TheCompInst.createFileManager();
- FileManager &FileMgr = TheCompInst.getFileManager();
- TheCompInst.createSourceManager(FileMgr);
- SourceManager &SourceMgr = TheCompInst.getSourceManager();
- HeaderSearchOptions &headerSearchOptions = TheCompInst.getHeaderSearchOpts();
- for (int i=0;i<array.size();i++) {
- string x = array[i];
- cout << "Adding " << x << " to search path\n";
- headerSearchOptions.AddPath(array[i],
- clang::frontend::Angled,
- false,
- false);
- }
- TheCompInst.createPreprocessor();
- TheCompInst.createASTContext();
- //Parse the gcov file.
- std::ifstream infile (argv[2]);
- string line;
- int linenum = 0;
- // Set the main file handled by the source manager to the input file.
- currFileName_g = sourcefile;
- const FileEntry *FileIn = FileMgr.getFile(sourcefile);
- SourceMgr.createMainFileID(FileIn);
- TheCompInst.getDiagnosticClient().BeginSourceFile(
- TheCompInst.getLangOpts(),
- &TheCompInst.getPreprocessor());
- // Create an AST consumer instance which is going to get called by
- // ParseAST.
- MyASTConsumer TheConsumer;
- // Parse the file to AST, registering our consumer as the AST consumer.
- ParseAST(TheCompInst.getPreprocessor(), &TheConsumer,
- TheCompInst.getASTContext());
- // At this point the rewriter's buffer should be full with the rewritten
- // file contents.
- exit (-1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement