Skip to content

Commit 65c50b7

Browse files
PeterChou1asl
authored andcommitted
add blog improve clang-doc blog posts
1 parent c13f39b commit 65c50b7

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
author: "Peter Chou"
3+
date: "2024-12-04"
4+
tags: ["GSoC", "clang-doc"]
5+
title: "GSoC 2024: Improve Clang Doc"
6+
---
7+
8+
Hi, my name is Peter, and this year I was involved in Google Summer of Code 2024. I worked on [improving the clang-doc documenation generator](https://discourse.llvm.org/t/improve-clang-doc-usability/76996)
9+
10+
Mentors: Petr Hosek and Paul Kirth
11+
12+
## Project Background
13+
14+
Clang-Doc is a documentation generator developed on top of libtooling, as an
15+
alternative to Doxygen. Development started in 2018, and continued through 2019,
16+
however it has since stalled. Currently, the tool can generate HTML, YAML, and markdown but the generated output has usability issues. This GSOC project was aimed to address the pain points regarding the output of the HTML, by adding support for various C++ constructs and reworking the css of the HTML output to be more user friendly.
17+
18+
## Work Done
19+
20+
The original scope of the project was to improve the output of clang-doc's generation. However during testing the tool was significantly slower than expected which made developing features for the tool impossible.
21+
Each compilation of the LLVM codebase was taking upwards of 10 hours on my local machine. Additionally the tool utilized a lot of memory and was prone to crashing with an out of memory error. Similar tools such as Doxygen and Hdoc ran in comparatively less time for the same codebase. This pointed to a significant bottleneck within clang-doc’s codepath when generating large scale software projects. Due to this the project scope quickly changed to improving the runtime of clang-doc so that it could run much faster. It was only during the latter half of the project did the scope change back to improving clang-doc’s generation.
22+
23+
### Added More Test Cases to Clang-Doc test suite
24+
25+
26+
Clang-doc previously had test’s which did not test the full scope of the tool especially with regards to the HTML or Markdown output of the tool. In order to make sure the optimization experiments were not accidentally breaking the tool, it was necessary to add more end to end tests.
27+
In summary I added four comprehensive tests which covered all features that we were not testing such as testing the generation for Enums, Namespace and Records for HTML and Markdown.
28+
29+
30+
### Improve clang-doc’s performance by 1.58 times
31+
32+
Internally the way clang-doc works is by leveraging libtooling's ASTVisitor class to parse the declaration in each project and serializing it into an internal format, which gets deserialized later when we output the final format. The bottleneck in clang-doc lied in the way clang-doc was doing redundant work when it was visiting each declaration.
33+
34+
For example if we had a the following project:
35+
36+
```cpp
37+
//File: Base.cpp
38+
39+
class Base {}
40+
```
41+
42+
43+
```cpp
44+
//File: A.cpp
45+
46+
class A : public Base {}
47+
```
48+
49+
```cpp
50+
//File: B.cpp
51+
52+
class B : public Base {}
53+
```
54+
55+
In this case the ASTVisitor invoke by clang-doc would visit serialized the Base class three times, once when it is parsing Base.cpp, another when its visiting A.cpp then B.cpp. This means any C++ project that heavily leverages inheritance would result in a lot of redundant work.
56+
57+
The optimization ended up being a simple memoization dictionary which kept track of a list of declaration that clang-doc had visited.
58+
59+
Here is a plot of the benchmarking numbers:
60+
61+
<div style="margin:0 auto;">
62+
<img src="/img/clang-doc-benchmark-numbers.png"><br/>
63+
</div>
64+
65+
66+
### Added Template Mustache HTML Backend
67+
68+
Clang-doc originally used an ad-hoc method of generating HTML, in this project I introduced a templating language as a way of reducing project complexity and reducing the ease of development. Two RFCs were made before arriving at the idea of introducing Mustache as a library. Originally the idea was to introduce a custom templating language, however upon further discussion it was decided that the complexity of designing and implementing a new templating language was too much.
69+
A LLVM community member suggested I implement a mustache as templating language.
70+
Mustache was the ideal templating language, since it was very simple to implement, and has a well defined spec that fit what was needed for clang-doc’s use case. The feedback to the RFC was generally positive however there was some a bit of pushback in regards to adding an HTML support library to LLVM.
71+
In terms of engineering wins, this library was able to cut the direct down on HTML backend significantly dropping 500 lines of code. This library was also designed for general purpose use around LLVM, since there are numerous places in LLVM where various tools generate html in its own way. Using the mustache templating library would be a nice way to standardize the codebase.
72+
73+
### Improve Clang-Doc HTML Output
74+
75+
The previous version of clang-doc’s output was a pretty minimal bare bones implementation. It had a sidebar that contained every single declaration within the project which created a large unnavigable UI. Typedef documentation was missing, plus method documentation was missing details such as whether or not the method was a const or virtual. There was no linking between other declarations in the project and there was no syntax highlighting on any language construct.
76+
77+
With the new mustache changes an additional backend was added using the specifier (--format=mhtml). That addresses these issues.
78+
79+
Below is a comparison of the same output between the two backends
80+
81+
82+
<div style="margin:0 auto;">
83+
<img src="/img/clang-doc-old-html-output.png"><br/>
84+
</div>
85+
86+
<div style="margin:0 auto;">
87+
<img src="/img/clang-doc-new-output.png"><br/>
88+
</div>
89+
90+
You can also visit the output project on my personal github.io page link
91+
[here](https://peterchou1.github.io/).
92+
93+
Note: this output is still a work in progress.
94+
95+
## Learning Insight
96+
97+
I've learned a lot in the past few months, thanks to GSOC I now have a much better idea of what it’s like to participate in a large open source project. I received a lot of feedback through PR’s, making RFC and collaborating with other GSOC members. I’d learned a lot about how to interact with the community and solicit feedback. I also learned a lot about instrumentation/profiling code having conducted many experiments in order to try to speed clang-doc up.
98+
99+
## Future Work
100+
101+
I plan to work on clang-doc until an MVP product can be generated and evaluated for the LLVM project. My remaining tasks include landing the mustache support library and clang-doc’s mustache backend, as well as gathering feedback from the LLVM community regarding clang-doc’s current output. Additionally, I intend to add test cases for the mustache HTML backend to ensure its robustness and functionality.
102+
103+
104+
105+
106+
## Conclusion
107+
108+
Overall the current state of clang-doc is much healthier than it was before. It now has much better test coverage across all its output, markdown, html, yaml. Whereas previously there were no e2e test cases that were not as comprehensive. The tool is significantly faster especially for large scale projects like LLVM making documentation generation and development a much better experience.
109+
The tool also has a simplified HTML backend that will be much easier to work with compared to before leading to a faster velocity for development.
110+
111+
112+
## Acknowledgements
113+
114+
I’d like to thank my mentors, Paul and Petr for their invaluable input when I encounter issues with the project. This year has been tough for me mentally, and I’d like to thank my mentors for being accommodating with me.
221 KB
Loading
71.2 KB
Loading

0 commit comments

Comments
 (0)