Extended source_location with column and translation unit info

This commit is contained in:
Bartek Kryza
2023-06-12 00:42:37 +02:00
parent 84ec4733cc
commit 43cc5cb49f
7 changed files with 55 additions and 27 deletions

View File

@@ -1,5 +1,7 @@
# CHANGELOG # CHANGELOG
* Extended source_location with column and translation unit info
### 0.3.7 ### 0.3.7
* Added regexp support to selected diagram filters (#51, #132) * Added regexp support to selected diagram filters (#51, #132)
* Added method type diagram filter (#145) * Added method type diagram filter (#145)

View File

@@ -151,6 +151,8 @@ public:
{ {
} }
TranslationUnitVisitor &visitor() { return visitor_; }
void HandleTranslationUnit(clang::ASTContext &ast_context) override void HandleTranslationUnit(clang::ASTContext &ast_context) override
{ {
visitor_.TraverseDecl(ast_context.getTranslationUnitDecl()); visitor_.TraverseDecl(ast_context.getTranslationUnitDecl());
@@ -172,15 +174,22 @@ public:
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer( std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
clang::CompilerInstance &CI, clang::StringRef /*file*/) override clang::CompilerInstance &CI, clang::StringRef /*file*/) override
{ {
return std::make_unique< auto ast_consumer = std::make_unique<
diagram_ast_consumer<DiagramModel, DiagramConfig, DiagramVisitor>>( diagram_ast_consumer<DiagramModel, DiagramConfig, DiagramVisitor>>(
CI, diagram_, config_); CI, diagram_, config_);
if constexpr (!std::is_same_v<DiagramModel,
clanguml::include_diagram::model::diagram>) {
ast_consumer->visitor().set_tu_path(getCurrentFile().str());
}
return ast_consumer;
} }
protected: protected:
bool BeginSourceFileAction(clang::CompilerInstance &ci) override 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<DiagramModel, if constexpr (std::is_same_v<DiagramModel,
clanguml::include_diagram::model::diagram>) { clanguml::include_diagram::model::diagram>) {

View File

@@ -32,7 +32,9 @@ std::string render_name(std::string name)
void to_json(nlohmann::json &j, const source_location &sl) 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) void to_json(nlohmann::json &j, const element &c)

View File

@@ -132,29 +132,6 @@ private:
}; };
} // namespace clanguml::common::model } // namespace clanguml::common::model
namespace std {
/*
template <> struct hash<clanguml::common::model::filesystem_path> {
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<std::string>{}(ns) +
clanguml::util::hash_seed(seed);
}
return seed;
}
};
*/
} // namespace std
namespace std { namespace std {
template <> template <>
struct hash<std::reference_wrapper<clanguml::common::model::source_file>> { struct hash<std::reference_wrapper<clanguml::common::model::source_file>> {

View File

@@ -40,10 +40,21 @@ public:
void set_file_relative(const std::string &file) { file_relative_ = file; } 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_; } unsigned int line() const { return line_; }
void set_line(const unsigned line) { line_ = 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_; } unsigned int location_id() const { return hash_; }
void set_location_id(unsigned int h) { hash_ = h; } void set_location_id(unsigned int h) { hash_ = h; }
@@ -51,7 +62,9 @@ public:
private: private:
std::string file_; std::string file_;
std::string file_relative_; std::string file_relative_;
std::string translation_unit_;
unsigned int line_{0}; unsigned int line_{0};
unsigned int column_{0};
unsigned int hash_{0}; unsigned int hash_{0};
}; };
} // namespace clanguml::common::model } // namespace clanguml::common::model

View File

@@ -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 clang::SourceManager &translation_unit_visitor::source_manager() const
{ {
return source_manager_; return source_manager_;
@@ -87,7 +100,7 @@ void translation_unit_visitor::set_source_location(
{ {
std::string file; std::string file;
unsigned line{}; unsigned line{};
[[maybe_unused]] unsigned column{}; unsigned column{};
if (location.isValid()) { if (location.isValid()) {
file = source_manager_.getFilename(location).str(); file = source_manager_.getFilename(location).str();
@@ -113,7 +126,9 @@ void translation_unit_visitor::set_source_location(
element.set_file(file); element.set_file(file);
element.set_file_relative(util::path_to_url( element.set_file_relative(util::path_to_url(
std::filesystem::relative(element.file(), relative_to_path_).string())); std::filesystem::relative(element.file(), relative_to_path_).string()));
element.set_translation_unit(tu_path().string());
element.set_line(line); element.set_line(line);
element.set_column(column);
element.set_location_id(location.getHashValue()); element.set_location_id(location.getHashValue());
} }

View File

@@ -55,6 +55,14 @@ public:
virtual ~translation_unit_visitor() = default; 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 * @brief Get clang::SourceManager
* @return Reference to @link clang::SourceManager used by this translation * @return Reference to @link clang::SourceManager used by this translation
@@ -108,5 +116,7 @@ private:
std::unique_ptr<comment::comment_visitor> comment_visitor_; std::unique_ptr<comment::comment_visitor> comment_visitor_;
std::filesystem::path relative_to_path_; std::filesystem::path relative_to_path_;
std::filesystem::path translation_unit_path_;
}; };
} // namespace clanguml::common::visitor } // namespace clanguml::common::visitor