Skip to content

Commit 948e269

Browse files
committed
refactor(lib): Info contains SourceInfo as composition
Info objects contain Source information rather than _being_ source information.
1 parent bd3e121 commit 948e269

File tree

13 files changed

+204
-155
lines changed

13 files changed

+204
-155
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// This is a derivative work. originally part of the LLVM Project.
3+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
// Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
8+
// Copyright (c) 2023 Krystian Stasiowski (sdkrystian@gmail.com)
9+
//
10+
// Official repository: https://github.com/cppalliance/mrdocs
11+
//
12+
13+
#ifndef MRDOCS_API_METADATA_INFO_FILEKIND_HPP
14+
#define MRDOCS_API_METADATA_INFO_FILEKIND_HPP
15+
16+
#include <mrdocs/Platform.hpp>
17+
#include <mrdocs/ADT/Nullable.hpp>
18+
#include <mrdocs/Dom.hpp>
19+
#include <string>
20+
21+
namespace clang::mrdocs {
22+
23+
enum class FileKind
24+
{
25+
/// File in the source directory
26+
Source,
27+
/// File in a system include directory
28+
System,
29+
/// File outside the source directory
30+
Other
31+
};
32+
33+
MRDOCS_DECL
34+
std::string_view
35+
toString(FileKind kind);
36+
37+
inline
38+
void
39+
tag_invoke(
40+
dom::ValueFromTag,
41+
dom::Value& v,
42+
FileKind kind)
43+
{
44+
v = toString(kind);
45+
}
46+
47+
} // clang::mrdocs
48+
49+
#endif // MRDOCS_API_METADATA_INFO_FILEKIND_HPP

include/mrdocs/Metadata/Info/InfoBase.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ namespace clang::mrdocs {
3030
/** Base class with common properties of all symbols
3131
*/
3232
struct MRDOCS_VISIBLE Info
33-
: SourceInfo
3433
{
34+
/** The source location information.
35+
*/
36+
SourceInfo Loc;
37+
3538
/** The unique identifier for this symbol.
3639
*/
3740
SymbolID id;
@@ -82,7 +85,7 @@ struct MRDOCS_VISIBLE Info
8285

8386
//--------------------------------------------
8487

85-
~Info() override = default;
88+
virtual ~Info() = default;
8689

8790
#define INFO(Type) constexpr bool is##Type() const noexcept { \
8891
return Kind == InfoKind::Type; \
@@ -246,7 +249,7 @@ tag_invoke(
246249
{
247250
io.map("doc", *I.javadoc);
248251
}
249-
io.map("loc", dynamic_cast<SourceInfo const&>(I));
252+
io.map("loc", I.Loc);
250253
}
251254

252255
/** Return the Info as a @ref dom::Value object.
@@ -267,7 +270,7 @@ Optional<Location>
267270
getPrimaryLocation(Info const& I)
268271
{
269272
return getPrimaryLocation(
270-
dynamic_cast<SourceInfo const&>(I),
273+
I.Loc,
271274
I.isRecord() || I.isEnum());
272275
}
273276

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//
2+
// This is a derivative work. originally part of the LLVM Project.
3+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
// Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
8+
// Copyright (c) 2023 Krystian Stasiowski (sdkrystian@gmail.com)
9+
//
10+
// Official repository: https://github.com/cppalliance/mrdocs
11+
//
12+
13+
#ifndef MRDOCS_API_METADATA_INFO_LOCATION_HPP
14+
#define MRDOCS_API_METADATA_INFO_LOCATION_HPP
15+
16+
#include <mrdocs/Platform.hpp>
17+
#include <mrdocs/ADT/Nullable.hpp>
18+
#include <mrdocs/Dom.hpp>
19+
#include <string>
20+
21+
namespace clang::mrdocs {
22+
23+
struct MRDOCS_DECL
24+
Location
25+
{
26+
/** The full file path
27+
*/
28+
std::string FullPath;
29+
30+
/** The file path relative to one of the search directories
31+
*/
32+
std::string ShortPath;
33+
34+
/** The file path relative to the source-root directory
35+
*/
36+
std::string SourcePath;
37+
38+
/** Line number within the file
39+
*/
40+
unsigned LineNumber = 0;
41+
42+
/** Whether this location has documentation.
43+
*/
44+
bool Documented = false;
45+
46+
//--------------------------------------------
47+
48+
constexpr
49+
Location(
50+
std::string_view const full_path = {},
51+
std::string_view const short_path = {},
52+
std::string_view const source_path = {},
53+
unsigned const line = 0,
54+
bool const documented = false)
55+
: FullPath(full_path)
56+
, ShortPath(short_path)
57+
, SourcePath(source_path)
58+
, LineNumber(line)
59+
, Documented(documented)
60+
{
61+
}
62+
63+
auto operator<=>(Location const&) const = default;
64+
};
65+
66+
MRDOCS_DECL
67+
void
68+
tag_invoke(
69+
dom::ValueFromTag,
70+
dom::Value& v,
71+
Location const& loc);
72+
73+
/** nullable_traits specialization for Location.
74+
75+
Semantics
76+
- The “null” (sentinel) state is any Location whose ShortPath is empty.
77+
- Creating a null value produces a Location with all fields defaulted
78+
and ShortPath empty.
79+
- Making an existing value null clears ShortPath and resets the other
80+
fields to their defaults.
81+
82+
Rationale
83+
- This mirrors the old LocationEmptyPredicate, which treated an empty
84+
ShortPath as “empty/null.”
85+
**/
86+
template<>
87+
struct nullable_traits<Location>
88+
{
89+
static constexpr bool
90+
is_null(Location const& v) noexcept
91+
{
92+
return v.ShortPath.empty();
93+
}
94+
95+
static constexpr Location
96+
null() noexcept
97+
{
98+
return Location{
99+
/*full_path*/ {},
100+
/*short_path*/ {},
101+
/*source_path*/ {},
102+
/*line*/ 0u,
103+
/*documented*/ false
104+
};
105+
}
106+
107+
static constexpr void
108+
make_null(Location& v) noexcept
109+
{
110+
v.FullPath.clear();
111+
v.ShortPath.clear(); // sentinel condition
112+
v.SourcePath.clear();
113+
v.LineNumber = 0;
114+
v.Documented = false;
115+
}
116+
};
117+
118+
} // clang::mrdocs
119+
120+
#endif // MRDOCS_API_METADATA_INFO_LOCATION_HPP

include/mrdocs/Metadata/Info/Source.hpp

Lines changed: 3 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -14,136 +14,20 @@
1414
#define MRDOCS_API_METADATA_INFO_SOURCE_HPP
1515

1616
#include <mrdocs/Platform.hpp>
17+
#include <mrdocs/Metadata/Info/Location.hpp>
1718
#include <mrdocs/ADT/Optional.hpp>
1819
#include <mrdocs/Dom.hpp>
1920
#include <string>
2021

2122
namespace clang::mrdocs {
2223

23-
enum class FileKind
24-
{
25-
/// File in the source directory
26-
Source,
27-
/// File in a system include directory
28-
System,
29-
/// File outside the source directory
30-
Other
31-
};
32-
33-
MRDOCS_DECL
34-
std::string_view
35-
toString(FileKind kind);
36-
37-
inline
38-
void
39-
tag_invoke(
40-
dom::ValueFromTag,
41-
dom::Value& v,
42-
FileKind kind)
43-
{
44-
v = toString(kind);
45-
}
46-
47-
struct MRDOCS_DECL
48-
Location
49-
{
50-
/** The full file path
51-
*/
52-
std::string FullPath;
53-
54-
/** The file path relative to one of the search directories
55-
*/
56-
std::string ShortPath;
57-
58-
/** The file path relative to the source-root directory
59-
*/
60-
std::string SourcePath;
61-
62-
/** Line number within the file
63-
*/
64-
unsigned LineNumber = 0;
65-
66-
/** Whether this location has documentation.
67-
*/
68-
bool Documented = false;
69-
70-
//--------------------------------------------
71-
72-
constexpr
73-
Location(
74-
std::string_view const full_path = {},
75-
std::string_view const short_path = {},
76-
std::string_view const source_path = {},
77-
unsigned const line = 0,
78-
bool const documented = false)
79-
: FullPath(full_path)
80-
, ShortPath(short_path)
81-
, SourcePath(source_path)
82-
, LineNumber(line)
83-
, Documented(documented)
84-
{
85-
}
86-
87-
auto operator<=>(Location const&) const = default;
88-
};
89-
90-
MRDOCS_DECL
91-
void
92-
tag_invoke(
93-
dom::ValueFromTag,
94-
dom::Value& v,
95-
Location const& loc);
96-
97-
/** nullable_traits specialization for Location.
98-
99-
Semantics
100-
- The “null” (sentinel) state is any Location whose ShortPath is empty.
101-
- Creating a null value produces a Location with all fields defaulted
102-
and ShortPath empty.
103-
- Making an existing value null clears ShortPath and resets the other
104-
fields to their defaults.
105-
106-
Rationale
107-
- This mirrors the old LocationEmptyPredicate, which treated an empty
108-
ShortPath as “empty/null.”
109-
**/
110-
template<>
111-
struct nullable_traits<Location>
112-
{
113-
static constexpr bool
114-
is_null(Location const& v) noexcept
115-
{
116-
return v.ShortPath.empty();
117-
}
118-
119-
static constexpr Location
120-
null() noexcept
121-
{
122-
return Location{
123-
/*full_path*/ {},
124-
/*short_path*/ {},
125-
/*source_path*/ {},
126-
/*line*/ 0u,
127-
/*documented*/ false
128-
};
129-
}
130-
131-
static constexpr void
132-
make_null(Location& v) noexcept
133-
{
134-
v.FullPath.clear();
135-
v.ShortPath.clear(); // sentinel condition
136-
v.SourcePath.clear();
137-
v.LineNumber = 0;
138-
v.Documented = false;
139-
}
140-
};
141-
14224
/** Stores source information for a declaration.
14325
*/
14426
struct MRDOCS_DECL
14527
SourceInfo
14628
{
29+
constexpr SourceInfo() = default;
30+
14731
/** Location where the entity was defined
14832
14933
KRYSTIAN NOTE: this is used for entities which cannot be
@@ -159,22 +43,9 @@ struct MRDOCS_DECL
15943
*/
16044
std::vector<Location> Loc;
16145

162-
constexpr SourceInfo const& asSourceInfo() const noexcept
163-
{
164-
return *this;
165-
}
166-
167-
constexpr SourceInfo& asSourceInfo() noexcept
168-
{
169-
return *this;
170-
}
171-
17246
constexpr virtual ~SourceInfo() = default;
17347

17448
auto operator<=>(SourceInfo const&) const = default;
175-
176-
protected:
177-
constexpr SourceInfo() = default;
17849
};
17950

18051
MRDOCS_DECL

0 commit comments

Comments
 (0)