Index: clang-tools-extra/clang-doc/HTMLGenerator.cpp =================================================================== --- clang-tools-extra/clang-doc/HTMLGenerator.cpp +++ clang-tools-extra/clang-doc/HTMLGenerator.cpp @@ -252,13 +252,20 @@ return LinkNode; } -static std::unique_ptr genTypeReference(const Reference &Type, - StringRef CurrentDirectory) { - if (Type.Path.empty()) - return llvm::make_unique(Type.Name); +static std::unique_ptr +genTypeReference(const Reference &Type, StringRef CurrentDirectory, + StringRef JumpToSection = "") { + if (Type.Path.empty()) { + if (JumpToSection.empty()) + return llvm::make_unique(Type.Name); + else + return genLink(Type.Name, "#" + JumpToSection); + } llvm::SmallString<128> Path = computeRelativePath(Type.Path, CurrentDirectory); llvm::sys::path::append(Path, Type.Name + ".html"); + if (!JumpToSection.empty()) + Path += ("#" + JumpToSection).str(); return genLink(Type.Name, Path); } @@ -285,6 +292,7 @@ std::vector> Out; Out.emplace_back(llvm::make_unique(HTMLTag::TAG_H2, "Enums")); + Out.back()->Attributes.try_emplace("id", "Enums"); Out.emplace_back(llvm::make_unique(HTMLTag::TAG_DIV)); auto &DivBody = Out.back(); for (const auto &E : Enums) { @@ -313,6 +321,7 @@ std::vector> Out; Out.emplace_back(llvm::make_unique(HTMLTag::TAG_H2, "Functions")); + Out.back()->Attributes.try_emplace("id", "Functions"); Out.emplace_back(llvm::make_unique(HTMLTag::TAG_DIV)); auto &DivBody = Out.back(); for (const auto &F : Functions) { @@ -330,6 +339,7 @@ std::vector> Out; Out.emplace_back(llvm::make_unique(HTMLTag::TAG_H2, "Members")); + Out.back()->Attributes.try_emplace("id", "Members"); Out.emplace_back(llvm::make_unique(HTMLTag::TAG_UL)); auto &ULBody = Out.back(); for (const auto &M : Members) { @@ -353,6 +363,7 @@ std::vector> Out; Out.emplace_back(llvm::make_unique(HTMLTag::TAG_H2, Title)); + Out.back()->Attributes.try_emplace("id", Title); Out.emplace_back(llvm::make_unique(HTMLTag::TAG_UL)); auto &ULBody = Out.back(); for (const auto &R : References) @@ -377,13 +388,24 @@ return Out; } +template ::value>> +static Index genInfoIndexItem(const std::vector &Infos, StringRef Title) { + Index Idx(Title, Title); + for (const auto &C : Infos) + Idx.Children.emplace_back(C.extractName(), + llvm::toHex(llvm::toStringRef(C.USR))); + return Idx; +} + static std::vector> genHTML(const Index &Index, StringRef InfoPath) { std::vector> Out; if (!Index.Name.empty()) { Out.emplace_back(llvm::make_unique(HTMLTag::TAG_SPAN)); auto &SpanBody = Out.back(); - SpanBody->Children.emplace_back(genTypeReference(Index, InfoPath)); + SpanBody->Children.emplace_back( + genTypeReference(Index, InfoPath, Index.JumpToSection)); } if (Index.Children.empty()) return Out; @@ -444,6 +466,8 @@ Out.emplace_back( llvm::make_unique(HTMLTag::TAG_H3, EnumType + I.Name)); + Out.back()->Attributes.try_emplace("id", + llvm::toHex(llvm::toStringRef(I.USR))); std::unique_ptr Node = genEnumMembersBlock(I.Members); if (Node) @@ -463,6 +487,10 @@ StringRef ParentInfoDir) { std::vector> Out; Out.emplace_back(llvm::make_unique(HTMLTag::TAG_H3, I.Name)); + // USR is used as id for functions instead of name to disambiguate function + // overloads. + Out.back()->Attributes.try_emplace("id", + llvm::toHex(llvm::toStringRef(I.USR))); Out.emplace_back(llvm::make_unique(HTMLTag::TAG_P)); auto &FunctionHeader = Out.back(); @@ -499,8 +527,8 @@ return Out; } -static std::vector> genHTML(const NamespaceInfo &I, - std::string &InfoTitle) { +static std::vector> +genHTML(const NamespaceInfo &I, Index &InfoIndex, std::string &InfoTitle) { std::vector> Out; if (I.Name.str() == "") InfoTitle = "Global Namespace"; @@ -527,11 +555,21 @@ genEnumsBlock(I.ChildEnums); AppendVector(std::move(ChildEnums), Out); + if (!I.ChildNamespaces.empty()) + InfoIndex.Children.emplace_back("Namespaces", "Namespaces"); + if (!I.ChildRecords.empty()) + InfoIndex.Children.emplace_back("Records", "Records"); + if (!I.ChildFunctions.empty()) + InfoIndex.Children.emplace_back( + genInfoIndexItem(I.ChildFunctions, "Functions")); + if (!I.ChildEnums.empty()) + InfoIndex.Children.emplace_back(genInfoIndexItem(I.ChildEnums, "Enums")); + return Out; } -static std::vector> genHTML(const RecordInfo &I, - std::string &InfoTitle) { +static std::vector> +genHTML(const RecordInfo &I, Index &InfoIndex, std::string &InfoTitle) { std::vector> Out; InfoTitle = (getTagType(I.TagType) + " " + I.Name).str(); Out.emplace_back(llvm::make_unique(HTMLTag::TAG_H1, InfoTitle)); @@ -576,6 +614,16 @@ genEnumsBlock(I.ChildEnums); AppendVector(std::move(ChildEnums), Out); + if (!I.Members.empty()) + InfoIndex.Children.emplace_back("Members", "Members"); + if (!I.ChildRecords.empty()) + InfoIndex.Children.emplace_back("Records", "Records"); + if (!I.ChildFunctions.empty()) + InfoIndex.Children.emplace_back( + genInfoIndexItem(I.ChildFunctions, "Functions")); + if (!I.ChildEnums.empty()) + InfoIndex.Children.emplace_back(genInfoIndexItem(I.ChildEnums, "Enums")); + return Out; } @@ -595,16 +643,17 @@ HTMLFile F; std::string InfoTitle; auto MainContentNode = llvm::make_unique(HTMLTag::TAG_DIV); + Index InfoIndex; switch (I->IT) { case InfoType::IT_namespace: { - std::vector> Nodes = - genHTML(*static_cast(I), InfoTitle); + std::vector> Nodes = genHTML( + *static_cast(I), InfoIndex, InfoTitle); AppendVector(std::move(Nodes), MainContentNode->Children); break; } case InfoType::IT_record: { - std::vector> Nodes = - genHTML(*static_cast(I), InfoTitle); + std::vector> Nodes = genHTML( + *static_cast(I), InfoIndex, InfoTitle); AppendVector(std::move(Nodes), MainContentNode->Children); break; } @@ -628,8 +677,11 @@ std::vector> BasicNodes = genCommonFileNodes(InfoTitle); AppendVector(std::move(BasicNodes), F.Children); - std::vector> Index = genHTML(CDCtx.Idx, I->Path); - AppendVector(std::move(Index), F.Children); + std::vector> MainIndex = genHTML(CDCtx.Idx, I->Path); + AppendVector(std::move(MainIndex), F.Children); + std::vector> InfoIndexHTML = + genHTML(InfoIndex, I->Path); + AppendVector(std::move(InfoIndexHTML), F.Children); F.Children.emplace_back(std::move(MainContentNode)); F.Render(OS); Index: clang-tools-extra/clang-doc/Representation.h =================================================================== --- clang-tools-extra/clang-doc/Representation.h +++ clang-tools-extra/clang-doc/Representation.h @@ -239,7 +239,7 @@ void mergeBase(Info &&I); bool mergeable(const Info &Other); - llvm::SmallString<16> extractName(); + llvm::SmallString<16> extractName() const; // Returns a reference to the parent scope (that is, the immediate parent // namespace or class in which this decl resides). @@ -340,11 +340,14 @@ struct Index : public Reference { Index() = default; + Index(StringRef Name, StringRef JumpToSection) + : Reference(Name), JumpToSection(JumpToSection) {} Index(SymbolID USR, StringRef Name, InfoType IT, StringRef Path) : Reference(USR, Name, IT, Path) {} bool operator==(const SymbolID &Other) const { return USR == Other; } bool operator<(const Index &Other) const { return Name < Other.Name; } + SmallString<16> JumpToSection; std::vector Children; void sort(); Index: clang-tools-extra/clang-doc/Representation.cpp =================================================================== --- clang-tools-extra/clang-doc/Representation.cpp +++ clang-tools-extra/clang-doc/Representation.cpp @@ -197,7 +197,7 @@ SymbolInfo::merge(std::move(Other)); } -llvm::SmallString<16> Info::extractName() { +llvm::SmallString<16> Info::extractName() const { if (!Name.empty()) return Name; Index: clang-tools-extra/unittests/clang-doc/HTMLGeneratorTest.cpp =================================================================== --- clang-tools-extra/unittests/clang-doc/HTMLGeneratorTest.cpp +++ clang-tools-extra/unittests/clang-doc/HTMLGeneratorTest.cpp @@ -44,26 +44,36 @@ std::string Expected = R"raw( namespace Namespace + + Namespaces + Records + Functions + OneFunction + + Enums + OneEnum + + namespace Namespace - Namespaces + Namespaces ChildNamespace - Records + Records ChildStruct - Functions + Functions - OneFunction + OneFunction OneFunction() - Enums + Enums - enum OneEnum + enum OneEnum )raw"; @@ -106,6 +116,16 @@ std::string Expected = R"raw( class r + + Members + Records + Functions + OneFunction + + Enums + OneEnum + + class r @@ -117,25 +137,25 @@ R"raw(">F , G - Members + Members private int X - Records + Records ChildStruct - Functions + Functions - OneFunction + OneFunction OneFunction() - Enums + Enums - enum OneEnum + enum OneEnum )raw"; @@ -172,7 +192,7 @@ - f + f float @@ -211,7 +231,7 @@ - enum class e + enum class e X @@ -271,7 +291,7 @@ - f + f void f(int I, int J)
OneFunction()
@@ -117,25 +137,25 @@ R"raw(">F , G
float @@ -211,7 +231,7 @@
void f(int I, int J)