From 66dcf1ed5d760281ca8262b1f894bae2f21dc10e Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Tue, 9 Jan 2024 22:13:39 +0100 Subject: [PATCH] Added is_system header file property to JSON include diagram generator --- src/common/model/source_file.h | 15 +++++++++++++++ .../generators/json/include_diagram_generator.cc | 3 +++ .../visitor/translation_unit_visitor.cc | 2 ++ tests/t40001/test_case.h | 6 +++--- tests/test_cases.h | 14 ++++++++++++++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/common/model/source_file.h b/src/common/model/source_file.h index b3a88a65..24cf26d5 100644 --- a/src/common/model/source_file.h +++ b/src/common/model/source_file.h @@ -117,6 +117,20 @@ public: */ source_file_t type() const { return type_; } + /** + * Set whether the file is a system header + * + * @param is_system Whether the file is a system header + */ + void set_system_header(bool is_system) { is_system_header_ = is_system; } + + /** + * Is the file a system header? + * + * @return True, if the source file is a system header + */ + bool is_system_header() const { return is_system_header_; } + /** * Get the source file's parent path. * @@ -185,6 +199,7 @@ private: filesystem_path path_{path_type::kFilesystem}; source_file_t type_{source_file_t::kDirectory}; bool is_absolute_{false}; + bool is_system_header_{false}; }; } // namespace clanguml::common::model diff --git a/src/include_diagram/generators/json/include_diagram_generator.cc b/src/include_diagram/generators/json/include_diagram_generator.cc index 309cc6ee..a1c6853b 100644 --- a/src/include_diagram/generators/json/include_diagram_generator.cc +++ b/src/include_diagram/generators/json/include_diagram_generator.cc @@ -78,6 +78,9 @@ void generator::generate(const source_file &f, nlohmann::json &parent) const j["type"] = "file"; j["file_kind"] = to_string(f.type()); + if (f.type() == common::model::source_file_t::kHeader) { + j["is_system"] = f.is_system_header(); + } parent["elements"].push_back(std::move(j)); } diff --git a/src/include_diagram/visitor/translation_unit_visitor.cc b/src/include_diagram/visitor/translation_unit_visitor.cc index 9b12cd15..000c213b 100644 --- a/src/include_diagram/visitor/translation_unit_visitor.cc +++ b/src/include_diagram/visitor/translation_unit_visitor.cc @@ -147,6 +147,7 @@ void translation_unit_visitor::include_visitor::process_internal_header( include_file.set_file( std::filesystem::absolute(include_path).lexically_normal().string()); include_file.set_line(0); + include_file.set_system_header(is_system); // Add relationship from the currently parsed source file to this // include file @@ -174,6 +175,7 @@ void translation_unit_visitor::include_visitor::process_external_system_header( f->set_name(include_path.string()); f->set_type(common::model::source_file_t::kHeader); f->set_id(common::to_id(include_path)); + f->set_system_header(true); const auto f_id = f->id(); diff --git a/tests/t40001/test_case.h b/tests/t40001/test_case.h index c07e8671..1bdef77c 100644 --- a/tests/t40001/test_case.h +++ b/tests/t40001/test_case.h @@ -67,10 +67,10 @@ TEST_CASE("t40001", "[test-case][include]") REQUIRE(IsFolder(j, "include/lib1")); REQUIRE(IsFolder(j, "src")); - REQUIRE(IsFile(j, "include/lib1/lib1.h")); - REQUIRE(IsFile(j, "include/t40001_include1.h")); + REQUIRE(IsHeader(j, "include/lib1/lib1.h")); + REQUIRE(IsHeader(j, "include/t40001_include1.h")); REQUIRE(IsFile(j, "src/t40001.cc")); - REQUIRE(IsFile(j, "yaml-cpp/yaml.h")); + REQUIRE(IsSystemHeader(j, "yaml-cpp/yaml.h")); REQUIRE(IsFile(j, "string")); diff --git a/tests/test_cases.h b/tests/test_cases.h index a1482ef3..c6662815 100644 --- a/tests/test_cases.h +++ b/tests/test_cases.h @@ -1391,6 +1391,20 @@ bool IsFile(const nlohmann::json &j, const std::string &name) return e && e->at("type") == "file"; } +bool IsSystemHeader(const nlohmann::json &j, const std::string &name) +{ + auto e = get_element(j, name); + return e && e->at("type") == "file" && e->at("file_kind") == "header" && + e->at("is_system"); +} + +bool IsHeader(const nlohmann::json &j, const std::string &name) +{ + auto e = get_element(j, name); + return e && e->at("type") == "file" && e->at("file_kind") == "header" && + !e->at("is_system"); +} + bool IsDeprecated(const nlohmann::json &j, const std::string &name) { auto e = get_element(j, expand_name(j, name));