Applied cppcoreguidelines-pro-type-static-cast-downcast nullptr access warnings

This commit is contained in:
Bartek Kryza
2022-12-23 22:37:05 +01:00
parent f07dc35e06
commit c1d7f28f57
3 changed files with 57 additions and 48 deletions

View File

@@ -34,9 +34,7 @@ using clanguml::class_diagram::model::diagram;
using clanguml::class_diagram::model::enum_;
using clanguml::class_diagram::model::method_parameter;
using clanguml::class_diagram::model::template_parameter;
using clanguml::class_diagram::model::type_alias;
using clanguml::common::model::access_t;
using clanguml::common::model::decorated_element;
using clanguml::common::model::namespace_;
using clanguml::common::model::relationship;
using clanguml::common::model::relationship_t;
@@ -132,18 +130,18 @@ bool translation_unit_visitor::VisitEnumDecl(clang::EnumDecl *enm)
// - the parent is a regular C++ class/struct
// - the parent is a class template declaration/specialization
std::optional<common::model::diagram_element::id_t> id_opt;
int64_t local_id =
static_cast<const clang::RecordDecl *>(parent)->getID();
const auto *parent_record_decl =
clang::dyn_cast<clang::RecordDecl>(parent);
int64_t local_id = parent_record_decl->getID();
id_opt = get_ast_local_id(local_id);
// If not, check if the parent template declaration is in the model
if (!id_opt) {
if (static_cast<const clang::RecordDecl *>(parent)
->getDescribedTemplate() != nullptr) {
local_id = static_cast<const clang::RecordDecl *>(parent)
->getDescribedTemplate()
->getID();
if (parent_record_decl->getDescribedTemplate() != nullptr) {
local_id = parent_record_decl->getDescribedTemplate()->getID();
id_opt = get_ast_local_id(local_id);
}
@@ -425,8 +423,11 @@ std::unique_ptr<class_> translation_unit_visitor::create_class_declaration(
// - the parent is a regular C++ class/struct
// - the parent is a class template declaration/specialization
std::optional<common::model::diagram_element::id_t> id_opt;
int64_t local_id =
static_cast<const clang::RecordDecl *>(parent)->getID();
const auto *parent_record_decl =
clang::dyn_cast<clang::RecordDecl>(parent);
int64_t local_id = parent_record_decl->getID();
// First check if the parent has been added to the diagram as regular
// class
@@ -434,11 +435,8 @@ std::unique_ptr<class_> translation_unit_visitor::create_class_declaration(
// If not, check if the parent template declaration is in the model
if (!id_opt) {
local_id = static_cast<const clang::RecordDecl *>(parent)
->getDescribedTemplate()
->getID();
if (static_cast<const clang::RecordDecl *>(parent)
->getDescribedTemplate() != nullptr)
local_id = parent_record_decl->getDescribedTemplate()->getID();
if (parent_record_decl->getDescribedTemplate() != nullptr)
id_opt = get_ast_local_id(local_id);
}
@@ -580,16 +578,20 @@ void translation_unit_visitor::process_template_record_containment(
const auto *parent = record.getParent(); //->getOuterLexicalRecordContext();
if ((parent != nullptr) &&
(static_cast<const clang::RecordDecl *>(parent)
->getDescribedTemplate() != nullptr)) {
auto id_opt =
get_ast_local_id(static_cast<const clang::RecordDecl *>(parent)
->getDescribedTemplate()
->getID());
if (parent != nullptr) {
if (const auto *record_decl =
clang::dyn_cast<clang::RecordDecl>(parent);
record_decl != nullptr) {
if (const auto *described_template =
record_decl->getDescribedTemplate();
described_template != nullptr) {
auto id_opt = get_ast_local_id(described_template->getID());
if (id_opt) {
element.add_relationship({relationship_t::kContainment, *id_opt});
if (id_opt) {
element.add_relationship(
{relationship_t::kContainment, *id_opt});
}
}
}
}
}
@@ -609,10 +611,12 @@ void translation_unit_visitor::process_record_containment(
element.set_namespace(namespace_declaration.value());
}
const auto id = common::to_id(
*static_cast<const clang::RecordDecl *>(record.getParent()));
element.add_relationship({relationship_t::kContainment, id});
if (const auto *record_decl =
clang::dyn_cast<clang::RecordDecl>(record.getParent());
record_decl != nullptr) {
element.add_relationship(
{relationship_t::kContainment, common::to_id(*record_decl)});
}
}
void translation_unit_visitor::process_class_bases(

View File

@@ -74,9 +74,11 @@ model::namespace_ get_tag_namespace(const clang::TagDecl &declaration)
// Now build up the namespace
std::deque<std::string> namespace_tokens;
while ((parent != nullptr) && parent->isNamespace()) {
const auto *ns_decl = static_cast<const clang::NamespaceDecl *>(parent);
if (!ns_decl->isInline() && !ns_decl->isAnonymousNamespace())
namespace_tokens.push_front(ns_decl->getNameAsString());
if (const auto *ns_decl = clang::dyn_cast<clang::NamespaceDecl>(parent);
ns_decl != nullptr) {
if (!ns_decl->isInline() && !ns_decl->isAnonymousNamespace())
namespace_tokens.push_front(ns_decl->getNameAsString());
}
parent = parent->getParent();
}
@@ -106,10 +108,11 @@ std::string get_tag_name(const clang::TagDecl &declaration)
const auto *cls_parent{declaration.getParent()};
while (cls_parent->isRecord()) {
auto parent_name =
static_cast<const clang::RecordDecl *>(cls_parent)
->getNameAsString();
record_parent_names.push_front(parent_name);
if (const auto *record_decl =
clang::dyn_cast<clang::RecordDecl>(cls_parent);
record_decl != nullptr) {
record_parent_names.push_front(record_decl->getNameAsString());
}
cls_parent = cls_parent->getParent();
}
return fmt::format("{}", fmt::join(record_parent_names, "##"));

View File

@@ -233,9 +233,13 @@ bool translation_unit_visitor::VisitCXXMethodDecl(clang::CXXMethodDecl *m)
return true;
if (!m->isThisDeclarationADefinition()) {
if (m->getDefinition() != nullptr)
return VisitCXXMethodDecl(
static_cast<clang::CXXMethodDecl *>(m->getDefinition()));
if (m->getDefinition() != nullptr) {
if (auto *method_definition =
clang::dyn_cast<clang::CXXMethodDecl>(m->getDefinition());
method_definition != nullptr) {
return VisitCXXMethodDecl(method_definition);
}
}
}
LOG_TRACE("Visiting method {} in class {} [{}]",
@@ -1232,8 +1236,10 @@ translation_unit_visitor::create_class_declaration(clang::CXXRecordDecl *cls)
// - the parent is a regular C++ class/struct
// - the parent is a class template declaration/specialization
std::optional<common::model::diagram_element::id_t> id_opt;
int64_t local_id =
static_cast<const clang::RecordDecl *>(parent)->getID();
const auto *parent_record_decl =
clang::dyn_cast<clang::RecordDecl>(parent);
int64_t local_id = parent_record_decl->getID();
// First check if the parent has been added to the diagram as
// regular class
@@ -1241,13 +1247,9 @@ translation_unit_visitor::create_class_declaration(clang::CXXRecordDecl *cls)
// If not, check if the parent template declaration is in the model
if (!id_opt &&
(static_cast<const clang::RecordDecl *>(parent)
->getDescribedTemplate() != nullptr)) {
local_id = static_cast<const clang::RecordDecl *>(parent)
->getDescribedTemplate()
->getID();
if (static_cast<const clang::RecordDecl *>(parent)
->getDescribedTemplate() != nullptr)
(parent_record_decl->getDescribedTemplate() != nullptr)) {
parent_record_decl->getDescribedTemplate()->getID();
if (parent_record_decl->getDescribedTemplate() != nullptr)
id_opt = get_unique_id(local_id);
}