Issue 131697
Summary [clang-doc] crashes when generating HTML without --repository flag
Labels new issue
Assignees
Reporter hulxv
    ## 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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to