Skip to content

correctly handle files included through symlinks #861

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 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 16 additions & 29 deletions src/lib/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3120,25 +3120,18 @@ ASTVisitor::
findFileInfo(clang::SourceLocation const loc)
{
MRDOCS_CHECK_OR(!loc.isInvalid(), nullptr);

// KRYSTIAN FIXME: we really should not be calling getPresumedLoc this often,
// it's quite expensive
auto const presumed = source_.getPresumedLoc(loc, false);
MRDOCS_CHECK_OR(!presumed.isInvalid(), nullptr);

FileEntry const* entry = source_.getFileEntryForID( presumed.getFileID());
MRDOCS_CHECK_OR(entry, nullptr);
// Find the presumed location, ignoring #line directives
PresumedLoc presumed = source_.getPresumedLoc(loc, false);
FileID id = presumed.getFileID();
if(id.isInvalid())
return nullptr;

// Find in the cache
if (auto const it = files_.find(entry); it != files_.end())
{
if(auto const it = files_.find(id); it != files_.end())
return std::addressof(it->second);
}

// Build FileInfo
auto const FI = buildFileInfo(entry);
MRDOCS_CHECK_OR(FI, nullptr);
auto [it, inserted] = files_.try_emplace(entry, std::move(*FI));
auto [it, inserted] = files_.try_emplace(
id, buildFileInfo(presumed.getFilename()));
return std::addressof(it->second);
}

Expand All @@ -3154,25 +3147,19 @@ findFileInfo(Decl const* D)
return findFileInfo(Loc);
}

std::optional<ASTVisitor::FileInfo>
ASTVisitor::
buildFileInfo(FileEntry const* entry)
{
std::string_view const file_path = entry->tryGetRealPathName();
MRDOCS_CHECK_OR(!file_path.empty(), std::nullopt);
return buildFileInfo(file_path);
}

ASTVisitor::FileInfo
ASTVisitor::
buildFileInfo(std::string_view const file_path)
buildFileInfo(std::string_view path)
{
FileInfo file_info;
file_info.full_path = file_path;
if (!files::isPosixStyle(file_info.full_path))
{
file_info.full_path = path;

if (! files::isAbsolute(file_info.full_path))
file_info.full_path = files::makeAbsolute(
file_info.full_path, config_->sourceRoot);

if (! files::isPosixStyle(file_info.full_path))
file_info.full_path = files::makePosixStyle(file_info.full_path);
}

// Attempts to get a relative path for the prefix
auto tryGetRelativePosixPath = [&file_info](std::string_view const prefix)
Expand Down
24 changes: 2 additions & 22 deletions src/lib/AST/ASTVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class ASTVisitor
if a file should be extracted or to add the
SourceInfo to an Info object.
*/
std::unordered_map<FileEntry const*, FileInfo> files_;
llvm::DenseMap<FileID, FileInfo> files_;

/* Determine how a Decl matched the filters
*/
Expand Down Expand Up @@ -1112,26 +1112,6 @@ class ASTVisitor
FileInfo*
findFileInfo(Decl const* D);

/* Build a FileInfo for a FileEntry

This function will build a FileInfo object for a
given Clang FileEntry object.

The function will extract the full path and short
path of the file, and store the information in the
FileInfo object.

This function is used as an auxiliary function to
`findFileInfo` when the FileInfo object does not
exist in the cache.

@param file The Clang FileEntry object to build the FileInfo for.
@return the FileInfo object.

*/
std::optional<FileInfo>
buildFileInfo(FileEntry const* entry);

/* Build a FileInfo for a string path

This function will build a FileInfo object for a
Expand All @@ -1153,7 +1133,7 @@ class ASTVisitor
@return the FileInfo object.
*/
FileInfo
buildFileInfo(std::string_view file_path);
buildFileInfo(std::string_view path);

/* Result of an upsert operation

Expand Down
4 changes: 4 additions & 0 deletions test-files/golden-tests/filters/file/excluded.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** A brief.
*/

void f();
39 changes: 39 additions & 0 deletions test-files/golden-tests/filters/file/include-symlink.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
= Reference
:mrdocs:

[#index]
== Global namespace


=== Functions

[cols=2]
|===
| Name
| Description

| <<f,`f`>>
| A brief&period;

|===

[#f]
== f


A brief&period;

=== Synopsis


Declared in `&lt;included&period;hpp&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
void
f();
----



[.small]#Created with https://www.mrdocs.com[MrDocs]#
1 change: 1 addition & 0 deletions test-files/golden-tests/filters/file/include-symlink.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "included.hpp"
55 changes: 55 additions & 0 deletions test-files/golden-tests/filters/file/include-symlink.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<html lang="en">
<head>
<title>Reference</title>
</head>
<body>
<div>
<h1>Reference</h1>
<div>
<div>
<h2 id="index">Global namespace</h2>
</div>
<h2>Functions</h2>
<table style="table-layout: fixed; width: 100%;">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#f"><code>f</code></a> </td><td><span><span>A brief.</span></span>
</td>
</tr>
</tbody>
</table>
</div>
<div>
<div>
<h2 id="f">f</h2>
<div>
<span><span>A brief.</span></span>


</div>
</div>
<div>
<h3>Synopsis</h3>
<div>
Declared in <code>&lt;included.hpp&gt;</code></div>
<pre>
<code class="source-code cpp">
void
f();
</code>
</pre>
</div>
</div>

</div>
<div>
<h4>Created with <a href="https://www.mrdocs.com">MrDocs</a></h4>
</div>
</body>
</html>
14 changes: 14 additions & 0 deletions test-files/golden-tests/filters/file/include-symlink.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<mrdocs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://github.com/cppalliance/mrdocs/raw/develop/mrdocs.rnc">
<namespace id="//////////////////////////8=">
<function name="f" id="s6nsa+zVhpzzrN+yUVPP5rvdXqs=">
<file short-path="included.hpp" source-path="included.hpp" line="4"/>
<doc>
<brief>
<text>A brief.</text>
</brief>
</doc>
</function>
</namespace>
</mrdocs>
3 changes: 3 additions & 0 deletions test-files/golden-tests/filters/file/include-symlink.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Exclude excluded.hpp
exclude:
- 'excluded.hpp'
1 change: 1 addition & 0 deletions test-files/golden-tests/filters/file/included.hpp