https://github.com/evelez7 created https://github.com/llvm/llvm-project/pull/190822
By default all JSON is serialized "pretty" with whitespace. This patch adds an option to serialize JSON without whitespace. This trims the size of the JSON folder for clang from around 1.3 GB to 785 MB. >From 18a4cf5b5d75a516cedd321d5acbf03241e3c715 Mon Sep 17 00:00:00 2001 From: Erick Velez <[email protected]> Date: Tue, 7 Apr 2026 10:02:17 -0700 Subject: [PATCH] [clang-doc] Add option for compact JSON By default all JSON is serialized "pretty" with whitespace. This patch adds an option to serialize JSON without whitespace. This trims the size of the JSON folder for clang from around 1.3 GB to 785 MB. --- clang-tools-extra/clang-doc/JSONGenerator.cpp | 10 ++++++++-- clang-tools-extra/clang-doc/Representation.cpp | 16 +++++++--------- clang-tools-extra/clang-doc/Representation.h | 3 ++- .../clang-doc/tool/ClangDocMain.cpp | 6 +++++- clang-tools-extra/test/clang-doc/compact.cpp | 10 ++++++++++ 5 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 clang-tools-extra/test/clang-doc/compact.cpp diff --git a/clang-tools-extra/clang-doc/JSONGenerator.cpp b/clang-tools-extra/clang-doc/JSONGenerator.cpp index 8db7eaafcfb30..f0b10bfd280e4 100644 --- a/clang-tools-extra/clang-doc/JSONGenerator.cpp +++ b/clang-tools-extra/clang-doc/JSONGenerator.cpp @@ -915,7 +915,10 @@ Error JSONGenerator::serializeIndex(StringRef RootDir) { raw_fd_ostream RootOS(IndexFilePath, FileErr, sys::fs::OF_Text); if (FileErr) return createFileError("cannot open file " + IndexFilePath, FileErr); - RootOS << llvm::formatv("{0:2}", ObjVal); + if (CDCtx->CompactJSON) + RootOS << llvm::formatv("{0}", ObjVal); + else + RootOS << llvm::formatv("{0:2}", ObjVal); return Error::success(); } @@ -1018,7 +1021,10 @@ Error JSONGenerator::generateDocForInfo(Info *I, raw_ostream &OS, case InfoType::IT_default: return createStringError(inconvertibleErrorCode(), "unexpected info type"); } - OS << llvm::formatv("{0:2}", llvm::json::Value(std::move(Obj))); + if (CDCtx.CompactJSON) + OS << llvm::formatv("{0}", llvm::json::Value(std::move(Obj))); + else + OS << llvm::formatv("{0:2}", llvm::json::Value(std::move(Obj))); return Error::success(); } diff --git a/clang-tools-extra/clang-doc/Representation.cpp b/clang-tools-extra/clang-doc/Representation.cpp index 1535fcc71df66..fbbb818e3d5ae 100644 --- a/clang-tools-extra/clang-doc/Representation.cpp +++ b/clang-tools-extra/clang-doc/Representation.cpp @@ -498,18 +498,16 @@ void Index::sort() { C.sort(); } -ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx, - StringRef ProjectName, bool PublicOnly, - StringRef OutDirectory, StringRef SourceRoot, - StringRef RepositoryUrl, - StringRef RepositoryLinePrefix, StringRef Base, - std::vector<std::string> UserStylesheets, - clang::DiagnosticsEngine &Diags, - OutputFormatTy Format, bool FTimeTrace) +ClangDocContext::ClangDocContext( + tooling::ExecutionContext *ECtx, StringRef ProjectName, bool PublicOnly, + StringRef OutDirectory, StringRef SourceRoot, StringRef RepositoryUrl, + StringRef RepositoryLinePrefix, StringRef Base, + std::vector<std::string> UserStylesheets, clang::DiagnosticsEngine &Diags, + OutputFormatTy Format, bool FTimeTrace, bool CompactJSON) : ECtx(ECtx), ProjectName(ProjectName), OutDirectory(OutDirectory), SourceRoot(std::string(SourceRoot)), UserStylesheets(UserStylesheets), Base(Base), Diags(Diags), Format(Format), PublicOnly(PublicOnly), - FTimeTrace(FTimeTrace) { + FTimeTrace(FTimeTrace), CompactJSON(CompactJSON) { llvm::SmallString<128> SourceRootDir(SourceRoot); if (SourceRoot.empty()) // If no SourceRoot was provided the current path is used as the default diff --git a/clang-tools-extra/clang-doc/Representation.h b/clang-tools-extra/clang-doc/Representation.h index aa298f99db1ea..bc7baf132bff5 100644 --- a/clang-tools-extra/clang-doc/Representation.h +++ b/clang-tools-extra/clang-doc/Representation.h @@ -727,7 +727,7 @@ struct ClangDocContext { StringRef RepositoryUrl, StringRef RepositoryCodeLinePrefix, StringRef Base, std::vector<std::string> UserStylesheets, clang::DiagnosticsEngine &Diags, OutputFormatTy Format, - bool FTimeTrace = false); + bool FTimeTrace = false, bool CompactJSON = false); tooling::ExecutionContext *ECtx; std::string ProjectName; // Name of project clang-doc is documenting. std::string OutDirectory; // Directory for outputting generated files. @@ -755,6 +755,7 @@ struct ClangDocContext { int Granularity; // Granularity of ftime trace bool PublicOnly; // Indicates if only public declarations are documented. bool FTimeTrace; // Indicates if ftime trace is turned on + bool CompactJSON; // Indicates if JSON is emitted without whitespace. }; // Ensure arena allocated types remain safe to allocate in the arena. diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp index f627ee5887528..13a33fe1474b0 100644 --- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp +++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp @@ -50,6 +50,10 @@ using clang::doc::OutputFormatTy; static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); static llvm::cl::OptionCategory ClangDocCategory("clang-doc options"); +static llvm::cl::opt<bool> + CompactJSON("compact", llvm::cl::desc("Serialize JSON without whitespace."), + llvm::cl::cat(ClangDocCategory)); + static llvm::cl::opt<std::string> ProjectName("project-name", llvm::cl::desc("Name of project."), llvm::cl::cat(ClangDocCategory)); @@ -309,7 +313,7 @@ Example usage for a project using a compile commands database: Executor->getExecutionContext(), ProjectName, PublicOnly, OutDirectory, SourceRoot, RepositoryUrl, RepositoryCodeLinePrefix, BaseDirectory, {UserStylesheets.begin(), UserStylesheets.end()}, Diags, FormatEnum, - FTimeTrace); + FTimeTrace, CompactJSON); if (Format == "html") ExitOnErr(getHtmlFiles(argv[0], CDCtx)); diff --git a/clang-tools-extra/test/clang-doc/compact.cpp b/clang-tools-extra/test/clang-doc/compact.cpp new file mode 100644 index 0000000000000..399ab12205001 --- /dev/null +++ b/clang-tools-extra/test/clang-doc/compact.cpp @@ -0,0 +1,10 @@ +// RUN: rm -rf %t && mkdir -p %t +// RUN: clang-doc --compact --doxygen --output=%t --format=json --executor=standalone %s +// RUN: FileCheck %s < %t/json/GlobalNamespace/_ZTV3Foo.json --check-prefix=CLASS +// RUN: FileCheck %s < %t/json/index.json --check-prefix=INDEX + +class Foo {}; + +// CLASS: {"Contexts":[{"DocumentationFileName":"index","End":true,"Name":"Global Namespace","QualName":"GlobalNamespace","RelativePath":"{{.*}}","USR":"0000000000000000000000000000000000000000"}],"DocumentationFileName":"_ZTV3Foo","HasContexts":true,"InfoType":"record","IsTypedef":false,"Location":{"Filename":"/home/erick/code/llvm/clang-tools-extra/test/clang-doc/compact.cpp","LineNumber":6},"MangledName":"_ZTV3Foo","Name":"Foo","Namespace":["GlobalNamespace"],"Path":"GlobalNamespace","TagType":"class","USR":"{{([0-9A-F]{40})}}"} + +// INDEX: {"Index":[{"Name":"GlobalNamespace","QualName":"GlobalNamespace","Type":"namespace","USR":"0000000000000000000000000000000000000000"}]} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
