Skip to content

[clang-doc] add async loading #93276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 30, 2024
Merged

[clang-doc] add async loading #93276

merged 10 commits into from
Jun 30, 2024

Conversation

PeterChou1
Copy link
Contributor

@PeterChou1 PeterChou1 commented May 24, 2024

Fixes #93273

This patch changes the way clang-doc loads html indexes. Previously clang-doc loaded the index via an index_json.js file which uses JSON.parse to parse the file. This patches changes that to use an async function called LoadIndex which enables asynchronous loading making the initial page load not be blocked by loading a large js object

@llvmbot
Copy link
Member

llvmbot commented May 24, 2024

@llvm/pr-subscribers-clang-tools-extra

Author: None (PeterChou1)

Changes

relevant issue: #93273


Full diff: https://github.com/llvm/llvm-project/pull/93276.diff

5 Files Affected:

  • (modified) clang-tools-extra/clang-doc/HTMLGenerator.cpp (+13-12)
  • (modified) clang-tools-extra/clang-doc/Representation.h (+1)
  • (modified) clang-tools-extra/clang-doc/Serialize.cpp (+1)
  • (modified) clang-tools-extra/clang-doc/assets/index.js (+5-2)
  • (modified) clang-tools-extra/clang-doc/tool/ClangDocMain.cpp (+6-1)
diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
index c0faf5f7e8fd9..fd3807014fd09 100644
--- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -964,7 +964,7 @@ static llvm::Error SerializeIndex(ClangDocContext &CDCtx) {
   std::error_code FileErr;
   llvm::SmallString<128> FilePath;
   llvm::sys::path::native(CDCtx.OutDirectory, FilePath);
-  llvm::sys::path::append(FilePath, "index_json.js");
+  llvm::sys::path::append(FilePath, "index.json");
   llvm::raw_fd_ostream OS(FilePath, FileErr, llvm::sys::fs::OF_None);
   if (FileErr != OK) {
     return llvm::createStringError(llvm::inconvertibleErrorCode(),
@@ -985,9 +985,7 @@ static llvm::Error SerializeIndex(ClangDocContext &CDCtx) {
       });
     });
   };
-  OS << "var JsonIndex = `\n";
   IndexToJSON(CDCtx.Idx);
-  OS << "`;\n";
   return llvm::Error::success();
 }
 
@@ -1050,30 +1048,33 @@ static llvm::Error CopyFile(StringRef FilePath, StringRef OutDirectory) {
   if (FileErr != OK) {
     return llvm::createStringError(llvm::inconvertibleErrorCode(),
                                    "error creating file " +
-                                       llvm::sys::path::filename(FilePath) +
+                                       FilePath +
                                        ": " + FileErr.message() + "\n");
   }
   return llvm::Error::success();
 }
 
 llvm::Error HTMLGenerator::createResources(ClangDocContext &CDCtx) {
-  auto Err = SerializeIndex(CDCtx);
-  if (Err)
+  if (auto Err = SerializeIndex(CDCtx)) {
     return Err;
-  Err = GenIndex(CDCtx);
-  if (Err)
+  }
+
+  if (auto Err = GenIndex(CDCtx)) {
     return Err;
+  }
 
   for (const auto &FilePath : CDCtx.UserStylesheets) {
-    Err = CopyFile(FilePath, CDCtx.OutDirectory);
-    if (Err)
+    if (auto Err = CopyFile(FilePath, CDCtx.OutDirectory)) {
       return Err;
+    }
   }
+
   for (const auto &FilePath : CDCtx.FilesToCopy) {
-    Err = CopyFile(FilePath, CDCtx.OutDirectory);
-    if (Err)
+    if (auto Err = CopyFile(FilePath, CDCtx.OutDirectory)) {
       return Err;
+    }
   }
+
   return llvm::Error::success();
 }
 
diff --git a/clang-tools-extra/clang-doc/Representation.h b/clang-tools-extra/clang-doc/Representation.h
index a6b144eb7fa2a..23323f1cbdf46 100644
--- a/clang-tools-extra/clang-doc/Representation.h
+++ b/clang-tools-extra/clang-doc/Representation.h
@@ -491,6 +491,7 @@ struct ClangDocContext {
   std::string SourceRoot;   // Directory where processed files are stored. Links
                             // to definition locations will only be generated if
                             // the file is in this dir.
+
   // URL of repository that hosts code used for links to definition locations.
   std::optional<std::string> RepositoryUrl;
   // Path of CSS stylesheets that will be copied to OutDirectory and used to
diff --git a/clang-tools-extra/clang-doc/Serialize.cpp b/clang-tools-extra/clang-doc/Serialize.cpp
index 3b074d849e8a9..49062af637ea0 100644
--- a/clang-tools-extra/clang-doc/Serialize.cpp
+++ b/clang-tools-extra/clang-doc/Serialize.cpp
@@ -32,6 +32,7 @@ populateParentNamespaces(llvm::SmallVector<Reference, 4> &Namespaces,
 
 static void populateMemberTypeInfo(MemberTypeInfo &I, const FieldDecl *D);
 
+
 // A function to extract the appropriate relative path for a given info's
 // documentation. The path returned is a composite of the parent namespaces.
 //
diff --git a/clang-tools-extra/clang-doc/assets/index.js b/clang-tools-extra/clang-doc/assets/index.js
index a5ac90f2e06e7..b659b20069ac5 100644
--- a/clang-tools-extra/clang-doc/assets/index.js
+++ b/clang-tools-extra/clang-doc/assets/index.js
@@ -82,6 +82,9 @@ function createIndex(Index) {
 document.addEventListener("DOMContentLoaded", function() {
   // JsonIndex is a variable from another file that contains the index
   // in JSON format
-  var Index = JSON.parse(JsonIndex);
-  createIndex(Index);
+  fetch("/index.json")
+      .then((response) => response.json())
+      .then((Index) => {
+        createIndex(Index);
+      });
 });
diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 21b581fa6df2e..af892b5402092 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -155,6 +155,10 @@ Example usage for a project using a compile commands database:
     return 1;
   }
 
+  // add option to customize url fragment
+  // such as https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clang-doc/ClangDocMain.cpp#L1
+
+
   // Fail early if an invalid format was provided.
   std::string Format = getFormatString();
   llvm::outs() << "Emiting docs in " << Format << " format.\n";
@@ -179,7 +183,7 @@ Example usage for a project using a compile commands database:
       SourceRoot,
       RepositoryUrl,
       {UserStylesheets.begin(), UserStylesheets.end()},
-      {"index.js", "index_json.js"}};
+      {"index.js"}};
 
   if (Format == "html") {
     void *MainAddr = (void *)(intptr_t)GetExecutablePath;
@@ -201,6 +205,7 @@ Example usage for a project using a compile commands database:
     CDCtx.FilesToCopy.emplace_back(IndexJS.str());
   }
 
+
   // Mapping phase
   llvm::outs() << "Mapping decls...\n";
   auto Err =

Copy link

github-actions bot commented May 24, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@ilovepi ilovepi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noted several items that need to be addressed in line before this lands. Both the commit title and description need to be updated. Please add a tag [clang-doc] to the commit title, and include the full details of the problem you're addressing and how this patch addresses them in the description.

If this fixes the issue you've referenced, add Fixes #93273 after the body of the descripton. You can find more info on GitHub interactions w/ commits here https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue

Lastly, this patch badly needs tests. Please create a PR with that demonstrates the existing behavior in a test (or tests) that we can land first. You can rebase this patch on top of it, and we can easily understand the delta between the new and old behaviors based on how the test changed.

@ilovepi ilovepi requested a review from petrhosek May 24, 2024 17:26
@PeterChou1 PeterChou1 changed the title Clang doc async [clang-doc] add async loading Jun 28, 2024
@PeterChou1 PeterChou1 requested a review from ilovepi June 29, 2024 01:25
Copy link
Contributor

@ilovepi ilovepi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo some minor nits, but please revise the commit message. The end is not clear, and looks like it may be a copy paste error. If you know how much this improves the performance/responsiveness of the generated documentation, that would also be good to include.

@ilovepi ilovepi merged commit a9b1e80 into llvm:main Jun 30, 2024
7 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 30, 2024

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-sie-win running on sie-win-worker while building clang-tools-extra at step 4 "clean-build-dir".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/46/builds/863

Here is the relevant piece of the build log for the reference:

Step 4 (clean-build-dir) failure: Delete failed. (failure)
Step 6 (build-unified-tree) failure: build (failure)
...
[663/4660] Building IntrinsicsHexagon.h...
[664/4660] Building DiagnosticSerializationKinds.inc...
[665/4660] Building IntrinsicsDirectX.h...
[666/4660] Building DiagnosticGroups.inc...
[667/4660] Building arm_mve_builtins.inc...
[668/4660] Building IntrinsicsPowerPC.h...
[669/4660] Building arm_fp16.inc...
[670/4660] Building IntrinsicsR600.h...
[671/4660] Building AttrList.inc...
[672/4660] Linking CXX executable bin\FileCheck.exe
FAILED: bin/FileCheck.exe 
cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=utils\FileCheck\CMakeFiles\FileCheck.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests  -- C:\PROGRA~2\MICROS~1\2019\BUILDT~1\VC\Tools\MSVC\1429~1.301\bin\HostX64\x64\link.exe /nologo utils\FileCheck\CMakeFiles\FileCheck.dir\FileCheck.cpp.obj utils\FileCheck\CMakeFiles\FileCheck.dir\__\__\resources\windows_version_resource.rc.res  /out:bin\FileCheck.exe /implib:lib\FileCheck.lib /pdb:bin\FileCheck.pdb /version:0.0 /machine:x64 /STACK:10000000 /INCREMENTAL:NO /subsystem:console  lib\LLVMFileCheck.lib  lib\LLVMSupport.lib  psapi.lib  shell32.lib  ole32.lib  uuid.lib  advapi32.lib  ws2_32.lib  ntdll.lib  delayimp.lib  -delayload:shell32.dll  -delayload:ole32.dll  lib\LLVMDemangle.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK: command "C:\PROGRA~2\MICROS~1\2019\BUILDT~1\VC\Tools\MSVC\1429~1.301\bin\HostX64\x64\link.exe /nologo utils\FileCheck\CMakeFiles\FileCheck.dir\FileCheck.cpp.obj utils\FileCheck\CMakeFiles\FileCheck.dir\__\__\resources\windows_version_resource.rc.res /out:bin\FileCheck.exe /implib:lib\FileCheck.lib /pdb:bin\FileCheck.pdb /version:0.0 /machine:x64 /STACK:10000000 /INCREMENTAL:NO /subsystem:console lib\LLVMFileCheck.lib lib\LLVMSupport.lib psapi.lib shell32.lib ole32.lib uuid.lib advapi32.lib ws2_32.lib ntdll.lib delayimp.lib -delayload:shell32.dll -delayload:ole32.dll lib\LLVMDemangle.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:bin\FileCheck.exe.manifest" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: cannot open file 'bin\FileCheck.exe'

[673/4660] Building AttrSubMatchRulesList.inc...
[674/4660] Building IntrinsicsS390.h...
[675/4660] Building IntrinsicsRISCV.h...
[676/4660] Building riscv_vector_builtins.inc...
[677/4660] Building RegularKeywordAttrInfo.inc...
[678/4660] Building IntrinsicsWebAssembly.h...
[679/4660] Building Builtins.inc...
[680/4660] Building BuiltinsBPF.inc...
[681/4660] Building IntrinsicsSPIRV.h...
[682/4660] Building arm_neon.inc...
[683/4660] Building IntrinsicsVE.h...
[684/4660] Building arm_mve_builtin_cg.inc...
[685/4660] Building IntrinsicsX86.h...
[686/4660] Building IntrinsicsXCore.h...
[687/4660] Building arm_sme_sema_rangechecks.inc...
[688/4660] Building arm_mve_builtin_sema.inc...
[689/4660] Building arm_sve_builtin_cg.inc...
[690/4660] Building arm_cde_builtin_aliases.inc...
[691/4660] Building arm_sme_builtins_za_state.inc...
[692/4660] Building arm_sme_streaming_attrs.inc...
[693/4660] Building arm_sve_typeflags.inc...
[694/4660] Building arm_sve_sema_rangechecks.inc...
[695/4660] Building arm_sve_streaming_attrs.inc...
[696/4660] Building riscv_sifive_vector_builtin_sema.inc...
[697/4660] Building arm_sme_builtins.inc...
[698/4660] Building arm_cde_builtins.inc...
[699/4660] Building arm_cde_builtin_cg.inc...
[700/4660] Building riscv_sifive_vector_builtins.inc...
[701/4660] Building riscv_sifive_vector_builtin_cg.inc...
[702/4660] Building AttrParserStringSwitches.inc...
[703/4660] Building AttrSubMatchRulesParserStringSwitches.inc...
[704/4660] Building AttrTemplateInstantiate.inc...
[705/4660] Building AttrParsedAttrList.inc...
[706/4660] Building AttrParsedAttrKinds.inc...

lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
Fixes llvm#93273

This patch changes the way clang-doc loads html indexes. Previously
clang-doc loaded the index via an index_json.js file which uses
JSON.parse to parse the file. This patches changes that to use an async
function called LoadIndex which enables asynchronous loading making the
initial page load not be blocked by loading a large JavaScript object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-doc: make side bar load asynchronously
4 participants