diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ea12794..6070581b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # CHANGELOG + * Extended source_location with column and translation unit info + ### 0.3.7 * Added regexp support to selected diagram filters (#51, #132) * Added method type diagram filter (#145) diff --git a/src/common/generators/generators.h b/src/common/generators/generators.h index 557319d9..2cd4630c 100644 --- a/src/common/generators/generators.h +++ b/src/common/generators/generators.h @@ -151,6 +151,8 @@ public: { } + TranslationUnitVisitor &visitor() { return visitor_; } + void HandleTranslationUnit(clang::ASTContext &ast_context) override { visitor_.TraverseDecl(ast_context.getTranslationUnitDecl()); @@ -172,15 +174,22 @@ public: std::unique_ptr CreateASTConsumer( clang::CompilerInstance &CI, clang::StringRef /*file*/) override { - return std::make_unique< + auto ast_consumer = std::make_unique< diagram_ast_consumer>( CI, diagram_, config_); + + if constexpr (!std::is_same_v) { + ast_consumer->visitor().set_tu_path(getCurrentFile().str()); + } + + return ast_consumer; } protected: bool BeginSourceFileAction(clang::CompilerInstance &ci) override { - LOG_DBG("Visiting source file: {}", getCurrentFile().str()); + LOG_WARN("Visiting source file: {}", getCurrentFile().str()); if constexpr (std::is_same_v) { diff --git a/src/common/generators/json/generator.cc b/src/common/generators/json/generator.cc index 1a5f8b0c..496970a5 100644 --- a/src/common/generators/json/generator.cc +++ b/src/common/generators/json/generator.cc @@ -32,7 +32,9 @@ std::string render_name(std::string name) void to_json(nlohmann::json &j, const source_location &sl) { - j = json{{"file", sl.file_relative()}, {"line", sl.line()}}; + j = json{{"file", sl.file_relative()}, + {"translation_unit", sl.translation_unit()}, {"line", sl.line()}, + {"column", sl.column()}}; } void to_json(nlohmann::json &j, const element &c) diff --git a/src/common/model/source_file.h b/src/common/model/source_file.h index 11401239..50cd8f8a 100644 --- a/src/common/model/source_file.h +++ b/src/common/model/source_file.h @@ -132,29 +132,6 @@ private: }; } // namespace clanguml::common::model -namespace std { - -/* - template <> struct hash { - std::size_t operator()( - const clanguml::common::model::filesystem_path &key) const - { - using clanguml::common::model::path; - - std::size_t seed = key.size(); - for (const auto &ns : key) { - seed ^= - std::hash{}(ns) + - clanguml::util::hash_seed(seed); - } - - return seed; - } - }; -*/ - -} // namespace std - namespace std { template <> struct hash> { diff --git a/src/common/model/source_location.h b/src/common/model/source_location.h index 3b2e89f5..e4a5e692 100644 --- a/src/common/model/source_location.h +++ b/src/common/model/source_location.h @@ -40,10 +40,21 @@ public: void set_file_relative(const std::string &file) { file_relative_ = file; } + const std::string &translation_unit() const { return translation_unit_; } + + void set_translation_unit(const std::string &translation_unit) + { + translation_unit_ = translation_unit; + } + unsigned int line() const { return line_; } void set_line(const unsigned line) { line_ = line; } + unsigned int column() const { return column_; } + + void set_column(const unsigned column) { column_ = column; } + unsigned int location_id() const { return hash_; } void set_location_id(unsigned int h) { hash_ = h; } @@ -51,7 +62,9 @@ public: private: std::string file_; std::string file_relative_; + std::string translation_unit_; unsigned int line_{0}; + unsigned int column_{0}; unsigned int hash_{0}; }; } // namespace clanguml::common::model \ No newline at end of file diff --git a/src/common/visitor/translation_unit_visitor.cc b/src/common/visitor/translation_unit_visitor.cc index 82014933..9dea82ea 100644 --- a/src/common/visitor/translation_unit_visitor.cc +++ b/src/common/visitor/translation_unit_visitor.cc @@ -38,6 +38,19 @@ translation_unit_visitor::translation_unit_visitor( } } +void translation_unit_visitor::set_tu_path( + const std::string &translation_unit_path) +{ + translation_unit_path_ = relative( + std::filesystem::path{translation_unit_path}, relative_to_path_); + translation_unit_path_.make_preferred(); +} + +const std::filesystem::path &translation_unit_visitor::tu_path() const +{ + return translation_unit_path_; +} + clang::SourceManager &translation_unit_visitor::source_manager() const { return source_manager_; @@ -87,7 +100,7 @@ void translation_unit_visitor::set_source_location( { std::string file; unsigned line{}; - [[maybe_unused]] unsigned column{}; + unsigned column{}; if (location.isValid()) { file = source_manager_.getFilename(location).str(); @@ -113,7 +126,9 @@ void translation_unit_visitor::set_source_location( element.set_file(file); element.set_file_relative(util::path_to_url( std::filesystem::relative(element.file(), relative_to_path_).string())); + element.set_translation_unit(tu_path().string()); element.set_line(line); + element.set_column(column); element.set_location_id(location.getHashValue()); } diff --git a/src/common/visitor/translation_unit_visitor.h b/src/common/visitor/translation_unit_visitor.h index 4faaf2f0..44139f51 100644 --- a/src/common/visitor/translation_unit_visitor.h +++ b/src/common/visitor/translation_unit_visitor.h @@ -55,6 +55,14 @@ public: virtual ~translation_unit_visitor() = default; + void set_tu_path(const std::string &translation_unit_path); + + /** + * @brief Return relative path to current translation unit + * @return Current translation unit path + */ + const std::filesystem::path &tu_path() const; + /** * @brief Get clang::SourceManager * @return Reference to @link clang::SourceManager used by this translation @@ -108,5 +116,7 @@ private: std::unique_ptr comment_visitor_; std::filesystem::path relative_to_path_; + + std::filesystem::path translation_unit_path_; }; } // namespace clanguml::common::visitor