Cleaned up code

This commit is contained in:
Bartek Kryza
2022-08-28 19:46:01 +02:00
parent 0701a082e9
commit feb19c23c7
10 changed files with 150 additions and 166 deletions

View File

@@ -18,8 +18,86 @@
#include "clang_utils.h"
#include <clang/Lex/Preprocessor.h>
namespace clanguml::common {
std::optional<clanguml::common::model::namespace_> get_enclosing_namespace(
const clang::DeclContext *decl)
{
if (!decl->getEnclosingNamespaceContext()->isNamespace())
return {};
const auto *namespace_declaration =
clang::cast<clang::NamespaceDecl>(decl->getEnclosingNamespaceContext());
if (namespace_declaration == nullptr) {
return {};
}
return clanguml::common::model::namespace_{
common::get_qualified_name(*namespace_declaration)};
}
std::string to_string(const clang::QualType &type, const clang::ASTContext &ctx,
bool try_canonical)
{
const clang::PrintingPolicy print_policy(ctx.getLangOpts());
auto result{type.getAsString(print_policy)};
if (try_canonical && result.find('<') != std::string::npos) {
auto canonical_type_name =
type.getCanonicalType().getAsString(print_policy);
auto result_qualified_template_name =
result.substr(0, result.find('<'));
auto result_template_arguments = result.substr(result.find('<'));
auto canonical_qualified_template_name =
canonical_type_name.substr(0, canonical_type_name.find('<'));
// Choose the longer name (why do I have to do this?)
if (result_qualified_template_name.size() <
canonical_qualified_template_name.size()) {
result =
canonical_qualified_template_name + result_template_arguments;
}
}
// Remove trailing spaces after commas in template arguments
clanguml::util::replace_all(result, ", ", ",");
return result;
}
std::string to_string(const clang::RecordType &type,
const clang::ASTContext &ctx, bool try_canonical)
{
return to_string(type.desugar(), ctx, try_canonical);
}
std::string get_source_text_raw(
clang::SourceRange range, const clang::SourceManager &sm)
{
return clang::Lexer::getSourceText(
clang::CharSourceRange::getCharRange(range), sm, clang::LangOptions())
.str();
}
std::string get_source_text(
clang::SourceRange range, const clang::SourceManager &sm)
{
clang::LangOptions lo;
auto start_loc = sm.getSpellingLoc(range.getBegin());
auto last_token_loc = sm.getSpellingLoc(range.getEnd());
auto end_loc = clang::Lexer::getLocForEndOfToken(last_token_loc, 0, sm, lo);
auto printable_range = clang::SourceRange{start_loc, end_loc};
return get_source_text_raw(printable_range, sm);
}
template <> id_t to_id(const std::string &full_name)
{
return std::hash<std::string>{}(full_name) >> 3;
@@ -52,8 +130,6 @@ template <> id_t to_id(const clang::CXXRecordDecl &declaration)
template <> id_t to_id(const clang::EnumType &t) { return to_id(*t.getDecl()); }
// template <> id_t to_id(const clang::TemplateSpecializationType &t);
template <> id_t to_id(const std::filesystem::path &file)
{
return to_id(file.lexically_normal().string());
@@ -74,5 +150,4 @@ template <> id_t to_id(const clang::TemplateArgument &template_argument)
throw std::runtime_error("Cannot generate id for template argument");
}
}

View File

@@ -39,6 +39,21 @@ template <typename T> std::string get_qualified_name(const T &declaration)
return qualified_name;
}
std::optional<clanguml::common::model::namespace_> get_enclosing_namespace(
const clang::DeclContext *decl);
std::string to_string(const clang::QualType &type, const clang::ASTContext &ctx,
bool try_canonical = true);
std::string to_string(const clang::RecordType &type,
const clang::ASTContext &ctx, bool try_canonical = true);
std::string get_source_text_raw(
clang::SourceRange range, const clang::SourceManager &sm);
std::string get_source_text(
clang::SourceRange range, const clang::SourceManager &sm);
template <typename T> id_t to_id(const T &declaration);
template <> id_t to_id(const std::string &full_name);

View File

@@ -198,8 +198,8 @@ void generator<C, D>::generate_plantuml_directives(
directive.replace(std::get<1>(alias_match),
std::get<2>(alias_match), element_opt.value().alias());
else {
LOG_ERROR(
"CANNOT FIND ALIAS TO ELEMENT {}", full_name.to_string());
LOG_ERROR("Cannot find clang-uml alias for element {}",
full_name.to_string());
directive.replace(std::get<1>(alias_match),
std::get<2>(alias_match), "UNKNOWN_ALIAS");
}
@@ -262,9 +262,6 @@ public:
{
visitor_.TraverseDecl(ast_context.getTranslationUnitDecl());
visitor_.finalize();
// LOG_DBG("= Finalized translation unit: {}",
// ast_context.getTranslationUnitDecl());
}
};

View File

@@ -229,8 +229,6 @@ tvl::value_t subclass_filter::match(const diagram &d, const element &e) const
if (!class_ref.has_value())
return false;
LOG_DBG("====================== LOOKING FOR PARENTS OF {} ===========", fn);
parents.emplace(class_ref.value());
cd.get_parents(parents);
@@ -239,15 +237,11 @@ tvl::value_t subclass_filter::match(const diagram &d, const element &e) const
for (const auto p : parents)
parents_names.push_back(p.get().full_name(false));
LOG_DBG("====================== FOUND PARENTS {} ==========",
fmt::join(parents_names, ", "));
// Now check if any of the parents matches the roots specified in the
// filter config
for (const auto &root : roots_) {
for (const auto &parent : parents) {
auto full_name = parent.get().full_name(false);
LOG_DBG("+++ COMPARING {} WITH {}", root, full_name);
if (root == full_name)
return true;
}