-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIrewriter.cpp
136 lines (109 loc) · 4.28 KB
/
CIrewriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <vector>
#include <system_error>
#include <iostream>
#include <algorithm>
#include "llvm/Support/Host.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/Lexer.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Parse/ParseAST.h"
#include "clang/Rewrite/Frontend/Rewriters.h"
#include "clang/Rewrite/Core/Rewriter.h"
using namespace clang;
std::vector<std::string> argument_list;
std::vector<std::string> argument_type;
#include "InfoCollector.h"
#include "Transformator.h"
int main(int argc, char **argv)
{
if (argc < 2)
{
llvm::errs() << "Usage: CIrewriter <options> <filename>\n";
return 1;
}
// Get filename
std::string fileName(argv[argc - 1]);
CompilerInstance compiler;
DiagnosticOptions diagnosticOptions;
compiler.createDiagnostics();
//compiler.createDiagnostics(argc, argv);
// Create an invocation that passes any flags to preprocessor
CompilerInvocation *Invocation = new CompilerInvocation;
CompilerInvocation::CreateFromArgs(*Invocation, argv + 1, argv + argc,
compiler.getDiagnostics());
compiler.setInvocation(Invocation);
// Set default target triple
std::shared_ptr<clang::TargetOptions> pto = std::make_shared<clang::TargetOptions>();
pto->Triple = llvm::sys::getDefaultTargetTriple();
TargetInfo *pti = TargetInfo::CreateTargetInfo(compiler.getDiagnostics(), pto);
compiler.setTarget(pti);
compiler.createFileManager();
compiler.createSourceManager(compiler.getFileManager());
// Allow C++ code to get rewritten
LangOptions langOpts;
//langOpts.GNUMode = 1;
//langOpts.CXXExceptions = 1;
//langOpts.RTTI = 1;
//langOpts.Bool = 1;
//langOpts.CPlusPlus = 1;
Invocation->setLangDefaults(langOpts,
clang::IK_CXX,
clang::LangStandard::lang_cxx1y);
compiler.createPreprocessor(clang::TU_Complete);
compiler.getPreprocessorOpts().UsePredefines = false;
compiler.createASTContext();
// Initialize rewriter
Rewriter Rewrite;
Rewrite.setSourceMgr(compiler.getSourceManager(), compiler.getLangOpts());
const FileEntry *pFile = compiler.getFileManager().getFile(fileName);
compiler.getSourceManager().setMainFileID( compiler.getSourceManager().createFileID( pFile, clang::SourceLocation(), clang::SrcMgr::C_User));
compiler.getDiagnosticClient().BeginSourceFile(compiler.getLangOpts(),
&compiler.getPreprocessor());
InfoCollectorConsumer infoCollectorAstConsumer(Rewrite);
TransformatorConsumer transformatorConsumer(Rewrite);
// Convert <file>.c to <file_out>.c
std::string outName (fileName);
size_t ext = outName.rfind(".");
if (ext == std::string::npos)
ext = outName.length();
outName.insert(ext, "_out");
llvm::errs() << "Output to: " << outName << "\n";
std::error_code OutErrorInfo;
std::error_code ok;
llvm::raw_fd_ostream outFile(llvm::StringRef(outName), OutErrorInfo, llvm::sys::fs::F_None);
if (OutErrorInfo == ok)
{
// Parse the AST
ParseAST(compiler.getPreprocessor(), &infoCollectorAstConsumer, compiler.getASTContext());
compiler.getDiagnosticClient().EndSourceFile();
ParseAST(compiler.getPreprocessor(), &transformatorConsumer, compiler.getASTContext());
compiler.getDiagnosticClient().EndSourceFile();
// Now output rewritten source code
const RewriteBuffer *RewriteBuf =
Rewrite.getRewriteBufferFor(compiler.getSourceManager().getMainFileID());
outFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
std::cout << std::string(RewriteBuf->begin(), RewriteBuf->end()) << std::endl;
}
else
{
llvm::errs() << "Cannot open " << outName << " for writing\n";
}
outFile.close();
return 0;
}