[clang-tools-extra] [llvm] [WIP] add a mustache backend (PR #108653)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 13 15:02:07 PDT 2024
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 62ae01201fc7185aca61d040c4f2df5303bae0ad 8d141ba80297ce024b0144f2522f7d662bf36568 --extensions cpp,h -- clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp llvm/include/llvm/Support/Mustache.h llvm/lib/Support/Mustache.cpp llvm/unittests/Support/MustacheTest.cpp clang-tools-extra/clang-doc/Generators.cpp clang-tools-extra/clang-doc/HTMLGenerator.cpp clang-tools-extra/clang-doc/Representation.h clang-tools-extra/clang-doc/tool/ClangDocMain.cpp clang-tools-extra/test/clang-doc/Inputs/basic-project/include/Shape.h
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
index ed65ac9651..e3532559a3 100644
--- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -1133,8 +1133,7 @@ static llvm::Error genIndex(const ClangDocContext &CDCtx) {
return llvm::Error::success();
}
-static llvm::Error
-copyFile(StringRef FilePath, StringRef OutDirectory) {
+static llvm::Error copyFile(StringRef FilePath, StringRef OutDirectory) {
llvm::SmallString<128> PathWrite;
llvm::sys::path::native(OutDirectory, PathWrite);
llvm::sys::path::append(PathWrite, llvm::sys::path::filename(FilePath));
diff --git a/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp b/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp
index bfa563ebdc..e8f3d2b78e 100644
--- a/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp
@@ -17,7 +17,6 @@ using namespace llvm::mustache;
namespace clang {
namespace doc {
-
class MustacheHTMLGenerator : public Generator {
public:
static const char *Format;
@@ -27,16 +26,15 @@ public:
llvm::Error createResources(ClangDocContext &CDCtx) override;
llvm::Error generateDocForInfo(Info *I, llvm::raw_ostream &OS,
const ClangDocContext &CDCtx) override;
-
};
class MustacheTemplateFile : public Template {
public:
- static ErrorOr<std::unique_ptr<MustacheTemplateFile>> createMustacheFile
- (StringRef FileName) {
+ static ErrorOr<std::unique_ptr<MustacheTemplateFile>>
+ createMustacheFile(StringRef FileName) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrError =
MemoryBuffer::getFile(FileName);
-
+
if (auto EC = BufferOrError.getError()) {
return EC;
}
@@ -44,7 +42,7 @@ public:
llvm::StringRef FileContent = Buffer->getBuffer();
return std::make_unique<MustacheTemplateFile>(FileContent);
}
-
+
Error registerPartialFile(StringRef Name, StringRef FileName) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrError =
MemoryBuffer::getFile(FileName);
@@ -64,9 +62,7 @@ static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
-
-llvm::Error
-setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
+llvm::Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
auto TemplateFilePath = CDCtx.MustacheTemplates.lookup("template");
auto Template = MustacheTemplateFile::createMustacheFile(TemplateFilePath);
if (auto EC = Template.getError()) {
@@ -76,10 +72,9 @@ setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
return llvm::Error::success();
}
-llvm::Error
-MustacheHTMLGenerator::generateDocs(llvm::StringRef RootDir,
- llvm::StringMap<std::unique_ptr<doc::Info>> Infos,
- const clang::doc::ClangDocContext &CDCtx) {
+llvm::Error MustacheHTMLGenerator::generateDocs(
+ llvm::StringRef RootDir, llvm::StringMap<std::unique_ptr<doc::Info>> Infos,
+ const clang::doc::ClangDocContext &CDCtx) {
if (auto Err = setupTemplateFiles(CDCtx)) {
return Err;
}
@@ -89,7 +84,7 @@ MustacheHTMLGenerator::generateDocs(llvm::StringRef RootDir,
llvm::StringMap<std::vector<doc::Info *>> FileToInfos;
for (const auto &Group : Infos) {
doc::Info *Info = Group.getValue().get();
-
+
llvm::SmallString<128> Path;
llvm::sys::path::native(RootDir, Path);
llvm::sys::path::append(Path, Info->getRelativeFilePath(""));
@@ -105,7 +100,7 @@ MustacheHTMLGenerator::generateDocs(llvm::StringRef RootDir,
llvm::sys::path::append(Path, Info->getFileBaseName() + ".html");
FileToInfos[Path].push_back(Info);
}
-
+
for (const auto &Group : FileToInfos) {
std::error_code FileErr;
llvm::raw_fd_ostream InfoOS(Group.getKey(), FileErr,
@@ -123,7 +118,7 @@ MustacheHTMLGenerator::generateDocs(llvm::StringRef RootDir,
return llvm::Error::success();
}
-Value extractValue(const Location &L,
+Value extractValue(const Location &L,
std::optional<StringRef> RepositoryUrl = std::nullopt) {
Object Obj = Object();
if (!L.IsFileInRootDir || !RepositoryUrl) {
@@ -133,7 +128,7 @@ Value extractValue(const Location &L,
SmallString<128> FileURL(*RepositoryUrl);
llvm::sys::path::append(FileURL, llvm::sys::path::Style::posix, L.Filename);
Obj.insert({"FileURL", FileURL});
-
+
return Obj;
}
@@ -147,7 +142,6 @@ Value extractValue(const Reference &I, StringRef CurrentDirectory) {
return Obj;
}
-
Value extractValue(const TypedefInfo &I) {
// Not Supported
return nullptr;
@@ -156,17 +150,17 @@ Value extractValue(const TypedefInfo &I) {
Value extractValue(const CommentInfo &I) {
Object Obj = Object();
Value Child = Object();
-
+
if (I.Kind == "FullComment") {
Value ChildArr = Array();
- for (const auto& C: I.Children)
+ for (const auto &C : I.Children)
ChildArr.getAsArray()->emplace_back(extractValue(*C));
Child.getAsObject()->insert({"Children", ChildArr});
Obj.insert({"FullComment", Child});
}
if (I.Kind == "ParagraphComment") {
Value ChildArr = Array();
- for (const auto& C: I.Children)
+ for (const auto &C : I.Children)
ChildArr.getAsArray()->emplace_back(extractValue(*C));
Child.getAsObject()->insert({"Children", ChildArr});
Obj.insert({"ParagraphComment", Child});
@@ -174,14 +168,14 @@ Value extractValue(const CommentInfo &I) {
if (I.Kind == "BlockCommandComment") {
Child.getAsObject()->insert({"Command", I.Name});
Value ChildArr = Array();
- for (const auto& C: I.Children)
+ for (const auto &C : I.Children)
ChildArr.getAsArray()->emplace_back(extractValue(*C));
Child.getAsObject()->insert({"Children", ChildArr});
Obj.insert({"BlockCommandComment", Child});
}
if (I.Kind == "TextComment")
Obj.insert({"TextComment", I.Text});
-
+
return Obj;
}
@@ -192,16 +186,16 @@ Value extractValue(const FunctionInfo &I, StringRef ParentInfoDir,
Obj.insert({"ID", llvm::toHex(llvm::toStringRef(I.USR))});
Obj.insert({"Access", getAccessSpelling(I.Access).str()});
Obj.insert({"ReturnType", extractValue(I.ReturnType.Type, ParentInfoDir)});
-
+
Value ParamArr = Array();
for (const auto &P : I.Params) {
ParamArr.getAsArray()->emplace_back(extractValue(P.Type, ParentInfoDir));
}
Obj.insert({"Params", ParamArr});
-
+
if (!I.Description.empty()) {
Value ArrDesc = Array();
- for (const CommentInfo& Child : I.Description)
+ for (const CommentInfo &Child : I.Description)
ArrDesc.getAsArray()->emplace_back(extractValue(Child));
Obj.insert({"FunctionComments", ArrDesc});
}
@@ -209,8 +203,8 @@ Value extractValue(const FunctionInfo &I, StringRef ParentInfoDir,
if (!CDCtx.RepositoryUrl)
Obj.insert({"Location", extractValue(*I.DefLoc)});
else
- Obj.insert({"Location", extractValue(*I.DefLoc,
- StringRef{*CDCtx.RepositoryUrl})});
+ Obj.insert({"Location",
+ extractValue(*I.DefLoc, StringRef{*CDCtx.RepositoryUrl})});
}
return Obj;
}
@@ -225,41 +219,41 @@ Value extractValue(const EnumInfo &I, const ClangDocContext &CDCtx) {
Obj.insert({"EnumName", EnumType});
Obj.insert({"HasComment", HasComment});
Obj.insert({"ID", llvm::toHex(llvm::toStringRef(I.USR))});
-
+
Value Arr = Array();
- for (const EnumValueInfo& M: I.Members) {
+ for (const EnumValueInfo &M : I.Members) {
Value EnumValue = Object();
EnumValue.getAsObject()->insert({"Name", M.Name});
if (!M.ValueExpr.empty())
EnumValue.getAsObject()->insert({"ValueExpr", M.ValueExpr});
else
EnumValue.getAsObject()->insert({"Value", M.Value});
-
+
if (!M.Description.empty()) {
Value ArrDesc = Array();
- for (const CommentInfo& Child : M.Description)
+ for (const CommentInfo &Child : M.Description)
ArrDesc.getAsArray()->emplace_back(extractValue(Child));
EnumValue.getAsObject()->insert({"EnumValueComments", ArrDesc});
}
Arr.getAsArray()->emplace_back(EnumValue);
}
Obj.insert({"EnumValues", Arr});
-
+
if (!I.Description.empty()) {
Value ArrDesc = Array();
- for (const CommentInfo& Child : I.Description)
+ for (const CommentInfo &Child : I.Description)
ArrDesc.getAsArray()->emplace_back(extractValue(Child));
Obj.insert({"EnumComments", ArrDesc});
}
-
+
if (I.DefLoc) {
if (!CDCtx.RepositoryUrl)
Obj.insert({"Location", extractValue(*I.DefLoc)});
else
- Obj.insert({"Location", extractValue(*I.DefLoc,
- StringRef{*CDCtx.RepositoryUrl})});
+ Obj.insert({"Location",
+ extractValue(*I.DefLoc, StringRef{*CDCtx.RepositoryUrl})});
}
-
+
return Obj;
}
@@ -269,59 +263,57 @@ Value extractValue(const NamespaceInfo &I, const ClangDocContext &CDCtx) {
if (I.Name.str() == "")
InfoTitle = "Global Namespace";
else
- InfoTitle = ("namespace " + I.Name).str();
-
+ InfoTitle = ("namespace " + I.Name).str();
+
StringRef BasePath = I.getRelativeFilePath("");
NamespaceValue.insert({"NamespaceTitle", InfoTitle});
NamespaceValue.insert({"NamespacePath", I.getRelativeFilePath("")});
-
+
if (!I.Description.empty()) {
Value ArrDesc = Array();
- for (const CommentInfo& Child : I.Description)
+ for (const CommentInfo &Child : I.Description)
ArrDesc.getAsArray()->emplace_back(extractValue(Child));
- NamespaceValue.insert({"NamespaceComments", ArrDesc });
+ NamespaceValue.insert({"NamespaceComments", ArrDesc});
}
Value ArrNamespace = Array();
- for (const Reference& Child : I.Children.Namespaces)
+ for (const Reference &Child : I.Children.Namespaces)
ArrNamespace.getAsArray()->emplace_back(extractValue(Child, BasePath));
-
+
if (!ArrNamespace.getAsArray()->empty())
NamespaceValue.insert({"Namespace", Object{{"Links", ArrNamespace}}});
-
+
Value ArrRecord = Array();
- for (const Reference& Child : I.Children.Records)
+ for (const Reference &Child : I.Children.Records)
ArrRecord.getAsArray()->emplace_back(extractValue(Child, BasePath));
-
+
if (!ArrRecord.getAsArray()->empty())
NamespaceValue.insert({"Record", Object{{"Links", ArrRecord}}});
-
+
Value ArrFunction = Array();
- for (const FunctionInfo& Child : I.Children.Functions)
- ArrFunction.getAsArray()->emplace_back(extractValue(Child, BasePath,
- CDCtx));
+ for (const FunctionInfo &Child : I.Children.Functions)
+ ArrFunction.getAsArray()->emplace_back(
+ extractValue(Child, BasePath, CDCtx));
if (!ArrFunction.getAsArray()->empty())
NamespaceValue.insert({"Function", Object{{"Obj", ArrFunction}}});
-
+
Value ArrEnum = Array();
- for (const EnumInfo& Child : I.Children.Enums)
+ for (const EnumInfo &Child : I.Children.Enums)
ArrEnum.getAsArray()->emplace_back(extractValue(Child, CDCtx));
-
+
if (!ArrEnum.getAsArray()->empty())
- NamespaceValue.insert({"Enums", Object{{"Obj", ArrEnum }}});
-
+ NamespaceValue.insert({"Enums", Object{{"Obj", ArrEnum}}});
+
Value ArrTypedefs = Array();
- for (const TypedefInfo& Child : I.Children.Typedefs)
+ for (const TypedefInfo &Child : I.Children.Typedefs)
ArrTypedefs.getAsArray()->emplace_back(extractValue(Child));
-
+
if (!ArrTypedefs.getAsArray()->empty())
- NamespaceValue.insert({"Typedefs", Object{{"Obj", ArrTypedefs }}});
-
+ NamespaceValue.insert({"Typedefs", Object{{"Obj", ArrTypedefs}}});
+
return NamespaceValue;
}
-
-
llvm::Error
MustacheHTMLGenerator::generateDocForInfo(Info *I, llvm::raw_ostream &OS,
const ClangDocContext &CDCtx) {
@@ -353,9 +345,8 @@ llvm::Error MustacheHTMLGenerator::createResources(ClangDocContext &CDCtx) {
const char *MustacheHTMLGenerator::Format = "mhtml";
-
-static GeneratorRegistry::Add<MustacheHTMLGenerator> MHTML(MustacheHTMLGenerator::Format,
- "Generator for mustache HTML output.");
+static GeneratorRegistry::Add<MustacheHTMLGenerator>
+ MHTML(MustacheHTMLGenerator::Format, "Generator for mustache HTML output.");
// This anchor is used to force the linker to link in the generated object
// file and thus register the generator.
diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index eee8dbf093..8a2620c651 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -99,12 +99,7 @@ URL of repository that hosts code.
Used for links to definition locations.)"),
llvm::cl::cat(ClangDocCategory));
-enum OutputFormatTy {
- md,
- yaml,
- html,
- mhtml
-};
+enum OutputFormatTy { md, yaml, html, mhtml };
static llvm::cl::opt<OutputFormatTy>
FormatEnum("format", llvm::cl::desc("Format for outputted docs."),
@@ -218,21 +213,21 @@ llvm::Error getMustacheHtmlFiles(const char *Argv0,
<< " falling back to default\n";
if (llvm::sys::fs::is_directory(std::string(UserAssetPath)))
return getAssetFiles(CDCtx);
-
+
void *MainAddr = (void *)(intptr_t)getExecutablePath;
std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);
llvm::SmallString<128> NativeClangDocPath;
llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
-
+
llvm::SmallString<128> AssetsPath;
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
llvm::sys::path::append(AssetsPath, "..", "share", "clang-doc");
-
+
llvm::SmallString<128> MustacheTemplate;
llvm::sys::path::native(AssetsPath, MustacheTemplate);
llvm::sys::path::append(MustacheTemplate, "template.mustache");
CDCtx.MustacheTemplates.insert({"template", MustacheTemplate.c_str()});
-
+
return llvm::Error::success();
}
@@ -308,7 +303,7 @@ Example usage for a project using a compile commands database:
return 1;
}
}
-
+
if (Format == "mhtml") {
if (auto Err = getMustacheHtmlFiles(argv[0], CDCtx)) {
llvm::errs() << toString(std::move(Err)) << "\n";
``````````
</details>
https://github.com/llvm/llvm-project/pull/108653
More information about the cfe-commits
mailing list