Skip to content

Commit

Permalink
Add L prefix to line number html fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
danakj committed Dec 12, 2023
1 parent ebf41bb commit 3ff01a4
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 52 deletions.
1 change: 1 addition & 0 deletions .github/workflows/subdoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ jobs:
--project-name Subspace \
--remove-source-path-prefix $PWD \
--add-source-path-prefix ${source_url} \
--source-path-line-prefix L \
/home/runner/work/subspace/subspace/sus
- name: Deploy
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/try.yml
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ jobs:
--project-name Subspace \
--remove-source-path-prefix $PWD \
--add-source-path-prefix ${source_url} \
--source-path-line-prefix L \
/home/runner/work/subspace/subspace/sus
bench:
Expand Down
2 changes: 1 addition & 1 deletion subdoc/lib/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct SourceLink {
};
Quality quality;
std::string file_path;
u32 line;
std::string line;

friend std::weak_ordering operator<=>(const SourceLink& lhs,
const SourceLink& rhs) noexcept {
Expand Down
11 changes: 11 additions & 0 deletions subdoc/lib/run_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ struct RunOptions {
macro_prefixes = sus::move(prefixes);
return sus::move(*this);
}
RunOptions set_generate_source_links(bool gen) && {
generate_source_links = gen;
return sus::move(*this);
}
RunOptions set_remove_path_prefix(sus::Option<std::string> prefix) && {
remove_path_prefix = sus::move(prefix);
return sus::move(*this);
Expand All @@ -57,6 +61,10 @@ struct RunOptions {
add_path_prefix = sus::move(prefix);
return sus::move(*this);
}
RunOptions set_source_line_prefix(sus::Option<std::string> prefix) && {
source_line_prefix = sus::move(prefix);
return sus::move(*this);
}

/// Whether to print progress while collecting documentation from souce files.
bool show_progress = true;
Expand Down Expand Up @@ -84,6 +92,9 @@ struct RunOptions {
/// A prefix to add to all paths in source links, after removing the prefix
/// specified by `remove_path_prefix`.
sus::Option<std::string> add_path_prefix;
/// A prefix to add to the source code line number html fragment. Github uses
/// an `L` as its prefix.
sus::Option<std::string> source_line_prefix;
};

} // namespace subdoc
19 changes: 15 additions & 4 deletions subdoc/lib/visit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1583,9 +1583,14 @@ class Visitor : public clang::RecursiveASTVisitor<Visitor> {
std::replace(canonical_path.begin(), canonical_path.end(), '\\', '/');

if (cx_.options.remove_path_prefix.is_some()) {
const auto& prefix = cx_.options.remove_path_prefix.as_value();
if (canonical_path.starts_with(prefix)) {
canonical_path = canonical_path.substr(prefix.size());
// Canonicalize the path to use `/` instead of `\`.
std::string canonical_prefix =
sus::clone(cx_.options.remove_path_prefix.as_value());
std::replace(canonical_prefix.begin(), canonical_prefix.end(), '\\',
'/');

if (canonical_path.starts_with(canonical_prefix)) {
canonical_path = canonical_path.substr(canonical_prefix.size());
if (canonical_path.starts_with("/"))
canonical_path = canonical_path.substr(1u);
}
Expand All @@ -1596,10 +1601,16 @@ class Visitor : public clang::RecursiveASTVisitor<Visitor> {
canonical_path.insert(0u, prefix);
}

std::ostringstream line;
if (cx_.options.source_line_prefix.is_some()) {
line << cx_.options.source_line_prefix.as_value();
}
line << sm.getLineNumber(sm.getFileID(loc), sm.getFileOffset(loc));

auto link = sus::Option<SourceLink>(SourceLink{
.quality = quality,
.file_path = sus::move(canonical_path),
.line = sm.getLineNumber(sm.getFileID(loc), sm.getFileOffset(loc)),
.line = sus::move(line).str(),
});
if (link > e.source_link) e.source_link = sus::move(link);
}
Expand Down
16 changes: 12 additions & 4 deletions subdoc/subdoc_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ int main(int argc, const char** argv) {
"specified by `--remove-source-path-prefix` is removed."),
llvm::cl::cat(option_category));

llvm::cl::opt<std::string> option_source_line_prefix(
"source-path-line-prefix",
llvm::cl::desc("A prefix to add to the line number on all source code "
"links. Github uses an `L` as its prefix."),
llvm::cl::cat(option_category));

llvm::cl::opt<bool> option_no_source_links(
"no-source-links",
llvm::cl::desc("Avoid generating links to source code."),
Expand Down Expand Up @@ -228,14 +234,16 @@ int main(int argc, const char** argv) {
.collect<sus::Vec<std::string>>();
run_options.generate_source_links = !option_no_source_links.getValue();
if (option_remove_path_prefix.getNumOccurrences() > 0) {
// Canonicalize the path to use `/` instead of `\`.
std::string canonical_path = option_remove_path_prefix.getValue();
std::replace(canonical_path.begin(), canonical_path.end(), '\\', '/');
run_options.remove_path_prefix = sus::some(sus::move(canonical_path));
run_options.remove_path_prefix =
sus::some(sus::move(option_remove_path_prefix.getValue()));
}
if (option_add_path_prefix.getNumOccurrences() > 0) {
run_options.add_path_prefix = sus::some(option_add_path_prefix.getValue());
}
if (option_source_line_prefix.getNumOccurrences() > 0) {
run_options.source_line_prefix =
sus::some(option_source_line_prefix.getValue());
}

auto fs = llvm::vfs::getRealFileSystem();
auto result = subdoc::run_files(comp_db, sus::move(run_against_files),
Expand Down
24 changes: 12 additions & 12 deletions subdoc/tests/aliases_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ TEST_F(SubDocTest, AliasUsingStruct) {
auto& e = db.find_alias_comment("7:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 8u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "8");
}
{
auto& e = db.find_alias_comment("10:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 11u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "11");
}
}

Expand All @@ -66,13 +66,13 @@ TEST_F(SubDocTest, AliasUsingConcept) {
auto& e = db.find_alias_comment("7:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 8u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "8");
}
{
auto& e = db.find_alias_comment("10:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 11u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "11");
}
}

Expand All @@ -98,7 +98,7 @@ TEST_F(SubDocTest, AliasUsingMethod) {
auto& e = db.find_alias_comment("10:7").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 11u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "11");
}

TEST_F(SubDocTest, AliasUsingEnumInNamespace) {
Expand All @@ -114,7 +114,7 @@ TEST_F(SubDocTest, AliasUsingEnumInNamespace) {
auto& e = db.find_alias_comment("3:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 4u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "4");
}

TEST_F(SubDocTest, DISABLED_AliasUsingEnum) {
Expand All @@ -141,13 +141,13 @@ TEST_F(SubDocTest, DISABLED_AliasUsingEnum) {
auto& e = db.find_alias_comment("8:7").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 9u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "9");
}
{
auto& e = db.find_alias_comment("13:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 14u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "14");
}
}

Expand Down Expand Up @@ -185,13 +185,13 @@ TEST_F(SubDocTest, AliasNamedTypeInNamespace) {
auto& e = db.find_alias_comment("7:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 8u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "8");
}
{
auto& e = db.find_alias_comment("10:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 11u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "11");
}
}

Expand All @@ -213,7 +213,7 @@ TEST_F(SubDocTest, AliasNamedTypeInRecord) {
auto& e = db.find_alias_comment("7:7").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 8u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "8");
}
}

Expand All @@ -233,6 +233,6 @@ TEST_F(SubDocTest, AliasUsingVariable) {
auto& e = db.find_alias_comment("6:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 7u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "7");
}
}
10 changes: 5 additions & 5 deletions subdoc/tests/fields_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TEST_F(SubDocTest, Field) {
auto& e = db.find_field_comment("3:7").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 4u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "4");
}

TEST_F(SubDocTest, StaticField) {
Expand All @@ -43,7 +43,7 @@ TEST_F(SubDocTest, StaticField) {
auto& e = db.find_field_comment("3:7").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 4u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "4");
}

TEST_F(SubDocTest, StaticFieldSplit) {
Expand All @@ -61,7 +61,7 @@ TEST_F(SubDocTest, StaticFieldSplit) {
auto& e = db.find_field_comment("3:7").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 7u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "7");
}

TEST_F(SubDocTest, PrivateField) {
Expand Down Expand Up @@ -133,12 +133,12 @@ TEST_F(SubDocTest, Variables) {
auto& e = db.find_variable_comment("2:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 3u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "3");
}
{
auto& e = db.find_variable_comment("5:7").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 6u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "6");
}
}
26 changes: 13 additions & 13 deletions subdoc/tests/functions_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TEST_F(SubDocTest, Function) {
auto& e = db.find_function_comment("2:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 3u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "3");
}

TEST_F(SubDocTest, FunctionOverloads) {
Expand All @@ -47,13 +47,13 @@ TEST_F(SubDocTest, FunctionOverloads) {
auto& e = db.find_function_comment("2:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 3u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "3");
}
{
auto& e = db.find_function_comment("7:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 8u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "8");
}
}

Expand All @@ -80,13 +80,13 @@ TEST_F(SubDocTest, FunctionOverloadsNoMerge) {
auto& e = db.find_function_comment("2:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 6u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "6");
}
{
auto& e = db.find_function_comment("7:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 11u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "11");
}
}

Expand All @@ -109,13 +109,13 @@ TEST_F(SubDocTest, FunctionOverloadsMerge) {
auto& e = db.find_function_comment("2:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 4u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "4");
}
{
auto& e = db.find_function_comment("7:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 9u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "9");
}
}

Expand Down Expand Up @@ -205,7 +205,7 @@ TEST_F(SubDocTest, ForwardDeclDocumented) {
auto& e = db.find_function_comment("2:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 4u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "4");
}

TEST_F(SubDocTest, ForwardDeclUndocumented) {
Expand All @@ -222,7 +222,7 @@ TEST_F(SubDocTest, ForwardDeclUndocumented) {
auto& e = db.find_function_comment("3:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 4u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "4");
}

TEST_F(SubDocTest, FunctionInNamedNamespace) {
Expand All @@ -239,7 +239,7 @@ TEST_F(SubDocTest, FunctionInNamedNamespace) {
auto& e = db.find_function_comment("3:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 4u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "4");
}

TEST_F(SubDocTest, FunctionInAnonymousNamespace) {
Expand Down Expand Up @@ -294,19 +294,19 @@ TEST_F(SubDocTest, FunctionFriend) {
auto& e = db.find_function_comment("3:7").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 4u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "4");
}
{
auto& e = db.find_function_comment("7:7").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 11u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "11");
}
{
auto& e = db.find_function_comment("13:5").unwrap();
ASSERT_TRUE(e.source_link.is_some());
EXPECT_EQ(e.source_link.as_ref().unwrap().file_path, "test.cc");
EXPECT_EQ(e.source_link.as_ref().unwrap().line, 14u);
EXPECT_EQ(e.source_link.as_ref().unwrap().line, "14");
}
}

Expand Down
Loading

0 comments on commit 3ff01a4

Please sign in to comment.