forked from hsutter/cppfront
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppfront.cpp
More file actions
177 lines (150 loc) · 5.78 KB
/
cppfront.cpp
File metadata and controls
177 lines (150 loc) · 5.78 KB
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
// Copyright (c) Herb Sutter
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//===========================================================================
// main - driver
//===========================================================================
#include "to_cpp1.h"
static auto flag_debug_output = false;
static cpp2::cmdline_processor::register_flag cmd_debug(
9,
"debug",
"Emit compiler debug output",
[]{ flag_debug_output = true; }
);
static auto flag_quiet = false;
static cpp2::cmdline_processor::register_flag cmd_quiet(
9,
"quiet",
"Print only error output",
[]{ flag_quiet = true; }
);
auto main(
int argc,
char* argv[]
)
-> int
{
using namespace cpp2;
cmdline.set_args(argc, argv);
cmdline.process_flags();
if (cmdline.help_was_requested()) {
return EXIT_SUCCESS;
}
if (cmdline.arguments().empty()) {
std::cerr << "cppfront: error: no input files (try -help)\n";
return EXIT_FAILURE;
}
if (std::filesystem::exists(flag_cwd)) {
std::filesystem::current_path(flag_cwd);
}
// For each Cpp2 source file
int exit_status = EXIT_SUCCESS;
for (auto const& arg : cmdline.arguments())
{
if (
arg.text.starts_with("-")
|| (arg.text.starts_with("/") && !contains(arg.text, '.'))
)
{
auto ambiguous = cmdline.flags_starting_with(arg.text.substr(1));
if (ambiguous.empty()) {
std::cerr << arg.text << " - unknown compiler flag name (try " << arg.text.front() << "help)\n";
}
else {
std::cerr << arg.text << " - ambiguous compiler flag name, did you mean one of these?\n";
for (auto const& a : ambiguous) {
std::cerr << " " << arg.text.front() << a << "\n";
}
}
return EXIT_FAILURE;
}
cpp2::timer t;
t.start();
auto& out = flag_cpp1_filename != "stdout" ? std::cout : std::cerr;
if (!flag_quiet) {
out << arg.text << "...";
}
// Load + lex + parse + sema
cppfront c(arg.text);
// Generate Cpp1 (this may catch additional late errors)
auto count = c.lower_to_cpp1();
// If there were no errors, say so and generate Cpp1
if (c.had_no_errors())
{
if (!flag_quiet)
{
if (!c.has_cpp1()) {
out << " ok (all Cpp2, passes safety checks)\n";
}
else if (c.has_cpp2()) {
out << " ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)\n";
}
else {
out << " ok (all Cpp1)\n";
}
if (flag_verbose) {
auto total = count.cpp1_lines + count.cpp2_lines;
auto total_lines = print_with_thousands(total);
out << " Cpp1 "
<< std::right << std::setw(unchecked_narrow<int>(total_lines.size()))
<< print_with_thousands(count.cpp1_lines) << " line" << (count.cpp1_lines != 1 ? "s" : "");
out << "\n Cpp2 "
<< std::right << std::setw(unchecked_narrow<int>(total_lines.size()))
<< print_with_thousands(count.cpp2_lines) << " line" << (count.cpp2_lines != 1 ? "s" : "");
if (total > 0) {
out << " (";
if (count.cpp1_lines == 0) {
out << 100;
}
else if (count.cpp2_lines / count.cpp1_lines > 25) {
out << std::setprecision(3)
<< 100.0 * count.cpp2_lines / total;
}
else {
out << 100 * count.cpp2_lines / total;
}
out << "%)";
}
t.stop();
auto total_time = print_with_thousands(t.elapsed().count());
std::cout << "\n Time " << total_time << " ms";
std::multimap< long long, std::string_view, std::greater<> > sorted_timers;
for (auto [name, t] : timers) {
sorted_timers.insert({t.elapsed().count(), name});
}
for (auto [elapsed, name] : sorted_timers) {
std::cout
<< "\n "
<< std::right << std::setw(unchecked_narrow<int>(total_time.size()))
<< print_with_thousands(elapsed) << " ms" << " in " << name;
}
}
out << "\n";
}
}
// Otherwise, print the errors
else
{
std::cerr << "\n";
c.print_errors();
std::cerr << "\n";
exit_status = EXIT_FAILURE;
}
// And, if requested, the debug information
if (flag_debug_output) {
c.debug_print();
}
}
//if (flag_internal_debug) {
// stackinstr::print_deepest();
// stackinstr::print_largest();
//}
return exit_status;
}