-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpp-step.cpp
219 lines (179 loc) · 6.94 KB
/
pp-step.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <unistd.h>
#endif
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/Options.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Lex/HeaderSearchOptions.h"
#include "clang/Lex/MacroArgs.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Lex/TokenConcatenation.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/OptTable.h"
using namespace clang;
struct log_expands : public PPCallbacks {
Preprocessor &pp;
SourceManager &sm;
TokenConcatenation tokCat;
log_expands(Preprocessor &, SourceManager &);
virtual void MacroExpands(const Token &, const MacroDefinition &, SourceRange,
const MacroArgs *);
private:
Token prevprev, prev;
void printToken(const Token& tok);
void flushPrinter();
};
void configureTarget(CompilerInstance &ci);
llvm::opt::InputArgList parseArgs(int argc, char **argv);
FileID getFileID(CompilerInstance &ci, llvm::opt::InputArgList &args);
void createPreprocessor(CompilerInstance &ci, llvm::opt::InputArgList &args);
void processFile(CompilerInstance &ci, const FileID &fileID);
int main(int argc, char *argv[]) {
#ifdef __EMSCRIPTEN__
EM_ASM(
if (ENVIRONMENT_IS_NODE) {
FS.mkdir('/local');
FS.mount(NODEFS, { root: '.' }, '/local');
FS.mkdir('/usr');
FS.mkdir('/usr/tce');
FS.mkdir('/usr/tce/packages');
FS.mkdir('/usr/tce/packages/gcc');
FS.mkdir('/usr/tce/packages/gcc/gcc-4.9.3');
FS.mkdir('/usr/tce/packages/gcc/gcc-4.9.3/include');
FS.mount(NODEFS, { root: '/usr/tce/packages/gcc/gcc-4.9.3/include' }, '/usr/tce/packages/gcc/gcc-4.9.3/include');
FS.mkdir('/usr/tce/packages/gcc/gcc-4.9.3/lib64');
FS.mkdir('/usr/tce/packages/gcc/gcc-4.9.3/lib64/gcc');
FS.mkdir('/usr/tce/packages/gcc/gcc-4.9.3/lib64/gcc/x86_64-unknown-linux-gnu');
FS.mkdir('/usr/tce/packages/gcc/gcc-4.9.3/lib64/gcc/x86_64-unknown-linux-gnu/4.9.3');
FS.mount(NODEFS, { root: '/usr/tce/packages/gcc/gcc-4.9.3/lib64/gcc/x86_64-unknown-linux-gnu/4.9.3' }, '/usr/tce/packages/gcc/gcc-4.9.3/lib64/gcc/x86_64-unknown-linux-gnu/4.9.3');
FS.mkdir('/usr/local');
FS.mkdir('/usr/local/include');
FS.mount(NODEFS, { root: '/usr/local/include' }, '/usr/local/include');
FS.mkdir('/usr/include');
FS.mount(NODEFS, { root: '/usr/include' }, '/usr/include');
}
);
chdir("/local");
#endif
CompilerInstance ci;
configureTarget(ci);
auto arguments = parseArgs(argc, argv);
createPreprocessor(ci, arguments);
auto fileID = getFileID(ci, arguments);
processFile(ci, fileID);
return EXIT_SUCCESS;
}
log_expands::log_expands(Preprocessor &p, SourceManager& s) : pp(p), sm(s), tokCat(p) {}
void log_expands::printToken(const Token& tok) {
if (tokCat.AvoidConcat(prevprev, prev, tok))
llvm::outs() << ' ';
llvm::outs() << pp.getSpelling(tok);
prevprev = prev;
prev = tok;
}
void log_expands::flushPrinter() {
prevprev = {};
prev = {};
}
void log_expands::MacroExpands(const Token &MacroNameTok,
const MacroDefinition &MD, SourceRange,
const MacroArgs *Args) {
if (!sm.isInMainFile(MacroNameTok.getLocation())) {
// llvm::outs() << "Skipping macro " << pp.getSpelling(MacroNameTok) << " because it is not expanded from the specified file\n";
return;
}
llvm::outs() << "expanding macro " << pp.getSpelling(MacroNameTok) << " into "<< '\n';
const auto macro = MD.getMacroInfo();
for (const auto &tok : macro->tokens())
printToken(tok);
flushPrinter();
llvm::outs() << '\n';
if (!macro->isFunctionLike() || Args == nullptr)
return;
llvm::outs() << '\t' << "where:" << '\n';
auto tokenAt = [Args](unsigned int index) {
return Args->getUnexpArgument(0) + index;
};
unsigned int i = 0;
for (const auto args : macro->args()) {
llvm::outs() << '\t' << '\t' << args->getNameStart() << " is ";
while (tokenAt(i)->isNot(tok::eof)) {
printToken(*tokenAt(i));
i++;
}
flushPrinter();
i++;
llvm::outs() << '\n';
}
}
void configureTarget(CompilerInstance &ci) {
ci.getDiagnosticOpts().ShowColors = 1;
auto diags = new TextDiagnosticPrinter(llvm::errs(), &ci.getDiagnosticOpts());
ci.createDiagnostics(diags);
std::shared_ptr<TargetOptions> topts(new clang::TargetOptions);
topts->Triple = LLVM_DEFAULT_TARGET_TRIPLE;
ci.setTarget(TargetInfo::CreateTargetInfo(ci.getDiagnostics(), topts));
}
llvm::opt::InputArgList parseArgs(int argc, char **argv) {
std::vector<const char *> ref;
for (int i = 1; i < argc; ++i)
ref.emplace_back(argv[i]);
unsigned missingIndex{0}, missingCount{0};
auto table = driver::createDriverOptTable();
return table->ParseArgs(ref, missingIndex, missingCount);
}
FileID getFileID(CompilerInstance &ci, llvm::opt::InputArgList &args) {
auto inputs = args.getAllArgValues(driver::options::OPT_INPUT);
if (inputs.size() == 1 && inputs[0] != "-") {
const FileEntry *pFile = ci.getFileManager().getFile(inputs[0]);
return ci.getSourceManager().getOrCreateFileID(
pFile, SrcMgr::CharacteristicKind::C_User);
}
llvm::errs()
<< "None or More than one source file was specified -- aborting\n";
exit(EXIT_FAILURE);
return ci.getSourceManager().createFileID(nullptr);
}
void createPreprocessor(CompilerInstance &ci, llvm::opt::InputArgList &args) {
ci.createFileManager();
ci.createSourceManager(ci.getFileManager());
#ifndef SYSTEM_HEADERS
#define SYSTEM_HEADERS
#endif
for (const auto dir : {SYSTEM_HEADERS})
ci.getHeaderSearchOpts().AddPath(dir, frontend::Angled, false, true);
using namespace driver::options;
using namespace llvm::opt;
for (const auto A : args.filtered(OPT_I))
ci.getHeaderSearchOpts().AddPath(A->getValue(), frontend::Quoted, false, true);
for (const auto A : args.filtered(OPT_isystem))
ci.getHeaderSearchOpts().AddPath(A->getValue(), frontend::Angled, false, true);
for (const auto A : args.filtered(OPT_D, OPT_U)) {
if (A->getOption().matches(OPT_D))
ci.getPreprocessorOpts().addMacroDef(A->getValue());
else
ci.getPreprocessorOpts().addMacroUndef(A->getValue());
}
ci.createPreprocessor(TranslationUnitKind::TU_Complete);
}
void processFile(CompilerInstance &ci, const FileID &fileID) {
ci.getSourceManager().setMainFileID(fileID);
ci.getPreprocessor().EnterMainSourceFile();
ci.getDiagnosticClient().BeginSourceFile(ci.getLangOpts(),
&ci.getPreprocessor());
std::unique_ptr<PPCallbacks> logger(new log_expands(ci.getPreprocessor(), ci.getSourceManager()));
ci.getPreprocessor().addPPCallbacks(std::move(logger));
Token tok;
do {
ci.getPreprocessor().Lex(tok);
if (ci.getDiagnostics().hasErrorOccurred())
break;
} while (tok.isNot(tok::eof));
ci.getDiagnosticClient().EndSourceFile();
}