Skip to content

Commit 53b7767

Browse files
committed
[WIP] add AttributeKind
1 parent 2d5e6a1 commit 53b7767

File tree

13 files changed

+129
-80
lines changed

13 files changed

+129
-80
lines changed

docs/modules/ROOT/pages/generators.adoc

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ The `Symbol` object represents a symbol extracted from the source code.The symbo
189189
| `Any`
190190
| The documentation for the symbol.
191191

192+
| `attributes`
193+
| `string[]`
194+
| The attributes of the symbol.
195+
192196
|===
193197

194198
Handlebars generators extend each symbol with the following fields:
@@ -326,10 +330,6 @@ When the symbol kind is `function`, the symbol object has the following addition
326330
| `bool`
327331
| Whether the function is deleted as written.
328332

329-
| `isNoReturn`
330-
| `bool`
331-
| Whether the function is noreturn.
332-
333333
| `isOverride`
334334
| `bool`
335335
| Whether the function is override.
@@ -350,10 +350,6 @@ When the symbol kind is `function`, the symbol object has the following addition
350350
| `bool`
351351
| Whether the function is final.
352352

353-
| `isNodiscard`
354-
| `bool`
355-
| Whether the function is nodiscard.
356-
357353
| `isExplicitObjectMemberFunction`
358354
| `bool`
359355
| Whether the function is an explicit object member function.
@@ -401,10 +397,6 @@ When the symbol kind is `function`, the symbol object has the following addition
401397
| `requires`
402398
| `string`
403399
| The `requires` expression of the function.
404-
405-
| `attributes`
406-
| `string[]`
407-
| The attributes of the function.
408400
|===
409401

410402
When the symbol kind is `typedef`, the symbol object has the following additional properties:
@@ -461,10 +453,6 @@ When the symbol kind is `variable`, the symbol object has the following addition
461453
| `initializer`
462454
| `string`
463455
| The initializer of the variable.
464-
465-
| `attributes`
466-
| `string[]`
467-
| The attributes of the variable.
468456
|===
469457

470458
When the symbol kind is `field` (i.e. non-static data members), the symbol object has the following additional properties:
@@ -480,14 +468,6 @@ When the symbol kind is `field` (i.e. non-static data members), the symbol objec
480468
| `string`
481469
| The default value of the field.
482470

483-
| `isMaybeUnused`
484-
| `bool`
485-
| Whether the field is maybe unused.
486-
487-
| `isDeprecated`
488-
| `bool`
489-
| Whether the field is deprecated.
490-
491471
| `isVariant`
492472
| `bool`
493473
| Whether the field is a variant.
@@ -500,17 +480,9 @@ When the symbol kind is `field` (i.e. non-static data members), the symbol objec
500480
| `bool`
501481
| Whether the field is a bitfield.
502482

503-
| `hasNoUniqueAddress`
504-
| `string`
505-
| Whether the field has the `[[no_unique_address]]` attribute.
506-
507483
| `bitfieldWidth`
508484
| `string`
509485
| The width of the bitfield.
510-
511-
| `attributes`
512-
| `string[]`
513-
| The attributes of the field.
514486
|===
515487

516488
When the symbol kind is `friend`, the symbol object has the following additional properties:

include/mrdocs/Metadata/Info.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <mrdocs/Platform.hpp>
1616
#include <mrdocs/Dom.hpp>
17+
#include <mrdocs/Dom/LazyArray.hpp>
1718
#include <mrdocs/Dom/LazyObject.hpp>
1819
#include <mrdocs/Metadata/ExtractionMode.hpp>
1920
#include <mrdocs/Metadata/Javadoc.hpp>
@@ -112,6 +113,10 @@ struct MRDOCS_VISIBLE Info
112113
*/
113114
std::optional<Javadoc> javadoc;
114115

116+
/** The attributes appertaining to this declaration.
117+
*/
118+
std::vector<AttributeKind> Attributes;
119+
115120
//--------------------------------------------
116121

117122
~Info() override = default;
@@ -298,6 +303,8 @@ tag_invoke(
298303
{
299304
io.map("doc", *I.javadoc);
300305
}
306+
307+
io.map("attributes", dom::LazyArray(I.Attributes));
301308
io.map("loc", dynamic_cast<SourceInfo const&>(I));
302309
}
303310

include/mrdocs/Metadata/Info/Field.hpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ struct FieldInfo final
4848
/** The width of the bitfield */
4949
ConstantExprInfo<std::uint64_t> BitfieldWidth;
5050

51-
// KRYSTIAN FIXME: nodiscard cannot be applied to fields; this should
52-
// instead be IsMaybeUnused. we should also store the spelling
53-
bool IsMaybeUnused = false;
54-
55-
bool IsDeprecated = false;
56-
57-
bool HasNoUniqueAddress = false;
58-
59-
std::vector<std::string> Attributes;
60-
6151
//--------------------------------------------
6252

6353
explicit FieldInfo(SymbolID ID) noexcept
@@ -86,17 +76,13 @@ tag_invoke(
8676
{
8777
io.map("default", I.Default.Written);
8878
}
89-
io.map("isMaybeUnused", I.IsMaybeUnused);
90-
io.map("isDeprecated", I.IsDeprecated);
9179
io.map("isVariant", I.IsVariant);
9280
io.map("isMutable", I.IsMutable);
9381
io.map("isBitfield", I.IsBitfield);
94-
io.map("hasNoUniqueAddress", I.HasNoUniqueAddress);
9582
if (I.IsBitfield)
9683
{
9784
io.map("bitfieldWidth", I.BitfieldWidth.Written);
9885
}
99-
io.map("attributes", dom::LazyArray(I.Attributes));
10086
}
10187

10288
/** Map the FieldInfo to a @ref dom::Value object.

include/mrdocs/Metadata/Info/Function.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ struct FunctionInfo final
160160
bool IsExplicitlyDefaulted = false;
161161
bool IsDeleted = false;
162162
bool IsDeletedAsWritten = false;
163-
bool IsNoReturn = false;
164-
bool IsOverride = false;
165163
bool HasTrailingReturn = false;
166-
bool IsNodiscard = false;
167164
bool IsExplicitObjectMemberFunction = false;
168165
ConstexprKind Constexpr = ConstexprKind::None;
169166
OperatorKind OverloadedOperator = OperatorKind::None;
@@ -178,6 +175,7 @@ struct FunctionInfo final
178175
bool IsConst = false;
179176
bool IsVolatile = false;
180177
bool IsFinal = false;
178+
bool IsOverride = false;
181179
ReferenceKind RefQualifier = ReferenceKind::None;
182180
ExplicitInfo Explicit;
183181

@@ -215,13 +213,11 @@ tag_invoke(
215213
io.map("isExplicitlyDefaulted", I.IsExplicitlyDefaulted);
216214
io.map("isDeleted", I.IsDeleted);
217215
io.map("isDeletedAsWritten", I.IsDeletedAsWritten);
218-
io.map("isNoReturn", I.IsNoReturn);
219-
io.map("isOverride", I.IsOverride);
220216
io.map("hasTrailingReturn", I.HasTrailingReturn);
221217
io.map("isConst", I.IsConst);
222218
io.map("isVolatile", I.IsVolatile);
223219
io.map("isFinal", I.IsFinal);
224-
io.map("isNodiscard", I.IsNodiscard);
220+
io.map("isOverride", I.IsOverride);
225221
io.map("isExplicitObjectMemberFunction", I.IsExplicitObjectMemberFunction);
226222
if (I.Constexpr != ConstexprKind::None)
227223
{
@@ -246,7 +242,6 @@ tag_invoke(
246242
{
247243
io.map("requires", I.Requires.Written);
248244
}
249-
io.map("attributes", dom::LazyArray(I.Attributes));
250245
}
251246

252247
/** Map the FunctionInfo to a @ref dom::Value object.

include/mrdocs/Metadata/Info/Variable.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <mrdocs/Metadata/Template.hpp>
1818
#include <mrdocs/Metadata/Info.hpp>
1919
#include <mrdocs/Metadata/Type.hpp>
20-
#include <mrdocs/Dom/LazyArray.hpp>
2120
#include <mrdocs/ADT/Polymorphic.hpp>
2221

2322
namespace clang::mrdocs {
@@ -89,7 +88,6 @@ tag_invoke(
8988
{
9089
io.map("initializer", I.Initializer.Written);
9190
}
92-
io.map("attributes", dom::LazyArray(I.Attributes));
9391
}
9492

9593
/** Map the VariableInfo to a @ref dom::Value object.

include/mrdocs/Metadata/Specifiers.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ enum class AccessKind
3636
Private,
3737
};
3838

39+
/** Attribute kinds.
40+
*/
41+
enum class AttributeKind
42+
{
43+
Deprecated,
44+
MaybeUnused,
45+
Nodiscard,
46+
Noreturn,
47+
NoUniqueAddress
48+
};
49+
3950
/** `constexpr`/`consteval` specifier kinds
4051
4152
[dcl.spec.general] p2: At most one of the `constexpr`, `consteval`,
@@ -209,6 +220,7 @@ enum class StorageClassKind
209220
};
210221

211222
MRDOCS_DECL dom::String toString(AccessKind kind) noexcept;
223+
MRDOCS_DECL dom::String toString(AttributeKind kind) noexcept;
212224
MRDOCS_DECL dom::String toString(ConstexprKind kind) noexcept;
213225
MRDOCS_DECL dom::String toString(ExplicitKind kind) noexcept;
214226
MRDOCS_DECL dom::String toString(NoexceptKind kind) noexcept;
@@ -278,6 +290,18 @@ tag_invoke(
278290
v = toString(kind);
279291
}
280292

293+
/** Return the AttributeKind as a @ref dom::Value string.
294+
*/
295+
inline
296+
void
297+
tag_invoke(
298+
dom::ValueFromTag,
299+
dom::Value& v,
300+
AttributeKind kind)
301+
{
302+
v = toString(kind);
303+
}
304+
281305
/** Return the ConstexprKind as a @ref dom::Value string.
282306
*/
283307
inline

src/lib/AST/ASTVisitor.cpp

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ populate(Info& I, bool const isNew, DeclTy const* D)
522522
{
523523
populate(I.javadoc, D);
524524
populate(dynamic_cast<SourceInfo&>(I), D);
525+
populateAttributes(I, D);
525526

526527
// All other information is redundant if the symbol is not new
527528
MRDOCS_CHECK_OR(isNew);
@@ -767,7 +768,6 @@ populate(
767768
I.IsExplicitlyDefaulted |= D->isExplicitlyDefaulted();
768769
I.IsDeleted |= D->isDeleted();
769770
I.IsDeletedAsWritten |= D->isDeletedAsWritten();
770-
I.IsNoReturn |= D->isNoReturn();
771771

772772
if (ConstexprSpecKind const CSK = D->getConstexprKind();
773773
CSK != ConstexprSpecKind::Unspecified)
@@ -780,7 +780,6 @@ populate(
780780
I.StorageClass = toStorageClassKind(SC);
781781
}
782782

783-
I.IsNodiscard |= D->hasAttr<WarnUnusedResultAttr>();
784783
I.IsExplicitObjectMemberFunction |= D->hasCXXExplicitFunctionObjectParameter();
785784

786785
ArrayRef<ParmVarDecl*> const params = D->parameters();
@@ -867,8 +866,6 @@ populate(
867866
}
868867
}
869868
}
870-
871-
populateAttributes(I, D);
872869
}
873870

874871
void
@@ -1024,7 +1021,6 @@ populate(
10241021
QT.removeLocalConst();
10251022
}
10261023
I.Type = toTypeInfo(QT);
1027-
populateAttributes(I, D);
10281024
}
10291025

10301026
void
@@ -1068,10 +1064,6 @@ populate(
10681064
I.IsBitfield = true;
10691065
populate(I.BitfieldWidth, D->getBitWidth());
10701066
}
1071-
I.HasNoUniqueAddress = D->hasAttr<NoUniqueAddressAttr>();
1072-
I.IsDeprecated = D->hasAttr<DeprecatedAttr>();
1073-
I.IsMaybeUnused = D->hasAttr<UnusedAttr>();
1074-
populateAttributes(I, D);
10751067
}
10761068

10771069
void
@@ -1568,11 +1560,53 @@ populate(
15681560
}));
15691561
}
15701562

1571-
template <std::derived_from<Info> InfoTy>
15721563
void
15731564
ASTVisitor::
1574-
populateAttributes(InfoTy& I, Decl const* D)
1575-
{
1565+
populateAttributes(Info& I, Decl const* D)
1566+
{
1567+
for(const Attr* AT : D->getAttrs())
1568+
{
1569+
switch(AT->getKind())
1570+
{
1571+
case attr::Kind::Deprecated:
1572+
addAttribute(I, AttributeKind::Deprecated);
1573+
break;
1574+
case attr::Kind::Unused:
1575+
addAttribute(I, AttributeKind::MaybeUnused);
1576+
break;
1577+
case attr::Kind::WarnUnusedResult:
1578+
addAttribute(I, AttributeKind::Nodiscard);
1579+
break;
1580+
case attr::Kind::NoReturn:
1581+
addAttribute(I, AttributeKind::Noreturn);
1582+
break;
1583+
case attr::Kind::NoUniqueAddress:
1584+
addAttribute(I, AttributeKind::NoUniqueAddress);
1585+
break;
1586+
default:
1587+
// KRYSTIAN TODO: handle more attributes types
1588+
break;
1589+
}
1590+
}
1591+
1592+
#if 0
1593+
if(D->hasAttr<DeprecatedAttr>())
1594+
addAttribute(I, AttributeKind::Deprecated);
1595+
1596+
if(D->hasAttr<UnusedAttr>())
1597+
addAttribute(I, AttributeKind::MaybeUnused);
1598+
1599+
if(D->hasAttr<WarnUnusedResultAttr>())
1600+
addAttribute(I, AttributeKind::Nodiscard);
1601+
1602+
if(D->hasAttr<NoReturnAttr>())
1603+
addAttribute(I, AttributeKind::Noreturn);
1604+
1605+
if(D->hasAttr<NoUniqueAddressAttr>())
1606+
addAttribute(I, AttributeKind::NoUniqueAddress);
1607+
#endif
1608+
1609+
#if 0
15761610
if constexpr (requires { I.Attributes; })
15771611
{
15781612
MRDOCS_CHECK_OR(D->hasAttrs());
@@ -1589,6 +1623,18 @@ populateAttributes(InfoTy& I, Decl const* D)
15891623
}
15901624
}
15911625
}
1626+
#endif
1627+
}
1628+
1629+
void
1630+
ASTVisitor::
1631+
addAttribute(
1632+
Info& I,
1633+
AttributeKind kind)
1634+
{
1635+
if(std::ranges::find(I.Attributes, kind) != I.Attributes.end())
1636+
return;
1637+
I.Attributes.emplace_back(kind);
15921638
}
15931639

15941640
void

src/lib/AST/ASTVisitor.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,11 @@ class ASTVisitor
698698
std::vector<Polymorphic<TArg>>& result,
699699
ASTTemplateArgumentListInfo const* args);
700700

701-
template <std::derived_from<Info> InfoTy>
702-
static
703701
void
704-
populateAttributes(InfoTy& I, Decl const* D);
702+
populateAttributes(Info& I, Decl const* D);
703+
704+
void
705+
addAttribute(Info& I, AttributeKind kind);
705706

706707
// =================================================
707708
// Populate function helpers

0 commit comments

Comments
 (0)