Skip to content

Commit 24ddd2c

Browse files
committed
refactor and add allSymbols
1 parent 0bc5d3b commit 24ddd2c

File tree

7 files changed

+228
-158
lines changed

7 files changed

+228
-158
lines changed

include/mrdox/Corpus.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ struct Corpus
3838
unambiguous reference to a symbol.
3939
*/
4040
llvm::StringMap<std::unique_ptr<Info>> USRToInfo;
41+
42+
/** List of everything
43+
*/
44+
std::vector<SymbolID> allSymbols;
45+
46+
//--------------------------------------------
47+
48+
/** Return a pointer to the Info with the matching USR, or nullptr.
49+
*/
50+
Info const*
51+
find(
52+
SymbolID const& id) const noexcept;
53+
54+
/** Insert Info into the index
55+
*/
56+
void
57+
insert(
58+
Info const* I);
4159
};
4260

4361
/** Build the intermediate representation of the code being documented.

source/lib/Generators.cpp

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -54,57 +54,6 @@ createResources(
5454
return llvm::Error::success();
5555
}
5656

57-
// A function to add a reference to Info in Idx.
58-
// Given an Info X with the following namespaces: [B,A]; a reference to X will
59-
// be added in the children of a reference to B, which should be also a child of
60-
// a reference to A, where A is a child of Idx.
61-
// Idx
62-
// |-- A
63-
// |--B
64-
// |--X
65-
// If the references to the namespaces do not exist, they will be created. If
66-
// the references already exist, the same one will be used.
67-
void Generator::addInfoToIndex(Index& Idx, const mrdox::Info* Info) {
68-
// Index pointer that will be moving through Idx until the first parent
69-
// namespace of Info (where the reference has to be inserted) is found.
70-
Index* I = &Idx;
71-
// The Namespace vector includes the upper-most namespace at the end so the
72-
// loop will start from the end to find each of the namespaces.
73-
for (const auto& R : llvm::reverse(Info->Namespace)) {
74-
// Look for the current namespace in the children of the index I is
75-
// pointing.
76-
auto It = llvm::find(I->Children, R.USR);
77-
if (It != I->Children.end()) {
78-
// If it is found, just change I to point the namespace reference found.
79-
I = &*It;
80-
}
81-
else {
82-
// If it is not found a new reference is created
83-
I->Children.emplace_back(R.USR, R.Name, R.RefType, R.Path);
84-
// I is updated with the reference of the new namespace reference
85-
I = &I->Children.back();
86-
}
87-
}
88-
// Look for Info in the vector where it is supposed to be; it could already
89-
// exist if it is a parent namespace of an Info already passed to this
90-
// function.
91-
auto It = llvm::find(I->Children, Info->USR);
92-
if (It == I->Children.end()) {
93-
// If it is not in the vector it is inserted
94-
I->Children.emplace_back(Info->USR, Info->extractName(), Info->IT,
95-
Info->Path);
96-
}
97-
else {
98-
// If it not in the vector we only check if Path and Name are not empty
99-
// because if the Info was included by a namespace it may not have those
100-
// values.
101-
if (It->Path.empty())
102-
It->Path = Info->Path;
103-
if (It->Name.empty())
104-
It->Name = Info->extractName();
105-
}
106-
}
107-
10857
// This anchor is used to force the linker to link in the generated object file
10958
// and thus register the generators.
11059
//

source/lib/Generators.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ class Generator
6262
Info* I, // VFALCO Why not const?
6363
llvm::raw_ostream& OS,
6464
Config const& cfg) = 0;
65-
66-
static
67-
void
68-
addInfoToIndex(
69-
Index& Idx,
70-
mrdox::Info const* Info);
7165
};
7266

7367
// VFALCO a global?

source/lib/jad/Corpus.cpp

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,81 @@ namespace mrdox {
2424

2525
//------------------------------------------------
2626

27+
// A function to add a reference to Info in Idx.
28+
// Given an Info X with the following namespaces: [B,A]; a reference to X will
29+
// be added in the children of a reference to B, which should be also a child of
30+
// a reference to A, where A is a child of Idx.
31+
// Idx
32+
// |-- A
33+
// |--B
34+
// |--X
35+
// If the references to the namespaces do not exist, they will be created. If
36+
// the references already exist, the same one will be used.
37+
void
38+
Corpus::
39+
insert(
40+
Info const* I)
41+
{
42+
// Index pointer that will be moving through Idx until the first parent
43+
// namespace of Info (where the reference has to be inserted) is found.
44+
Index* pi = &Idx;
45+
// The Namespace vector includes the upper-most namespace at the end so the
46+
// loop will start from the end to find each of the namespaces.
47+
for (const auto& R : llvm::reverse(I->Namespace))
48+
{
49+
// Look for the current namespace in the children of the index pi is
50+
// pointing.
51+
auto It = llvm::find(pi->Children, R.USR);
52+
if (It != pi->Children.end())
53+
{
54+
// If it is found, just change pi to point the namespace reference found.
55+
pi = &*It;
56+
}
57+
else
58+
{
59+
// If it is not found a new reference is created
60+
pi->Children.emplace_back(R.USR, R.Name, R.RefType, R.Path);
61+
// pi is updated with the reference of the new namespace reference
62+
pi = &pi->Children.back();
63+
}
64+
}
65+
// Look for Info in the vector where it is supposed to be; it could already
66+
// exist if it is a parent namespace of an Info already passed to this
67+
// function.
68+
auto It = llvm::find(pi->Children, I->USR);
69+
if (It == pi->Children.end())
70+
{
71+
// If it is not in the vector it is inserted
72+
pi->Children.emplace_back(I->USR, I->extractName(), I->IT,
73+
I->Path);
74+
}
75+
else
76+
{
77+
// If it not in the vector we only check if Path and Name are not empty
78+
// because if the Info was included by a namespace it may not have those
79+
// values.
80+
if (It->Path.empty())
81+
It->Path = I->Path;
82+
if (It->Name.empty())
83+
It->Name = I->extractName();
84+
}
85+
86+
allSymbols.emplace_back(I->USR);
87+
}
88+
89+
Info const*
90+
Corpus::
91+
find(
92+
SymbolID const& id) const noexcept
93+
{
94+
auto it = USRToInfo.find(llvm::toHex(llvm::toStringRef(id)));
95+
if(it != USRToInfo.end())
96+
return it->second.get();
97+
return nullptr;
98+
}
99+
100+
//------------------------------------------------
101+
27102
std::unique_ptr<Corpus>
28103
buildCorpus(
29104
tooling::ToolExecutor& ex,
@@ -114,7 +189,7 @@ buildCorpus(
114189
// Add a reference to this Info in the Index
115190
{
116191
std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
117-
Generator::addInfoToIndex(corpus.Idx, Reduced.get().get());
192+
corpus.insert(Reduced.get().get());
118193
}
119194

120195
// Save in the result map (needs a lock due to threaded access).
@@ -135,6 +210,29 @@ buildCorpus(
135210
"an error occurred"));
136211
}
137212

213+
//
214+
// Finish up
215+
//
216+
217+
// Sort allSymbols by fully qualified name
218+
{
219+
std::string temp[2];
220+
llvm::sort(
221+
corpus.allSymbols,
222+
[&](SymbolID const& id0,
223+
SymbolID const& id1) noexcept
224+
{
225+
auto s0 = corpus.find(id0)->getFullyQualifiedName(temp[0]);
226+
auto s1 = corpus.find(id1)->getFullyQualifiedName(temp[1]);
227+
int rv = s0.compare_insensitive(s1);
228+
if(rv < 0)
229+
return true;
230+
if(rv > 0)
231+
return false;
232+
return s0.compare(s1) < 0;
233+
});
234+
}
235+
138236
return std::make_unique<Corpus>(std::move(corpus));
139237
}
140238

source/lib/jad/Info.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,21 @@ calculateRelativeFilePath(
146146
return path::relative_path(FilePath);
147147
}
148148

149+
llvm::StringRef
150+
Info::
151+
getFullyQualifiedName(
152+
std::string& temp) const
153+
{
154+
temp.clear();
155+
for(auto const& ns : llvm::reverse(Namespace))
156+
{
157+
temp.append(ns.Name.data(), ns.Name.size());
158+
temp.append("::");
159+
}
160+
auto s = extractName();
161+
temp.append(s.data(), s.size());
162+
return temp;
163+
}
164+
149165
} // mrdox
150166
} // clang

source/lib/jad/Info.hpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <llvm/ADT/SmallVector.h>
2121
#include <array>
2222
#include <vector>
23+
#include <string>
2324

2425
namespace clang {
2526
namespace mrdox {
@@ -28,23 +29,6 @@ namespace mrdox {
2829
*/
2930
struct Info
3031
{
31-
virtual ~Info() = default;
32-
Info(Info const &Other) = delete;
33-
Info(Info&& Other) = default;
34-
35-
explicit
36-
Info(
37-
InfoType IT = InfoType::IT_default,
38-
SymbolID USR = SymbolID(),
39-
llvm::StringRef Name = llvm::StringRef(),
40-
llvm::StringRef Path = llvm::StringRef())
41-
: USR(USR)
42-
, IT(IT)
43-
, Name(Name)
44-
, Path(Path)
45-
{
46-
}
47-
4832
/** Unique identifier for the declaration.
4933
*/
5034
SymbolID USR = SymbolID();
@@ -69,9 +53,24 @@ struct Info
6953
// generated file will be saved
7054
llvm::SmallString<128> Path;
7155

72-
//
73-
//---
74-
//
56+
//--------------------------------------------
57+
58+
virtual ~Info() = default;
59+
Info(Info const &Other) = delete;
60+
Info(Info&& Other) = default;
61+
62+
explicit
63+
Info(
64+
InfoType IT = InfoType::IT_default,
65+
SymbolID USR = SymbolID(),
66+
llvm::StringRef Name = llvm::StringRef(),
67+
llvm::StringRef Path = llvm::StringRef())
68+
: USR(USR)
69+
, IT(IT)
70+
, Name(Name)
71+
, Path(Path)
72+
{
73+
}
7574

7675
bool mergeable(const Info& Other);
7776
void mergeBase(Info&& I);
@@ -83,6 +82,12 @@ struct Info
8382

8483
/// Returns the basename that should be used for this Info.
8584
llvm::SmallString<16> getFileBaseName() const;
85+
86+
/** Return the fully qualified name.
87+
*/
88+
llvm::StringRef
89+
getFullyQualifiedName(
90+
std::string& temp) const;
8691
};
8792

8893
llvm::SmallString<64>

0 commit comments

Comments
 (0)