Skip to content

Commit ff33c4b

Browse files
authored
correctly handle files included through symlinks
1 parent 99bf927 commit ff33c4b

File tree

9 files changed

+135
-51
lines changed

9 files changed

+135
-51
lines changed

src/lib/AST/ASTVisitor.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,25 +3120,18 @@ ASTVisitor::
31203120
findFileInfo(clang::SourceLocation const loc)
31213121
{
31223122
MRDOCS_CHECK_OR(!loc.isInvalid(), nullptr);
3123-
3124-
// KRYSTIAN FIXME: we really should not be calling getPresumedLoc this often,
3125-
// it's quite expensive
3126-
auto const presumed = source_.getPresumedLoc(loc, false);
3127-
MRDOCS_CHECK_OR(!presumed.isInvalid(), nullptr);
3128-
3129-
FileEntry const* entry = source_.getFileEntryForID( presumed.getFileID());
3130-
MRDOCS_CHECK_OR(entry, nullptr);
3123+
// Find the presumed location, ignoring #line directives
3124+
PresumedLoc presumed = source_.getPresumedLoc(loc, false);
3125+
FileID id = presumed.getFileID();
3126+
if(id.isInvalid())
3127+
return nullptr;
31313128

31323129
// Find in the cache
3133-
if (auto const it = files_.find(entry); it != files_.end())
3134-
{
3130+
if(auto const it = files_.find(id); it != files_.end())
31353131
return std::addressof(it->second);
3136-
}
31373132

3138-
// Build FileInfo
3139-
auto const FI = buildFileInfo(entry);
3140-
MRDOCS_CHECK_OR(FI, nullptr);
3141-
auto [it, inserted] = files_.try_emplace(entry, std::move(*FI));
3133+
auto [it, inserted] = files_.try_emplace(
3134+
id, buildFileInfo(presumed.getFilename()));
31423135
return std::addressof(it->second);
31433136
}
31443137

@@ -3154,25 +3147,19 @@ findFileInfo(Decl const* D)
31543147
return findFileInfo(Loc);
31553148
}
31563149

3157-
std::optional<ASTVisitor::FileInfo>
3158-
ASTVisitor::
3159-
buildFileInfo(FileEntry const* entry)
3160-
{
3161-
std::string_view const file_path = entry->tryGetRealPathName();
3162-
MRDOCS_CHECK_OR(!file_path.empty(), std::nullopt);
3163-
return buildFileInfo(file_path);
3164-
}
3165-
31663150
ASTVisitor::FileInfo
31673151
ASTVisitor::
3168-
buildFileInfo(std::string_view const file_path)
3152+
buildFileInfo(std::string_view path)
31693153
{
31703154
FileInfo file_info;
3171-
file_info.full_path = file_path;
3172-
if (!files::isPosixStyle(file_info.full_path))
3173-
{
3155+
file_info.full_path = path;
3156+
3157+
if (! files::isAbsolute(file_info.full_path))
3158+
file_info.full_path = files::makeAbsolute(
3159+
file_info.full_path, config_->sourceRoot);
3160+
3161+
if (! files::isPosixStyle(file_info.full_path))
31743162
file_info.full_path = files::makePosixStyle(file_info.full_path);
3175-
}
31763163

31773164
// Attempts to get a relative path for the prefix
31783165
auto tryGetRelativePosixPath = [&file_info](std::string_view const prefix)

src/lib/AST/ASTVisitor.hpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class ASTVisitor
138138
if a file should be extracted or to add the
139139
SourceInfo to an Info object.
140140
*/
141-
std::unordered_map<FileEntry const*, FileInfo> files_;
141+
llvm::DenseMap<FileID, FileInfo> files_;
142142

143143
/* Determine how a Decl matched the filters
144144
*/
@@ -1112,26 +1112,6 @@ class ASTVisitor
11121112
FileInfo*
11131113
findFileInfo(Decl const* D);
11141114

1115-
/* Build a FileInfo for a FileEntry
1116-
1117-
This function will build a FileInfo object for a
1118-
given Clang FileEntry object.
1119-
1120-
The function will extract the full path and short
1121-
path of the file, and store the information in the
1122-
FileInfo object.
1123-
1124-
This function is used as an auxiliary function to
1125-
`findFileInfo` when the FileInfo object does not
1126-
exist in the cache.
1127-
1128-
@param file The Clang FileEntry object to build the FileInfo for.
1129-
@return the FileInfo object.
1130-
1131-
*/
1132-
std::optional<FileInfo>
1133-
buildFileInfo(FileEntry const* entry);
1134-
11351115
/* Build a FileInfo for a string path
11361116
11371117
This function will build a FileInfo object for a
@@ -1153,7 +1133,7 @@ class ASTVisitor
11531133
@return the FileInfo object.
11541134
*/
11551135
FileInfo
1156-
buildFileInfo(std::string_view file_path);
1136+
buildFileInfo(std::string_view path);
11571137

11581138
/* Result of an upsert operation
11591139
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** A brief.
2+
*/
3+
4+
void f();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
= Reference
2+
:mrdocs:
3+
4+
[#index]
5+
== Global namespace
6+
7+
8+
=== Functions
9+
10+
[cols=2]
11+
|===
12+
| Name
13+
| Description
14+
15+
| <<f,`f`>>
16+
| A brief&period;
17+
18+
|===
19+
20+
[#f]
21+
== f
22+
23+
24+
A brief&period;
25+
26+
=== Synopsis
27+
28+
29+
Declared in `&lt;included&period;hpp&gt;`
30+
31+
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
32+
----
33+
void
34+
f();
35+
----
36+
37+
38+
39+
[.small]#Created with https://www.mrdocs.com[MrDocs]#
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "included.hpp"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<html lang="en">
2+
<head>
3+
<title>Reference</title>
4+
</head>
5+
<body>
6+
<div>
7+
<h1>Reference</h1>
8+
<div>
9+
<div>
10+
<h2 id="index">Global namespace</h2>
11+
</div>
12+
<h2>Functions</h2>
13+
<table style="table-layout: fixed; width: 100%;">
14+
<thead>
15+
<tr>
16+
<th>Name</th>
17+
<th>Description</th>
18+
</tr>
19+
</thead>
20+
<tbody>
21+
<tr>
22+
<td><a href="#f"><code>f</code></a> </td><td><span><span>A brief.</span></span>
23+
</td>
24+
</tr>
25+
</tbody>
26+
</table>
27+
</div>
28+
<div>
29+
<div>
30+
<h2 id="f">f</h2>
31+
<div>
32+
<span><span>A brief.</span></span>
33+
34+
35+
</div>
36+
</div>
37+
<div>
38+
<h3>Synopsis</h3>
39+
<div>
40+
Declared in <code>&lt;included.hpp&gt;</code></div>
41+
<pre>
42+
<code class="source-code cpp">
43+
void
44+
f();
45+
</code>
46+
</pre>
47+
</div>
48+
</div>
49+
50+
</div>
51+
<div>
52+
<h4>Created with <a href="https://www.mrdocs.com">MrDocs</a></h4>
53+
</div>
54+
</body>
55+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<mrdocs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://github.com/cppalliance/mrdocs/raw/develop/mrdocs.rnc">
4+
<namespace id="//////////////////////////8=">
5+
<function name="f" id="s6nsa+zVhpzzrN+yUVPP5rvdXqs=">
6+
<file short-path="included.hpp" source-path="included.hpp" line="4"/>
7+
<doc>
8+
<brief>
9+
<text>A brief.</text>
10+
</brief>
11+
</doc>
12+
</function>
13+
</namespace>
14+
</mrdocs>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Exclude excluded.hpp
2+
exclude:
3+
- 'excluded.hpp'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
excluded.hpp

0 commit comments

Comments
 (0)