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

Fix manual parsing of Windows source location paths (#217) #220

Merged
merged 1 commit into from
Dec 15, 2023
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
13 changes: 12 additions & 1 deletion src/common/clang_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -836,12 +836,23 @@ bool parse_source_location(const std::string &location_str, std::string &file,
if (tokens.size() < 3)
return false;

if (tokens.size() == 4) {
// Handle Windows paths
decltype(tokens) tmp_tokens{};
tmp_tokens.emplace_back(
fmt::format("{}:{}", tokens.at(0), tokens.at(1)));
tmp_tokens.emplace_back(tokens.at(2));
tmp_tokens.emplace_back(tokens.at(3));

tokens = std::move(tmp_tokens);
}

file = tokens.at(0);
try {
line = std::stoi(tokens.at(1));
}
catch (std::invalid_argument &e) {
line = 0;
return false;
}

try {
Expand Down
2 changes: 1 addition & 1 deletion src/common/visitor/translation_unit_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void translation_unit_visitor::set_source_location(
file_path = fs::absolute(file_path);
}

file_path = fs::canonical(file_path);
file_path = fs::weakly_canonical(file_path);

file = file_path.string();

Expand Down
44 changes: 44 additions & 0 deletions tests/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,47 @@ TEST_CASE("Test is_relative_to", "[unit-test]")
CHECK(is_relative_to(child, base1));
CHECK_FALSE(is_relative_to(child, base2));
}

TEST_CASE("Test parse_source_location", "[unit-test]")
{
using namespace clanguml::common;

const std::string int_template_str{"ns1::ns2::class1<int>"};

std::string file;
unsigned line{};
unsigned column{};
bool result = false;

result = parse_source_location("/a/b/c/d/test.cpp:123", file, line, column);

CHECK_FALSE(result);

result = false, file = "", line = 0, column = 0;
result =
parse_source_location("/a/b/c/d/test.cpp:123:456", file, line, column);

CHECK(result);
CHECK(file == "/a/b/c/d/test.cpp");
CHECK(line == 123);
CHECK(column == 456);

result = false, file = "", line = 0, column = 0;

result = parse_source_location(
"E:\\test\\src\\main.cpp:123", file, line, column);

CHECK_FALSE(result);

result = false, file = "", line = 0, column = 0;

result = parse_source_location(
"E:\\test\\src\\main.cpp:123:456", file, line, column);

CHECK(result);
CHECK(file == "E:\\test\\src\\main.cpp");
CHECK(line == 123);
CHECK(column == 456);

result = false, file = "", line = 0, column = 0;
}