//===-- sotoc/src/main.cpp ------------------------------------------------===////// The LLVM Compiler Infrastructure//// This file is distributed under the University of Illinois Open Source// License. See LICENSE.TXT for details.////===----------------------------------------------------------------------===////===----------------------------------------------------------------------===//#include<cstdlib>#include<memory>#include<sstream>#include<string>#include"clang/Frontend/FrontendActions.h"#include"clang/Tooling/CommonOptionsParser.h"#include"clang/Tooling/Tooling.h"// Declares llvm::cl::extrahelp.#include"clang/AST/ASTConsumer.h"#include"clang/AST/ASTContext.h"#include"clang/AST/RecursiveASTVisitor.h"#include"clang/Basic/SourceManager.h"#include"clang/Frontend/CompilerInstance.h"#include"clang/Rewrite/Core/Rewriter.h"#include"llvm/Support/CommandLine.h"#include"llvm/Support/Process.h"#include"llvm/Support/raw_ostream.h"#include"Debug.h"#include"DeclResolver.h"#include"TargetCode.h"#include"TargetCodeFragment.h"#include"Visitors.h"usingnamespaceclang::tooling;usingnamespacellvm;classTargetRegionTransformer:publicclang::ASTConsumer{TargetCode&Code;clang::Rewriter&TargetCodeRewriter;public:TargetRegionTransformer(TargetCode&Code,clang::Rewriter&TargetCodeRewriter):Code(Code),TargetCodeRewriter(TargetCodeRewriter){}voidHandleTranslationUnit(clang::ASTContext&Context)final{// create space to store information types and functionsTypeDeclResolverTypes;// Functinos are... special because they can reference new typesFunctionDeclResolverFunctions(Types);// read target code information from AST into TargetCodeFindTargetCodeVisitorFindCodeVisitor(Code,Types,Functions,Context);FindCodeVisitor.TraverseDecl(Context.getTranslationUnitDecl());Functions.orderAndAddFragments(Code);Types.orderAndAddFragments(Code);}};classSourceTransformAction:publicclang::ASTFrontendAction{clang::RewriterTargetCodeRewriter;TargetCode*Code;public:voidEndSourceFileAction()override{// std::error_code error_code;// llvm::raw_fd_ostream outFile("output.txt", error_code,// llvm::sys::fs::F_Append);DEBUGP("Generating Code");Code->generateCode(llvm::outs());// outFile.close();deleteCode;}std::unique_ptr<clang::ASTConsumer>CreateASTConsumer(clang::CompilerInstance&CI,clang::StringRef)final{TargetCodeRewriter.setSourceMgr(CI.getSourceManager(),CI.getLangOpts());// TargetCode holds all necessary information about source locations of// target regions to extract that codeCode=newTargetCode(TargetCodeRewriter);returnstd::unique_ptr<clang::ASTConsumer>(newTargetRegionTransformer(*Code,TargetCodeRewriter));}};intSotocDebugLevel=0;staticllvm::cl::OptionCategorySotocCategory("sotoc options");staticllvm::cl::extrahelpMoreHelp("\nExtracts code in OpenMP target regions from source file and ""generates target function code");intmain(intargc,constchar**argv){autooption=clang::tooling::CommonOptionsParser::create(argc,argv,SotocCategory);if(!option){return-1;}clang::tooling::ClangTooltool(option->getCompilations(),option->getSourcePathList());#ifdef SOTOC_DEBUGSotocDebugLevel=std::atoi(sys::Process::GetEnv("SOTOC_DEBUG").getValueOr("0").c_str());#endifDEBUGP("Starting source transformation tool");returntool.run(clang::tooling::newFrontendActionFactory<SourceTransformAction>().get());}