diff --git a/src/common/clang_utils.cc b/src/common/clang_utils.cc index 22879a3e..4f8491ab 100644 --- a/src/common/clang_utils.cc +++ b/src/common/clang_utils.cc @@ -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 { diff --git a/src/common/visitor/translation_unit_visitor.cc b/src/common/visitor/translation_unit_visitor.cc index e30deeee..451959ac 100644 --- a/src/common/visitor/translation_unit_visitor.cc +++ b/src/common/visitor/translation_unit_visitor.cc @@ -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(); diff --git a/tests/test_util.cc b/tests/test_util.cc index 193e6293..2658cbcb 100644 --- a/tests/test_util.cc +++ b/tests/test_util.cc @@ -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"}; + + 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; +} \ No newline at end of file