[clang-tools-extra] [clang-doc] add a JSON generator (PR #142483)
Paul Kirth via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 9 15:48:40 PDT 2025
================
@@ -0,0 +1,175 @@
+#include "ClangDocTest.h"
+#include "Generators.h"
+#include "Representation.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace doc {
+
+static std::unique_ptr<Generator> getJSONGenerator() {
+ auto G = doc::findGeneratorByName("json");
+ if (!G)
+ return nullptr;
+ return std::move(G.get());
+}
+
+TEST(JSONGeneratorTest, emitRecordJSON) {
+ RecordInfo I;
+ I.Name = "Foo";
+ // FIXME: FullName is not emitted correctly.
+ I.FullName = "";
+ I.IsTypeDef = false;
+ I.Namespace.emplace_back(EmptySID, "GlobalNamespace", InfoType::IT_namespace);
+ I.Path = "GlobalNamespace";
+ I.DefLoc = Location(1, 1, "main.cpp");
+ I.TagType = TagTypeKind::Class;
+
+ I.Template = TemplateInfo();
+ I.Template->Params.emplace_back("class T");
+
+ I.Children.Enums.emplace_back();
+ I.Children.Enums.back().Name = "Color";
+ I.Children.Enums.back().Scoped = false;
+ I.Children.Enums.back().Members.emplace_back();
+ I.Children.Enums.back().Members.back().Name = "RED";
+ I.Children.Enums.back().Members.back().Value = "0";
+
+ I.Members.emplace_back(TypeInfo("int"), "X", AccessSpecifier::AS_protected);
+
+ I.Bases.emplace_back(EmptySID, "F", "path/to/F", true,
+ AccessSpecifier::AS_public, true);
+ I.Bases.back().Children.Functions.emplace_back();
+ I.Bases.back().Children.Functions.back().Name = "InheritedFunctionOne";
+ I.Bases.back().Members.emplace_back(TypeInfo("int"), "N",
+ AccessSpecifier::AS_public);
+
+ // F is in the global namespace
+ I.Parents.emplace_back(EmptySID, "F", InfoType::IT_record, "");
+ I.VirtualParents.emplace_back(EmptySID, "G", InfoType::IT_record,
+ "path::to::G::G", "path/to/G");
+
+ I.Children.Records.emplace_back(EmptySID, "ChildStruct", InfoType::IT_record,
+ "path::to::A::r::ChildStruct", "path/to/A/r");
+ I.Children.Functions.emplace_back();
+ I.Children.Functions.back().Name = "OneFunction";
+
+ auto G = getJSONGenerator();
+ assert(G);
+ std::string Buffer;
+ llvm::raw_string_ostream Actual(Buffer);
+ auto Err = G->generateDocForInfo(&I, Actual, ClangDocContext());
+ assert(!Err);
+ std::string Expected = R"raw({
+ "Bases": [
+ {
+ "Access": "public",
+ "FullName": "",
+ "IsParent": true,
+ "IsTypedef": false,
+ "IsVirtual": true,
+ "Name": "F",
+ "Path": "path/to/F",
+ "PublicFunctions": [
+ {
+ "IsStatic": false,
+ "Name": "InheritedFunctionOne",
+ "ReturnType": {
+ "IsBuiltIn": false,
+ "IsTemplate": false,
+ "Name": "",
+ "QualName": "",
+ "USR": "0000000000000000000000000000000000000000"
+ },
+ "USR": "0000000000000000000000000000000000000000"
+ }
+ ],
+ "PublicMembers": [
+ {
+ "Name": "N",
+ "Type": "int"
+ }
+ ],
+ "TagType": "struct",
+ "USR": "0000000000000000000000000000000000000000"
+ }
+ ],
+ "Enums": [
+ {
+ "Members": [
+ {
+ "Name": "RED",
+ "Value": "0"
+ }
+ ],
+ "Name": "Color",
+ "Scoped": false,
+ "USR": "0000000000000000000000000000000000000000"
+ }
+ ],
+ "FullName": "",
+ "IsTypedef": false,
+ "Location": {
+ "Filename": "main.cpp",
+ "LineNumber": 1
+ },
+ "Name": "Foo",
+ "Namespace": [
+ "GlobalNamespace"
+ ],
+ "Parents": [
+ {
+ "Name": "F",
+ "Path": "",
+ "QualName": "",
+ "USR": "0000000000000000000000000000000000000000"
+ }
+ ],
+ "Path": "GlobalNamespace",
+ "ProtectedMembers": [
+ {
+ "Name": "X",
+ "Type": "int"
+ }
+ ],
+ "PublicFunctions": [
+ {
+ "IsStatic": false,
+ "Name": "OneFunction",
+ "ReturnType": {
+ "IsBuiltIn": false,
+ "IsTemplate": false,
+ "Name": "",
+ "QualName": "",
+ "USR": "0000000000000000000000000000000000000000"
+ },
+ "USR": "0000000000000000000000000000000000000000"
+ }
+ ],
+ "Records": [
+ {
+ "Name": "ChildStruct",
+ "Path": "path/to/A/r",
----------------
ilovepi wrote:
I'm guessing the path separator will be an issue on Windows...
https://github.com/llvm/llvm-project/pull/142483
More information about the cfe-commits
mailing list