-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathcxtokenizer.cpp
828 lines (724 loc) · 26.5 KB
/
cxtokenizer.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
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
// Copyright (C) 2017-2023 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#include "cxtokenizer.hpp"
#include <cctype>
#include "libclang_visitor.hpp"
#include "parse_error.hpp"
using namespace cppast;
detail::cxtoken::cxtoken(const CXTranslationUnit& tu_unit, const CXToken& token)
: value_(clang_getTokenSpelling(tu_unit, token)), kind_(clang_getTokenKind(token))
{}
namespace
{
bool cursor_is_function(CXCursorKind kind)
{
return kind == CXCursor_FunctionDecl || kind == CXCursor_CXXMethod
|| kind == CXCursor_Constructor || kind == CXCursor_Destructor
|| kind == CXCursor_ConversionFunction;
}
bool cursor_is_var(CXCursorKind kind)
{
return kind == CXCursor_VarDecl || kind == CXCursor_FieldDecl;
}
bool is_in_range(const CXSourceLocation& loc, const CXSourceRange& range)
{
auto begin = clang_getRangeStart(range);
auto end = clang_getRangeEnd(range);
CXFile f_loc, f_begin, f_end;
unsigned l_loc, l_begin, l_end;
clang_getSpellingLocation(loc, &f_loc, &l_loc, nullptr, nullptr);
clang_getSpellingLocation(begin, &f_begin, &l_begin, nullptr, nullptr);
clang_getSpellingLocation(end, &f_end, &l_end, nullptr, nullptr);
return l_loc >= l_begin && l_loc < l_end && clang_File_isEqual(f_loc, f_begin);
}
// heuristic to detect when the type of a variable is declared inline,
// i.e. `struct foo {} f`
bool has_inline_type_definition(CXCursor var_decl)
{
auto type_decl = clang_getTypeDeclaration(clang_getCursorType(var_decl));
if (clang_Cursor_isNull(type_decl))
return false;
auto type_loc = clang_getCursorLocation(type_decl);
auto var_range = clang_getCursorExtent(var_decl);
return is_in_range(type_loc, var_range);
}
class simple_tokenizer
{
public:
explicit simple_tokenizer(const CXTranslationUnit& tu, const CXSourceRange& range) : tu_(tu)
{
clang_tokenize(tu, range, &tokens_, &no_);
}
~simple_tokenizer()
{
clang_disposeTokens(tu_, tokens_, no_);
}
simple_tokenizer(const simple_tokenizer&) = delete;
simple_tokenizer& operator=(const simple_tokenizer&) = delete;
unsigned size() const noexcept
{
return no_;
}
const CXToken& operator[](unsigned i) const noexcept
{
DEBUG_ASSERT(i < no_, detail::assert_handler{});
return tokens_[i];
}
std::string get_spelling(std::size_t length) noexcept
{
// might need multiple tokens, because [[, for example, is treated as two separate tokens
std::string result;
for (auto cur = 0u; cur < no_; ++cur)
{
auto cur_spelling = detail::cxstring(clang_getTokenSpelling(tu_, tokens_[cur]));
result += cur_spelling.c_str();
if (result.length() >= length)
return result;
}
return result;
}
private:
CXTranslationUnit tu_;
CXToken* tokens_;
unsigned no_;
};
CXSourceLocation get_next_location_impl(const CXTranslationUnit& tu, CXFile file,
const CXSourceLocation& loc, int inc = 1)
{
DEBUG_ASSERT(clang_Location_isFromMainFile(loc), detail::assert_handler{});
unsigned offset;
clang_getSpellingLocation(loc, nullptr, nullptr, nullptr, &offset);
if (inc >= 0)
offset += unsigned(inc);
else
offset -= unsigned(-inc);
return clang_getLocationForOffset(tu, file, offset);
}
CXSourceLocation get_next_location(const CXTranslationUnit& tu, const CXFile& file,
const CXSourceLocation& loc, std::size_t token_length)
{
// simple move over by token_length
return get_next_location_impl(tu, file, loc, int(token_length));
}
CXSourceLocation get_prev_location(const CXTranslationUnit& tu, const CXFile& file,
const CXSourceLocation& loc, std::size_t token_length)
{
auto inc = 1;
while (true)
{
auto loc_before = get_next_location_impl(tu, file, loc, -inc);
DEBUG_ASSERT(!clang_equalLocations(loc_before, loc), detail::assert_handler{});
if (!clang_Location_isFromMainFile(loc_before))
// out of range
return clang_getNullLocation();
simple_tokenizer tokenizer(tu, clang_getRange(loc_before, loc));
auto token_location = clang_getTokenLocation(tu, tokenizer[0]);
if (clang_equalLocations(loc_before, token_location))
{
// actually found a new token and not just whitespace
// loc_before is now the last character of the new token
// need to move by token_length - 1 to get to the first character
return get_next_location_impl(tu, file, loc, -1 * (inc + int(token_length) - 1));
}
else
++inc;
}
return clang_getNullLocation();
}
bool token_at_is(const CXTranslationUnit& tu, const CXFile& file, const CXSourceLocation& loc,
const char* token_str)
{
auto length = std::strlen(token_str);
auto loc_after = get_next_location(tu, file, loc, length);
if (!clang_Location_isFromMainFile(loc_after))
return false;
simple_tokenizer tokenizer(tu, clang_getRange(loc, loc_after));
return tokenizer.get_spelling(length) == token_str;
}
bool consume_if_token_at_is(const CXTranslationUnit& tu, const CXFile& file, CXSourceLocation& loc,
const char* token_str)
{
auto length = std::strlen(token_str);
auto loc_after = get_next_location(tu, file, loc, length);
if (!clang_Location_isFromMainFile(loc_after))
return false;
simple_tokenizer tokenizer(tu, clang_getRange(loc, loc_after));
if (tokenizer.get_spelling(length) == token_str)
{
loc = loc_after;
return true;
}
else
return false;
}
bool token_before_is(const CXTranslationUnit& tu, const CXFile& file, const CXSourceLocation& loc,
const char* token_str)
{
auto length = std::strlen(token_str);
auto loc_before = get_prev_location(tu, file, loc, length);
if (!clang_Location_isFromMainFile(loc_before))
return false;
simple_tokenizer tokenizer(tu, clang_getRange(loc_before, loc));
return tokenizer.get_spelling(length) == token_str;
}
bool consume_if_token_before_is(const CXTranslationUnit& tu, const CXFile& file,
CXSourceLocation& loc, const char* token_str)
{
auto length = std::strlen(token_str);
auto loc_before = get_prev_location(tu, file, loc, length);
if (!clang_Location_isFromMainFile(loc_before))
return false;
simple_tokenizer tokenizer(tu, clang_getRange(loc_before, loc));
if (tokenizer.get_spelling(length) == token_str)
{
loc = loc_before;
return true;
}
else
return false;
}
struct Extent
{
CXSourceRange first_part;
CXSourceRange second_part;
};
// clang_getCursorExtent() is somehow broken in various ways
// this function returns the actual CXSourceRange that covers all parts required for parsing
// might include more tokens
// this function is the reason you shouldn't use libclang
Extent get_extent(const CXTranslationUnit& tu, const CXFile& file, const CXCursor& cur)
{
auto extent = clang_getCursorExtent(cur);
auto begin = clang_getRangeStart(extent);
auto end = clang_getRangeEnd(extent);
auto kind = clang_getCursorKind(cur);
// first need to extend the range to capture attributes that are before the declaration
if (cursor_is_function(kind) || cursor_is_function(clang_getTemplateCursorKind(cur))
|| kind == CXCursor_VarDecl || kind == CXCursor_FieldDecl || kind == CXCursor_ParmDecl
|| kind == CXCursor_NonTypeTemplateParameter)
{
while (token_before_is(tu, file, begin, "]]") || token_before_is(tu, file, begin, ")"))
{
auto save_begin = begin;
if (consume_if_token_before_is(tu, file, begin, "]]"))
{
while (!consume_if_token_before_is(tu, file, begin, "[["))
begin = get_prev_location(tu, file, begin, 1);
}
else if (consume_if_token_before_is(tu, file, begin, ")"))
{
// maybe alignas specifier
auto paren_count = 1;
for (auto last_begin = begin; paren_count != 0; last_begin = begin)
{
if (token_before_is(tu, file, begin, "("))
--paren_count;
else if (token_before_is(tu, file, begin, ")"))
++paren_count;
begin = get_prev_location(tu, file, begin, 1);
DEBUG_ASSERT(!clang_equalLocations(last_begin, begin),
detail::parse_error_handler{}, cur,
"infinite loop in alignas parsing");
}
if (!consume_if_token_before_is(tu, file, begin, "alignas"))
{
// not alignas
begin = save_begin;
break;
}
}
}
}
if (cursor_is_function(kind) || cursor_is_function(clang_getTemplateCursorKind(cur)))
{
if (clang_CXXMethod_isDefaulted(cur) || !clang_isCursorDefinition(cur))
{
// defaulted or declaration: extend until semicolon
while (!token_at_is(tu, file, end, ";"))
end = get_next_location(tu, file, end, 1);
}
else
{
// declaration: remove body, we don't care about that
auto has_children = false;
detail::visit_children(cur, [&](const CXCursor& child) {
if (has_children)
return;
else if (clang_getCursorKind(child) == CXCursor_CompoundStmt
|| clang_getCursorKind(child) == CXCursor_CXXTryStmt
|| clang_getCursorKind(child) == CXCursor_InitListExpr)
{
auto child_extent = clang_getCursorExtent(child);
end = clang_getRangeStart(child_extent);
has_children = true;
}
});
}
}
else if (cursor_is_var(kind) || cursor_is_var(clang_getTemplateCursorKind(cur)))
{
// need to extend until the semicolon
while (!token_at_is(tu, file, end, ";"))
end = get_next_location(tu, file, end, 1);
if (has_inline_type_definition(cur))
{
// the type is declared inline,
// remove the type definition from the range
auto type_cursor = clang_getTypeDeclaration(clang_getCursorType(cur));
auto type_extent = clang_getCursorExtent(type_cursor);
auto type_begin = clang_getRangeStart(type_extent);
auto type_end = clang_getRangeEnd(type_extent);
return {clang_getRange(begin, type_begin), clang_getRange(type_end, end)};
}
}
else if (kind == CXCursor_TemplateTypeParameter && token_at_is(tu, file, end, "("))
{
// if you have decltype as default argument for a type template parameter
// libclang doesn't include the parameters
auto next = get_next_location(tu, file, end, 1);
auto prev = end;
for (auto paren_count = 1; paren_count != 0; next = get_next_location(tu, file, next, 1))
{
if (token_at_is(tu, file, next, "("))
++paren_count;
else if (token_at_is(tu, file, next, ")"))
--paren_count;
prev = next;
}
end = next;
}
else if (kind == CXCursor_TemplateTemplateParameter && token_at_is(tu, file, end, "<"))
{
// if you have a template template parameter in a template template parameter,
// the tokens are all messed up, only contain the `template`
// first: skip to closing angle bracket
// luckily no need to handle expressions here
auto next = get_next_location(tu, file, end, 1);
for (auto angle_count = 1; angle_count != 0; next = get_next_location(tu, file, next, 1))
{
if (token_at_is(tu, file, next, ">"))
--angle_count;
else if (token_at_is(tu, file, next, ">>"))
angle_count -= 2;
else if (token_at_is(tu, file, next, "<"))
++angle_count;
}
// second: skip until end of parameter
// no need to handle default, so look for '>' or ','
while (!token_at_is(tu, file, next, ">") && !token_at_is(tu, file, next, ","))
next = get_next_location(tu, file, next, 1);
// now we found the proper end of the token
end = get_prev_location(tu, file, next, 1);
}
else if ((kind == CXCursor_TemplateTypeParameter || kind == CXCursor_NonTypeTemplateParameter
|| kind == CXCursor_TemplateTemplateParameter))
{
// variadic tokens in unnamed parameter not included
consume_if_token_at_is(tu, file, end, "...");
}
else if (kind == CXCursor_EnumDecl && !token_at_is(tu, file, end, ";"))
{
while (!token_at_is(tu, file, end, ";"))
end = get_next_location(tu, file, end, 1);
}
else if (kind == CXCursor_EnumConstantDecl && !token_at_is(tu, file, end, ","))
{
// need to support attributes
// just give up and extend the range to the range of the entire enum...
auto parent = clang_getCursorLexicalParent(cur);
end = clang_getRangeEnd(clang_getCursorExtent(parent));
}
else if (kind == CXCursor_UnexposedDecl)
{
// include semicolon, if necessary
if (token_at_is(tu, file, end, ";"))
end = get_next_location(tu, file, end, 1);
}
return Extent{clang_getRange(begin, end), clang_getNullRange()};
}
} // namespace
detail::cxtokenizer::cxtokenizer(const CXTranslationUnit& tu, const CXFile& file,
const CXCursor& cur)
{
auto extent = get_extent(tu, file, cur);
simple_tokenizer tokenizer(tu, extent.first_part);
tokens_.reserve(tokenizer.size());
for (auto i = 0u; i != tokenizer.size(); ++i)
tokens_.emplace_back(tu, tokenizer[i]);
if (!clang_Range_isNull(extent.second_part))
{
simple_tokenizer second_tokenizer(tu, extent.second_part);
tokens_.reserve(tokens_.size() + second_tokenizer.size());
for (auto i = 0u; i != second_tokenizer.size(); ++i)
tokens_.emplace_back(tu, second_tokenizer[i]);
}
}
void detail::skip(detail::cxtoken_stream& stream, const char* str)
{
if (*str)
{
// non-empty string
DEBUG_ASSERT(!stream.done(), parse_error_handler{}, stream.cursor(),
format("expected '", str, "', got exhausted stream"));
auto& token = stream.peek();
DEBUG_ASSERT(token == str, parse_error_handler{}, stream.cursor(),
format("expected '", str, "', got '", token.c_str(), "'"));
stream.bump();
}
}
namespace
{
bool starts_with(const char*& str, const detail::cxtoken& t)
{
if (std::strncmp(str, t.c_str(), t.value().length()) != 0)
return false;
str += t.value().length();
while (*str == ' ' || *str == '\t')
++str;
return true;
}
} // namespace
bool detail::skip_if(detail::cxtoken_stream& stream, const char* str, bool multi_token)
{
if (!*str)
return true;
else if (stream.done())
return false;
auto save = stream.cur();
do
{
auto& token = stream.peek();
if (!starts_with(str, token) || (!multi_token && *str != '\0'))
{
stream.set_cur(save);
return false;
}
stream.bump();
} while (multi_token && *str);
return true;
}
namespace
{
// whether or not the current angle bracket can be a comparison
// note: this is a heuristic I hope works often enough
bool is_comparison(CXTokenKind last_kind, const detail::cxtoken& cur, CXTokenKind next_kind)
{
if (cur == "<")
return last_kind == CXToken_Literal;
else if (cur == ">")
return next_kind == CXToken_Literal;
return false;
}
} // namespace
detail::closing_bracket_pos detail::find_closing_bracket(detail::cxtoken_stream stream)
{
auto template_bracket = false;
auto open_bracket = stream.peek().c_str();
const char* close_bracket = nullptr;
if (skip_if(stream, "("))
close_bracket = ")";
else if (skip_if(stream, "{"))
close_bracket = "}";
else if (skip_if(stream, "["))
close_bracket = "]";
else if (skip_if(stream, "<"))
{
close_bracket = ">";
template_bracket = true;
}
else
DEBUG_UNREACHABLE(parse_error_handler{}, stream.cursor(),
format("expected a bracket, got '", stream.peek().c_str(), "'"));
auto bracket_count = 1;
auto paren_count = 0; // internal nested parenthesis
auto last_token = CXToken_Comment;
auto last_was_double_angle = false;
while (!stream.done() && bracket_count != 0)
{
last_was_double_angle = false;
auto& cur = stream.get();
if (paren_count == 0 && cur == open_bracket
&& !is_comparison(last_token, cur, stream.peek().kind()))
++bracket_count;
else if (paren_count == 0 && cur == close_bracket
&& !is_comparison(last_token, cur, stream.peek().kind()))
--bracket_count;
else if (paren_count == 0 && template_bracket && cur == ">>")
{
// maximal munch
bracket_count -= 2;
last_was_double_angle = true;
}
else if (cur == "(" || cur == "{" || cur == "[")
++paren_count;
else if (cur == ")" || cur == "}" || cur == "]")
--paren_count;
last_token = cur.kind();
}
DEBUG_ASSERT(bracket_count == 0 && paren_count == 0, parse_error_handler{}, stream.cursor(),
"find_closing_bracket() internal parse error");
if (last_was_double_angle)
{
return {stream.cur(), stream.cur(), true};
}
else
{
auto after = stream.cur();
stream.bump_back();
return {stream.cur(), after, false};
}
}
void detail::skip_brackets(detail::cxtoken_stream& stream)
{
auto closing = find_closing_bracket(stream);
stream.set_cur(closing.after);
}
detail::cxtoken_iterator detail::find_sequence(detail::cxtoken_stream stream,
detail::cxtoken_iterator start,
detail::cxtoken_iterator end)
{
detail::cxtoken_iterator search_start = stream.cur();
while (search_start != stream.end())
{
detail::cxtoken_iterator search_iter = search_start;
detail::cxtoken_iterator seq_iter = start;
bool failed = false;
while (!failed && search_iter != stream.end() && seq_iter != end)
{
if (search_iter->value() != seq_iter->value()
|| search_iter->kind() != seq_iter->kind())
{
failed = true;
}
else
{
++search_iter;
++seq_iter;
}
}
if (!failed)
return search_start;
++search_start;
}
return stream.end();
}
namespace
{
type_safe::optional<std::string> parse_attribute_using(detail::cxtoken_stream& stream)
{
// using identifier :
if (skip_if(stream, "using"))
{
DEBUG_ASSERT(stream.peek().kind() == CXToken_Identifier, detail::parse_error_handler{},
stream.cursor(), "expected identifier");
auto scope = stream.get().value().std_str();
skip(stream, ":");
return scope;
}
else
return type_safe::nullopt;
}
cpp_attribute_kind get_attribute_kind(const std::string& name)
{
if (name == "carries_dependency")
return cpp_attribute_kind::carries_dependency;
else if (name == "deprecated")
return cpp_attribute_kind::deprecated;
else if (name == "fallthrough")
return cpp_attribute_kind::fallthrough;
else if (name == "maybe_unused")
return cpp_attribute_kind::maybe_unused;
else if (name == "nodiscard")
return cpp_attribute_kind::nodiscard;
else if (name == "noreturn")
return cpp_attribute_kind::noreturn;
else
return cpp_attribute_kind::unknown;
}
cpp_token_string parse_attribute_arguments(detail::cxtoken_stream& stream)
{
auto end = find_closing_bracket(stream);
skip(stream, "(");
auto arguments = detail::to_string(stream, end.bracket, end.unmunch);
stream.set_cur(end.bracket);
skip(stream, ")");
return arguments;
}
cpp_attribute parse_attribute_token(detail::cxtoken_stream& stream,
type_safe::optional<std::string> scope)
{
// (identifier ::)_opt identifier ( '(' some tokens ')' )_opt ..._opt
// parse name
DEBUG_ASSERT(stream.peek().kind() == CXToken_Identifier
|| stream.peek().kind() == CXToken_Keyword,
detail::parse_error_handler{}, stream.cursor(), "expected identifier");
auto name = stream.get().value().std_str();
if (skip_if(stream, "::"))
{
// name was actually a scope, so parse name again
DEBUG_ASSERT(!scope, detail::parse_error_handler{}, stream.cursor(),
"attribute using + scope not allowed");
scope = std::move(name);
DEBUG_ASSERT(stream.peek().kind() == CXToken_Identifier
|| stream.peek().kind() == CXToken_Keyword,
detail::parse_error_handler{}, stream.cursor(), "expected identifier");
name = stream.get().value().std_str();
}
// parse arguments
type_safe::optional<cpp_token_string> arguments;
if (stream.peek() == "(")
arguments = parse_attribute_arguments(stream);
// parse variadic token
auto is_variadic = skip_if(stream, "...");
// get kind
auto kind = get_attribute_kind(name);
if (!scope && kind != cpp_attribute_kind::unknown)
return cpp_attribute(kind, std::move(arguments));
else
return cpp_attribute(std::move(scope), std::move(name), std::move(arguments), is_variadic);
}
bool parse_attribute_impl(cpp_attribute_list& result, detail::cxtoken_stream& stream)
{
if (skip_if(stream, "[") && stream.peek() == "[")
{
// C++11 attribute
// [[<attribute>]]
// ^
skip(stream, "[");
auto scope = parse_attribute_using(stream);
while (!skip_if(stream, "]"))
{
auto attribute = parse_attribute_token(stream, scope);
result.push_back(std::move(attribute));
detail::skip_if(stream, ",");
}
// [[<attribute>]]
// ^
skip(stream, "]");
return true;
}
else if (skip_if(stream, "alignas"))
{
// alignas specifier
// alignas(<some arguments>)
// ^
auto arguments = parse_attribute_arguments(stream);
result.push_back(cpp_attribute(cpp_attribute_kind::alignas_, std::move(arguments)));
}
else if (skip_if(stream, "__attribute__") && stream.peek() == "(")
{
// GCC/clang attributes
// __attribute__((<attribute>))
// ^^
skip(stream, "(");
skip(stream, "(");
auto scope = parse_attribute_using(stream);
while (!skip_if(stream, ")"))
{
auto attribute = parse_attribute_token(stream, scope);
result.push_back(std::move(attribute));
detail::skip_if(stream, ",");
}
skip(stream, ")");
return true;
}
else if (skip_if(stream, "__declspec"))
{
// MSVC declspec
// __declspec(<attribute>)
// ^
skip(stream, "(");
auto scope = parse_attribute_using(stream);
while (!skip_if(stream, ")"))
{
auto attribute = parse_attribute_token(stream, scope);
result.push_back(std::move(attribute));
detail::skip_if(stream, ",");
}
return true;
}
return false;
}
} // namespace
cpp_attribute_list detail::parse_attributes(detail::cxtoken_stream& stream, bool skip_anway)
{
cpp_attribute_list result;
while (parse_attribute_impl(result, stream))
skip_anway = false;
if (skip_anway)
stream.bump();
return result;
}
namespace
{
cpp_token_kind get_kind(const detail::cxtoken& token)
{
switch (token.kind())
{
case CXToken_Punctuation:
return cpp_token_kind::punctuation;
case CXToken_Keyword:
return cpp_token_kind::keyword;
case CXToken_Identifier:
return cpp_token_kind::identifier;
case CXToken_Literal: {
auto spelling = token.value().std_str();
if (spelling.find('.') != std::string::npos && spelling.find('\"') == std::string::npos)
return cpp_token_kind::float_literal;
else if (std::isdigit(spelling.front()))
return cpp_token_kind::int_literal;
else if (spelling.back() == '\'')
return cpp_token_kind::char_literal;
else
return cpp_token_kind::string_literal;
}
case CXToken_Comment:
break;
}
DEBUG_UNREACHABLE(detail::assert_handler{});
return cpp_token_kind::punctuation;
}
} // namespace
cpp_token_string detail::to_string(cxtoken_stream& stream, cxtoken_iterator end, bool unmunch)
{
cpp_token_string::builder builder;
while (stream.cur() != end)
{
auto& token = stream.get();
builder.add_token(cpp_token(get_kind(token), token.c_str()));
}
if (unmunch)
builder.unmunch();
return builder.finish();
}
bool detail::append_scope(detail::cxtoken_stream& stream, std::string& scope)
{
// add identifiers and "::" to current scope name,
// clear if there is any other token in between, or mismatched combination
if (stream.peek().kind() == CXToken_Identifier)
{
if (!scope.empty() && scope.back() != ':')
scope.clear();
scope += stream.get().c_str();
}
else if (stream.peek() == "::")
{
if (!scope.empty() && scope.back() == ':')
scope.clear();
scope += stream.get().c_str();
}
else if (stream.peek() == "<")
{
auto pos = detail::find_closing_bracket(stream);
scope += detail::to_string(stream, pos.bracket, pos.unmunch).as_string();
if (!detail::skip_if(stream, ">>"))
detail::skip(stream, ">");
scope += ">";
}
else
{
scope.clear();
return false;
}
return true;
}