Skip to content

Commit

Permalink
support source paths in Location objects
Browse files Browse the repository at this point in the history
Location objects only contained the full path and the path relative to the search directories. This commit includes a new field for the source path, which contains the path relative to the `source-root`.

#feat

fix #798
  • Loading branch information
alandefreitas committed Jan 17, 2025
1 parent 4665e2f commit 90fcf10
Show file tree
Hide file tree
Showing 118 changed files with 1,467 additions and 1,443 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ if (MRDOCS_BUILD_TESTS)
DEPENDS mrdocs.rnc)
add_custom_target(mrdocs_rng ALL DEPENDS mrdocs.rng)

file(GLOB_RECURSE XML_SOURCES CONFIGURE_DEPENDS test-files/*.xml)
file(GLOB_RECURSE XML_SOURCES CONFIGURE_DEPENDS test-files/golden-tests/*.xml)
add_test(NAME xml-lint
COMMAND ${LIBXML2_XMLLINT_EXECUTABLE} --dropdtd --noout
--relaxng ${CMAKE_CURRENT_BINARY_DIR}/mrdocs.rng ${XML_SOURCES}
Expand Down
20 changes: 10 additions & 10 deletions docs/modules/ROOT/pages/generators.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -862,25 +862,25 @@ The location object has the following properties:
|===
|Property |Type| Description

| `path`
| `fullPath`
| `string`
| The path of the source file.
| The full path of the source file.

| `file`
| `shortPath`
| `string`
| The filename of the source file.
| The path of the source file relative to the search directories.

| `sourcePath`
| `string`
| The path of the source file relative to the `source-root`.

| `line`
| `integer`
| The line number of the symbol.

| `kind`
| `string`
| The kind of file (e.g., `source`, `system`, `other`).
| The line number of the symbol at this location.

| `documented`
| `bool`
| Whether the symbol is documented.
| Whether the symbol is documented at this location.
|===

[#tparam-fields]
Expand Down
2 changes: 1 addition & 1 deletion docs/mrdocs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
},
"source-root": {
"default": "<config-dir>",
"description": "Path to the root directory of the source code. This path is used as a default for input files and a base for relative paths formed from absolute paths.",
"description": "Path to the root directory of the source code. This path is used as a default for input files and a base for relative paths formed from absolute paths. This should typically be the root directory of the git project, as relative paths formed from it can be used to create links to these source files in the repository. Templates use the `base-url` option to create links to the source code.",
"title": "Path to the root directory of the source code",
"type": "string"
},
Expand Down
22 changes: 14 additions & 8 deletions include/mrdocs/Metadata/Source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ struct MRDOCS_DECL
{
/** The full file path
*/
std::string Path;
std::string FullPath;

/** Name of the file
/** The file path relative to one of the search directories
*/
std::string Filename;
std::string ShortPath;

/** The file path relative to the source-root directory
*/
std::string SourcePath;

/** Line number within the file
*/
Expand All @@ -68,12 +72,14 @@ struct MRDOCS_DECL
//--------------------------------------------

Location(
std::string_view const filepath = {},
std::string_view const filename = {},
std::string_view const full_path = {},
std::string_view const short_path = {},
std::string_view const source_path = {},
unsigned const line = 0,
bool const documented = false)
: Path(filepath)
, Filename(filename)
: FullPath(full_path)
, ShortPath(short_path)
, SourcePath(source_path)
, LineNumber(line)
, Documented(documented)
{
Expand All @@ -92,7 +98,7 @@ struct LocationEmptyPredicate
constexpr bool operator()(
Location const& loc) const noexcept
{
return loc.Filename.empty();
return loc.ShortPath.empty();
}
};

Expand Down
4 changes: 3 additions & 1 deletion mrdocs.rnc
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ grammar
Location =
element file
{
attribute path {text},
# attribute full-path {text},
attribute short-path {text},
attribute source-path {text},
attribute line {text},
attribute class {"def"} ?,
empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
--}}
Declared in {{#>markup/code~}}
{{ str "<" }}{{#unless (and @root.config.base-url (eq dcl.kind "source"))~}}
{{dcl.file}}
{{ str "<" }}{{#if (and @root.config.base-url dcl.shortPath dcl.sourcePath)~}}
{{#>markup/a href=(concat @root.config.base-url dcl.sourcePath '#L' dcl.line)}}{{dcl.shortPath}}{{/markup/a}}
{{~else~}}
{{#>markup/a href=(concat @root.config.base-url dcl.file '#L' dcl.line)}}{{dcl.file}}{{/markup/a}}
{{~/unless~}}{{ str ">" }}
{{dcl.shortPath}}
{{~/if~}}{{ str ">" }}
{{~/markup/code}}
26 changes: 16 additions & 10 deletions src/lib/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ populate(
{
return;
}
I.DefLoc.emplace(file->full_path, file->short_path, line, documented);
I.DefLoc.emplace(file->full_path, file->short_path, file->source_path, line, documented);
}
else
{
Expand All @@ -705,13 +705,13 @@ populate(
[line, file](const Location& l)
{
return l.LineNumber == line &&
l.Path == file->full_path;
l.FullPath == file->full_path;
});
if (existing != I.Loc.end())
{
return;
}
I.Loc.emplace_back(file->full_path, file->short_path, line, documented);
I.Loc.emplace_back(file->full_path, file->short_path, file->source_path, line, documented);
}
}

Expand Down Expand Up @@ -2762,6 +2762,15 @@ buildFileInfo(std::string_view const file_path)
return tryGetRelativePosixPath(posixPrefix);
};

// Populate file relative to source-root
if (files::isAbsolute(config_->sourceRoot))
{
if (auto shortPath = tryGetRelativePath(config_->sourceRoot))
{
file_info.source_path = std::string(*shortPath);
}
}

// Find the best match for the file path in the search directories
for (HeaderSearch& HS = sema_.getPreprocessor().getHeaderSearchInfo();
DirectoryLookup const& DL : HS.search_dir_range())
Expand All @@ -2780,14 +2789,11 @@ buildFileInfo(std::string_view const file_path)
}
}

// Fallback to sourceRoot
if (files::isAbsolute(config_->sourceRoot))
// Fallback to the source root
if (!file_info.source_path.empty())
{
if (auto shortPath = tryGetRelativePath(config_->sourceRoot))
{
file_info.short_path = std::string(*shortPath);
return file_info;
}
file_info.short_path = file_info.source_path;
return file_info;
}

// Fallback to system search paths in PATH
Expand Down
3 changes: 3 additions & 0 deletions src/lib/AST/ASTVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class ASTVisitor
// The file path relative to a search directory.
std::string short_path;

// The file path relative to the source-root directory.
std::string source_path;

// Whether this file passes the file filters
std::optional<bool> passesFilters;
};
Expand Down
14 changes: 10 additions & 4 deletions src/lib/Gen/xml/XMLWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,10 +626,14 @@ XMLWriter::
writeSourceInfo(
SourceInfo const& I)
{
if(I.DefLoc)
if (I.DefLoc)
{
writeLocation(*I.DefLoc, true);
for(auto const& loc : I.Loc)
}
for (auto const& loc: I.Loc)
{
writeLocation(loc, false);
}
}

void
Expand All @@ -639,9 +643,11 @@ writeLocation(
bool def)
{
tags_.write("file", {}, {
{ "path", loc.Filename },
// { "full-path", loc.FullPath },
{ "short-path", loc.ShortPath },
{ "source-path", loc.SourcePath },
{ "line", std::to_string(loc.LineNumber) },
{ "class", "def", def } });
{ "class", "def", def }});
}

//------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Lib/ConfigOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{
"name": "source-root",
"brief": "Path to the root directory of the source code",
"details": "Path to the root directory of the source code. This path is used as a default for input files and a base for relative paths formed from absolute paths.",
"details": "Path to the root directory of the source code. This path is used as a default for input files and a base for relative paths formed from absolute paths. This should typically be the root directory of the git project, as relative paths formed from it can be used to create links to these source files in the repository. Templates use the `base-url` option to create links to the source code.",
"type": "dir-path",
"default": "<config-dir>",
"relative-to": "<config-dir>",
Expand Down
8 changes: 4 additions & 4 deletions src/lib/Metadata/Reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ struct LocationEqual
Location const& L1) const noexcept
{
return
std::tie(L0.LineNumber, L0.Filename) ==
std::tie(L1.LineNumber, L1.Filename);
std::tie(L0.LineNumber, L0.FullPath) ==
std::tie(L1.LineNumber, L1.FullPath);
}
};

Expand All @@ -43,8 +43,8 @@ struct LocationLess
Location const& L1) const noexcept
{
return
std::tie(L0.LineNumber, L0.Filename) <
std::tie(L1.LineNumber, L1.Filename);
std::tie(L0.LineNumber, L0.FullPath) <
std::tie(L1.LineNumber, L1.FullPath);
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/lib/Metadata/Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ tag_invoke(
IO& io,
Location const& loc)
{
io.map("path", loc.Path);
io.map("file", loc.Filename);
io.map("fullPath", loc.FullPath);
io.map("shortPath", loc.ShortPath);
io.map("sourcePath", loc.SourcePath);
io.map("line", loc.LineNumber);
io.map("documented", loc.Documented);
}
Expand Down
2 changes: 1 addition & 1 deletion test-files/golden-tests/core/libcxx.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<template>
<tparam name="T" class="type"/>
<function name="sqrt" id="ulFDUE1svTCX8fV/h8EIp4NNbWs=">
<file path="libcxx.cpp" line="149"/>
<file short-path="libcxx.cpp" source-path="libcxx.cpp" line="149"/>
<return>
<type name="T"/>
</return>
Expand Down
2 changes: 1 addition & 1 deletion test-files/golden-tests/core/utf-8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:noNamespaceSchemaLocation="https://github.com/cppalliance/mrdocs/raw/develop/mrdocs.rnc">
<namespace id="//////////////////////////8=">
<function name="Христос_воскрес" id="s6XCVcQ8RMTkyc8SmoMVVgJrVgs=">
<file path="utf-8.cpp" line="1" class="def"/>
<file short-path="utf-8.cpp" source-path="utf-8.cpp" line="1" class="def"/>
<return>
<type name="bool"/>
</return>
Expand Down
2 changes: 1 addition & 1 deletion test-files/golden-tests/filters/file/include-self.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<namespace id="//////////////////////////8=">
<namespace name="TEST" id="C0LRBO38MuY9CAdrRQ/gbJutBAw=">
<struct name="SUCCESS" id="3fDo50p0XYyS9fd9RqKyi2wjy/M=">
<file path="include-self.cpp" line="3" class="def"/>
<file short-path="include-self.cpp" source-path="include-self.cpp" line="3" class="def"/>
</struct>
</namespace>
</namespace>
Expand Down
10 changes: 5 additions & 5 deletions test-files/golden-tests/filters/symbol-name/blacklist_0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
<namespace id="//////////////////////////8=">
<namespace name="N0" id="YAQKEfxRmUjg43kkhT1n65uziVY=">
<function name="f0" id="YuxL8gi2heeMoessDsonV4JrAE8=">
<file path="blacklist_0.cpp" line="3"/>
<file path="blacklist_0.cpp" line="16"/>
<file short-path="blacklist_0.cpp" source-path="blacklist_0.cpp" line="3"/>
<file short-path="blacklist_0.cpp" source-path="blacklist_0.cpp" line="16"/>
</function>
</namespace>
<namespace name="N3" id="N8VuRKoz/jzHCbelCVCZNKALQAQ=">
</namespace>
<namespace name="N4" id="5KW7wj+cGonGOIACqamGb1QGyoA=">
<namespace name="N5" id="9FbH1+tQ3QOHCAntoCpAF02+8Kk=">
<function name="f1" id="sK1hKPIAxfUIYUOIhOe9SnoXfg0=">
<file path="blacklist_0.cpp" line="43"/>
<file short-path="blacklist_0.cpp" source-path="blacklist_0.cpp" line="43"/>
</function>
</namespace>
<namespace name="N6" id="MNQItF4HxM3PrWN0lDhIV9SWyJ0=">
<function name="f1" id="A2qNEj3jle5vHK6wdVGm8fPrpmA=">
<file path="blacklist_0.cpp" line="49"/>
<file short-path="blacklist_0.cpp" source-path="blacklist_0.cpp" line="49"/>
</function>
</namespace>
</namespace>
Expand All @@ -27,7 +27,7 @@
</namespace>
<namespace name="N9" id="u4ylO6On5J13jZbcYjnClcxqHJ4=">
<function name="g0" id="n2jChqyQNCb7NN3FjohUkKbB2M8=">
<file path="blacklist_0.cpp" line="64"/>
<file short-path="blacklist_0.cpp" source-path="blacklist_0.cpp" line="64"/>
</function>
</namespace>
</namespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<namespace name="B" id="kPgq2AM8TvyGDGm5jEWfqYlOPmY=">
<namespace name="U" id="X48ZrJbrwHITObLn6vxlcgApA20=">
<namespace-alias name="E" id="TDj0Rc30uD7P5e+F1gsHzHoaV2U=">
<file path="excluded-namespace-alias.cpp" line="7"/>
<file short-path="excluded-namespace-alias.cpp" source-path="excluded-namespace-alias.cpp" line="7"/>
<aliased name="B::S::E" id="3Bv76oUtQq1hnBVVkwioGwKa0oU="/>
</namespace-alias>
</namespace>
Expand Down
Loading

0 comments on commit 90fcf10

Please sign in to comment.