@@ -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
369398namespace
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
413532namespace
0 commit comments