Skip to content

Commit ccb2aaa

Browse files
committed
Implement attribute parsing
1 parent 151e0b1 commit ccb2aaa

15 files changed

Lines changed: 512 additions & 86 deletions

‎include/cppast/cpp_attribute.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace cppast
1717
/// The known C++ attributes.
1818
enum class cpp_attribute_kind
1919
{
20+
// update get_attribute_kind() in tokenizer, when updating this
21+
2022
alignas_,
2123
carries_dependency,
2224
deprecated,
@@ -52,6 +54,8 @@ namespace cppast
5254
case cpp_attribute_kind::unknown:
5355
return "unknown";
5456
}
57+
58+
return "<error>";
5559
}
5660
} // namespace detail
5761

‎include/cppast/cpp_class.hpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,19 @@ namespace cppast
144144
}
145145

146146
/// \effects Builds a [cppast::cpp_base_class]() and adds it.
147-
void base_class(std::string name, std::unique_ptr<cpp_type> type,
148-
cpp_access_specifier_kind access, bool is_virtual)
147+
cpp_base_class& base_class(std::string name, std::unique_ptr<cpp_type> type,
148+
cpp_access_specifier_kind access, bool is_virtual)
149149
{
150-
add_base_class(
150+
return add_base_class(
151151
cpp_base_class::build(std::move(name), std::move(type), access, is_virtual));
152152
}
153153

154154
/// \effects Adds a new base class.
155-
void add_base_class(std::unique_ptr<cpp_base_class> base) noexcept
155+
cpp_base_class& add_base_class(std::unique_ptr<cpp_base_class> base) noexcept
156156
{
157+
auto bptr = base.get();
157158
class_->bases_.push_back(*class_, std::move(base));
159+
return *bptr;
158160
}
159161

160162
/// \effects Builds a [cppast::cpp_access_specifier]() and adds it.

‎include/cppast/cpp_entity.hpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ namespace cppast
136136
attributes_.push_back(std::move(attr));
137137
}
138138

139+
/// \effects Adds multiple arguments for that entity.
140+
void add_attribute(const cpp_attribute_list& list) noexcept
141+
{
142+
attributes_.insert(attributes_.end(), list.begin(), list.end());
143+
}
144+
139145
/// \returns The specified user data.
140146
void* user_data() const noexcept
141147
{

‎src/libclang/class_parser.cpp‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@ using namespace cppast;
1212

1313
namespace
1414
{
15-
cpp_class_kind parse_class_kind(const CXCursor& cur)
15+
cpp_class_kind parse_class_kind(detail::cxtoken_stream& stream)
1616
{
17-
auto kind = clang_getTemplateCursorKind(cur);
17+
auto kind = clang_getTemplateCursorKind(stream.cursor());
1818
if (kind == CXCursor_NoDeclFound)
19-
kind = clang_getCursorKind(cur);
19+
kind = clang_getCursorKind(stream.cursor());
20+
21+
if (detail::skip_if(stream, "template"))
22+
// skip template parameters
23+
detail::skip_brackets(stream);
24+
25+
detail::skip_if(stream, "friend");
26+
2027
switch (kind)
2128
{
2229
case CXCursor_ClassDecl:
30+
detail::skip(stream, "class");
2331
return cpp_class_kind::class_t;
2432
case CXCursor_StructDecl:
33+
detail::skip(stream, "struct");
2534
return cpp_class_kind::struct_t;
2635
case CXCursor_UnionDecl:
36+
detail::skip(stream, "union");
2737
return cpp_class_kind::union_t;
2838
default:
2939
break;
@@ -32,11 +42,18 @@ namespace
3242
return cpp_class_kind::class_t;
3343
}
3444

35-
cpp_class::builder make_class_builder(const CXCursor& cur)
45+
cpp_class::builder make_class_builder(const detail::parse_context& context, const CXCursor& cur)
3646
{
37-
auto kind = parse_class_kind(cur);
38-
auto name = detail::get_cursor_name(cur);
39-
return cpp_class::builder(name.c_str(), kind);
47+
detail::cxtokenizer tokenizer(context.tu, context.file, cur);
48+
detail::cxtoken_stream stream(tokenizer, cur);
49+
50+
auto kind = parse_class_kind(stream);
51+
auto attributes = detail::parse_attributes(stream);
52+
auto name = detail::get_cursor_name(cur);
53+
54+
auto result = cpp_class::builder(name.c_str(), kind);
55+
result.get().add_attribute(attributes);
56+
return result;
4057
}
4158

4259
cpp_access_specifier_kind convert_access(const CXCursor& cur)
@@ -76,15 +93,16 @@ namespace
7693

7794
// [<attribute>] [virtual] [<access>] <name>
7895
// can't use spelling to get the name
79-
detail::skip_attribute(stream);
96+
auto attributes = detail::parse_attributes(stream);
8097
if (is_virtual)
8198
detail::skip(stream, "virtual");
8299
detail::skip_if(stream, to_string(access));
83100

84101
auto name = detail::to_string(stream, stream.end()).as_string();
85102

86-
auto type = detail::parse_type(context, class_cur, clang_getCursorType(cur));
87-
builder.base_class(std::move(name), std::move(type), access, is_virtual);
103+
auto type = detail::parse_type(context, class_cur, clang_getCursorType(cur));
104+
auto& base = builder.base_class(std::move(name), std::move(type), access, is_virtual);
105+
base.add_attribute(attributes);
88106
}
89107
}
90108

@@ -100,7 +118,7 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_class(const detail::parse_context&
100118
auto is_friend = false;
101119
#endif
102120

103-
auto builder = make_class_builder(cur);
121+
auto builder = make_class_builder(context, cur);
104122
type_safe::optional<cpp_entity_ref> semantic_parent;
105123
if (!is_friend)
106124
{

‎src/libclang/cxtokenizer.cpp‎

Lines changed: 130 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,16 @@ namespace
7373
};
7474

7575
bool token_after_is(const CXTranslationUnit& tu, const CXFile& file, const CXCursor& cur,
76-
const CXSourceLocation& loc, const char* token_str)
76+
const CXSourceLocation& loc, const char* token_str, int inc = 1)
7777
{
78-
auto loc_after = get_next_location(tu, file, loc);
78+
auto loc_after = get_next_location(tu, file, loc, inc);
79+
if (!clang_Location_isFromMainFile(loc_after))
80+
return false;
7981

80-
simple_tokenizer tokenizer(tu, clang_getRange(loc, loc_after), cur);
82+
simple_tokenizer tokenizer(tu,
83+
inc > 0 ? clang_getRange(loc, loc_after) :
84+
clang_getRange(loc_after, loc),
85+
cur);
8186
detail::cxstring spelling(clang_getTokenSpelling(tu, tokenizer[0u]));
8287
return spelling == token_str;
8388
}
@@ -96,10 +101,27 @@ namespace
96101
auto end = clang_getRangeEnd(extent);
97102

98103
auto kind = clang_getCursorKind(cur);
104+
if ((cursor_is_function(kind) || cursor_is_function(clang_getTemplateCursorKind(cur))
105+
|| kind == CXCursor_VarDecl || kind == CXCursor_FieldDecl || kind == CXCursor_ParmDecl
106+
|| kind == CXCursor_NonTypeTemplateParameter)
107+
&& token_after_is(tu, file, cur, begin, "]", -2)
108+
&& token_after_is(tu, file, cur, begin, "]", -3))
109+
{
110+
while (!token_after_is(tu, file, cur, begin, "[", -1)
111+
&& !token_after_is(tu, file, cur, begin, "[", -2))
112+
begin = get_next_location(tu, file, begin, -1);
113+
114+
begin = get_next_location(tu, file, begin, -3);
115+
DEBUG_ASSERT(token_after_is(tu, file, cur, begin, "[")
116+
&& token_after_is(tu, file, cur, get_next_location(tu, file, begin),
117+
"["),
118+
detail::parse_error_handler{}, cur,
119+
"error in pre-function attribute parsing");
120+
}
121+
99122
if (cursor_is_function(kind) || cursor_is_function(clang_getTemplateCursorKind(cur)))
100123
{
101124
auto is_definition = false;
102-
103125
// if a function we need to remove the body
104126
// it does not need to be parsed
105127
detail::visit_children(cur, [&](const CXCursor& child) {
@@ -216,6 +238,13 @@ namespace
216238
while (!token_after_is(tu, file, cur, end, ";"))
217239
end = get_next_location(tu, file, end);
218240
}
241+
else if (kind == CXCursor_EnumConstantDecl && !token_after_is(tu, file, cur, end, ",")
242+
&& !token_after_is(tu, file, cur, end, ";"))
243+
{
244+
while (!token_after_is(tu, file, cur, end, ",")
245+
&& !token_after_is(tu, file, cur, end, ";"))
246+
end = get_next_location(tu, file, end);
247+
}
219248
else if (kind == CXCursor_FieldDecl || kind == CXCursor_ParmDecl
220249
|| kind == CXCursor_NonTypeTemplateParameter
221250
|| kind == CXCursor_TemplateTemplateParameter
@@ -368,14 +397,103 @@ void detail::skip_brackets(detail::cxtoken_stream& stream)
368397

369398
namespace
370399
{
371-
bool skip_attribute_impl(detail::cxtoken_stream& stream)
400+
type_safe::optional<std::string> parse_attribute_using(detail::cxtoken_stream& stream)
401+
{
402+
// using identifier :
403+
if (skip_if(stream, "using"))
404+
{
405+
DEBUG_ASSERT(stream.peek().kind() == CXToken_Identifier, detail::parse_error_handler{},
406+
stream.cursor(), "expected identifier");
407+
auto scope = stream.get().value().std_str();
408+
skip(stream, ":");
409+
410+
return scope;
411+
}
412+
else
413+
return type_safe::nullopt;
414+
}
415+
416+
cpp_attribute_kind get_attribute_kind(const std::string& name)
417+
{
418+
if (name == "carries_dependency")
419+
return cpp_attribute_kind::carries_dependency;
420+
else if (name == "deprecated")
421+
return cpp_attribute_kind::deprecated;
422+
else if (name == "fallthrough")
423+
return cpp_attribute_kind::fallthrough;
424+
else if (name == "maybe_unused")
425+
return cpp_attribute_kind::maybe_unused;
426+
else if (name == "nodiscard")
427+
return cpp_attribute_kind::nodiscard;
428+
else if (name == "noreturn")
429+
return cpp_attribute_kind::noreturn;
430+
else
431+
return cpp_attribute_kind::unknown;
432+
}
433+
434+
cpp_attribute parse_attribute_token(detail::cxtoken_stream& stream,
435+
type_safe::optional<std::string> scope)
436+
{
437+
// (identifier ::)_opt identifier ( '(' some tokens ')' )_opt ..._opt
438+
439+
// parse name
440+
DEBUG_ASSERT(stream.peek().kind() == CXToken_Identifier, detail::parse_error_handler{},
441+
stream.cursor(), "expected identifier");
442+
auto name = stream.get().value().std_str();
443+
if (skip_if(stream, "::"))
444+
{
445+
// name was actually a scope, so parse name again
446+
DEBUG_ASSERT(!scope, detail::parse_error_handler{}, stream.cursor(),
447+
"attribute using + scope not allowed");
448+
scope = std::move(name);
449+
450+
DEBUG_ASSERT(stream.peek().kind() == CXToken_Identifier, detail::parse_error_handler{},
451+
stream.cursor(), "expected identifier");
452+
name = stream.get().value().std_str();
453+
}
454+
455+
// parse arguments
456+
type_safe::optional<cpp_token_string> arguments;
457+
if (stream.peek() == "(")
458+
{
459+
auto end = find_closing_bracket(stream);
460+
skip(stream, "(");
461+
462+
arguments = detail::to_string(stream, end);
463+
464+
stream.set_cur(end);
465+
skip(stream, ")");
466+
}
467+
468+
// parse variadic token
469+
auto is_variadic = skip_if(stream, "...");
470+
471+
// get kind
472+
auto kind = get_attribute_kind(name);
473+
if (!scope && kind != cpp_attribute_kind::unknown)
474+
return cpp_attribute(kind, std::move(arguments));
475+
else
476+
return cpp_attribute(std::move(scope), std::move(name), std::move(arguments),
477+
is_variadic);
478+
}
479+
480+
bool parse_attribute_impl(cpp_attribute_list& result, detail::cxtoken_stream& stream)
372481
{
373482
if (skip_if(stream, "[") && stream.peek() == "[")
374483
{
375484
// C++11 attribute
376485
// [[<attribute>]]
377486
// ^
378-
skip_brackets(stream);
487+
skip(stream, "[");
488+
489+
auto scope = parse_attribute_using(stream);
490+
while (!skip_if(stream, "]"))
491+
{
492+
auto attribute = parse_attribute_token(stream, scope);
493+
result.push_back(std::move(attribute));
494+
detail::skip_if(stream, ",");
495+
}
496+
379497
// [[<attribute>]]
380498
// ^
381499
skip(stream, "]");
@@ -402,12 +520,13 @@ namespace
402520
}
403521
}
404522

405-
bool detail::skip_attribute(detail::cxtoken_stream& stream)
523+
cpp_attribute_list detail::parse_attributes(detail::cxtoken_stream& stream)
406524
{
407-
auto any = false;
408-
while (skip_attribute_impl(stream))
409-
any = true;
410-
return any;
525+
cpp_attribute_list result;
526+
while (parse_attribute_impl(result, stream))
527+
{
528+
}
529+
return result;
411530
}
412531

413532
namespace

‎src/libclang/cxtokenizer.hpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string>
99
#include <vector>
1010

11+
#include <cppast/cpp_attribute.hpp>
1112
#include <cppast/cpp_token.hpp>
1213

1314
#include "raii_wrapper.hpp"
@@ -189,8 +190,8 @@ namespace cppast
189190
// note: < might not work in the arguments of a template specialization
190191
void skip_brackets(cxtoken_stream& stream);
191192

192-
// skips an attribute
193-
bool skip_attribute(cxtoken_stream& stream);
193+
// parses attributes
194+
cpp_attribute_list parse_attributes(cxtoken_stream& stream);
194195

195196
// converts a token range to a string
196197
cpp_token_string to_string(cxtoken_stream& stream, cxtoken_iterator end);

‎src/libclang/enum_parser.cpp‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ namespace
2525

2626
// <identifier> [<attribute>],
2727
// or: <identifier> [<attribute>] = <expression>,
28-
auto& name = stream.get().value();
29-
detail::skip_attribute(stream);
28+
auto& name = stream.get().value();
29+
auto attributes = detail::parse_attributes(stream);
3030

3131
std::unique_ptr<cpp_expression> value;
3232
if (detail::skip_if(stream, "="))
@@ -40,8 +40,10 @@ namespace
4040
});
4141
}
4242

43-
return cpp_enum_value::build(*context.idx, detail::get_entity_id(cur), name.c_str(),
44-
std::move(value));
43+
auto result = cpp_enum_value::build(*context.idx, detail::get_entity_id(cur), name.c_str(),
44+
std::move(value));
45+
result->add_attribute(attributes);
46+
return result;
4547
}
4648

4749
cpp_enum::builder make_enum_builder(const detail::parse_context& context, const CXCursor& cur,
@@ -51,11 +53,11 @@ namespace
5153
detail::cxtokenizer tokenizer(context.tu, context.file, cur);
5254
detail::cxtoken_stream stream(tokenizer, cur);
5355

54-
// [<attribute>] enum [class] [<attribute>] name [: type] {
55-
detail::skip_attribute(stream);
56+
// enum [class/struct] [<attribute>] name [: type] {
5657
detail::skip(stream, "enum");
57-
auto scoped = detail::skip_if(stream, "class");
58-
detail::skip_attribute(stream);
58+
auto scoped = detail::skip_if(stream, "class") || detail::skip_if(stream, "struct");
59+
auto attributes = detail::parse_attributes(stream);
60+
5961
std::string scope;
6062
while (!detail::skip_if(stream, name.c_str()))
6163
if (!detail::append_scope(stream, scope))
@@ -70,7 +72,9 @@ namespace
7072
auto type = detail::parse_type(context, cur, clang_getEnumDeclIntegerType(cur));
7173
auto type_given = detail::skip_if(stream, ":");
7274

73-
return cpp_enum::builder(name.c_str(), scoped, std::move(type), type_given);
75+
auto result = cpp_enum::builder(name.c_str(), scoped, std::move(type), type_given);
76+
result.get().add_attribute(attributes);
77+
return result;
7478
}
7579
}
7680

0 commit comments

Comments
 (0)