Skip to content

Commit 040b99d

Browse files
committed
Corpus lookup returns references
#improvement
1 parent 37786fd commit 040b99d

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

include/mrdocs/Corpus.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ class MRDOCS_VISIBLE
9090
/** Return the Info for the matching string in a given context.
9191
*/
9292
virtual
93-
Expected<Info const*>
93+
Expected<std::reference_wrapper<Info const>>
9494
lookup(SymbolID const& context, std::string_view name) const = 0;
9595

9696
/** Return the Info for the matching string in the global context.
9797
*/
98-
Expected<Info const*>
98+
Expected<std::reference_wrapper<Info const>>
9999
lookup(std::string_view name) const
100100
{
101101
return lookup(SymbolID::global, name);

src/lib/Lib/CorpusImpl.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -221,22 +221,22 @@ isTransparent(Info const& info)
221221
}
222222
}
223223

224-
Expected<Info const*>
224+
Expected<std::reference_wrapper<Info const>>
225225
CorpusImpl::
226226
lookup(SymbolID const& context, std::string_view const name) const
227227
{
228228
return lookupImpl(*this, context, name);
229229
}
230230

231-
Expected<Info const*>
231+
Expected<std::reference_wrapper<Info const>>
232232
CorpusImpl::
233233
lookup(SymbolID const& context, std::string_view name)
234234
{
235235
return lookupImpl(*this, context, name);
236236
}
237237

238238
template <class Self>
239-
Expected<Info const*>
239+
Expected<std::reference_wrapper<Info const>>
240240
CorpusImpl::
241241
lookupImpl(Self&& self, SymbolID const& context, std::string_view name)
242242
{
@@ -253,7 +253,7 @@ lookupImpl(Self&& self, SymbolID const& context, std::string_view name)
253253
name,
254254
self.Corpus::qualifiedName(*self.find(context))));
255255
}
256-
return info;
256+
return std::cref(*info);
257257
}
258258
Expected<ParsedRef> const s = parseRef(name);
259259
if (!s)
@@ -273,7 +273,7 @@ lookupImpl(Self&& self, SymbolID const& context, std::string_view name)
273273
name,
274274
self.Corpus::qualifiedName(*contextPtr)));
275275
}
276-
return res;
276+
return std::cref(*res);
277277
}
278278

279279
template <class Self>

src/lib/Lib/CorpusImpl.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <mutex>
2727
#include <string>
2828
#include <set>
29+
#include <functional>
2930

3031
namespace clang::mrdocs {
3132

@@ -91,10 +92,10 @@ class CorpusImpl final : public Corpus
9192
Info const*
9293
find(SymbolID const& id) const noexcept override;
9394

94-
Expected<Info const*>
95+
Expected<std::reference_wrapper<Info const>>
9596
lookup(SymbolID const& context, std::string_view name) const override;
9697

97-
Expected<Info const*>
98+
Expected<std::reference_wrapper<Info const>>
9899
lookup(SymbolID const& context, std::string_view name);
99100

100101
/** Build metadata for a set of translation units.
@@ -141,7 +142,7 @@ class CorpusImpl final : public Corpus
141142

142143
template <class Self>
143144
static
144-
Expected<Info const*>
145+
Expected<std::reference_wrapper<Info const>>
145146
lookupImpl(
146147
Self&& self,
147148
SymbolID const& context,

src/lib/Metadata/Finalizers/JavadocFinalizer.cpp

+14-17
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,10 @@ void
112112
JavadocFinalizer::
113113
finalize(doc::Reference& ref, bool const emitWarning)
114114
{
115-
if (Expected<Info const*> res
116-
= corpus_.lookup(current_context_->id, ref.string))
115+
if (auto resRef = corpus_.lookup(current_context_->id, ref.string))
117116
{
118-
Info const* resPtr = *res;
119-
MRDOCS_ASSERT(resPtr);
120-
ref.id = resPtr->id;
117+
Info const& res = *resRef;
118+
ref.id = res.id;
121119
}
122120
else if (
123121
emitWarning &&
@@ -136,7 +134,7 @@ finalize(doc::Reference& ref, bool const emitWarning)
136134
" {}",
137135
corpus_.Corpus::qualifiedName(*current_context_),
138136
ref.string,
139-
res.error().reason());
137+
resRef.error().reason());
140138
refWarned_.insert({ref.string, current_context_->Name});
141139
}
142140
}
@@ -280,8 +278,8 @@ copyBriefAndDetails(Javadoc& javadoc)
280278
}
281279

282280
// Find the node to copy from
283-
Expected<Info const*> res = corpus_.lookup(current_context_->id, copied->string);
284-
if (!res || !*res)
281+
auto resRef = corpus_.lookup(current_context_->id, copied->string);
282+
if (!resRef)
285283
{
286284
if (corpus_.config->warnings &&
287285
corpus_.config->warnBrokenRef &&
@@ -292,25 +290,24 @@ copyBriefAndDetails(Javadoc& javadoc)
292290
" {}",
293291
corpus_.Corpus::qualifiedName(*current_context_),
294292
copied->string,
295-
res.error().reason());
293+
resRef.error().reason());
296294
}
297295
continue;
298296
}
299297

300298
// Ensure the source node is finalized
301-
Info const* resPtr = *res;
302-
MRDOCS_ASSERT(resPtr);
303-
if (!finalized_.contains(resPtr))
299+
Info const& res = *resRef;
300+
if (!finalized_.contains(&res))
304301
{
305-
operator()(const_cast<Info&>(*resPtr));
302+
operator()(const_cast<Info&>(res));
306303
}
307-
if (!resPtr->javadoc)
304+
if (!res.javadoc)
308305
{
309306
if (corpus_.config->warnings &&
310307
corpus_.config->warnBrokenRef &&
311308
!refWarned_.contains({copied->string, current_context_->Name}))
312309
{
313-
auto resPrimaryLoc = getPrimaryLocation(*resPtr);
310+
auto resPrimaryLoc = getPrimaryLocation(res);
314311
this->warn(
315312
"{}: Failed to copy documentation from '{}' (no documentation available).\n"
316313
" No documentation available.\n"
@@ -320,15 +317,15 @@ copyBriefAndDetails(Javadoc& javadoc)
320317
copied->string,
321318
resPrimaryLoc->FullPath,
322319
resPrimaryLoc->LineNumber,
323-
corpus_.Corpus::qualifiedName(*resPtr));
320+
corpus_.Corpus::qualifiedName(res));
324321
}
325322
continue;
326323
}
327324

328325
// Copy brief and details
329326
bool const copyBrief = copied->parts == doc::Parts::all || copied->parts == doc::Parts::brief;
330327
bool const copyDetails = copied->parts == doc::Parts::all || copied->parts == doc::Parts::description;
331-
Javadoc const& src = *resPtr->javadoc;
328+
Javadoc const& src = *res.javadoc;
332329
if (copyBrief && !javadoc.brief)
333330
{
334331
javadoc.brief = src.brief;

0 commit comments

Comments
 (0)