<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/131697>131697</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-doc] crashes when generating HTML without --repository flag
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
hulxv
</td>
</tr>
</table>
<pre>
## Issue Description
After merging PR #122566, the clang-doc tool crashes with an assertion failure when attempting to generate HTML documentation without explicitly specifying the `--repository` flag. The tool fails with:
```
/usr/include/c++/14.2.1/optional:475: constexpr _Tp& std::_Optional_base_impl<_Tp, _Dp>::_M_get() [with _Tp = llvm::StringRef; _Dp = std::_Optional_base<llvm::StringRef, true, true>]: Assertion 'this->_M_is_engaged()' failed.
```
## Steps to Reproduce
1. Build clang-doc from the latest source after merging PR #122566
2. Run the tool on a C++ file with HTML output format but without the `--repository` flag:
```
clang-doc src/Calculator.cpp --format=html -- -I./include
```
## Root Cause
The issue stems from a logical condition change in PR #122566. In the `writeFileDefinition` function, the condition was changed from:
```cpp
if (!L.IsFileInRootDir || !RepositoryUrl)
```
to:
```cpp
if (!L.IsFileInRootDir && !RepositoryUrl)
```
This change means that when `RepositoryUrl` is not provided (which happens when `--repository` flag is omitted), the function proceeds to code paths that attempt to dereference the optional without first checking if it contains a value.
## Solution
The fix is straightforward - revert the condition back to its original state:
```diff
static std::unique_ptr<TagNode>
writeFileDefinition(const Location &L,
std::optional<StringRef> RepositoryUrl = std::nullopt) {
- if (!L.IsFileInRootDir && !RepositoryUrl)
+ if (!L.IsFileInRootDir || !RepositoryUrl)
return std::make_unique<TagNode>(
HTMLTag::TAG_P, "Defined at line " + std::to_string(L.LineNumber) +
" of file " + L.Filename);
```
This change ensures that if RepositoryUrl is not provided, the function takes a safe path that doesn't attempt to dereference the empty optional.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVkuP8joS_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