FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppfront/source/common.h at main · iopenstack/cppfront · GitHub
iopenstack
/
cppfront
Public
forked from
hsutter/cppfront
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppfront
/
source
/
common.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
1363 lines (1181 loc) · 38.6 KB
Breadcrumbs
cppfront
/
source
/
common.h
Copy path
File metadata and controls
1363 lines (1181 loc) · 38.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
Copyright 2022-2024 Herb Sutter
//
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
Part of the Cppfront Project, under the Apache License v2.0 with LLVM Exceptions.
//
See https://github.com/hsutter/cppfront/blob/main/LICENSE for license information.
//
We want cppfront to build cleanly at very high warning levels, with warnings
//
as errors -- so disable a handful that fire incorrectly due to compiler bugs
#
ifdef
_MSC_VER
#
pragma
warning(disable: 4456 4706)
#
endif
#
if
defined(__GNUC__) && __GNUC__ >= 13 && !defined(__clang_major__)
#
pragma
GCC diagnostic ignored "-Wdangling-reference"
#
endif
//
Disable some clang conversion warnings:
//
cppfront uses signed integer types for indices and container sizes.
//
Note: We don't pop the diagnostic because we want them disabled in the
//
entire cppfront translation unit.
#
ifdef
__clang__
#
pragma
clang diagnostic push
#
pragma
clang diagnostic ignored "-Wsign-conversion"
#
endif
#
include
"
cpp2util.h
"
//
===========================================================================
//
When defined, builds cppfront in an instrumented (and slower) mode that
//
prints more information under -_debug
//
===========================================================================
//
#define CPP2_DEBUG_BUILD
//
===========================================================================
//
Common types
//
===========================================================================
#
ifndef
CPP2_COMMON_H
#
define
CPP2_COMMON_H
#
include
<
cassert
>
#
include
<
cctype
>
#
include
<
chrono
>
#
include
<
iomanip
>
#
include
<
unordered_map
>
namespace
cpp2
{
//
-----------------------------------------------------------------------
//
//
source_line: represents a source code line
//
//
-----------------------------------------------------------------------
//
struct
source_line
{
std::string text;
enum
class
category
:
u8
{ empty, preprocessor, comment,
import
, cpp1, cpp2, rawstring };
category cat;
bool
all_tokens_are_densely_spaced =
true
;
//
to be overridden in lexing if they're not
source_line
(
std::string_view t = {},
category c = category::empty
)
: text{t}
, cat{c}
{ }
auto
indent
()
const
-> int
{
return
unchecked_narrow<
int
>(
std::find_if_not
( text.
begin
(), text.
end
(), &isspace )
- text.
begin
());
}
auto
prefix
()
const
-> std::string
{
switch
(cat) {
break
;
case
category::empty:
return
"
/* */
"
;
break
;
case
category::preprocessor:
return
"
/* # */
"
;
break
;
case
category::comment:
return
"
/* / */
"
;
break
;
case
category::
import
:
return
"
/* i */
"
;
break
;
case
category::cpp1:
return
"
/* 1 */
"
;
break
;
case
category::cpp2:
return
"
/* 2 */
"
;
break
;
case
category::rawstring:
return
"
/* R */
"
;
break
;
default
:
assert
(
false
&&
"
ICE: illegal category
"
);
abort
();
}
}
};
using
lineno_t
=
int32_t
;
using
colno_t
=
int32_t
;
//
not int16_t... encountered >80,000 char line during testing
using
index_t
=
int32_t
;
struct
source_position
{
lineno_t
lineno;
//
one-based offset into program source
colno_t
colno;
//
one-based offset into line
source_position
(
lineno_t
l =
1
,
colno_t
c =
1
)
: lineno{ l }, colno{ c }
{
}
auto
operator
<=>(source_position
const
&)
const
=
default
;
auto
to_string
()
const
-> std::string
{
return
"
(
"
+
std::to_string
(lineno) +
"
,
"
+
std::to_string
(colno) +
"
)
"
;
}
};
struct
comment
{
enum
class
comment_kind
:
u8
{ line_comment =
0
, stream_comment };
comment_kind kind;
source_position start;
source_position end;
std::string text;
mutable
bool
dbg_was_printed =
false
;
};
struct
string_parts
{
struct
cpp_code
{ std::string text; };
struct
raw_string
{ std::string text; };
enum
adds_sequences :
u8
{ no_ends =
0
, on_the_beginning =
1
, on_the_end =
2
, on_both_ends =
3
};
string_parts
(
const
std::string& beginseq,
const
std::string& endseq,
adds_sequences strateg)
: begin_seq{beginseq}
, end_seq{endseq}
, strategy{strateg}
{
if
(!(strategy & on_the_beginning)) {
parts.
push_back
(raw_string{
"
"
});
}
}
void
add_code
(
const
std::string& text) { parts.
push_back
(cpp_code{text}); }
void
add_string
(
const
std::string& text) { parts.
push_back
(raw_string{text}); }
void
add_string
(
const
std::string_view& text) { parts.
push_back
(raw_string{
std::string
(text)}); }
void
clear
() { parts.
clear
(); }
auto
empty
()
const
{
return
parts.
empty
(); }
auto
generate
()
const
-> std::string
{
if
(parts.
empty
()) {
return
(strategy & on_the_beginning ? begin_seq : std::string{})
+ (strategy & on_the_end ? end_seq : std::string{});
}
auto
result =
std::visit
(begin_visit{begin_seq, strategy},
parts.
front
());
if
(
std::ssize
(parts) >
1
) {
auto
it1 = parts.
cbegin
();
auto
it2 = parts.
cbegin
()+
1
;
for
(;it2 != parts.
cend
(); ++it1, ++it2) {
result +=
std::visit
(generator_visit{begin_seq, end_seq}, *it1, *it2);
}
}
if
(!(strategy & on_the_end)) {
result +=
std::visit
([
this
](
const
auto
& lhs) {
return
generator_visit{begin_seq, end_seq}(lhs, raw_string{
"
"
});
}, parts.
back
());
}
result +=
std::visit
(end_visit{end_seq, strategy}, parts.
back
());
return
result;
}
auto
is_expanded
()
const
-> bool {
for
(
const
auto
& p : parts) {
if
(std::holds_alternative<cpp_code>(p)) {
return
true
;
}
}
return
false
;
}
private:
std::string begin_seq;
std::string end_seq;
adds_sequences strategy;
std::vector<std::variant<raw_string, cpp_code>> parts;
struct
begin_visit
{
std::string begin_seq;
adds_sequences strategy;
auto
operator
()(
const
raw_string& part)
const
-> std::string {
return
(strategy & on_the_beginning ? begin_seq :
"
"
) + part.
text
;
}
auto
operator
()(
const
cpp_code& part)
const
-> std::string {
return
part.
text
;
}
};
struct
end_visit
{
std::string end_seq;
adds_sequences strategy;
auto
operator
()(
const
raw_string&)
const
-> std::string {
return
strategy & on_the_end ? end_seq :
"
"
;
}
auto
operator
()(
const
cpp_code&)
const
-> std::string {
return
{};
}
};
struct
generator_visit
{
std::string begin_seq;
std::string end_seq;
auto
operator
()(
const
raw_string&,
const
cpp_code& part )
const
-> std::string {
return
end_seq +
"
+
"
+ part.
text
;
}
auto
operator
()(
const
cpp_code&,
const
raw_string& part )
const
-> std::string {
return
"
+
"
+ begin_seq + part.
text
;
}
auto
operator
()(
const
raw_string&,
const
raw_string& part )
const
-> std::string {
return
part.
text
;
}
auto
operator
()(
const
cpp_code&,
const
cpp_code& part )
const
-> std::string {
return
"
+
"
+ part.
text
;
}
};
};
struct
raw_string
{
source_position start;
std::string text;
std::string opening_seq;
std::string closing_seq;
bool
should_interpolate =
false
;
};
struct
multiline_raw_string
{
std::string text;
source_position end = {
0
,
0
};
};
//
-----------------------------------------------------------------------
//
//
Digit classification, with '\'' digit separators
//
//
-----------------------------------------------------------------------
//
//
G binary-digit:
//
G one of '0' '1'
//
G
auto
is_binary_digit
(
char
c)
-> bool
{
return
c ==
'
0
'
|| c ==
'
1
'
;
}
//
G digit: one of
//
G binary-digit
//
G one of '2' '3' '4' '5' '6' '7' '8' '9'
//
G
auto
is_digit
(
char
c)
-> bool
{
return
isdigit
(c);
}
//
G hexadecimal-digit:
//
G digit
//
G one of 'A' 'B' 'C' 'D' 'E' 'F'
//
G
auto
is_hexadecimal_digit
(
char
c)
-> bool
{
return
isxdigit
(c);
}
//
G nondigit:
//
G one of 'a'..'z'
//
G one of 'A'..'Z'
//
G _
//
G
auto
is_nondigit
(
char
c)
-> bool
{
return
isalpha
(c)
|| c ==
'
_
'
;
}
//
G identifier-start:
//
G nondigit
//
G
auto
is_identifier_start
(
char
c)
-> bool
{
return
is_nondigit
(c);
}
//
G identifier-continue:
//
G digit
//
G nondigit
//
G
auto
is_identifier_continue
(
char
c)
-> bool
{
return
is_digit
(c)
||
is_nondigit
(c)
;
}
//
G identifier:
//
G '__identifier__' keyword [Note: without whitespace before the keyword]
//
G identifier-start
//
G identifier identifier-continue
//
G 'operator' operator
//
G
auto
starts_with_identifier
(std::string_view s)
-> int
{
if
(
is_identifier_start
(s[
0
])) {
auto
j =
1
;
while
(
j <
std::ssize
(s)
&&
is_identifier_continue
(s[j])
)
{
++j;
}
return
j;
}
return
0
;
}
//
Helper to allow one of the above or a digit separator
//
Example: is_separator_or( is_binary_digit (c) )
//
auto
is_separator_or
(
auto
pred,
char
c)
-> bool
{
return
c ==
'
\'
'
||
pred
(c)
;
}
auto
peek
(
std::string_view line,
int
i,
int
num
)
{
return
(i + num <
std::ssize
(line) && i + num >=
0
)
? line[i + num]
:
'
\0
'
;
}
//
G encoding-prefix: one of
//
G 'u8' 'u' 'uR' 'u8R' 'U' 'UR' 'L' 'LR' 'R'
//
G
auto
is_encoding_prefix_and
(std::string_view line,
int
i,
char
next) {
auto
peek1 =
peek
(line, i,
1
);
auto
peek2 =
peek
(line, i,
2
);
auto
peek3 =
peek
(line, i,
3
);
if
(line[i] == next) {
return
1
; }
//
"
else
if
(line[i] ==
'
u
'
) {
if
(peek1 == next) {
return
2
; }
//
u"
else
if
(peek1 ==
'
8
'
&& peek2 == next) {
return
3
; }
//
u8"
else
if
(peek1 ==
'
R
'
&& peek2 == next) {
return
3
; }
//
uR"
else
if
(peek1 ==
'
8
'
&& peek2 ==
'
R
'
&& peek3 == next) {
return
4
; }
//
u8R"
}
else
if
(line[i] ==
'
U
'
) {
if
(peek1 == next) {
return
2
; }
//
U"
else
if
(peek1 ==
'
R
'
&& peek2 == next) {
return
3
; }
//
UR"
}
else
if
(line[i] ==
'
L
'
) {
if
(peek1 == next) {
return
2
; }
//
L"
else
if
(peek1 ==
'
R
'
&& peek2 == next) {
return
3
; }
//
LR"
}
else
if
(line[i] ==
'
R
'
&& peek1 == next) {
return
2
; }
//
R"
return
0
;
}
//
Bool to string
//
template
<
typename
T>
requires
std::is_same_v<T, std::string>
auto
_as
(
bool
b)
-> T
{
return
b ?
"
true
"
:
"
false
"
;
}
//
Explicit cast
//
template
<
typename
T>
auto
_as
(
auto
x)
-> T
{
return
T
(x);
}
//
String path prefix from filename
//
auto
strip_path
(std::string
const
& file)
-> std::string
{
auto
i =
std::ssize
(file)-
1
;
while
(
i >=
0
&& file[i] !=
'
\\
'
&& file[i] !=
'
/
'
)
{
--i;
}
return
{file, _as<
size_t
>(i+
1
)};
}
//
-----------------------------------------------------------------------
//
//
String: A helper workaround for passing a string literal as a
//
template argument
//
//
-----------------------------------------------------------------------
//
template
<std::
size_t
N>
struct
String
{
constexpr
String
(
const
char
(&str)[N])
{
std::copy_n
(str, N, value);
}
auto
operator
<=>(String
const
&)
const
= default;
char
value[N] = {};
};
//
-----------------------------------------------------------------------
//
//
Misc helpers
//
//
-----------------------------------------------------------------------
//
auto
replace_all
(std::string& s, std::string_view what, std::string_view with)
{
for
(
std::string::size_type pos{};
s.
npos
!= (pos = s.
find
(what.
data
(), pos, what.
length
()));
pos += with.
length
()
)
{
s.
replace
(pos, what.
length
(), with.
data
(), with.
length
());
}
return
s;
}
auto
to_upper
(
char
c)
-> char
{
//
C toupper is only not-UB in [0,127] and returns the wrong type,
//
so wrap the range check and the type cast here in one place...
//
note the 126 (not 127) is intentional to avoid a GCC warning
if
(
0
<= c && c <=
126
) {
return
static_cast
<
char
>(
std::toupper
(c)); }
//
else
return
c;
}
auto
to_upper_and_underbar
(std::string_view s)
-> std::string
{
auto
ret = std::string{s};
for
(
char
& c : ret) {
if
(
std::isalnum
(c)) { c =
to_upper
(c); }
else
{ c =
'
_
'
; }
}
return
ret;
}
auto
is_empty_or_a_decimal_number
(std::string_view s)
-> bool
{
auto
size =
std::ssize
(s);
if
(size ==
0
) {
return
true
; }
auto
i =
0
;
while
(i < size &&
isspace
(s[i]) ) { ++i; }
while
(i < size &&
isdigit
(s[i]) ) { ++i; }
while
(i < size &&
isspace
(s[i]) ) { ++i; }
return
i == size;
}
auto
starts_with
(
std::string
const
& s,
std::string_view sv
)
-> bool
{
return
std::string_view
(s).
starts_with
(sv);
}
auto
contains
(
auto
const
& range,
auto
const
& value
)
-> bool
{
return
std::find
(
range.
begin
(),
range.
end
(),
value
)
!= range.
end
();
}
auto
contains
(
std::string
const
& s,
auto
const
& value
)
-> bool
{
return
s.
find
(value) != s.
npos
;
}
//
Print an integer with 1,000's separators (always commas, not locale-driven)
template
<
typename
T>
requires
std::is_integral_v<T>
//
Note: `std::integral` concept not yet available in Apple Clang
auto
print_with_thousands
(T val)
-> std::string
{
auto
ret =
std::to_string
(val %
10
);
auto
pos =
0
;
while
((val /=
10
) >
0
) {
if
((++pos %
3
) ==
0
) { ret =
'
,
'
+ ret; }
ret =
std::to_string
(val %
10
) + ret;
}
return
ret;
}
//
In keep trying to write string+string_view, and it ought to Just Work without
//
the current workarounds. Not having that is a minor impediment to using safe
//
and efficient string_views, which we should be encouraging. So for my own use
//
and to remove that minor impediment to writing safe and efficient code, I'm
//
just going to add this until we get P2591 in C++26(?) -- See: wg21.link/p2591
//
template
<
class
charT
,
class
traits
,
class
Allocator
>
[[nodiscard]]
constexpr
auto
operator
+(
std::basic_string<charT, traits, Allocator> lhs,
std::
type_identity_t
<std::basic_string_view<charT, traits>> rhs
)
-> std::basic_string<charT, traits, Allocator>
{
return
lhs.
append
(rhs);
}
template
<
class
charT
,
class
traits
,
class
Allocator
>
[[nodiscard]]
constexpr
auto
operator
+(
std::
type_identity_t
<std::basic_string_view<charT, traits>> lhs,
std::basic_string<charT, traits, Allocator> rhs
)
-> std::basic_string<charT, traits, Allocator>
{
return
rhs.
insert
(
0
, lhs);
}
//
-----------------------------------------------------------------------
//
//
Command line handling
//
//
-----------------------------------------------------------------------
//
class
cmdline_processor
{
bool
help_requested =
false
;
struct
arg
{
int
pos;
std::string text;
arg
(
int
p,
char
* t) : pos{p}, text{t} { }
};
std::vector<arg> args;
using
callback0 =
void
(*)();
using
callback1 =
void
(*)(std::string
const
&);
struct
flag
{
int
group =
0
;
std::string name;
int
unique_prefix =
0
;
std::string description;
callback0 handler0;
callback1 handler1;
std::string synonym;
bool
opt_out;
flag
(
int
g, std::string_view n, std::string_view d, callback0 h0, callback1 h1, std::string_view s,
bool
o)
: group{g}, name{n}, description{d}, handler0{h0}, handler1{h1}, synonym{s}, opt_out{o}
{ }
auto
get_name
(
bool
indicate_short_name =
false
)
const
{
auto
n = name.
substr
(
0
, unique_prefix);
if
(unique_prefix <
std::ssize
(name)) {
auto
name_length = _as<
int
>(
std::min
(name.
find
(
'
'
), name.
size
()));
if
(indicate_short_name) {
n +=
"
[
"
;
}
n += name.
substr
(unique_prefix, name_length - unique_prefix);
if
(indicate_short_name) {
n +=
"
]
"
;
}
n += name.
substr
(name_length);
}
return
n;
}
};
std::vector<flag> flags;
int
max_flag_length =
0
;
std::unordered_map<
int
, std::string> labels = {
{
2
,
"
Additional dynamic safety check controls
"
},
{
4
,
"
Support for constrained target environments
"
},
{
8
,
"
Cpp1 file content options
"
},
{
9
,
"
Cppfront output options
"
}
};
static
auto
print
(std::string_view s,
int
width =
0
)
-> void
{
if
(width >
0
) {
std::cout <<
std::setw
(width) << std::left;
}
std::cout << s;
}
public:
auto
flags_starting_with
(
std::string_view s,
bool
indicate_short_name =
true
)
-> std::vector<std::string>
{
auto
ret = std::vector<std::string>{};
for
(
auto
const
& flag : flags) {
if
(flag.
name
.
starts_with
(s)) {
ret.
push_back
( flag.
get_name
(indicate_short_name) );
}
}
return
ret;
}
auto
process_flags
()
-> void
{
constexpr
auto
processed = -
1
;
//
Calculate the unique prefixes
for
(
auto
flag1 = flags.
begin
(); flag1 != flags.
end
(); ++flag1) {
for
(
auto
flag2 = flag1+
1
; flag2 != flags.
end
(); ++flag2) {
int
i =
0
;
while
(
i <
std::ssize
(flag1->
name
)
&& i <
std::ssize
(flag2->
name
)
&& flag1->
name
[i] !=
'
'
&& flag2->
name
[i] !=
'
'
&& flag1->
name
[i] == flag2->
name
[i]
)
{
++i;
}
//
Record that we found the unique prefix must be at least this long
flag1->
unique_prefix
=
std::max
( flag1->
unique_prefix
, i+
1
);
flag2->
unique_prefix
=
std::max
( flag2->
unique_prefix
, i+
1
);
}
}
//
Look for matches
for
(
auto
arg = args.
begin
(); arg != args.
end
(); ++arg)
{
//
The arg should never be empty, but we're going to do a [0]
//
subscript next so we should either check or assert
if
(arg->
text
.
empty
()) {
continue
;
}
//
Provide a way to ignore the rest of the command line
//
for the purpose of looking for switches
if
(arg->
text
==
"
--
"
) {
arg->
pos
= processed;
break
;
}
for
(
auto
& flag : flags) {
auto
length_to_match =
std::max
(flag.
unique_prefix
, _as<
int
>(arg->
text
.
length
())-
1
);
if
(
flag.
opt_out
&& arg->
text
.
ends_with
(
"
-
"
)
)
{
length_to_match =
std::max
(flag.
unique_prefix
, _as<
int
>(arg->
text
.
length
())-
2
);
}
//
Allow a switch to start with either - or /
if
(arg->
text
.
starts_with
(
"
-
"
+ flag.
name
.
substr
(
0
, length_to_match))
|| arg->
text
.
starts_with
(
"
/
"
+ flag.
name
.
substr
(
0
, length_to_match))
|| arg->
text
==
"
-
"
+ flag.
synonym
|| arg->
text
==
"
/
"
+ flag.
synonym
)
{
assert
(flag.
handler0
|| flag.
handler1
);
//
If this is a standalone switch, just process it
if
(flag.
handler0
) {
flag.
handler0
();
}
//
Else
else
{
//
If this is a switch that could be suffixed with "-" to opt out
if
(flag.
opt_out
) {
flag.
handler1
( arg->
text
.
ends_with
(
"
-
"
) ?
"
-
"
:
"
"
);
}
//
Else this is a switch that takes the next arg as its value, so pass that
else
{
if
(arg+
1
== args.
end
()) {
print
(
"
Missing argument to option
"
+ arg->
text
+
"
(try -help)
\n
"
);
help_requested =
true
;
break
;
}
arg->
pos
= processed;
++arg;
//
move to next argument, which is the argument to this switch
flag.
handler1
(arg->
text
);
}
}
arg->
pos
= processed;
break
;
}
}
}
std::erase_if
( args, [=](
auto
& arg){
return
arg.
pos
== processed; } );
}
auto
print_help
()
-> void
{
help_requested =
true
;
std::sort
(
flags.
begin
(),
flags.
end
(),
[](
auto
& a,
auto
& b){
return
a.
group
< b.
group
|| (a.
group
== b.
group
&& a.
name
< b.
name
); }
);
print
(
"
\n
Usage: cppfront [options] file ...
\n
"
);
print
(
"
\n
file source file(s) (can be 'stdin')
\n
"
);
print
(
"
\n
Options:
\n
"
);
int
last_group = -
1
;
for
(
auto
& flag : flags) {
//
Skip hidden flags
if
(flag.
name
.
front
() ==
'
_
'
) {
continue
;
}
if
(last_group != flag.
group
) {
print
(
"
\n
"
);
last_group = flag.
group
;
if
(!labels[flag.
group
].
empty
()) {
print
( labels[flag.
group
] +
"
\n
"
);
}
}
print
(
"
-
"
);
auto
n = flag.
get_name
(
true
);
if
(flag.
opt_out
) {
n +=
"
[-]
"
;
}
if
(!flag.
synonym
.
empty
()) {
n +=
"
, -
"
+ flag.
synonym
;
}
print
(n, max_flag_length +
3
);
print
(flag.
description
);
print
(
"
\n
"
);
}
}
auto
add_flag
(
int
group,
std::string_view name,
std::string_view description,
callback0 handler0,
callback1 handler1,
std::string_view synonym,
bool
opt_out
)
-> void
{
flags.
emplace_back
( group, name, description, handler0, handler1, synonym, opt_out );
auto
length =
std::ssize
(name);
if
(opt_out) { length +=
3
; }
//
space to print "[-]"
if
(max_flag_length < length) {
max_flag_length = unchecked_narrow<
int
>(length);
}
}
struct
register_flag
{
register_flag
(
int
group,
std::string_view name,
std::string_view description,
callback0 handler0,
callback1 handler1 = {},
std::string_view synonym = {},
bool
opt_out =
false
);
};
auto
set_args
(
int
argc,
char
* argv[]
)
-> void
{
for
(
auto
i =
1
; i < argc; ++i) {
args.
emplace_back
( i, argv[i] );
}
}
auto
help_was_requested
()
-> bool
{
return
help_requested;
}
auto
arguments
()
-> std::vector<arg>&
{
return
args;
}
//
This is used only by the owner of the 'main' branch
//
to generate stable build version strings
auto
gen_build
()
-> void
{
help_requested =
true
;
constexpr
std::string_view a = __DATE__;
constexpr
std::string_view b = __TIME__;
std::unordered_map<std::string_view,
char
> m = { {
"
Jan
"
,
'
1
'
}, {
"
Feb
"
,
'
2
'
}, {
"
Mar
"
,
'
3
'
}, {
"
Apr
"
,
'
4
'
}, {
"
May
"
,
'
5
'
}, {
"
Jun
"
,
'
6
'
}, {
"
Jul
"
,
'
7
'
}, {
"
Aug
"
,
'
8
'
}, {
"
Sep
"
,
'
9
'
}, {
"
Oct
"
,
'
A
'
}, {
"
Nov
"
,
'
B
'
}, {
"
Dec
"
,
'
C
'
} };
auto
stamp =
std::to_string
(
atoi
(&a[
9
])-
15
);
stamp += m[a.
substr
(
0
,
3
)];
stamp += a.
substr
(
4
,
2
);
stamp +=
"
:
"
;
stamp += b.
substr
(
0
,
2
);
stamp += b.
substr
(
3
,
2
);
for
(
auto
& c : stamp) {
if
(c ==
'
'
) { c =
'
0
'
; } }
print
(
"
\"
"
+ stamp +
"
\"
"
);
}
auto
print_version
()
-> void
{
help_requested =
true
;
print
(
"
\n
cppfront compiler
"
#
include
"
version.info
"
"
Build
"
#
include
"
build.info
"
);
print
(
"
\n
SPDX-License-Identifier Apache-2.0 WITH LLVM-exception
"
);
print
(
"
\n
Copyright (c) 2022-2024 Herb Sutter
\n
"
);
}
} cmdline;
cmdline_processor::register_flag::register_flag
(
int
group,
std::string_view name,
std::string_view description,
callback0 handler0,
callback1 handler1,
std::string_view synonym,
bool
opt_out
)
{
cmdline.
add_flag
( group, name, description, handler0, handler1, synonym, opt_out );
}
static
cmdline_processor::register_flag
cmd_help
(
0
,
"
help
"
,
"
Print help
"
,
[]{ cmdline.
print_help
(); },
nullptr
,
"
?
"
);
static
cmdline_processor::register_flag
cmd_version
(
0
,
"
version
"
,
"
Print version information
"
,
[]{ cmdline.
print_version
(); }
);
static
cmdline_processor::register_flag
cmd_gen_build
(
0
,
"
_gen_build
"
,
"
Generate build information
"
,
[]{ cmdline.
gen_build
(); }
);
static
auto
flag_verbose =
false
;
static
cmdline_processor::register_flag
cmd_verbose
(
9
,
"
verbose
"
,
"
Print verbose output and statistics
"
,
[]{ flag_verbose =
true
; }
);
static
auto
flag_internal_debug =
false
;
static
cmdline_processor::register_flag
cmd_internal_debug
(
0
,
"
_debug
"
,
"
Generate internal debug data, implies -verbose
"
,
[]{ flag_internal_debug =
true
; flag_verbose =
true
; }
);
static
auto
flag_print_colon_errors =
false
;
static
cmdline_processor::register_flag
cmd_print_colon_errors
(
9
,
"
format-colon-errors
"
,
"
Emit ':line:col:' format for messages - lights up some tools
"
,
[]{ flag_print_colon_errors =
true
; }
);
//
-----------------------------------------------------------------------
//
//
error: represents a user-readable error message
//
//
-----------------------------------------------------------------------
//
struct
error_entry
{
source_position where;
std::string msg;
bool
internal =
false
;
bool
fallback =
false
;
//
only emit this message if there was nothing better
error_entry
(
source_position w,
std::string_view m,
bool
i =
false
,
bool
f =
false
)
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL