-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
97 lines (85 loc) · 2.65 KB
/
main.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
#include <printer/IRNodeFinder.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <llvm/ADT/Optional.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/IR/Module.h>
#include <llvm/LineEditor/LineEditor.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/raw_ostream.h>
using namespace llvm;
using namespace clang;
using namespace clang::tooling;
static llvm::cl::OptionCategory IRPrinter("IR Printer Sample");
// static cl::opt<bool> colors("use-color", cl::init(true), cl::desc("Enable or disable color output"),
// cl::cat(IRPrinter));
namespace {
StringRef lexWord(StringRef word) {
StringRef::iterator begin{word.begin()};
StringRef::iterator end{word.end()};
while (true) {
if (begin == end) {
return StringRef(begin, 0);
}
if (!isWhitespace(*begin)) {
break;
}
++begin;
}
StringRef::iterator wordbegin = begin;
while (true) {
++begin;
if (begin == end || isWhitespace(*begin)) {
return StringRef(wordbegin, begin - wordbegin);
}
}
}
} // namespace
int main(int argc, const char** argv) {
CommonOptionsParser op(argc, argv, IRPrinter);
irprinter::IRNodeFinder ir(op);
auto ret = ir.parse();
if (ret != 0) {
llvm::outs() << "Error parsing. Quitting...\n";
return ret;
}
llvm::LineEditor le("ir-printer");
while (llvm::Optional<std::string> line = le.readLine()) {
StringRef ref = *line;
auto cmd = lexWord(ref);
if (cmd == "q" || cmd == "quit") {
break;
} else if (cmd == "g" || cmd == "generate") {
ir.parse();
} else if (cmd == "f" || cmd == "flag") {
auto str = lexWord(StringRef(cmd.end(), ref.end() - cmd.end()));
llvm::outs() << "Set flag to " << str << ". Re-generating module...\n";
ir.setOptFlag(str);
ir.parse();
} else if (cmd == "dump") {
ir.dump();
} else if (cmd == "l" || cmd == "list" || cmd == "p" || cmd == "print") {
auto str = lexWord(StringRef(cmd.end(), ref.end() - cmd.end()));
if (str == "") {
str = ".*";
} else {
std::string error;
llvm::Regex r(str);
if (!r.isValid(error)) {
llvm::outs() << "Invalid regex (" << error << "): \"" << str << "\"\n";
continue;
}
}
if (cmd == "p" || cmd == "print") {
ir.printFunction(str);
} else {
ir.listFunction(str);
}
} else if (cmd == "d" || cmd == "demangle") {
auto str = lexWord(StringRef(cmd.end(), ref.end() - cmd.end()));
auto demangled_name = irprinter::IRNodeFinder::demangle(str);
llvm::outs() << "Demangled name: " << demangled_name << "\n";
}
llvm::outs().flush();
}
return 0;
}