Added initial structure for include diagram generation

This commit is contained in:
Bartek Kryza
2022-04-04 23:55:37 +02:00
parent 0301173a52
commit 46e8885c41
39 changed files with 1268 additions and 356 deletions
@@ -0,0 +1,64 @@
/**
* src/package_diagram/generators/plantuml/package_diagram_generator.cc
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "include_diagram_generator.h"
#include "util/error.h"
namespace clanguml::include_diagram::generators::plantuml {
generator::generator(diagram_config &config, diagram_model &model)
: common_generator<diagram_config, diagram_model>{config, model}
{
}
void generator::generate_relationships(
const source_file &f, std::ostream &ostr) const
{
LOG_DBG("Generating relationships for file {}", f.full_name(true));
}
void generator::generate(const source_file &f, std::ostream &ostr) const
{
LOG_DBG("Generating source_file {}", f.name());
}
void generator::generate(std::ostream &ostr) const
{
ostr << "@startuml" << '\n';
generate_plantuml_directives(ostr, m_config.puml().before);
for (const auto &p : m_model) {
generate(dynamic_cast<source_file &>(*p), ostr);
ostr << '\n';
}
// Process package relationships
for (const auto &p : m_model) {
generate_relationships(dynamic_cast<source_file &>(*p), ostr);
ostr << '\n';
}
generate_config_layout_hints(ostr);
generate_plantuml_directives(ostr, m_config.puml().after);
ostr << "@enduml" << '\n';
}
}
@@ -0,0 +1,70 @@
/**
* src/include_diagram/generators/plantuml/include_diagram_generator.h
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "common/generators/plantuml/generator.h"
#include "common/model/package.h"
#include "common/model/relationship.h"
#include "config/config.h"
#include "cx/compilation_database.h"
#include "include_diagram/model/diagram.h"
#include "include_diagram/model/source_file.h"
#include "include_diagram/visitor/translation_unit_visitor.h"
#include "util/util.h"
#include <cppast/cpp_entity_index.hpp>
#include <cppast/libclang_parser.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
namespace clanguml {
namespace include_diagram {
namespace generators {
namespace plantuml {
using diagram_config = clanguml::config::include_diagram;
using diagram_model = clanguml::include_diagram::model::diagram;
template <typename C, typename D>
using common_generator =
clanguml::common::generators::plantuml::generator<C, D>;
using clanguml::common::model::access_t;
using clanguml::common::model::package;
using clanguml::common::model::relationship_t;
using clanguml::include_diagram::model::source_file;
using namespace clanguml::util;
class generator : public common_generator<diagram_config, diagram_model> {
public:
generator(diagram_config &config, diagram_model &model);
void generate_relationships(const source_file &p, std::ostream &ostr) const;
void generate(const source_file &e, std::ostream &ostr) const;
void generate(std::ostream &ostr) const;
};
}
}
}
}
+65
View File
@@ -0,0 +1,65 @@
/**
* src/include_diagram/model/diagram.cc
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "diagram.h"
#include "util/error.h"
#include "util/util.h"
namespace clanguml::include_diagram::model {
common::model::diagram_t diagram::type() const
{
return common::model::diagram_t::kPackage;
}
type_safe::optional_ref<const common::model::diagram_element> diagram::get(
const std::string &full_name) const
{
return get_file(full_name);
}
void diagram::add_file(std::unique_ptr<include_diagram::model::source_file> &&f)
{
LOG_DBG("Adding source file: {}, {}", f->name(), f->full_name(true));
files_.emplace_back(*f);
add_element(f->path(), std::move(f));
}
type_safe::optional_ref<const include_diagram::model::source_file>
diagram::get_file(const std::string &name) const
{
for (const auto &p : files_) {
if (p.get().full_name(false) == name) {
return {p};
}
}
return type_safe::nullopt;
}
std::string diagram::to_alias(const std::string &full_name) const
{
LOG_DBG("Looking for alias for {}", full_name);
return full_name;
}
}
+59
View File
@@ -0,0 +1,59 @@
/**
* src/include_diagram/model/diagram.h
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "common/model/diagram.h"
#include "common/model/package.h"
#include "source_file.h"
#include <type_safe/optional_ref.hpp>
#include <string>
#include <vector>
namespace clanguml::include_diagram::model {
class diagram : public clanguml::common::model::diagram,
public clanguml::common::model::nested_trait<source_file,
filesystem_path> {
public:
diagram() = default;
diagram(const diagram &) = delete;
diagram(diagram &&) = default;
diagram &operator=(const diagram &) = delete;
diagram &operator=(diagram &&) = default;
common::model::diagram_t type() const override;
type_safe::optional_ref<const common::model::diagram_element> get(
const std::string &full_name) const;
void add_file(std::unique_ptr<include_diagram::model::source_file> &&f);
type_safe::optional_ref<const include_diagram::model::source_file> get_file(
const std::string &name) const;
std::string to_alias(const std::string &full_name) const;
private:
std::vector<
type_safe::object_ref<const include_diagram::model::source_file, false>>
files_;
};
}
+19
View File
@@ -0,0 +1,19 @@
/**
* src/include_diagram/model/source_file.cc
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "source_file.h"
+95
View File
@@ -0,0 +1,95 @@
/**
* src/include_diagram/model/source_file.h
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "common/model/diagram_element.h"
#include "common/model/nested_trait.h"
#include "common/model/path.h"
#include "common/model/stylable_element.h"
#include "util/util.h"
#include <spdlog/spdlog.h>
#include <type_safe/optional_ref.hpp>
#include <set>
#include <string>
#include <vector>
namespace clanguml::include_diagram::model {
enum class source_file_type { kDirectory, kHeader, kImplementation };
struct fs_path_sep {
static constexpr std::string_view value = "/";
};
using filesystem_path = common::model::path<fs_path_sep>;
class source_file
: public common::model::diagram_element,
public common::model::stylable_element,
public common::model::nested_trait<common::model::diagram_element,
filesystem_path> {
public:
source_file() = default;
void set_path(const filesystem_path &p) { path_ = p; }
source_file(const source_file &) = delete;
source_file(source_file &&) = default;
source_file &operator=(const source_file &) = delete;
source_file &operator=(source_file &&) = delete;
const filesystem_path &path() const { return path_; }
std::string full_name(bool relative) const override
{
return (path_ | name()).to_string();
}
void add_file(std::unique_ptr<include_diagram::model::source_file> &&f)
{
LOG_DBG("Adding source file: {}, {}", f->name(), f->full_name(true));
add_element(f->path(), std::move(f));
}
private:
filesystem_path path_;
};
}
namespace std {
template <> struct hash<clanguml::include_diagram::model::filesystem_path> {
std::size_t operator()(
const clanguml::include_diagram::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) + 0x6a3712b5 + (seed << 6) +
(seed >> 2);
}
return seed;
}
};
}
@@ -0,0 +1,43 @@
/**
* src/include_diagram/model/visitor/element_visitor_context.cc
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "element_visitor_context.h"
#include "translation_unit_context.h"
namespace clanguml::include_diagram::visitor {
template <typename T>
element_visitor_context<T>::element_visitor_context(
clanguml::include_diagram::model::diagram &diagram, T &element)
: element_{element}
, diagram_{diagram}
{
}
template <typename T> T &element_visitor_context<T>::element()
{
return element_;
}
template <typename T>
clanguml::include_diagram::model::diagram &element_visitor_context<T>::diagram()
{
return diagram_;
}
}
@@ -0,0 +1,42 @@
/**
* src/include_diagram/model/visitor/element_visitor_context.h
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "include_diagram/model/diagram.h"
namespace clanguml::include_diagram::visitor {
class translation_unit_context;
template <typename T> class element_visitor_context {
public:
element_visitor_context(
clanguml::include_diagram::model::diagram &diagram, T &element);
T &element();
clanguml::include_diagram::model::diagram &diagram();
private:
translation_unit_context *ctx_;
T &element_;
clanguml::include_diagram::model::diagram &diagram_;
};
}
@@ -0,0 +1,63 @@
/**
* src/include_diagram/visitor/translation_unit_context.cc
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "translation_unit_context.h"
#include "cx/util.h"
namespace clanguml::include_diagram::visitor {
translation_unit_context::translation_unit_context(
cppast::cpp_entity_index &idx,
clanguml::include_diagram::model::diagram &diagram,
const clanguml::config::include_diagram &config)
: entity_index_{idx}
, diagram_{diagram}
, config_{config}
{
}
const cppast::cpp_entity_index &translation_unit_context::entity_index() const
{
return entity_index_;
}
const clanguml::config::include_diagram &
translation_unit_context::config() const
{
return config_;
}
clanguml::include_diagram::model::diagram &translation_unit_context::diagram()
{
return diagram_;
}
void translation_unit_context::set_current_file(
type_safe::optional_ref<include_diagram::model::source_file> f)
{
current_file_ = f;
}
type_safe::optional_ref<include_diagram::model::source_file>
translation_unit_context::get_current_file() const
{
return current_file_;
}
}
@@ -0,0 +1,62 @@
/**
* src/include_diagram/visitor/translation_unit_context.h
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "common/model/package.h"
#include "config/config.h"
#include "include_diagram/model/diagram.h"
#include <cppast/cpp_entity_index.hpp>
#include <cppast/cpp_namespace.hpp>
#include <cppast/cpp_type.hpp>
#include <type_safe/reference.hpp>
namespace clanguml::include_diagram::visitor {
class translation_unit_context {
public:
translation_unit_context(cppast::cpp_entity_index &idx,
clanguml::include_diagram::model::diagram &diagram,
const clanguml::config::include_diagram &config);
const cppast::cpp_entity_index &entity_index() const;
const clanguml::config::include_diagram &config() const;
clanguml::include_diagram::model::diagram &diagram();
void set_current_file(
type_safe::optional_ref<include_diagram::model::source_file> p);
type_safe::optional_ref<include_diagram::model::source_file>
get_current_file() const;
private:
// Reference to the cppast entity index
cppast::cpp_entity_index &entity_index_;
// Reference to the output diagram model
clanguml::include_diagram::model::diagram &diagram_;
// Reference to class diagram config
const clanguml::config::include_diagram &config_;
type_safe::optional_ref<include_diagram::model::source_file> current_file_;
};
}
@@ -0,0 +1,53 @@
/**
* src/include_diagram/visitor/translation_unit_visitor.cc
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "translation_unit_visitor.h"
#include <cppast/cpp_entity_kind.hpp>
#include <cppast/cpp_preprocessor.hpp>
namespace clanguml::include_diagram::visitor {
using clanguml::include_diagram::model::diagram;
translation_unit_visitor::translation_unit_visitor(
cppast::cpp_entity_index &idx,
clanguml::include_diagram::model::diagram &diagram,
const clanguml::config::include_diagram &config)
: ctx{idx, diagram, config}
{
}
void translation_unit_visitor::operator()(const cppast::cpp_entity &file)
{
cppast::visit(file,
[&, this](const cppast::cpp_entity &e, cppast::visitor_info info) {
if (e.kind() == cppast::cpp_entity_kind::include_directive_t) {
const auto &inc =
static_cast<const cppast::cpp_include_directive &>(e);
auto file_name = std::filesystem::path(inc.full_path());
auto f = std::make_unique<model::source_file>();
f->set_path(file_name.parent_path().string());
f->set_name(file_name.filename().string());
}
});
}
}
@@ -0,0 +1,51 @@
/**
* src/include_diagram/visitor/translation_unit_visitor.h
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "config/config.h"
#include "cx/cursor.h"
#include "include_diagram/model/diagram.h"
#include "include_diagram/visitor/translation_unit_context.h"
#include <clang-c/CXCompilationDatabase.h>
#include <clang-c/Index.h>
#include <cppast/visitor.hpp>
#include <type_safe/reference.hpp>
#include <common/model/enums.h>
#include <common/model/package.h>
#include <functional>
#include <map>
#include <memory>
#include <string>
namespace clanguml::include_diagram::visitor {
class translation_unit_visitor {
public:
translation_unit_visitor(cppast::cpp_entity_index &idx,
clanguml::include_diagram::model::diagram &diagram,
const clanguml::config::include_diagram &config);
void operator()(const cppast::cpp_entity &file);
private:
// ctx allows to track current visitor context, e.g. current namespace
translation_unit_context ctx;
};
}