Fix manual parsing of Windows source location paths (#217)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user