-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinclude_guard.cpp
289 lines (264 loc) · 8.47 KB
/
include_guard.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <string>
#include <vector>
#include <array>
#include <iostream>
#include <filesystem>
#include <fstream>
#if __has_include (<experimental/iterator>)
# include <experimental/iterator> // make_ostream_joiner
#else
# define NO_EXPERIMENTAL_ITERATOR
#endif
using namespace std::literals;
namespace fs = std::filesystem;
enum class TriState { Yes, No, Undefined };
enum class ArgumentError { Success, Invalid, NoArg, ExtraArg };
// Consider these extensions as C++ files, use `#pragma once` by default for
// them
std::array kCppExtensions = { ".hh"sv, ".hpp"sv, ".hxx"sv };
namespace Options
{
const char *lib_prefix = nullptr;
bool keep_full_path = false;
TriState use_pragma_override = TriState::Undefined;
bool force = false;
}
bool G_did_skip_because_not_empty = false;
void
get_macro_name (const fs::path &path, std::string ¯o_name)
{
const std::string file_name = (Options::keep_full_path
? path.generic_string ()
: path.filename ().generic_string ());
macro_name.clear ();
if (Options::lib_prefix)
{
macro_name.append (Options::lib_prefix);
macro_name.push_back ('_');
}
for (auto c : file_name)
{
if (c & 0x80)
{
// Leave unicode untouched
macro_name.push_back (c);
}
else
{
if (std::isalnum (c))
macro_name.push_back (std::toupper (c));
else
macro_name.push_back ('_');
}
}
}
void
process_file (const fs::path &path)
{
const std::string path_str = path.generic_string ();
if (!fs::exists (path))
{
std::cout << path_str << ": file does not exist\n";
return;
}
if (!fs::is_regular_file (path))
{
std::cout << path_str << ": not a regular file (skipping)\n";
return;
}
if (!fs::is_empty (path) && !Options::force)
{
G_did_skip_because_not_empty = true;
std::cout << path_str << ": not empty (skipping)\n";
return;
}
const bool use_pragma = (Options::use_pragma_override != TriState::Undefined
? Options::use_pragma_override == TriState::Yes
: std::any_of (kCppExtensions.begin (),
kCppExtensions.end (),
[&path](std::string_view ext) {
return path.extension () == ext;
}));
std::ofstream stream (path_str);
static std::string macro_name = {};
std::cout << path_str << std::endl;
if (use_pragma)
stream << "#pragma once\n";
else
{
get_macro_name (path, macro_name);
stream << "#ifndef " << macro_name << '\n';
stream << "#define " << macro_name << '\n';
stream << "#endif /* " << macro_name << " */\n";
}
}
ArgumentError
handle_short_option (char flag)
{
switch (flag)
{
break; case 'p': Options::use_pragma_override = TriState::Yes;
break; case 'P': Options::use_pragma_override = TriState::No;
break; case 'f': Options::force = true;
break; case 'F': Options::keep_full_path = true;
break; default: return ArgumentError::Invalid;
}
return ArgumentError::Success;
}
ArgumentError
handle_long_option (std::string_view arg, std::string_view &option_name_out)
{
const std::size_t eq_pos = arg.find ('=');
const bool has_value = eq_pos != std::string_view::npos;
const std::string_view option = arg.substr (0, eq_pos);
const std::string_view value = has_value ? arg.substr (eq_pos + 1) : "";
option_name_out = option;
if (option == "full-path"sv)
{
if (has_value)
return ArgumentError::ExtraArg;
Options::keep_full_path = true;
}
else if (option == "force"sv)
{
if (has_value)
return ArgumentError::ExtraArg;
Options::force = true;
}
else if (option == "pragma"sv)
{
if (has_value)
return ArgumentError::ExtraArg;
Options::use_pragma_override = TriState::Yes;
}
else if (option == "no-pragma"sv)
{
if (has_value)
return ArgumentError::ExtraArg;
Options::use_pragma_override = TriState::No;
}
else if (option == "lib-prefix"sv)
{
if (!has_value)
return ArgumentError::NoArg;
Options::lib_prefix = value.data ();
}
else
return ArgumentError::Invalid;
return ArgumentError::Success;
}
constexpr std::string_view
argument_error_text (ArgumentError err)
{
switch (err)
{
break; case ArgumentError::Success:
// Should never be called if err is ArgumentError::Success,
// this case exists just to disable compiler warnings.
return {};
break; case ArgumentError::Invalid:
return "does not exist"sv;
break; case ArgumentError::NoArg:
return "requires an argument"sv;
break; case ArgumentError::ExtraArg:
return "does not take an argument"sv;
}
// Also to disable warnings
return {};
}
void
usage (const char *program)
{
std::cout << "Usage: " << program << " [OPTION]... [FILE]...\n";
std::cout << "Write a C/C++ include guard into the FILEs.\n";
std::cout << "\nOptions:\n";
std::cout << " -f, --force Overwrite non-empty files.\n";
std::cout << " -p, --pragma Always use '#pragma once' instead of macros.\n";
std::cout << " -P, --no-pragma Never use '#pragma once'.\n";
std::cout << " -F, --full-path Use the entire given path to generate macro names instead\n";
std::cout << " of just the file name.\n";
std::cout << " --lib-prefix=PREFIX\n";
std::cout << " Add 'PREFIX_' in front of macro names.\n";
std::cout << "\nBy default '#pragma once' is used for C++ files and macros for C files.\n";
std::cout << "The extensions to classify a file as C++ are: ";
#ifdef NO_EXPERIMENTAL_ITERATOR
std::cout << kCppExtensions[0];
for (std::size_t i = 1; i < kCppExtensions.size (); ++i)
std::cout << ", " << kCppExtensions[i];
#else
std::copy (kCppExtensions.begin (), kCppExtensions.end (),
std::experimental::make_ostream_joiner (std::cout, ", "));
#endif
std::cout << ".\n";
std::cout << "\nMacro names are derived from the file names by:\n";
std::cout << " 1) Replacing alpha-numeric ascii characters with their upper-case variant.\n";
std::cout << " 2) Replacing all other ascii characters with underscores.\n";
std::cout << " 3) Copying all unicode characters literally." << std::endl;
}
int
main (const int argc, const char *argv[])
{
#ifdef _WIN32
// Windows (powershell) gives the full path for the program,
// replace it with just the file name.
// May not always be what we want but it's cleaner over all.
const std::string _program = fs::path (argv[0]).filename ().string ();
argv[0] = _program.c_str ();
#endif
std::vector<fs::path> files;
auto usage_tip = [&argv]() {
std::cerr << "Try '" << argv[0] << " --help' for more information\n";
};
for (int i = 1; i < argc; ++i)
{
if ("--help"sv == argv[i])
{
usage (argv[0]);
return 0;
}
else if (argv[i][0] == '-')
{
if (argv[i][1] == '-')
{
std::string_view option_name;
const auto result = handle_long_option (std::string_view (argv[i] + 2),
option_name);
if (result != ArgumentError::Success)
{
std::cerr << argv[0] << ": option ‘--" << option_name
<< "’ " << argument_error_text (result) << '\n';
usage_tip ();
return 1;
}
}
else
{
std::string_view flags {argv[i] + 1};
for (auto c : flags)
{
const auto result = handle_short_option (c);
if (result != ArgumentError::Success)
{
std::cerr << argv[0] << ": invalid option -- " << c << '\n';
usage_tip ();
return 1;
}
}
}
}
else
files.emplace_back (argv[i]);
}
if (files.empty ())
{
std::cout << "No files.\n";
return 0;
}
std::cout << "Generating include guards...\n";
for (const auto &f : files)
{
process_file (f);
}
if (G_did_skip_because_not_empty)
std::cout << "Note: run with -f/--force to overwrite non-empty files.\n";
}