Skip to content

Commit 3778849

Browse files
committed
updated working copy
1 parent 42d2995 commit 3778849

14 files changed

+401
-404
lines changed

clang-tools-extra/clang-doc/BitcodeReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ llvm::Error parseRecord(const Record &R, unsigned ID, llvm::StringRef Blob,
234234
switch (ID) {
235235
case TYPEDEF_USR:
236236
return decodeRecord(R, I->USR, Blob);
237+
case TYPEDEF_DECLARATION:
238+
return decodeRecord(R, I->TypeDeclaration, Blob);
237239
case TYPEDEF_NAME:
238240
return decodeRecord(R, I->Name, Blob);
239241
case TYPEDEF_DEFLOCATION:

clang-tools-extra/clang-doc/BitcodeWriter.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ static const llvm::IndexedMap<RecordIdDsc, RecordIdToIndexFunctor>
200200
{TEMPLATE_PARAM_CONTENTS, {"Contents", &StringAbbrev}},
201201
{TEMPLATE_SPECIALIZATION_OF, {"SpecializationOf", &SymbolIDAbbrev}},
202202
{TYPEDEF_USR, {"USR", &SymbolIDAbbrev}},
203+
{TYPEDEF_DECLARATION, {"Declaration", &StringAbbrev}},
203204
{TYPEDEF_NAME, {"Name", &StringAbbrev}},
204205
{TYPEDEF_DEFLOCATION, {"DefLocation", &LocationAbbrev}},
205206
{TYPEDEF_IS_USING, {"IsUsing", &BoolAbbrev}}};
@@ -235,7 +236,8 @@ static const std::vector<std::pair<BlockId, std::vector<RecordId>>>
235236
{ENUM_VALUE_NAME, ENUM_VALUE_VALUE, ENUM_VALUE_EXPR}},
236237
// Typedef Block
237238
{BI_TYPEDEF_BLOCK_ID,
238-
{TYPEDEF_USR, TYPEDEF_NAME, TYPEDEF_DEFLOCATION, TYPEDEF_IS_USING}},
239+
{TYPEDEF_USR, TYPEDEF_NAME, TYPEDEF_DECLARATION, TYPEDEF_DEFLOCATION,
240+
TYPEDEF_IS_USING}},
239241
// Namespace Block
240242
{BI_NAMESPACE_BLOCK_ID,
241243
{NAMESPACE_USR, NAMESPACE_NAME, NAMESPACE_PATH}},
@@ -430,10 +432,6 @@ void ClangDocBitcodeWriter::emitBlock(const Reference &R, FieldId Field) {
430432
if (R.USR == EmptySID && R.Name.empty())
431433
return;
432434

433-
if (R.Name == "const Shape &") {
434-
llvm::outs() << "const Shape & USR: " <<
435-
llvm::toHex(llvm::toStringRef(R.USR)) << "\n";
436-
}
437435
StreamSubBlockGuard Block(Stream, BI_REFERENCE_BLOCK_ID);
438436
emitRecord(R.USR, REFERENCE_USR);
439437
emitRecord(R.Name, REFERENCE_NAME);
@@ -454,6 +452,7 @@ void ClangDocBitcodeWriter::emitBlock(const TypedefInfo &T) {
454452
StreamSubBlockGuard Block(Stream, BI_TYPEDEF_BLOCK_ID);
455453
emitRecord(T.USR, TYPEDEF_USR);
456454
emitRecord(T.Name, TYPEDEF_NAME);
455+
emitRecord(T.TypeDeclaration, TYPEDEF_DECLARATION);
457456
for (const auto &N : T.Namespace)
458457
emitBlock(N, FieldId::F_namespace);
459458
for (const auto &CI : T.Description)

clang-tools-extra/clang-doc/BitcodeWriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ enum RecordId {
135135
TEMPLATE_SPECIALIZATION_OF,
136136
TYPEDEF_USR,
137137
TYPEDEF_NAME,
138+
TYPEDEF_DECLARATION,
138139
TYPEDEF_DEFLOCATION,
139140
TYPEDEF_IS_USING,
140141
RI_LAST,

clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp

Lines changed: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ void generateIndex(Info *I) {
2525
switch (I->IT) {
2626
case InfoType::IT_namespace: {
2727
const NamespaceInfo& N = *static_cast<clang::doc::NamespaceInfo *>(I);
28-
SmallString<16> Name = N.getFileBaseName();
2928
std::string HexId = llvm::toHex(llvm::toStringRef(N.USR));
3029
Index[HexId] = HexId;
3130
for (const EnumInfo& E : N.Children.Enums)
@@ -36,15 +35,12 @@ void generateIndex(Info *I) {
3635
}
3736
case InfoType::IT_record: {
3837
const RecordInfo& R = *static_cast<clang::doc::RecordInfo *>(I);
39-
SmallString<16> Name = R.getFileBaseName();
4038
std::string HexId = llvm::toHex(llvm::toStringRef(R.USR));
4139
Index[HexId] = HexId;
42-
for (const EnumInfo& E : R.Children.Enums) {
40+
for (const EnumInfo& E : R.Children.Enums)
4341
Index[llvm::toHex(llvm::toStringRef(E.USR))] = HexId;
44-
}
45-
for (const TypedefInfo& T : R.Children.Typedefs) {
42+
for (const TypedefInfo& T : R.Children.Typedefs)
4643
Index[llvm::toHex(llvm::toStringRef(T.USR))] = HexId;
47-
}
4844
break;
4945
}
5046
case InfoType::IT_enum:
@@ -127,10 +123,12 @@ setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
127123
auto CommentFilePath = CDCtx.MustacheTemplates.lookup("comments-template");
128124
auto FunctionFilePath = CDCtx.MustacheTemplates.lookup("function-template");
129125
auto EnumFilePath = CDCtx.MustacheTemplates.lookup("enum-template");
126+
auto TypeDefFilePath = CDCtx.MustacheTemplates.lookup("typedef-template");
130127
std::vector<std::pair<StringRef, StringRef>> Partials = {
131128
{"Comments", CommentFilePath},
132129
{"FunctionPartial", FunctionFilePath},
133-
{"EnumPartial", EnumFilePath}
130+
{"EnumPartial", EnumFilePath},
131+
{"TypedefPartial", TypeDefFilePath}
134132
};
135133

136134
auto Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials);
@@ -152,8 +150,6 @@ MustacheHTMLGenerator::generateDocs(llvm::StringRef RootDir,
152150
const clang::doc::ClangDocContext &CDCtx) {
153151
if (auto Err = setupTemplateFiles(CDCtx))
154152
return Err;
155-
// Track which directories we already tried to create.
156-
llvm::StringSet<> CreatedDirs;
157153
// Collect all output by file name and create the necessary directories.
158154
llvm::StringMap<std::vector<doc::Info *>> FileToInfos;
159155
for (const auto &Group : Infos) {
@@ -182,41 +178,6 @@ MustacheHTMLGenerator::generateDocs(llvm::StringRef RootDir,
182178
return llvm::Error::success();
183179
}
184180

185-
Value extractValue(const Location &L,
186-
std::optional<StringRef> RepositoryUrl = std::nullopt) {
187-
Object Obj = Object();
188-
Obj.insert({"LineNumber", L.LineNumber});
189-
Obj.insert({"Filename", L.Filename});
190-
191-
if (!L.IsFileInRootDir || !RepositoryUrl)
192-
return Obj;
193-
194-
SmallString<128> FileURL(*RepositoryUrl);
195-
llvm::sys::path::append(FileURL, llvm::sys::path::Style::posix, L.Filename);
196-
FileURL += "#" + std::to_string(L.LineNumber);
197-
Obj.insert({"FileURL", FileURL});
198-
199-
return Obj;
200-
}
201-
202-
Value extractValue(const Reference &I, StringRef CurrentDirectory) {
203-
llvm::SmallString<64> Path = I.getRelativeFilePath(CurrentDirectory);
204-
llvm::sys::path::append(Path, I.getFileBaseName() + ".html");
205-
llvm::sys::path::native(Path, llvm::sys::path::Style::posix);
206-
Object Obj = Object();
207-
Obj.insert({"Link", Path});
208-
Obj.insert({"Name", I.Name});
209-
Obj.insert({"QualName", I.QualName});
210-
Obj.insert({"ID", llvm::toHex(llvm::toStringRef(I.USR))});
211-
return Obj;
212-
}
213-
214-
215-
Value extractValue(const TypedefInfo &I) {
216-
// Not Supported
217-
return nullptr;
218-
}
219-
220181
Value extractValue(const CommentInfo &I) {
221182
Object Obj = Object();
222183
Value Child = Object();
@@ -238,6 +199,7 @@ Value extractValue(const CommentInfo &I) {
238199
if (I.Kind == "BlockCommandComment") {
239200
Child.getAsObject()->insert({"Command", I.Name});
240201
Value ChildArr = Array();
202+
241203
for (const auto& C: I.Children)
242204
ChildArr.getAsArray()->emplace_back(extractValue(*C));
243205
Child.getAsObject()->insert({"Children", ChildArr});
@@ -250,6 +212,56 @@ Value extractValue(const CommentInfo &I) {
250212
}
251213

252214

215+
Value extractValue(const std::optional<Location> &Loc,
216+
std::optional<StringRef> RepositoryUrl = std::nullopt) {
217+
218+
Object Obj = Object();
219+
if (Loc.has_value()) {
220+
Location L = *Loc;
221+
SmallString<128> Filename(llvm::sys::path::filename(L.Filename));
222+
Obj.insert({"LineNumber", L.LineNumber});
223+
Obj.insert({"Filename", Filename});
224+
if (!RepositoryUrl)
225+
return Obj;
226+
// This link only works specifically for github
227+
SmallString<128> FileURL(*RepositoryUrl);
228+
llvm::sys::path::append(FileURL, llvm::sys::path::Style::posix, "blob/main");
229+
llvm::sys::path::append(FileURL, llvm::sys::path::Style::posix, L.Filename);
230+
FileURL += "#L" + std::to_string(L.LineNumber);
231+
Obj.insert({"FileURL", FileURL});
232+
}
233+
return Obj;
234+
}
235+
236+
Value extractValue(const Reference &I, StringRef CurrentDirectory) {
237+
llvm::SmallString<64> Path = I.getRelativeFilePath(CurrentDirectory);
238+
llvm::sys::path::append(Path, I.getFileBaseName() + ".html");
239+
llvm::sys::path::native(Path, llvm::sys::path::Style::posix);
240+
Object Obj = Object();
241+
Obj.insert({"Link", Path});
242+
Obj.insert({"Name", I.Name});
243+
Obj.insert({"QualName", I.QualName});
244+
Obj.insert({"ID", llvm::toHex(llvm::toStringRef(I.USR))});
245+
return Obj;
246+
}
247+
248+
249+
Value extractValue(const TypedefInfo &I, const ClangDocContext& CDCtx) {
250+
Object Obj = Object();
251+
Obj.insert({"ID", llvm::toHex(llvm::toStringRef(I.USR))});
252+
Obj.insert({"TypeDeclaration", I.TypeDeclaration});
253+
Obj.insert({"Name", I.Name});
254+
Obj.insert({"IsAlias", I.IsUsing});
255+
Obj.insert({"Location", extractValue(I.DefLoc, CDCtx.RepositoryUrl)});
256+
if (!I.Description.empty()) {
257+
Value ArrDesc = Array();
258+
for (const CommentInfo& Child : I.Description)
259+
ArrDesc.getAsArray()->emplace_back(extractValue(Child));
260+
Obj.insert({"TypeDefComments", ArrDesc});
261+
}
262+
return Obj;
263+
}
264+
253265
// Function to replace a substring within a SmallString with another SmallString
254266
void replaceSubstring(llvm::SmallString<256> &Input,
255267
const llvm::SmallString<16> &From,
@@ -383,14 +395,7 @@ Value extractValue(const FunctionInfo &I, StringRef ParentInfoDir,
383395
ArrDesc.getAsArray()->emplace_back(extractValue(Child));
384396
Obj.insert({"FunctionComments", ArrDesc});
385397
}
386-
if (I.DefLoc.has_value()) {
387-
Location L = *I.DefLoc;
388-
if (CDCtx.RepositoryUrl.has_value())
389-
Obj.insert({"Location", extractValue(L,
390-
StringRef{*CDCtx.RepositoryUrl})});
391-
else
392-
Obj.insert({"Location", extractValue(L)});
393-
}
398+
Obj.insert({"Location", extractValue(I.DefLoc, CDCtx.RepositoryUrl)});
394399
return Obj;
395400
}
396401

@@ -429,16 +434,7 @@ Value extractValue(const EnumInfo &I, const ClangDocContext &CDCtx) {
429434
ArrDesc.getAsArray()->emplace_back(extractValue(Child));
430435
Obj.insert({"EnumComments", ArrDesc});
431436
}
432-
433-
if (I.DefLoc.has_value()) {
434-
Location L = *I.DefLoc;
435-
if (CDCtx.RepositoryUrl.has_value())
436-
Obj.insert({"Location", extractValue(L,
437-
StringRef{*CDCtx.RepositoryUrl})});
438-
else
439-
Obj.insert({"Location", extractValue(L)});
440-
}
441-
437+
Obj.insert({"Location", extractValue(I.DefLoc, CDCtx.RepositoryUrl)});
442438
return Obj;
443439
}
444440

@@ -493,10 +489,15 @@ void extractScopeChildren(const ScopeChildren &S, Object &Obj,
493489

494490
Value ArrTypedefs = Array();
495491
for (const TypedefInfo& Child : S.Typedefs)
496-
ArrTypedefs.getAsArray()->emplace_back(extractValue(Child));
492+
ArrTypedefs.getAsArray()->emplace_back(extractValue(Child, CDCtx));
497493

498494
if (!ArrTypedefs.getAsArray()->empty())
499495
Obj.insert({"Typedefs", Object{{"Obj", ArrTypedefs }}});
496+
497+
498+
llvm::raw_fd_ostream os(1, false);
499+
llvm::json::OStream jStream(os, /*Indent=*/2);
500+
jStream.value(ArrTypedefs);
500501
}
501502

502503
Value extractValue(const NamespaceInfo &I, const ClangDocContext &CDCtx) {
@@ -536,16 +537,7 @@ Value extractValue(const RecordInfo &I, const ClangDocContext &CDCtx) {
536537
I.VirtualParents)});
537538

538539
RecordValue.insert({"RecordType", getTagType(I.TagType)});
539-
540-
if (I.DefLoc.has_value()) {
541-
Location L = *I.DefLoc;
542-
if (CDCtx.RepositoryUrl.has_value())
543-
RecordValue.insert({"Location", extractValue(L,
544-
StringRef{*CDCtx.RepositoryUrl})});
545-
else
546-
RecordValue.insert({"Location", extractValue(L)});
547-
}
548-
540+
RecordValue.insert({"Location", extractValue(I.DefLoc, CDCtx.RepositoryUrl)});
549541
StringRef BasePath = I.getRelativeFilePath("");
550542
extractScopeChildren(I.Children, RecordValue, BasePath, CDCtx);
551543
Value PublicMembers = Array();

clang-tools-extra/clang-doc/Mapper.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ static llvm::StringSet<> USRVisited;
2222
static llvm::sys::Mutex USRVisitedGuard;
2323

2424
template <typename T> bool isTypedefAnonRecord(const T *D) {
25-
if (const auto *C = dyn_cast<CXXRecordDecl>(D)) {
25+
if (const auto *C = dyn_cast<CXXRecordDecl>(D))
2626
return C->getTypedefNameForAnonDecl();
27-
}
2827
return false;
2928
}
3029

@@ -121,16 +120,20 @@ int MapASTVisitor::getLine(const NamedDecl *D,
121120
return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
122121
}
123122

123+
124124
llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,
125125
const ASTContext &Context,
126126
llvm::StringRef RootDir,
127127
bool &IsFileInRootDir) const {
128128
llvm::SmallString<128> File(Context.getSourceManager()
129129
.getPresumedLoc(D->getBeginLoc())
130130
.getFilename());
131+
llvm::SmallString<128> Result;
131132
IsFileInRootDir = false;
133+
Result = llvm::sys::path::remove_leading_dotslash(File);
134+
Result = llvm::SmallString<128>(llvm::sys::path::convert_to_slash(Result));
132135
if (RootDir.empty() || !File.starts_with(RootDir))
133-
return File;
136+
return Result;
134137
IsFileInRootDir = true;
135138
llvm::SmallString<128> Prefix(RootDir);
136139
// replace_path_prefix removes the exact prefix provided. The result of
@@ -139,8 +142,9 @@ llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,
139142
// ends with a / and the result has the desired format.
140143
if (!llvm::sys::path::is_separator(Prefix.back()))
141144
Prefix += llvm::sys::path::get_separator();
142-
llvm::sys::path::replace_path_prefix(File, Prefix, "");
143-
return File;
145+
146+
llvm::sys::path::replace_path_prefix(Result, Prefix, "");
147+
return Result;
144148
}
145149

146150
} // namespace doc

clang-tools-extra/clang-doc/Representation.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,15 @@ struct TypedefInfo : public SymbolInfo {
422422
void merge(TypedefInfo &&I);
423423

424424
TypeInfo Underlying;
425-
426-
// Inidicates if this is a new C++ "using"-style typedef:
425+
// Underlying type declaration
426+
SmallString<16> TypeDeclaration;
427+
// Indicates if this is a new C++ "using"-style typedef:
427428
// using MyVector = std::vector<int>
428429
// False means it's a C-style typedef:
429430
// typedef std::vector<int> MyVector;
430431
bool IsUsing = false;
432+
433+
std::vector<CommentInfo> Description;
431434
};
432435

433436
struct BaseRecordInfo : public RecordInfo {
@@ -465,8 +468,9 @@ struct EnumValueInfo {
465468
// Stores the user-supplied initialization expression for this enumeration
466469
// constant. This will be empty for implicit enumeration values.
467470
SmallString<16> ValueExpr;
468-
469-
std::vector<CommentInfo> Description; /// Comment description of this field.
471+
472+
/// Comment description of this field.
473+
std::vector<CommentInfo> Description;
470474
};
471475

472476
// TODO: Expand to allow for documenting templating.

0 commit comments

Comments
 (0)