11#include "clang/Basic/Version.h"
12#include "llvm/ADT/StringExtras.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/StringSet.h"
15#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/JSON.h"
17#include "llvm/Support/Path.h"
18#include "llvm/Support/raw_ostream.h"
53 constexpr HTMLTag(TagType Value) : Value(Value) {}
55 operator TagType()
const {
return Value; }
56 operator bool() =
delete;
58 bool isSelfClosing()
const;
59 StringRef toString()
const;
71 HTMLNode(NodeType Type) :
Type(
Type) {}
72 virtual ~HTMLNode() =
default;
74 virtual void render(llvm::raw_ostream &
OS,
int IndentationLevel) = 0;
78struct TextNode :
public HTMLNode {
79 TextNode(
const Twine &
Text)
80 : HTMLNode(NodeType::NODE_TEXT),
Text(
Text.str()) {}
83 void render(llvm::raw_ostream &
OS,
int IndentationLevel)
override;
86struct TagNode :
public HTMLNode {
87 TagNode(HTMLTag Tag) : HTMLNode(NodeType::NODE_TAG),
Tag(
Tag) {}
88 TagNode(HTMLTag Tag,
const Twine &
Text) : TagNode(
Tag) {
89 Children.emplace_back(std::make_unique<TextNode>(
Text.str()));
93 std::vector<std::unique_ptr<HTMLNode>>
Children;
94 std::vector<std::pair<std::string, std::string>>
97 void render(llvm::raw_ostream &
OS,
int IndentationLevel)
override;
100constexpr const char *kDoctypeDecl =
"<!DOCTYPE html>";
103 std::vector<std::unique_ptr<HTMLNode>>
Children;
104 void render(llvm::raw_ostream &
OS) {
105 OS << kDoctypeDecl <<
"\n";
106 for (
const auto &
C : Children) {
115bool HTMLTag::isSelfClosing()
const {
117 case HTMLTag::TAG_META:
118 case HTMLTag::TAG_LINK:
121 case HTMLTag::TAG_DIV:
122 case HTMLTag::TAG_FOOTER:
123 case HTMLTag::TAG_H1:
124 case HTMLTag::TAG_H2:
125 case HTMLTag::TAG_H3:
126 case HTMLTag::TAG_HEADER:
127 case HTMLTag::TAG_LI:
128 case HTMLTag::TAG_MAIN:
129 case HTMLTag::TAG_OL:
131 case HTMLTag::TAG_SCRIPT:
132 case HTMLTag::TAG_SPAN:
133 case HTMLTag::TAG_TITLE:
134 case HTMLTag::TAG_UL:
137 llvm_unreachable(
"Unhandled HTMLTag::TagType");
140StringRef HTMLTag::toString()
const {
144 case HTMLTag::TAG_DIV:
146 case HTMLTag::TAG_FOOTER:
148 case HTMLTag::TAG_H1:
150 case HTMLTag::TAG_H2:
152 case HTMLTag::TAG_H3:
154 case HTMLTag::TAG_HEADER:
156 case HTMLTag::TAG_LI:
158 case HTMLTag::TAG_LINK:
160 case HTMLTag::TAG_MAIN:
162 case HTMLTag::TAG_META:
164 case HTMLTag::TAG_OL:
168 case HTMLTag::TAG_SCRIPT:
170 case HTMLTag::TAG_SPAN:
172 case HTMLTag::TAG_TITLE:
174 case HTMLTag::TAG_UL:
177 llvm_unreachable(
"Unhandled HTMLTag::TagType");
180void TextNode::render(llvm::raw_ostream &
OS,
int IndentationLevel) {
181 OS.indent(IndentationLevel * 2);
182 printHTMLEscaped(
Text,
OS);
185void TagNode::render(llvm::raw_ostream &
OS,
int IndentationLevel) {
187 bool InlineChildren =
true;
189 if (
C->Type == NodeType::NODE_TAG) {
190 InlineChildren =
false;
193 OS.indent(IndentationLevel * 2);
194 OS <<
"<" <<
Tag.toString();
196 OS <<
" " <<
A.first <<
"=\"" <<
A.second <<
"\"";
197 if (
Tag.isSelfClosing()) {
204 bool NewLineRendered =
true;
206 int ChildrenIndentation =
207 InlineChildren || !NewLineRendered ? 0 : IndentationLevel + 1;
208 C->render(
OS, ChildrenIndentation);
209 if (!InlineChildren && (
C ==
Children.back() ||
210 (
C->Type != NodeType::NODE_TEXT ||
211 (&
C + 1)->get()->Type != NodeType::NODE_TEXT))) {
213 NewLineRendered =
true;
215 NewLineRendered =
false;
218 OS.indent(IndentationLevel * 2);
219 OS <<
"</" <<
Tag.toString() <<
">";
222template <
typename Derived,
typename Base,
223 typename = std::enable_if<std::is_base_of<Derived, Base>::value>>
225 std::vector<Base> &Original) {
226 std::move(New.begin(), New.end(), std::back_inserter(Original));
238 if (Destination == Origin)
242 llvm::sys::path::const_iterator FileI = llvm::sys::path::begin(Destination);
243 llvm::sys::path::const_iterator FileE = llvm::sys::path::end(Destination);
244 llvm::sys::path::const_iterator DirI = llvm::sys::path::begin(Origin);
245 llvm::sys::path::const_iterator DirE = llvm::sys::path::end(Origin);
251 while (FileI != FileE && DirI != DirE && *FileI == *DirI) {
255 SmallString<128> Result;
258 while (DirI != DirE) {
259 llvm::sys::path::append(Result,
"..");
263 while (FileI != FileE) {
264 llvm::sys::path::append(Result, *FileI);
272static std::vector<std::unique_ptr<TagNode>>
274 std::vector<std::unique_ptr<TagNode>>
Out;
276 auto LinkNode = std::make_unique<TagNode>(HTMLTag::TAG_LINK);
277 LinkNode->Attributes.emplace_back(
"rel",
"stylesheet");
279 llvm::sys::path::append(StylesheetPath,
280 llvm::sys::path::filename(FilePath));
282 llvm::sys::path::native(StylesheetPath, llvm::sys::path::Style::posix);
283 LinkNode->Attributes.emplace_back(
"href", std::string(StylesheetPath));
284 Out.emplace_back(std::move(LinkNode));
289static std::vector<std::unique_ptr<TagNode>>
291 std::vector<std::unique_ptr<TagNode>>
Out;
295 auto IndexJSONNode = std::make_unique<TagNode>(HTMLTag::TAG_SCRIPT);
296 llvm::sys::path::append(IndexJSONPath,
"index_json.js");
297 llvm::sys::path::native(IndexJSONPath, llvm::sys::path::Style::posix);
298 IndexJSONNode->Attributes.emplace_back(
"src", std::string(IndexJSONPath));
299 Out.emplace_back(std::move(IndexJSONNode));
301 for (
const auto &FilePath : CDCtx.
JsScripts) {
303 auto ScriptNode = std::make_unique<TagNode>(HTMLTag::TAG_SCRIPT);
304 llvm::sys::path::append(ScriptPath, llvm::sys::path::filename(FilePath));
306 llvm::sys::path::native(ScriptPath, llvm::sys::path::Style::posix);
307 ScriptNode->Attributes.emplace_back(
"src", std::string(ScriptPath));
308 Out.emplace_back(std::move(ScriptNode));
313static std::unique_ptr<TagNode>
genLink(
const Twine &
Text,
const Twine &Link) {
314 auto LinkNode = std::make_unique<TagNode>(HTMLTag::TAG_A,
Text);
315 LinkNode->Attributes.emplace_back(
"href", Link.str());
319static std::unique_ptr<HTMLNode>
321 std::optional<StringRef> JumpToSection = std::nullopt) {
322 if (
Type.Path.empty()) {
324 return std::make_unique<TextNode>(
Type.Name);
327 llvm::SmallString<64>
Path =
Type.getRelativeFilePath(CurrentDirectory);
328 llvm::sys::path::append(
Path,
Type.getFileBaseName() +
".html");
331 llvm::sys::path::native(
Path, llvm::sys::path::Style::posix);
333 Path += (
"#" + *JumpToSection).str();
337static std::vector<std::unique_ptr<HTMLNode>>
339 const StringRef &CurrentDirectory) {
340 std::vector<std::unique_ptr<HTMLNode>>
Out;
341 for (
const auto &R : Refs) {
342 if (&R != Refs.begin())
343 Out.emplace_back(std::make_unique<TextNode>(
", "));
349static std::vector<std::unique_ptr<TagNode>>
350genHTML(
const EnumInfo &I,
const ClangDocContext &CDCtx);
351static std::vector<std::unique_ptr<TagNode>>
352genHTML(
const FunctionInfo &I,
const ClangDocContext &CDCtx,
353 StringRef ParentInfoDir);
355static std::vector<std::unique_ptr<TagNode>>
361 std::vector<std::unique_ptr<TagNode>>
Out;
362 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_H2,
"Enums"));
363 Out.back()->Attributes.emplace_back(
"id",
"Enums");
364 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_DIV));
365 auto &DivBody =
Out.back();
366 for (
const auto &
E : Enums) {
367 std::vector<std::unique_ptr<TagNode>> Nodes =
genHTML(
E, CDCtx);
373static std::unique_ptr<TagNode>
378 auto List = std::make_unique<TagNode>(HTMLTag::TAG_UL);
379 for (
const auto &
M : Members)
380 List->Children.emplace_back(
381 std::make_unique<TagNode>(HTMLTag::TAG_LI,
M.Name));
385static std::vector<std::unique_ptr<TagNode>>
388 if (Functions.empty())
391 std::vector<std::unique_ptr<TagNode>>
Out;
392 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_H2,
"Functions"));
393 Out.back()->Attributes.emplace_back(
"id",
"Functions");
394 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_DIV));
395 auto &DivBody =
Out.back();
396 for (
const auto &F : Functions) {
397 std::vector<std::unique_ptr<TagNode>> Nodes =
398 genHTML(F, CDCtx, ParentInfoDir);
404static std::vector<std::unique_ptr<TagNode>>
406 StringRef ParentInfoDir) {
410 std::vector<std::unique_ptr<TagNode>>
Out;
411 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_H2,
"Members"));
412 Out.back()->Attributes.emplace_back(
"id",
"Members");
413 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_UL));
414 auto &ULBody =
Out.back();
415 for (
const auto &
M : Members) {
416 std::string Access = getAccessSpelling(
M.Access).str();
418 Access = Access +
" ";
419 auto LIBody = std::make_unique<TagNode>(HTMLTag::TAG_LI);
420 LIBody->Children.emplace_back(std::make_unique<TextNode>(Access));
421 LIBody->Children.emplace_back(
genReference(
M.Type, ParentInfoDir));
422 LIBody->Children.emplace_back(std::make_unique<TextNode>(
" " +
M.Name));
423 ULBody->Children.emplace_back(std::move(LIBody));
428static std::vector<std::unique_ptr<TagNode>>
430 llvm::StringRef Title, StringRef ParentPath) {
434 std::vector<std::unique_ptr<TagNode>>
Out;
435 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_H2, Title));
436 Out.back()->Attributes.emplace_back(
"id", std::string(Title));
437 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_UL));
438 auto &ULBody =
Out.back();
440 auto LiNode = std::make_unique<TagNode>(HTMLTag::TAG_LI);
441 LiNode->Children.emplace_back(
genReference(R, ParentPath));
442 ULBody->Children.emplace_back(std::move(LiNode));
447static std::unique_ptr<TagNode>
451 return std::make_unique<TagNode>(
452 HTMLTag::TAG_P,
"Defined at line " + std::to_string(L.LineNumber) +
453 " of file " + L.Filename);
455 llvm::sys::path::append(FileURL, llvm::sys::path::Style::posix, L.Filename);
456 auto Node = std::make_unique<TagNode>(HTMLTag::TAG_P);
457 Node->Children.emplace_back(std::make_unique<TextNode>(
"Defined at line "));
459 std::make_unique<TagNode>(HTMLTag::TAG_A, std::to_string(L.LineNumber));
462 LocNumberNode->Attributes.emplace_back(
463 "href", (FileURL +
"#" + std::to_string(L.LineNumber)).str());
464 Node->Children.emplace_back(std::move(LocNumberNode));
465 Node->Children.emplace_back(std::make_unique<TextNode>(
" of file "));
466 auto LocFileNode = std::make_unique<TagNode>(
467 HTMLTag::TAG_A, llvm::sys::path::filename(FileURL));
468 LocFileNode->Attributes.emplace_back(
"href", std::string(FileURL));
469 Node->Children.emplace_back(std::move(LocFileNode));
473static std::vector<std::unique_ptr<TagNode>>
474genHTML(
const Index &Index, StringRef InfoPath,
bool IsOutermostList);
479static std::vector<std::unique_ptr<TagNode>>
482 std::vector<std::unique_ptr<TagNode>>
Out;
483 auto MetaNode = std::make_unique<TagNode>(HTMLTag::TAG_META);
484 MetaNode->Attributes.emplace_back(
"charset",
"utf-8");
485 Out.emplace_back(std::move(MetaNode));
486 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_TITLE, Title));
487 std::vector<std::unique_ptr<TagNode>> StylesheetsNodes =
490 std::vector<std::unique_ptr<TagNode>> JsNodes =
499 auto HeaderNode = std::make_unique<TagNode>(HTMLTag::TAG_HEADER,
ProjectName);
500 HeaderNode->Attributes.emplace_back(
"id",
"project-title");
510 std::vector<std::unique_ptr<TagNode>> &MainContentInnerNodes,
511 const Index &InfoIndex) {
512 auto MainNode = std::make_unique<TagNode>(HTMLTag::TAG_MAIN);
514 auto LeftSidebarNode = std::make_unique<TagNode>(HTMLTag::TAG_DIV);
515 LeftSidebarNode->Attributes.emplace_back(
"id",
"sidebar-left");
516 LeftSidebarNode->Attributes.emplace_back(
"path", std::string(InfoPath));
517 LeftSidebarNode->Attributes.emplace_back(
518 "class",
"col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left");
520 auto MainContentNode = std::make_unique<TagNode>(HTMLTag::TAG_DIV);
521 MainContentNode->Attributes.emplace_back(
"id",
"main-content");
522 MainContentNode->Attributes.emplace_back(
523 "class",
"col-xs-12 col-sm-9 col-md-8 main-content");
524 appendVector(std::move(MainContentInnerNodes), MainContentNode->Children);
526 auto RightSidebarNode = std::make_unique<TagNode>(HTMLTag::TAG_DIV);
527 RightSidebarNode->Attributes.emplace_back(
"id",
"sidebar-right");
528 RightSidebarNode->Attributes.emplace_back(
529 "class",
"col-xs-6 col-sm-6 col-md-2 sidebar sidebar-offcanvas-right");
530 std::vector<std::unique_ptr<TagNode>> InfoIndexHTML =
531 genHTML(InfoIndex, InfoPath,
true);
532 appendVector(std::move(InfoIndexHTML), RightSidebarNode->Children);
534 MainNode->Children.emplace_back(std::move(LeftSidebarNode));
535 MainNode->Children.emplace_back(std::move(MainContentNode));
536 MainNode->Children.emplace_back(std::move(RightSidebarNode));
544 auto FooterNode = std::make_unique<TagNode>(HTMLTag::TAG_FOOTER);
545 auto SpanNode = std::make_unique<TagNode>(
546 HTMLTag::TAG_SPAN, clang::getClangToolFullVersion(
"clang-doc"));
547 SpanNode->Attributes.emplace_back(
"class",
"no-break");
548 FooterNode->Children.emplace_back(std::move(SpanNode));
555 std::vector<std::unique_ptr<TagNode>> &MainContentNodes,
559 std::vector<std::unique_ptr<TagNode>> HeadNodes =
562 std::unique_ptr<TagNode> MainNode =
567 F.Children.emplace_back(std::move(HeaderNode));
568 F.Children.emplace_back(std::move(MainNode));
569 F.Children.emplace_back(std::move(FooterNode));
575 typename = std::enable_if<std::is_base_of<T, Info>::value>>
577 Index Idx(Title, Title);
578 for (
const auto &
C : Infos)
579 Idx.
Children.emplace_back(
C.extractName(),
580 llvm::toHex(llvm::toStringRef(
C.USR)));
584static std::vector<std::unique_ptr<TagNode>>
586 std::vector<std::unique_ptr<TagNode>>
Out;
588 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_SPAN));
589 auto &SpanBody =
Out.back();
593 SpanBody->Children.emplace_back(
599 HTMLTag ListHTMLTag = IsOutermostList ? HTMLTag::TAG_OL : HTMLTag::TAG_UL;
600 Out.emplace_back(std::make_unique<TagNode>(ListHTMLTag));
601 const auto &UlBody =
Out.back();
603 auto LiBody = std::make_unique<TagNode>(HTMLTag::TAG_LI);
604 std::vector<std::unique_ptr<TagNode>> Nodes =
genHTML(
C, InfoPath,
false);
606 UlBody->Children.emplace_back(std::move(LiBody));
612 if (I.
Kind ==
"FullComment") {
613 auto FullComment = std::make_unique<TagNode>(HTMLTag::TAG_DIV);
614 for (
const auto &Child : I.
Children) {
617 FullComment->Children.emplace_back(std::move(
Node));
619 return std::move(FullComment);
622 if (I.
Kind ==
"ParagraphComment") {
623 auto ParagraphComment = std::make_unique<TagNode>(HTMLTag::TAG_P);
624 for (
const auto &Child : I.
Children) {
627 ParagraphComment->Children.emplace_back(std::move(
Node));
629 if (ParagraphComment->Children.empty())
631 return std::move(ParagraphComment);
634 if (I.
Kind ==
"TextComment") {
637 return std::make_unique<TextNode>(I.
Text);
642static std::unique_ptr<TagNode>
genHTML(
const std::vector<CommentInfo> &
C) {
643 auto CommentBlock = std::make_unique<TagNode>(HTMLTag::TAG_DIV);
644 for (
const auto &Child :
C) {
645 if (std::unique_ptr<HTMLNode>
Node =
genHTML(Child))
646 CommentBlock->Children.emplace_back(std::move(
Node));
651static std::vector<std::unique_ptr<TagNode>>
653 std::vector<std::unique_ptr<TagNode>>
Out;
654 std::string EnumType = I.
Scoped ?
"enum class " :
"enum ";
657 std::make_unique<TagNode>(HTMLTag::TAG_H3, EnumType + I.
Name));
658 Out.back()->Attributes.emplace_back(
"id",
659 llvm::toHex(llvm::toStringRef(I.
USR)));
663 Out.emplace_back(std::move(
Node));
670 *I.
DefLoc, StringRef{*CDCtx.RepositoryUrl}));
673 std::string Description;
680static std::vector<std::unique_ptr<TagNode>>
682 StringRef ParentInfoDir) {
683 std::vector<std::unique_ptr<TagNode>>
Out;
684 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_H3, I.
Name));
687 Out.back()->Attributes.emplace_back(
"id",
688 llvm::toHex(llvm::toStringRef(I.
USR)));
690 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_P));
691 auto &FunctionHeader =
Out.back();
693 std::string Access = getAccessSpelling(I.
Access).str();
695 FunctionHeader->Children.emplace_back(
696 std::make_unique<TextNode>(Access +
" "));
698 FunctionHeader->Children.emplace_back(
700 FunctionHeader->Children.emplace_back(std::make_unique<TextNode>(
" "));
702 FunctionHeader->Children.emplace_back(
703 std::make_unique<TextNode>(I.
Name +
"("));
705 for (
const auto &P : I.
Params) {
706 if (&P != I.
Params.begin())
707 FunctionHeader->Children.emplace_back(std::make_unique<TextNode>(
", "));
708 FunctionHeader->Children.emplace_back(
genReference(P.Type, ParentInfoDir));
709 FunctionHeader->Children.emplace_back(
710 std::make_unique<TextNode>(
" " + P.Name));
712 FunctionHeader->Children.emplace_back(std::make_unique<TextNode>(
")"));
719 *I.
DefLoc, StringRef{*CDCtx.RepositoryUrl}));
722 std::string Description;
729static std::vector<std::unique_ptr<TagNode>>
731 std::string &InfoTitle) {
732 std::vector<std::unique_ptr<TagNode>>
Out;
733 if (I.
Name.str() ==
"")
734 InfoTitle =
"Global Namespace";
736 InfoTitle = (
"namespace " + I.
Name).str();
738 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_H1, InfoTitle));
740 std::string Description;
746 std::vector<std::unique_ptr<TagNode>> ChildNamespaces =
749 std::vector<std::unique_ptr<TagNode>> ChildRecords =
753 std::vector<std::unique_ptr<TagNode>> ChildFunctions =
756 std::vector<std::unique_ptr<TagNode>> ChildEnums =
761 InfoIndex.
Children.emplace_back(
"Namespaces",
"Namespaces");
763 InfoIndex.
Children.emplace_back(
"Records",
"Records");
774static std::vector<std::unique_ptr<TagNode>>
776 std::string &InfoTitle) {
777 std::vector<std::unique_ptr<TagNode>>
Out;
779 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_H1, InfoTitle));
786 *I.
DefLoc, StringRef{*CDCtx.RepositoryUrl}));
789 std::string Description;
793 std::vector<std::unique_ptr<HTMLNode>> Parents =
795 std::vector<std::unique_ptr<HTMLNode>> VParents =
797 if (!Parents.empty() || !VParents.empty()) {
798 Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_P));
799 auto &PBody =
Out.back();
800 PBody->Children.emplace_back(std::make_unique<TextNode>(
"Inherits from "));
803 else if (VParents.empty())
807 PBody->Children.emplace_back(std::make_unique<TextNode>(
", "));
812 std::vector<std::unique_ptr<TagNode>> Members =
815 std::vector<std::unique_ptr<TagNode>> ChildRecords =
819 std::vector<std::unique_ptr<TagNode>> ChildFunctions =
822 std::vector<std::unique_ptr<TagNode>> ChildEnums =
827 InfoIndex.
Children.emplace_back(
"Members",
"Members");
829 InfoIndex.
Children.emplace_back(
"Records",
"Records");
840static std::vector<std::unique_ptr<TagNode>>
842 std::string &InfoTitle) {
853 llvm::StringMap<std::unique_ptr<doc::Info>> Infos,
864 llvm::StringMap<std::unique_ptr<doc::Info>> Infos,
867 llvm::StringSet<> CreatedDirs;
870 llvm::StringMap<std::vector<doc::Info *>> FileToInfos;
871 for (
const auto &Group : Infos) {
874 llvm::SmallString<128>
Path;
875 llvm::sys::path::native(RootDir,
Path);
877 if (!CreatedDirs.contains(
Path)) {
878 if (std::error_code Err = llvm::sys::fs::create_directories(
Path);
879 Err != std::error_code()) {
880 return llvm::createStringError(Err,
"Failed to create directory '%s'.",
883 CreatedDirs.insert(
Path);
890 for (
const auto &Group : FileToInfos) {
891 std::error_code FileErr;
892 llvm::raw_fd_ostream InfoOS(Group.getKey(), FileErr,
893 llvm::sys::fs::OF_None);
895 return llvm::createStringError(FileErr,
"Error opening file '%s'",
896 Group.getKey().str().c_str());
905 for (
const auto &
Info : Group.getValue()) {
912 return llvm::Error::success();
917 std::string InfoTitle;
918 std::vector<std::unique_ptr<TagNode>> MainContentNodes;
923 InfoIndex, CDCtx, InfoTitle);
927 InfoIndex, CDCtx, InfoTitle);
941 return llvm::createStringError(llvm::inconvertibleErrorCode(),
942 "unexpected info type");
946 MainContentNodes, InfoIndex, CDCtx);
949 return llvm::Error::success();
967 llvm_unreachable(
"Unknown InfoType");
972 std::error_code FileErr;
973 llvm::SmallString<128> FilePath;
975 llvm::sys::path::append(FilePath,
"index_json.js");
976 llvm::raw_fd_ostream
OS(FilePath, FileErr, llvm::sys::fs::OF_None);
978 return llvm::createStringError(llvm::inconvertibleErrorCode(),
979 "error creating index file: " +
983 llvm::json::OStream J(
OS, 2);
984 std::function<void(
Index)> IndexToJSON = [&](
const Index &I) {
986 J.attribute(
"USR", toHex(llvm::toStringRef(I.USR)));
987 J.attribute(
"Name", I.Name);
988 J.attribute(
"RefType",
getRefType(I.RefType));
989 J.attribute(
"Path", I.getRelativeFilePath(
""));
990 J.attributeArray(
"Children", [&] {
991 for (
const Index &
C : I.Children)
996 OS <<
"async function LoadIndex() {\nreturn";
997 IndexToJSON(CDCtx.
Idx);
999 return llvm::Error::success();
1006 auto MainNode = std::make_unique<TagNode>(HTMLTag::TAG_MAIN);
1008 auto LeftSidebarNode = std::make_unique<TagNode>(HTMLTag::TAG_DIV);
1009 LeftSidebarNode->Attributes.emplace_back(
"id",
"sidebar-left");
1010 LeftSidebarNode->Attributes.emplace_back(
"path",
"");
1011 LeftSidebarNode->Attributes.emplace_back(
1012 "class",
"col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left");
1013 LeftSidebarNode->Attributes.emplace_back(
"style",
"flex: 0 100%;");
1015 MainNode->Children.emplace_back(std::move(LeftSidebarNode));
1021 std::error_code FileErr, OK;
1022 llvm::SmallString<128> IndexPath;
1023 llvm::sys::path::native(CDCtx.
OutDirectory, IndexPath);
1024 llvm::sys::path::append(IndexPath,
"index.html");
1025 llvm::raw_fd_ostream IndexOS(IndexPath, FileErr, llvm::sys::fs::OF_None);
1026 if (FileErr != OK) {
1027 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1028 "error creating main index: " +
1034 std::vector<std::unique_ptr<TagNode>> HeadNodes =
1041 F.Children.emplace_back(std::move(HeaderNode));
1042 F.Children.emplace_back(std::move(MainNode));
1043 F.Children.emplace_back(std::move(FooterNode));
1047 return llvm::Error::success();
1051 llvm::SmallString<128> PathWrite;
1053 llvm::sys::path::append(PathWrite, llvm::sys::path::filename(FilePath));
1054 llvm::SmallString<128> PathRead;
1055 llvm::sys::path::native(FilePath, PathRead);
1057 std::error_code FileErr = llvm::sys::fs::copy_file(PathRead, PathWrite);
1058 if (FileErr != OK) {
1059 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1060 "error creating file " +
1061 llvm::sys::path::filename(FilePath) +
1062 ": " + FileErr.message() +
"\n");
1064 return llvm::Error::success();
1080 for (
const auto &FilePath : CDCtx.
JsScripts) {
1085 return llvm::Error::success();
1089 "Generator for HTML output.");
static llvm::cl::opt< std::string > ProjectName("project-name", llvm::cl::desc("Name of project."), llvm::cl::cat(ClangDocCategory))
static llvm::cl::opt< std::string > OutDirectory("output", llvm::cl::desc("Directory for outputting generated files."), llvm::cl::init("docs"), llvm::cl::cat(ClangDocCategory))
static llvm::cl::opt< std::string > RepositoryUrl("repository", llvm::cl::desc(R"(
URL of repository that hosts code.
Used for links to definition locations.)"), llvm::cl::cat(ClangDocCategory))
CompiledFragmentImpl & Out
std::vector< std::pair< std::string, std::string > > Attributes
std::vector< std::unique_ptr< HTMLNode > > Children
std::vector< HeaderHandle > Path
::clang::DynTypedNode Node
const google::protobuf::Message & M
llvm::raw_string_ostream OS
Generator for HTML documentation.
static const char * Format
llvm::Error createResources(ClangDocContext &CDCtx) override
llvm::Error generateDocs(StringRef RootDir, llvm::StringMap< std::unique_ptr< doc::Info > > Infos, const ClangDocContext &CDCtx) override
llvm::Error generateDocForInfo(Info *I, llvm::raw_ostream &OS, const ClangDocContext &CDCtx) override
static std::unique_ptr< TagNode > genInfoFileMainNode(StringRef InfoPath, std::vector< std::unique_ptr< TagNode > > &MainContentInnerNodes, const Index &InfoIndex)
static std::vector< std::unique_ptr< TagNode > > genHTML(const EnumInfo &I, const ClangDocContext &CDCtx)
static std::unique_ptr< HTMLNode > genReference(const Reference &Type, StringRef CurrentDirectory, std::optional< StringRef > JumpToSection=std::nullopt)
std::string getTagType(TagTypeKind AS)
static std::vector< std::unique_ptr< TagNode > > genStylesheetsHTML(StringRef InfoPath, const ClangDocContext &CDCtx)
static std::vector< std::unique_ptr< HTMLNode > > genReferenceList(const llvm::SmallVectorImpl< Reference > &Refs, const StringRef &CurrentDirectory)
static std::vector< std::unique_ptr< TagNode > > genRecordMembersBlock(const llvm::SmallVector< MemberTypeInfo, 4 > &Members, StringRef ParentInfoDir)
static std::vector< std::unique_ptr< TagNode > > genReferencesBlock(const std::vector< Reference > &References, llvm::StringRef Title, StringRef ParentPath)
static std::unique_ptr< TagNode > genFileHeaderNode(StringRef ProjectName)
static llvm::Error serializeIndex(ClangDocContext &CDCtx)
static std::unique_ptr< TagNode > genLink(const Twine &Text, const Twine &Link)
static GeneratorRegistry::Add< HTMLGenerator > HTML(HTMLGenerator::Format, "Generator for HTML output.")
static std::unique_ptr< TagNode > genFileFooterNode()
volatile int HTMLGeneratorAnchorSource
static std::vector< std::unique_ptr< TagNode > > genFileHeadNodes(StringRef Title, StringRef InfoPath, const ClangDocContext &CDCtx)
static llvm::Error genIndex(const ClangDocContext &CDCtx)
static Index genInfoIndexItem(const std::vector< T > &Infos, StringRef Title)
static std::unique_ptr< TagNode > writeFileDefinition(const Location &L, std::optional< StringRef > RepositoryUrl=std::nullopt)
static std::string getRefType(InfoType IT)
static llvm::Error copyFile(StringRef FilePath, StringRef OutDirectory)
static std::vector< std::unique_ptr< TagNode > > genJsScriptsHTML(StringRef InfoPath, const ClangDocContext &CDCtx)
static HTMLFile genInfoFile(StringRef Title, StringRef InfoPath, std::vector< std::unique_ptr< TagNode > > &MainContentNodes, const Index &InfoIndex, const ClangDocContext &CDCtx)
static std::unique_ptr< TagNode > genIndexFileMainNode()
static std::vector< std::unique_ptr< TagNode > > genFunctionsBlock(const std::vector< FunctionInfo > &Functions, const ClangDocContext &CDCtx, StringRef ParentInfoDir)
static std::unique_ptr< TagNode > genEnumMembersBlock(const llvm::SmallVector< EnumValueInfo, 4 > &Members)
static std::vector< std::unique_ptr< TagNode > > genEnumsBlock(const std::vector< EnumInfo > &Enums, const ClangDocContext &CDCtx)
static void appendVector(std::vector< Derived > &&New, std::vector< Base > &Original)
static SmallString< 128 > computeRelativePath(StringRef Destination, StringRef Origin)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
std::optional< std::string > RepositoryUrl
std::vector< std::string > UserStylesheets
std::vector< std::string > JsScripts
llvm::SmallVector< EnumValueInfo, 4 > Members
llvm::SmallVector< FieldTypeInfo, 4 > Params
std::optional< SmallString< 16 > > JumpToSection
std::vector< Index > Children
llvm::SmallString< 16 > getFileBaseName() const
Returns the basename that should be used for this Info.
std::vector< CommentInfo > Description
llvm::SmallString< 128 > Path
llvm::SmallString< 64 > getRelativeFilePath(const StringRef &CurrentPath) const
Returns the file path for this Info relative to CurrentPath.
llvm::SmallVector< MemberTypeInfo, 4 > Members
llvm::SmallVector< Reference, 4 > VirtualParents
llvm::SmallVector< Reference, 4 > Parents
std::vector< Reference > Records
std::vector< FunctionInfo > Functions
std::vector< Reference > Namespaces
std::vector< EnumInfo > Enums
std::optional< Location > DefLoc