Skip to content
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

[clang-doc][fix] crashes when generating HTML without --repository #131698

Closed
wants to merge 1 commit into from

Conversation

hulxv
Copy link
Contributor

@hulxv hulxv commented Mar 18, 2025

Fix #131697

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

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

Author: Mohamed Emad (hulxv)

Changes

Fix #131697


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

1 Files Affected:

  • (modified) clang-tools-extra/clang-doc/HTMLGenerator.cpp (+1-1)
diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
index 18a0de826630c..37d94ace17477 100644
--- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -494,7 +494,7 @@ genReferencesBlock(const std::vector<Reference> &References,
 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);

@petrhosek petrhosek requested review from ilovepi and petrhosek March 18, 2025 18:48
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.

Thanks for the PR, but I don't think this is logic is quite right as it stands. There should also be some accompanying test changes to reflect what's being fixed. As this patch stands it removes basic functionality.

@@ -494,7 +494,7 @@ genReferencesBlock(const std::vector<Reference> &References,
static std::unique_ptr<TagNode>
writeFileDefinition(const Location &L,
std::optional<StringRef> RepositoryUrl = std::nullopt) {
if (!L.IsFileInRootDir && !RepositoryUrl)
if (!L.IsFileInRootDir || !RepositoryUrl)
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic isn't correct, and happens to prevent us from effectively using the --repository= string anywhere, as evidenced by our tests.

I have some additional test changes that I'm in the process of adding that will hopefully cover this case.

An alternative would be to change the deref of the option to use .value_or("") instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

#131894 adds more complete testing logic around the --repository flag. see if the behavior you're trying to fix reproduces in the tests w/ that PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#131894 adds more complete testing logic around the --repository flag. see if the behavior you're trying to fix reproduces in the tests w/ that PR?

yup, it's correct but I think we need to add some tests for single files also

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This logic isn't correct, and happens to prevent us from effectively using the --repository= string anywhere, as evidenced by our tests.

The whole next logic depends on RepositoryUrl. how can we continue using it if it's not passed?

An alternative would be to change the deref of the option to use .value_or("") instead.

If RepositoryUrl is not passed, shouldn't we use file:// protocol as I think? In this case, the FileUrl should be something like file:///path/to/file, not just an empty string

Copy link
Contributor

Choose a reason for hiding this comment

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

The whole next logic depends on RepositoryUrl. how can we continue using it if it's not passed?

What I mean is that often !L.IsFileInRootDir || !RepositoryUrl is true when we want to use the --repository flag, meaning we never use the URL in situations where we should.

If RepositoryUrl is not passed, shouldn't we use file:// protocol as I think? In this case, the FileUrl should be something like file:///path/to/file, not just an empty string

Maybe. I think we're still generating a valid path in the HTML, but different generation methods are welcome. Some of this may be obsoleted after @PeterChou1 finishes landing #108653 and the prerequisite Mustache support in LLVM support.

@hulxv
Copy link
Contributor Author

hulxv commented Mar 19, 2025

@ilovepi
I see you opened a PR to solve the same issue here. Wouldn't be better if I continue working on it with your suggestions? 😅

@ilovepi
Copy link
Contributor

ilovepi commented Mar 19, 2025

@ilovepi I see you opened a PR to solve the same issue here. Wouldn't be better if I continue working on it with your suggestions? 😅

I'm fine either way, but I wanted some kind of testing so I could verify the fix.

@ilovepi
Copy link
Contributor

ilovepi commented Mar 20, 2025

hey, sorry, I ended up landing #131939. There are two reasons: 1) I really wanted to fix that crash ASAP. 2) I made the mistake of stacking other patches on top of it with graphite, and it turns out it isn't easy to reorder them. As a result I couldn't fix the basic functionality of HTML links without reuploading the entire rest of the stack. Normally, I'd much prefer to just mentor you through the process, but I really needed to fix those links.

If you're still interested in working on this though, I think the test could be improved , and I wouldn't mid seeing that code cleaned up. In particular, I'm wondering how great of an idea it is to use those optionals, when we end up w/ a .value_or(""). The amount of unique_ptr we're using is also not the greatest. And I wouldn't be surprised if using ASan/TSan on something like LLVM wouldn't find several bugs. Lastly, the testing story in clang-doc is quite weak. We've improved it a lot over last year's GSOC, but I still don't think its all that well exercised.

@hulxv hulxv closed this Mar 25, 2025
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] crashes when generating HTML without --repository flag
3 participants