Skip to content

Commit 7a96c10

Browse files
committed
add Corpus
1 parent 5b572f6 commit 7a96c10

File tree

11 files changed

+164
-97
lines changed

11 files changed

+164
-97
lines changed

include/mrdox/ClangDocContext.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ struct ClangDocContext
6363

6464
std::unique_ptr<Generator> G;
6565

66-
Index Idx;
66+
//Index Idx;
6767

68-
llvm::StringMap<std::unique_ptr<mrdox::Info>> USRToInfo;
68+
//llvm::StringMap<std::unique_ptr<mrdox::Info>> USRToInfo;
6969
};
7070

7171
//------------------------------------------------
@@ -94,7 +94,8 @@ doMapping(
9494
*/
9595
llvm::Error
9696
buildIndex(
97-
ClangDocContext& CDCtx);
97+
ClangDocContext& CDCtx,
98+
Corpus& corpus);
9899

99100
} // mrdox
100101
} // clang

include/mrdox/Corpus.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 ([email protected])
8+
//
9+
// Official repository: https://github.com/cppalliance/mrdox
10+
//
11+
12+
#ifndef MRDOX_CORPUS_HPP
13+
#define MRDOX_CORPUS_HPP
14+
15+
#include "Representation.h"
16+
17+
namespace clang {
18+
namespace mrdox {
19+
20+
/** The collection of declarations in extracted form.
21+
*/
22+
struct Corpus
23+
{
24+
Corpus(Corpus const&) = delete;
25+
Corpus& operator=(Corpus const&) = delete;
26+
27+
Corpus() = default;
28+
29+
Index Idx;
30+
31+
llvm::StringMap<std::unique_ptr<mrdox::Info>> USRToInfo;
32+
};
33+
34+
} // mrdox
35+
} // clang
36+
37+
#endif

include/mrdox/Setup.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 ([email protected])
8+
//
9+
// Official repository: https://github.com/cppalliance/mrdox
10+
//
11+
12+
#ifndef MRDOX_SETUP_HPP
13+
#define MRDOX_SETUP_HPP
14+
15+
namespace clang {
16+
namespace mrdox {
17+
18+
} // mrdox
19+
} // clang
20+
21+
#endif

source/lib/AsciidocGenerator.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111

1212
#include <mrdox/ClangDocContext.hpp>
13+
#include <mrdox/Corpus.hpp>
1314
#include "Generators.h"
1415
#include "Representation.h"
1516
#include "llvm/ADT/StringRef.h"
@@ -658,7 +659,8 @@ serializeReference(
658659
// emit all_files.adoc
659660
llvm::Error
660661
serializeIndex(
661-
ClangDocContext& CDCtx)
662+
ClangDocContext& CDCtx,
663+
Corpus& corpus)
662664
{
663665
std::error_code FileErr;
664666
llvm::SmallString<128> FilePath;
@@ -670,13 +672,13 @@ serializeIndex(
670672
"error creating index file: " +
671673
FileErr.message());
672674

673-
CDCtx.Idx.sort();
675+
corpus.Idx.sort();
674676
os << "# All Files";
675677
if (!CDCtx.ProjectName.empty())
676678
os << " for " << CDCtx.ProjectName;
677679
os << "\n";
678680

679-
for (auto C : CDCtx.Idx.Children)
681+
for (auto C : corpus.Idx.Children)
680682
serializeReference(os, C, 0);
681683

682684
return llvm::Error::success();
@@ -685,7 +687,8 @@ serializeIndex(
685687
// emit index.adoc
686688
llvm::Error
687689
genIndex(
688-
ClangDocContext& CDCtx)
690+
ClangDocContext& CDCtx,
691+
Corpus& corpus)
689692
{
690693
std::error_code FileErr;
691694
llvm::SmallString<128> FilePath;
@@ -696,9 +699,9 @@ genIndex(
696699
return llvm::createStringError(llvm::inconvertibleErrorCode(),
697700
"error creating index file: " +
698701
FileErr.message());
699-
CDCtx.Idx.sort();
702+
corpus.Idx.sort();
700703
os << "# " << CDCtx.ProjectName << " C/C++ Reference\n";
701-
for (auto C : CDCtx.Idx.Children) {
704+
for (auto C : corpus.Idx.Children) {
702705
if (!C.Children.empty()) {
703706
const char* Type;
704707
switch (C.RefType) {
@@ -749,7 +752,8 @@ class AsciidocGenerator : public Generator
749752

750753
llvm::Error
751754
createResources(
752-
ClangDocContext& CDCtx) override;
755+
ClangDocContext& CDCtx,
756+
Corpus& corpus) override;
753757

754758
llvm::Error
755759
generateDocForInfo(
@@ -845,15 +849,16 @@ generateDocForInfo(
845849
llvm::Error
846850
AsciidocGenerator::
847851
createResources(
848-
ClangDocContext& CDCtx)
852+
ClangDocContext& CDCtx,
853+
Corpus& corpus)
849854
{
850855
// Write an all_files.adoc
851-
auto Err = serializeIndex(CDCtx);
856+
auto Err = serializeIndex(CDCtx, corpus);
852857
if (Err)
853858
return Err;
854859

855860
// Generate the index page.
856-
Err = genIndex(CDCtx);
861+
Err = genIndex(CDCtx, corpus);
857862
if (Err)
858863
return Err;
859864

source/lib/BitcodeReader.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,17 +1024,21 @@ ClangDocBitcodeReader::skipUntilRecordOrBlock(unsigned& BlockOrRecordID) {
10241024
llvm_unreachable("Premature stream end.");
10251025
}
10261026

1027-
llvm::Error ClangDocBitcodeReader::validateStream() {
1027+
llvm::Error
1028+
ClangDocBitcodeReader::
1029+
validateStream()
1030+
{
10281031
if (Stream.AtEndOfStream())
10291032
return llvm::createStringError(llvm::inconvertibleErrorCode(),
10301033
"premature end of stream");
10311034

10321035
// Sniff for the signature.
1033-
for (int Idx = 0; Idx != 4; ++Idx) {
1036+
for (int i = 0; i != 4; ++i)
1037+
{
10341038
Expected<llvm::SimpleBitstreamCursor::word_t> MaybeRead = Stream.Read(8);
10351039
if (!MaybeRead)
10361040
return MaybeRead.takeError();
1037-
else if (MaybeRead.get() != BitCodeConstants::Signature[Idx])
1041+
else if (MaybeRead.get() != BitCodeConstants::Signature[i])
10381042
return llvm::createStringError(llvm::inconvertibleErrorCode(),
10391043
"invalid bitcode signature");
10401044
}

source/lib/ClangDocContext.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ doMapping(
225225

226226
llvm::Error
227227
buildIndex(
228-
ClangDocContext& CDCtx)
228+
ClangDocContext& CDCtx,
229+
Corpus& corpus)
229230
{
230231
// Collect values into output by key.
231232
// In ToolResults, the Key is the hashed USR and the value is the
@@ -284,13 +285,13 @@ buildIndex(
284285
// Add a reference to this Info in the Index
285286
{
286287
std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
287-
clang::mrdox::Generator::addInfoToIndex(CDCtx.Idx, Reduced.get().get());
288+
clang::mrdox::Generator::addInfoToIndex(corpus.Idx, Reduced.get().get());
288289
}
289290

290291
// Save in the result map (needs a lock due to threaded access).
291292
{
292293
std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
293-
CDCtx.USRToInfo[Group.getKey()] = std::move(Reduced.get());
294+
corpus.USRToInfo[Group.getKey()] = std::move(Reduced.get());
294295
}
295296
});
296297
}

source/lib/Generators.cpp

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,40 @@ namespace mrdox {
1818

1919
llvm::Expected<std::unique_ptr<Generator>>
2020
findGeneratorByName(llvm::StringRef Format) {
21-
for (const auto &Generator : GeneratorRegistry::entries()) {
22-
if (Generator.getName() != Format)
23-
continue;
24-
return Generator.instantiate();
25-
}
26-
return createStringError(llvm::inconvertibleErrorCode(),
27-
"can't find generator: " + Format);
21+
for (const auto& Generator : GeneratorRegistry::entries()) {
22+
if (Generator.getName() != Format)
23+
continue;
24+
return Generator.instantiate();
25+
}
26+
return createStringError(llvm::inconvertibleErrorCode(),
27+
"can't find generator: " + Format);
2828
}
2929

3030
// Enum conversion
3131

3232
std::string getTagType(TagTypeKind AS) {
33-
switch (AS) {
34-
case TagTypeKind::TTK_Class:
35-
return "class";
36-
case TagTypeKind::TTK_Union:
37-
return "union";
38-
case TagTypeKind::TTK_Interface:
39-
return "interface";
40-
case TagTypeKind::TTK_Struct:
41-
return "struct";
42-
case TagTypeKind::TTK_Enum:
43-
return "enum";
44-
}
45-
llvm_unreachable("Unknown TagTypeKind");
33+
switch (AS) {
34+
case TagTypeKind::TTK_Class:
35+
return "class";
36+
case TagTypeKind::TTK_Union:
37+
return "union";
38+
case TagTypeKind::TTK_Interface:
39+
return "interface";
40+
case TagTypeKind::TTK_Struct:
41+
return "struct";
42+
case TagTypeKind::TTK_Enum:
43+
return "enum";
44+
}
45+
llvm_unreachable("Unknown TagTypeKind");
4646
}
4747

48-
llvm::Error Generator::createResources(ClangDocContext &CDCtx) {
49-
return llvm::Error::success();
48+
llvm::Error
49+
Generator::
50+
createResources(
51+
ClangDocContext& CDCtx,
52+
Corpus& corpus)
53+
{
54+
return llvm::Error::success();
5055
}
5156

5257
// A function to add a reference to Info in Idx.
@@ -59,43 +64,45 @@ llvm::Error Generator::createResources(ClangDocContext &CDCtx) {
5964
// |--X
6065
// If the references to the namespaces do not exist, they will be created. If
6166
// the references already exist, the same one will be used.
62-
void Generator::addInfoToIndex(Index &Idx, const mrdox::Info *Info) {
63-
// Index pointer that will be moving through Idx until the first parent
64-
// namespace of Info (where the reference has to be inserted) is found.
65-
Index *I = &Idx;
66-
// The Namespace vector includes the upper-most namespace at the end so the
67-
// loop will start from the end to find each of the namespaces.
68-
for (const auto &R : llvm::reverse(Info->Namespace)) {
69-
// Look for the current namespace in the children of the index I is
70-
// pointing.
71-
auto It = llvm::find(I->Children, R.USR);
72-
if (It != I->Children.end()) {
73-
// If it is found, just change I to point the namespace reference found.
74-
I = &*It;
75-
} else {
76-
// If it is not found a new reference is created
77-
I->Children.emplace_back(R.USR, R.Name, R.RefType, R.Path);
78-
// I is updated with the reference of the new namespace reference
79-
I = &I->Children.back();
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();
80105
}
81-
}
82-
// Look for Info in the vector where it is supposed to be; it could already
83-
// exist if it is a parent namespace of an Info already passed to this
84-
// function.
85-
auto It = llvm::find(I->Children, Info->USR);
86-
if (It == I->Children.end()) {
87-
// If it is not in the vector it is inserted
88-
I->Children.emplace_back(Info->USR, Info->extractName(), Info->IT,
89-
Info->Path);
90-
} else {
91-
// If it not in the vector we only check if Path and Name are not empty
92-
// because if the Info was included by a namespace it may not have those
93-
// values.
94-
if (It->Path.empty())
95-
It->Path = Info->Path;
96-
if (It->Name.empty())
97-
It->Name = Info->extractName();
98-
}
99106
}
100107

101108
// This anchor is used to force the linker to link in the generated object file
@@ -106,11 +113,11 @@ void Generator::addInfoToIndex(Index &Idx, const mrdox::Info *Info) {
106113
//
107114
extern volatile int AsciidocGeneratorAnchorSource;
108115
static int LLVM_ATTRIBUTE_UNUSED AsciidocGeneratorAnchorDest =
109-
AsciidocGeneratorAnchorSource;
116+
AsciidocGeneratorAnchorSource;
110117

111118
extern volatile int XMLGeneratorAnchorSource;
112119
static int LLVM_ATTRIBUTE_UNUSED XMLGeneratorAnchorDest =
113-
AsciidocGeneratorAnchorSource;
120+
AsciidocGeneratorAnchorSource;
114121

115122
} // namespace mrdox
116123
} // namespace clang

source/lib/Generators.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_GENERATOR_H
1616
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_GENERATOR_H
1717

18+
#include <mrdox/Corpus.hpp>
1819
#include "Representation.h"
1920
#include "llvm/Support/Error.h"
2021
#include "llvm/Support/Registry.h"
@@ -50,7 +51,8 @@ class Generator
5051
virtual
5152
llvm::Error
5253
createResources(
53-
ClangDocContext& CDCtx);
54+
ClangDocContext& CDCtx,
55+
Corpus& corpus);
5456

5557
// Write out one specific decl info to the destination stream.
5658
virtual

0 commit comments

Comments
 (0)