Applied clang-tidy nullptr access warnings

This commit is contained in:
Bartek Kryza
2022-12-24 18:33:40 +01:00
parent d82ac4a46b
commit 354bfcf67c
2 changed files with 81 additions and 77 deletions

View File

@@ -125,33 +125,29 @@ bool translation_unit_visitor::VisitEnumDecl(clang::EnumDecl *enm)
const auto *parent = enm->getParent(); const auto *parent = enm->getParent();
if ((parent != nullptr) && parent->isRecord()) {
// Here we have 2 options, either:
// - 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; std::optional<common::model::diagram_element::id_t> id_opt;
if (parent != nullptr) {
const auto *parent_record_decl = const auto *parent_record_decl =
clang::dyn_cast<clang::RecordDecl>(parent); clang::dyn_cast<clang::RecordDecl>(parent);
if (parent_record_decl != nullptr) {
int64_t local_id = parent_record_decl->getID(); int64_t local_id = parent_record_decl->getID();
// First check if the parent has been added to the diagram as
// regular class
id_opt = get_ast_local_id(local_id); id_opt = get_ast_local_id(local_id);
// If not, check if the parent template declaration is in the model // If not, check if the parent template declaration is in the model
if (!id_opt) { if (!id_opt) {
if (parent_record_decl->getDescribedTemplate() != nullptr) {
local_id = parent_record_decl->getDescribedTemplate()->getID(); local_id = parent_record_decl->getDescribedTemplate()->getID();
if (parent_record_decl->getDescribedTemplate() != nullptr)
id_opt = get_ast_local_id(local_id); id_opt = get_ast_local_id(local_id);
} }
} }
if (!id_opt) {
LOG_WARN("Unknown parent for enum {}", qualified_name);
return true;
} }
if (id_opt) {
auto parent_class = diagram_.get_class(*id_opt); auto parent_class = diagram_.get_class(*id_opt);
assert(parent_class); assert(parent_class);
@@ -418,19 +414,17 @@ std::unique_ptr<class_> translation_unit_visitor::create_class_declaration(
const auto *parent = cls->getParent(); const auto *parent = cls->getParent();
if ((parent != nullptr) && parent->isRecord()) {
// Here we have 2 options, either:
// - 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; std::optional<common::model::diagram_element::id_t> id_opt;
if (parent != nullptr) {
const auto *parent_record_decl = const auto *parent_record_decl =
clang::dyn_cast<clang::RecordDecl>(parent); clang::dyn_cast<clang::RecordDecl>(parent);
if (parent_record_decl != nullptr) {
int64_t local_id = parent_record_decl->getID(); int64_t local_id = parent_record_decl->getID();
// First check if the parent has been added to the diagram as regular // First check if the parent has been added to the diagram as
// class // regular class
id_opt = get_ast_local_id(local_id); id_opt = get_ast_local_id(local_id);
// If not, check if the parent template declaration is in the model // If not, check if the parent template declaration is in the model
@@ -439,9 +433,13 @@ std::unique_ptr<class_> translation_unit_visitor::create_class_declaration(
if (parent_record_decl->getDescribedTemplate() != nullptr) if (parent_record_decl->getDescribedTemplate() != nullptr)
id_opt = get_ast_local_id(local_id); id_opt = get_ast_local_id(local_id);
} }
}
}
assert(id_opt); if (id_opt) {
// Here we have 2 options, either:
// - the parent is a regular C++ class/struct
// - the parent is a class template declaration/specialization
auto parent_class = diagram_.get_class(*id_opt); auto parent_class = diagram_.get_class(*id_opt);
assert(parent_class); assert(parent_class);

View File

@@ -1038,19 +1038,20 @@ bool translation_unit_visitor::process_class_template_method_call_expression(
return false; return false;
if (is_callee_valid_template_specialization(dependent_member_callee)) { if (is_callee_valid_template_specialization(dependent_member_callee)) {
if (const auto *tst = dependent_member_callee->getBaseType()
->getAs<clang::TemplateSpecializationType>();
tst != nullptr) {
const auto *template_declaration = const auto *template_declaration =
dependent_member_callee->getBaseType() tst->getTemplateName().getAsTemplateDecl();
->getAs<clang::TemplateSpecializationType>()
->getTemplateName()
.getAsTemplateDecl();
std::string callee_method_full_name; std::string callee_method_full_name;
// First check if the primary template is already in the // First check if the primary template is already in the
// participants map // participants map
if (get_participant(template_declaration).has_value()) { if (get_participant(template_declaration).has_value()) {
callee_method_full_name = callee_method_full_name = get_participant(template_declaration)
get_participant(template_declaration).value().full_name(false) + .value()
.full_name(false) +
"::" + dependent_member_callee->getMember().getAsString(); "::" + dependent_member_callee->getMember().getAsString();
for (const auto &[id, p] : diagram().participants()) { for (const auto &[id, p] : diagram().participants()) {
@@ -1075,12 +1076,15 @@ bool translation_unit_visitor::process_class_template_method_call_expression(
callee_method_full_name = get_participant(argument_template) callee_method_full_name = get_participant(argument_template)
.value() .value()
.full_name(false) + .full_name(false) +
"::" + dependent_member_callee->getMember().getAsString(); "::" +
dependent_member_callee->getMember().getAsString();
for (const auto &[id, p] : diagram().participants()) { for (const auto &[id, p] : diagram().participants()) {
const auto p_full_name = p->full_name(false); const auto p_full_name = p->full_name(false);
if (p_full_name.find(callee_method_full_name + "(") == 0) { if (p_full_name.find(callee_method_full_name + "(") ==
// TODO: This selects the first matching template method 0) {
// TODO: This selects the first matching template
// method
// without considering arguments!!! // without considering arguments!!!
m.set_to(id); m.set_to(id);
break; break;
@@ -1091,12 +1095,14 @@ bool translation_unit_visitor::process_class_template_method_call_expression(
return false; return false;
} }
m.set_message_name(dependent_member_callee->getMember().getAsString()); m.set_message_name(
dependent_member_callee->getMember().getAsString());
if (get_unique_id(template_declaration->getID())) if (get_unique_id(template_declaration->getID()))
diagram().add_active_participant( diagram().add_active_participant(
get_unique_id(template_declaration->getID()).value()); get_unique_id(template_declaration->getID()).value());
} }
}
else { else {
LOG_DBG("Skipping call due to unresolvable " LOG_DBG("Skipping call due to unresolvable "
"CXXDependentScopeMemberExpr at {}", "CXXDependentScopeMemberExpr at {}",
@@ -1191,9 +1197,7 @@ bool translation_unit_visitor::is_callee_valid_template_specialization(
if (tst == nullptr) if (tst == nullptr)
return false; return false;
return !(dependent_member_expr->getBaseType() return !(tst->isPointerType());
->getAs<clang::TemplateSpecializationType>()
->isPointerType());
} }
bool translation_unit_visitor::is_smart_pointer( bool translation_unit_visitor::is_smart_pointer(
@@ -1239,6 +1243,8 @@ translation_unit_visitor::create_class_declaration(clang::CXXRecordDecl *cls)
const auto *parent_record_decl = const auto *parent_record_decl =
clang::dyn_cast<clang::RecordDecl>(parent); clang::dyn_cast<clang::RecordDecl>(parent);
assert(parent_record_decl != nullptr);
int64_t local_id = parent_record_decl->getID(); int64_t local_id = parent_record_decl->getID();
// First check if the parent has been added to the diagram as // First check if the parent has been added to the diagram as