-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessor.cpp
More file actions
4941 lines (4581 loc) · 172 KB
/
Copy pathpreprocessor.cpp
File metadata and controls
4941 lines (4581 loc) · 172 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
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
#include "preprocessor.h"
#include "helpers/casting.h"
#include "abi/abi_policy.h"
#include "abi/darwin_blocks.h"
#include "attributes.h"
#include "builtin_registry.h"
#include "constexpr/pp_consteval.h"
#include "perf_stats.h"
#include "target_feature_gate.h"
#include "token_spelling.h"
#include <ctime>
#include <cctype>
#include <filesystem>
#include <iostream>
#include <array>
#include <optional>
uint32_t PreProcess::intern_ident(std::string_view name) {
if (uint32_t id = idents.lookup(name)) {
return id;
}
return idents.intern(sm->spellings.store(name), [&](std::string_view s) {
return aburi_lookup_keyword(s, lang_opts);
});
}
void PreProcess::install_macro(MacroDefinition mac) {
uint32_t id = intern_ident(mac.name);
mac.name_ident = id;
idents.info(id).maybe_macro = true;
macro_table[id] = std::move(mac);
}
MacroDefinition* PreProcess::find_macro_by_name(std::string_view name) {
uint32_t id = idents.lookup(name);
if (id == 0) {
return nullptr;
}
auto it = macro_table.find(id);
return it == macro_table.end() ? nullptr : &it->second;
}
HideSetId PreProcess::hide_set_intern(std::vector<uint32_t> sorted_names) {
if (sorted_names.empty()) {
return 0;
}
auto [it, inserted] = hide_set_dedup_.emplace(
sorted_names, static_cast<HideSetId>(hide_sets_.size() + 1));
if (inserted) {
hide_sets_.push_back(std::move(sorted_names));
}
return it->second;
}
HideSetId PreProcess::hide_set_insert(HideSetId set, uint32_t name_ident) {
uint32_t name_id = name_ident;
if (set == 0) {
return hide_set_intern({name_id});
}
uint64_t memo_key = (static_cast<uint64_t>(set) << 32) | name_id;
auto memo = hide_insert_memo_.find(memo_key);
if (memo != hide_insert_memo_.end()) {
return memo->second;
}
const std::vector<uint32_t>& base = hide_sets_[set - 1];
std::vector<uint32_t> merged;
merged.reserve(base.size() + 1);
auto pos = std::lower_bound(base.begin(), base.end(), name_id);
merged.assign(base.begin(), pos);
if (pos == base.end() || *pos != name_id) {
merged.push_back(name_id);
}
merged.insert(merged.end(), pos, base.end());
HideSetId result = hide_set_intern(std::move(merged));
hide_insert_memo_.emplace(memo_key, result);
return result;
}
HideSetId PreProcess::hide_set_union(HideSetId lhs, HideSetId rhs) {
if (lhs == 0 || lhs == rhs) {
return rhs;
}
if (rhs == 0) {
return lhs;
}
if (lhs > rhs) {
std::swap(lhs, rhs);
}
uint64_t memo_key = (static_cast<uint64_t>(lhs) << 32) | rhs;
auto memo = hide_union_memo_.find(memo_key);
if (memo != hide_union_memo_.end()) {
return memo->second;
}
const std::vector<uint32_t>& a = hide_sets_[lhs - 1];
const std::vector<uint32_t>& b = hide_sets_[rhs - 1];
std::vector<uint32_t> merged;
merged.reserve(a.size() + b.size());
std::set_union(a.begin(), a.end(), b.begin(), b.end(),
std::back_inserter(merged));
HideSetId result = hide_set_intern(std::move(merged));
hide_union_memo_.emplace(memo_key, result);
return result;
}
HideSetId PreProcess::hide_set_intersect(HideSetId lhs, HideSetId rhs) {
if (lhs == 0 || rhs == 0) {
return 0;
}
if (lhs == rhs) {
return lhs;
}
if (lhs > rhs) {
std::swap(lhs, rhs);
}
uint64_t memo_key = (static_cast<uint64_t>(lhs) << 32) | rhs;
auto memo = hide_intersect_memo_.find(memo_key);
if (memo != hide_intersect_memo_.end()) {
return memo->second;
}
const std::vector<uint32_t>& a = hide_sets_[lhs - 1];
const std::vector<uint32_t>& b = hide_sets_[rhs - 1];
std::vector<uint32_t> common;
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(),
std::back_inserter(common));
HideSetId result = hide_set_intern(std::move(common));
hide_intersect_memo_.emplace(memo_key, result);
return result;
}
bool PreProcess::hide_set_contains(HideSetId set, uint32_t name_ident) const {
if (set == 0 || name_ident == 0) {
return false;
}
const std::vector<uint32_t>& names = hide_sets_[set - 1];
return std::binary_search(names.begin(), names.end(), name_ident);
}
enum class DirectiveKind {
Define, Undef, Line, Error, Warning, Pragma, Include, IncludeNext,
Import, If, Ifdef, Ifndef, Else, Elif, Elifdef, Elifndef, Embed, Endif,
Ident, Sccs, Unknown
};
static std::string perf_header_key(const std::shared_ptr<FileSrc>& file) {
if (!file) {
return "";
}
if (file->directory.empty()) {
return file->file_name;
}
return (std::filesystem::path(file->directory) / file->file_name).string();
}
static DirectiveKind classify_directive(std::string_view name) {
static const std::unordered_map<std::string_view, DirectiveKind> table = {
{"define", DirectiveKind::Define},
{"undef", DirectiveKind::Undef},
{"line", DirectiveKind::Line},
{"error", DirectiveKind::Error},
{"warning", DirectiveKind::Warning},
{"pragma", DirectiveKind::Pragma},
{"include", DirectiveKind::Include},
{"include_next", DirectiveKind::IncludeNext},
{"import", DirectiveKind::Import},
{"if", DirectiveKind::If},
{"ifdef", DirectiveKind::Ifdef},
{"ifndef", DirectiveKind::Ifndef},
{"else", DirectiveKind::Else},
{"elif", DirectiveKind::Elif},
{"elifdef", DirectiveKind::Elifdef},
{"elifndef", DirectiveKind::Elifndef},
{"embed", DirectiveKind::Embed},
{"endif", DirectiveKind::Endif},
{"ident", DirectiveKind::Ident},
{"sccs", DirectiveKind::Sccs},
};
auto it = table.find(std::string_view(name));
return it != table.end() ? it->second : DirectiveKind::Unknown;
}
static bool is_power_of_two(size_t value) {
return value != 0 && (value & (value - 1)) == 0;
}
static bool is_c23_family_standard(const std::string& std_name) {
return std_name == "c23" || std_name == "gnu23" ||
std_name == "c2x" || std_name == "gnu2x";
}
static std::optional<size_t> parse_pack_alignment_value(const Token& tok, size_t max_alignment) {
if (tok.type != TokenType::INTEGER_CONST && tok.type != TokenType::PP_NUMBER) {
return std::nullopt;
}
const std::string& text = std::string(tok.value);
size_t i = 0;
while (i < text.size() && std::isdigit(static_cast<unsigned char>(text[i]))) {
++i;
}
if (i == 0) {
return std::nullopt;
}
size_t value = 0;
try {
value = static_cast<size_t>(std::stoull(text.substr(0, i)));
} catch (...) {
return std::nullopt;
}
if (!is_power_of_two(value)) {
return std::nullopt;
}
if (value > max_alignment) {
return std::nullopt;
}
return value;
}
void PreProcess::set_perf_profiler(PerfProfiler* profiler) {
perf_profiler = profiler;
perf_full_detail = profiler != nullptr && profiler->wants_full();
if (sm) {
sm->perf_profiler = profiler;
}
}
static std::optional<WarningId> warning_id_from_flag(std::string_view flag) {
if (flag.rfind("-W", 0) == 0) {
flag.remove_prefix(2);
} else if (flag.rfind("W", 0) == 0) {
flag.remove_prefix(1);
}
if (flag.rfind("no-", 0) == 0) {
flag.remove_prefix(3);
}
if (flag == "deprecated-declarations") return WarningId::DeprecatedDeclarations;
if (flag == "discarded-qualifiers") return WarningId::DiscardedQualifiers;
if (flag == "unknown-attributes") return WarningId::UnknownAttributes;
if (flag == "attributes") return WarningId::Attributes;
if (flag == "override-init") return WarningId::OverrideInit;
if (flag == "varargs") return WarningId::Varargs;
return std::nullopt;
}
static std::string_view byte_spelling(unsigned char value) {
static const std::array<std::string, 256> table = [] {
std::array<std::string, 256> out;
for (size_t i = 0; i < out.size(); ++i) {
out[i] = std::to_string(i);
}
return out;
}();
return table[value];
}
static std::vector<Token> tokenize_pragma_text(std::string_view text, SrcLoc loc,
SpellingArena& arena) {
text = arena.store(text);
auto is_pp_identifier_start = [](unsigned char ch, char raw) {
return raw == '_' || raw == '$' || std::isalpha(ch);
};
auto is_pp_identifier_continue = [&](unsigned char ch, char raw) {
return raw == '_' || raw == '$' || std::isalnum(ch);
};
std::vector<Token> tokens;
size_t i = 0;
while (i < text.size()) {
unsigned char ch = static_cast<unsigned char>(text[i]);
if (std::isspace(ch)) {
++i;
continue;
}
if (is_pp_identifier_start(ch, text[i])) {
size_t start = i++;
while (i < text.size()) {
unsigned char c = static_cast<unsigned char>(text[i]);
if (!is_pp_identifier_continue(c, text[i])) break;
++i;
}
tokens.emplace_back(TokenType::IDENTIFIER, text.substr(start, i - start), loc);
continue;
}
if (std::isdigit(ch)) {
size_t start = i++;
while (i < text.size() && std::isdigit(static_cast<unsigned char>(text[i]))) {
++i;
}
tokens.emplace_back(TokenType::INTEGER_CONST, text.substr(start, i - start), loc);
continue;
}
if (text[i] == '(') {
tokens.emplace_back(TokenType::LEFT_PAREN, "(", loc);
++i;
continue;
}
if (text[i] == ')') {
tokens.emplace_back(TokenType::RIGHT_PAREN, ")", loc);
++i;
continue;
}
if (text[i] == ',') {
tokens.emplace_back(TokenType::COMMA, ",", loc);
++i;
continue;
}
if (text[i] == '"') {
++i;
std::string value;
while (i < text.size()) {
char c = text[i];
if (c == '\\' && i + 1 < text.size()) {
value += text[i + 1];
i += 2;
continue;
}
if (c == '"') {
++i;
break;
}
value += c;
++i;
}
tokens.emplace_back(TokenType::STRING_LITERAL, arena.store(value), loc);
continue;
}
++i;
}
return tokens;
}
static std::optional<std::string> parse_pragma_macro_name(const std::vector<Token>& tokens) {
if (tokens.size() != 4) {
return std::nullopt;
}
if (tokens[1].type != TokenType::LEFT_PAREN ||
tokens[2].type != TokenType::STRING_LITERAL ||
tokens[3].type != TokenType::RIGHT_PAREN) {
return std::nullopt;
}
return std::string(tokens[2].value);
}
static bool token_can_carry_literal_suffix(TokenType type) {
switch (type) {
case TokenType::INTEGER_CONST:
case TokenType::UNSIGNED_INTEGER_CONST:
case TokenType::LONG_CONST:
case TokenType::UNSIGNED_LONG_CONST:
case TokenType::LONG_LONG_CONST:
case TokenType::UNSIGNED_LONG_LONG_CONST:
case TokenType::BITINT_CONST:
case TokenType::UNSIGNED_BITINT_CONST:
case TokenType::FLOAT_CONST:
case TokenType::DOUBLE_CONST:
case TokenType::LONG_DOUBLE_CONST:
case TokenType::CHAR_LITERAL:
case TokenType::STRING_LITERAL:
return true;
default:
return false;
}
}
static std::string preprocessor_token_spelling(
const Token& token,
const IdentTable& idents) {
std::string spelling = aburi::token_spelling_for_output(token);
if (token.ident != 0 && token_can_carry_literal_suffix(token.type)) {
spelling += idents.info(token.ident).spelling;
}
return spelling;
}
static std::string token_sequence_spelling_for_macro_dump(
const std::vector<Token>& tokens,
const IdentTable& idents) {
std::string text;
bool first = true;
for (const auto& token : tokens) {
if (!first) {
text.push_back(' ');
}
text += preprocessor_token_spelling(token, idents);
first = false;
}
return text;
}
static std::vector<Token> builtin_macro_tokens_for_dump(const PreProcess& pp,
const MacroDefinition& mdef) {
std::vector<Token> result;
switch (mdef.builtin_kind) {
case MacroDefinition::BuiltinKind::Line:
result.emplace_back(TokenType::INTEGER_CONST, "1", SrcLoc());
break;
case MacroDefinition::BuiltinKind::File:
case MacroDefinition::BuiltinKind::BaseFile:
result.emplace_back(TokenType::STRING_LITERAL, pp.base_file_name.empty() ? "<stdin>" : pp.base_file_name,
SrcLoc());
break;
case MacroDefinition::BuiltinKind::FileName:
result.emplace_back(TokenType::STRING_LITERAL,
pp.sm->spellings.store(
std::filesystem::path(pp.base_file_name.empty() ? "<stdin>" : pp.base_file_name)
.filename().string()),
SrcLoc());
break;
case MacroDefinition::BuiltinKind::Counter:
result.emplace_back(TokenType::INTEGER_CONST, "0", SrcLoc());
break;
case MacroDefinition::BuiltinKind::Date:
result.emplace_back(TokenType::STRING_LITERAL, pp.builtin_date, SrcLoc());
break;
case MacroDefinition::BuiltinKind::Time:
result.emplace_back(TokenType::STRING_LITERAL, pp.builtin_time, SrcLoc());
break;
case MacroDefinition::BuiltinKind::Stdc:
result.emplace_back(TokenType::INTEGER_CONST, "1", SrcLoc());
break;
case MacroDefinition::BuiltinKind::StdcVersion: {
auto stdc_version = pp.lang_opts.stdc_version_macro_value();
if (stdc_version.has_value()) {
result.emplace_back(TokenType::LONG_CONST,
pp.sm->spellings.store(std::to_string(*stdc_version)), SrcLoc());
}
break;
}
case MacroDefinition::BuiltinKind::StdcHosted:
result.emplace_back(TokenType::INTEGER_CONST, "1", SrcLoc());
break;
case MacroDefinition::BuiltinKind::CPlusPlus: {
auto cplusplus = pp.lang_opts.cplusplus_macro_value();
if (cplusplus.has_value()) {
result.emplace_back(TokenType::LONG_CONST,
pp.sm->spellings.store(std::to_string(*cplusplus)), SrcLoc());
}
break;
}
case MacroDefinition::BuiltinKind::None:
break;
}
return result;
}
static std::string escape_line_marker_filename(std::string_view file) {
std::string escaped;
escaped.reserve(file.size());
for (char ch : file) {
if (ch == '\\' || ch == '"') {
escaped.push_back('\\');
}
escaped.push_back(ch);
}
return escaped;
}
static const SLocEntry* resolve_file_entry_for_loc(const SourceManager& sm, SrcLoc loc) {
if (loc.isInvalid()) {
return nullptr;
}
const SLocEntry* entry = &sm.getEntryForLocation(loc);
while (entry->is_expansion) {
loc = entry->macro_src.caller;
if (loc.isInvalid()) {
return nullptr;
}
entry = &sm.getEntryForLocation(loc);
}
if (!entry->file_src) {
return nullptr;
}
return entry;
}
static LiteralPrefix merge_literal_prefix(LiteralPrefix lhs, LiteralPrefix rhs) {
if (lhs == rhs) {
return lhs;
}
if (lhs == LiteralPrefix::None) {
return rhs;
}
if (rhs == LiteralPrefix::None) {
return lhs;
}
return lhs;
}
static bool parse_has_include_operand(const std::vector<Token>& tokens, std::string& header, bool& is_system) {
if (tokens.empty()) {
return false;
}
const Token& first = tokens[0];
if (first.type == TokenType::STRING_LITERAL) {
if (tokens.size() != 1) {
return false;
}
is_system = false;
header = first.value;
return true;
}
if (first.type == TokenType::LESS_THAN || first.value == "<") {
if (tokens.size() < 2) {
return false;
}
const Token& last = tokens.back();
if (!(last.type == TokenType::GREATER_THAN || last.value == ">")) {
return false;
}
is_system = true;
header.clear();
for (size_t i = 1; i + 1 < tokens.size(); ++i) {
if (tokens[i].type == TokenType::STRING_LITERAL) {
header += "\"" + std::string(tokens[i].value) + "\"";
} else {
header += tokens[i].value;
}
}
return true;
}
return false;
}
static std::string basename_from_path(const std::string& path) {
if (path.empty()) {
return path;
}
std::filesystem::path p(path);
auto name = p.filename().string();
if (name.empty()) {
return path;
}
return name;
}
static bool is_builtin_defined_name(std::string_view name) {
return name == "__has_attribute" ||
name == "__has_builtin" ||
name == "__has_c_attribute" ||
name == "__has_cpp_attribute" ||
name == "__has_embed" ||
name == "__has_extension" ||
name == "__has_feature" ||
name == "__has_warning" ||
name == "__is_identifier" ||
name == "__has_include" ||
name == "__has_include_next";
}
static std::string canonicalize_preprocessor_attribute_name(const std::string& name) {
std::string canonical = name;
size_t scope_pos = canonical.rfind("::");
if (scope_pos != std::string::npos) {
canonical = canonical.substr(scope_pos + 2);
}
return aburi::canonicalize_attribute_name(canonical);
}
struct HasQueryOperand {
std::string query;
std::vector<std::string> scope_segments;
std::string leaf_name;
bool is_string_literal = false;
};
static std::optional<HasQueryOperand> extract_has_query_operand(const std::vector<Token>& tokens) {
auto skip_whitespace = [&](size_t& index) {
while (index < tokens.size() && tokens[index].type == TokenType::Whitespace) {
++index;
}
};
size_t index = 0;
skip_whitespace(index);
if (index >= tokens.size()) {
return std::nullopt;
}
HasQueryOperand operand;
if (tokens[index].type == TokenType::STRING_LITERAL) {
operand.query = tokens[index].value;
operand.leaf_name = tokens[index].value;
operand.is_string_literal = true;
++index;
skip_whitespace(index);
if (index != tokens.size()) {
return std::nullopt;
}
return operand;
}
std::vector<std::string> segments;
if (!tokens[index].isIdentifierLike()) {
return std::nullopt;
}
while (true) {
segments.push_back(std::string(tokens[index].value));
++index;
skip_whitespace(index);
if (index >= tokens.size()) {
break;
}
bool has_scope_resolution = false;
size_t scope_token_count = 0;
if (tokens[index].type == TokenType::SCOPE_RESOLUTION) {
has_scope_resolution = true;
scope_token_count = 1;
} else if (index + 1 < tokens.size() &&
tokens[index].type == TokenType::COLON &&
tokens[index + 1].type == TokenType::COLON) {
has_scope_resolution = true;
scope_token_count = 2;
}
if (!has_scope_resolution) {
return std::nullopt;
}
index += scope_token_count;
skip_whitespace(index);
if (index >= tokens.size() || !tokens[index].isIdentifierLike()) {
return std::nullopt;
}
}
if (segments.empty()) {
return std::nullopt;
}
operand.leaf_name = segments.back();
if (segments.size() > 1) {
operand.scope_segments.assign(segments.begin(), segments.end() - 1);
}
for (size_t i = 0; i < segments.size(); ++i) {
if (i != 0) {
operand.query += "::";
}
operand.query += segments[i];
}
return operand;
}
static std::optional<Token> extract_identifier_query_operand(
const std::vector<Token>& tokens) {
auto skip_whitespace = [&](size_t& index) {
while (index < tokens.size() &&
tokens[index].type == TokenType::Whitespace) {
++index;
}
};
size_t index = 0;
skip_whitespace(index);
if (index >= tokens.size() || !tokens[index].isIdentifierLike()) {
return std::nullopt;
}
Token operand = tokens[index++];
skip_whitespace(index);
if (index != tokens.size()) {
return std::nullopt;
}
return operand;
}
static std::string canonicalize_attribute_namespace(const std::string& ns) {
if (ns == "__gnu__") {
return "gnu";
}
if (ns == "_Clang" || ns == "__clang__") {
return "clang";
}
return ns;
}
static uint64_t cpp_standard_attribute_value(const std::string& canonical_name,
const LangOptions& lang_opts,
const TargetInfo* target_info) {
if (!aburi::AttributeRegistry::instance().is_active(canonical_name)) {
return 0;
}
if (canonical_name == "no_unique_address") {
const bool cxx20 = lang_opts.is_cxx_mode() &&
(lang_opts.standard.empty() || lang_opts.is_cxx20_or_later());
const bool itanium = target_info &&
abi_policy_for_target(*target_info).cxx_abi == CxxAbiKind::Itanium;
return cxx20 && itanium ? 201803ULL : 0;
}
static const std::unordered_map<std::string, uint64_t> values = {
{"noreturn", 200809ULL},
{"deprecated", 201309ULL},
{"fallthrough", 201603ULL},
{"nodiscard", 201907ULL},
{"maybe_unused", 201603ULL},
{"no_unique_address", 201803ULL},
{"likely", 201803ULL},
{"unlikely", 201803ULL},
{"assume", 202207ULL},
};
auto it = values.find(canonical_name);
return it != values.end() ? it->second : 0;
}
static bool supports_gnu_attribute_name(const std::string& canonical_name) {
return aburi::AttributeRegistry::instance().is_active(canonical_name);
}
static uint64_t has_cpp_attribute_value(const HasQueryOperand& operand,
const LangOptions& lang_opts,
const TargetInfo* target_info) {
if (operand.is_string_literal) {
return 0;
}
const std::string canonical_name =
canonicalize_preprocessor_attribute_name(operand.query);
if (operand.scope_segments.empty()) {
return cpp_standard_attribute_value(canonical_name, lang_opts,
target_info);
}
if (operand.scope_segments.size() != 1) {
return 0;
}
const std::string canonical_ns =
canonicalize_attribute_namespace(operand.scope_segments.front());
if (canonical_ns == "msvc") {
return 0;
}
if (canonical_ns == "clang") {
return 0;
}
if (canonical_ns == "gnu") {
return supports_gnu_attribute_name(canonical_name) ? 1 : 0;
}
return 0;
}
static uint64_t c_standard_attribute_value(const std::string& canonical_name) {
if (!aburi::AttributeRegistry::instance().is_active(canonical_name)) {
return 0;
}
static const std::unordered_map<std::string, uint64_t> values = {
{"deprecated", 201904ULL},
{"fallthrough", 201904ULL},
{"maybe_unused", 201904ULL},
{"nodiscard", 202003ULL},
{"noreturn", 202202ULL},
{"_Noreturn", 202202ULL},
{"unsequenced", 202207ULL},
{"reproducible", 202207ULL},
};
auto it = values.find(canonical_name);
return it != values.end() ? it->second : 0;
}
static uint64_t has_c_attribute_value(const HasQueryOperand& operand) {
if (operand.is_string_literal) {
return 0;
}
const std::string canonical_name =
canonicalize_preprocessor_attribute_name(operand.query);
if (operand.scope_segments.empty()) {
return c_standard_attribute_value(canonical_name);
}
if (operand.scope_segments.size() != 1) {
return 0;
}
const std::string canonical_ns =
canonicalize_attribute_namespace(operand.scope_segments.front());
if (canonical_ns == "gnu") {
return supports_gnu_attribute_name(canonical_name) ? 1 : 0;
}
return 0;
}
static bool has_feature_name(const std::string& name,
const LangOptions& lang_opts,
const TargetInfo* target_info) {
if (name == "blocks") {
if (!target_info) {
return false;
}
return darwin_blocks::blocks_enabled_for_langopts(lang_opts, *target_info);
}
static const std::unordered_set<std::string> kFeatures = {
"attribute_deprecated_with_message"
};
if (lang_opts.is_objc()) {
static const std::unordered_set<std::string> kObjCFeatures = {
"objc_instancetype", "objc_generics", "objc_generics_variance",
"objc_kindof", "objc_class_property",
"objc_fixed_enum", "objc_bridge_id",
"objc_bridge_id_on_typedefs", "nullability",
"nullability_on_arrays", "assume_nonnull",
"attribute_availability", "attribute_availability_with_message",
"enumerator_attributes"
};
if (kObjCFeatures.contains(name)) {
return true;
}
if (lang_opts.is_objc_arc() &&
(name == "objc_arc" || name == "objc_arc_weak")) {
return true;
}
}
if (kFeatures.contains(name)) {
return true;
}
if (name == "cxx_concepts") {
return lang_opts.is_cxx20_or_later();
}
if (name == "cxx_atomic") {
return lang_opts.is_cxx_mode();
}
if (name == "cxx_raw_string_literals") {
return lang_opts.is_cxx_mode();
}
if (name == "cxx_rtti") {
return lang_opts.is_cxx_mode();
}
if (name == "cxx_exceptions") {
return lang_opts.is_cxx_mode() && lang_opts.exceptions_enabled;
}
if (name == "c_atomic") {
if (!lang_opts.is_c_mode()) {
return false;
}
if (lang_opts.standard.empty()) {
return true;
}
return lang_opts.standard == "c11" || lang_opts.standard == "gnu11" ||
lang_opts.standard == "c17" || lang_opts.standard == "gnu17" ||
is_c23_family_standard(lang_opts.standard);
}
return false;
}
static bool has_extension_name(const std::string& name,
const LangOptions& lang_opts,
const TargetInfo* target_info) {
if (name == "c_atomic") {
return true;
}
if (name == "cxx_atomic") {
return lang_opts.is_cxx_mode();
}
return has_feature_name(name, lang_opts, target_info);
}
inline bool is_hspace(char c) {
return c == ' ' || c == '\t' || c == '\f' || c == '\v';
}
std::string_view ltrim_hspace(std::string_view text) {
size_t i = 0;
while (i < text.size() && is_hspace(text[i])) {
++i;
}
return text.substr(i);
}
std::string_view trim_hspace(std::string_view text) {
size_t start = 0;
size_t end = text.size();
while (start < end && is_hspace(text[start])) {
++start;
}
while (end > start && is_hspace(text[end - 1])) {
--end;
}
return text.substr(start, end - start);
}
bool parse_pp_identifier(std::string_view& text, std::string_view& ident) {
auto is_pp_identifier_start = [](unsigned char ch, char raw) {
return raw == '_' || raw == '$' || std::isalpha(ch);
};
auto is_pp_identifier_continue = [&](unsigned char ch, char raw) {
return raw == '_' || raw == '$' || std::isalnum(ch);
};
text = ltrim_hspace(text);
if (text.empty()) {
return false;
}
const unsigned char first = static_cast<unsigned char>(text[0]);
if (!is_pp_identifier_start(first, text[0])) {
return false;
}
size_t i = 1;
while (i < text.size()) {
const unsigned char c = static_cast<unsigned char>(text[i]);
if (!is_pp_identifier_continue(c, text[i])) {
break;
}
++i;
}
ident = text.substr(0, i);
text.remove_prefix(i);
return true;
}
bool parse_pragma_once_line(std::string_view line) {
std::string_view word;
if (!parse_pp_identifier(line, word)) {
return false;
}
if (word != "once") {
return false;
}
return trim_hspace(line).empty();
}
bool parse_if_not_defined_line_fast(std::string_view line, std::string& macro) {
line = ltrim_hspace(line);
if (line.empty() || line.front() != '!') {
return false;
}
line.remove_prefix(1);
line = ltrim_hspace(line);
if (!line.starts_with("defined")) {
return false;
}
if (line.size() > 7) {
const unsigned char boundary = static_cast<unsigned char>(line[7]);
if (line[7] == '_' || line[7] == '$' || std::isalnum(boundary)) {
return false;
}
}
line.remove_prefix(7);
line = ltrim_hspace(line);
std::string_view ident;
if (!line.empty() && line.front() == '(') {
line.remove_prefix(1);
if (!parse_pp_identifier(line, ident)) {
return false;
}
line = ltrim_hspace(line);
if (line.empty() || line.front() != ')') {
return false;
}
line.remove_prefix(1);
} else {
if (!parse_pp_identifier(line, ident)) {
return false;
}
}
if (!trim_hspace(line).empty()) {
return false;
}
macro.assign(ident.data(), ident.size());
return true;
}
bool detect_include_guard_fast(const std::string& text, std::string& guard_macro, bool& saw_pragma_once) {
saw_pragma_once = false;
guard_macro.clear();
enum class GuardScanState {
Prefix,
ExpectDefine,
InGuard,
AfterGuard
};
GuardScanState state = GuardScanState::Prefix;
bool in_block_comment = false;
size_t pos = 0;
int depth = 0;
while (pos <= text.size()) {
const size_t line_end = text.find('\n', pos);
const size_t effective_end = (line_end == std::string::npos) ? text.size() : line_end;
std::string_view line(text.data() + pos, effective_end - pos);
pos = (line_end == std::string::npos) ? text.size() + 1 : line_end + 1;
while (true) {
line = ltrim_hspace(line);
if (line.empty()) {
break;
}
if (in_block_comment) {
const size_t comment_end = line.find("*/");
if (comment_end == std::string::npos) {
line = {};
break;
}
line.remove_prefix(comment_end + 2);
in_block_comment = false;
continue;
}