From f07dc35e06d5bbacf8951e1d467b0da1e77fd79d Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Fri, 23 Dec 2022 21:26:15 +0100 Subject: [PATCH] Applied clang-tidy nullptr access warnings --- .../visitor/translation_unit_visitor.cc | 184 +++++++++--------- src/common/clang_utils.cc | 22 +-- src/common/model/diagram.h | 2 +- src/common/visitor/comment/clang_visitor.cc | 14 +- .../visitor/translation_unit_visitor.cc | 45 +++-- .../visitor/translation_unit_visitor.cc | 44 ++--- 6 files changed, 164 insertions(+), 147 deletions(-) diff --git a/src/class_diagram/visitor/translation_unit_visitor.cc b/src/class_diagram/visitor/translation_unit_visitor.cc index f1d3d94d..d0703597 100644 --- a/src/class_diagram/visitor/translation_unit_visitor.cc +++ b/src/class_diagram/visitor/translation_unit_visitor.cc @@ -625,14 +625,16 @@ void translation_unit_visitor::process_class_bases( cp.set_name(name_and_ns.to_string()); - if (base.getType()->getAs() != nullptr) - cp.set_id(common::to_id( - *base.getType()->getAs()->getDecl())); - else if (base.getType()->getAs() != - nullptr) { - auto template_specialization_ptr = build_template_instantiation( - *base.getType()->getAs(), - {}); + if (const auto *record_type = + base.getType()->getAs(); + record_type != nullptr) { + cp.set_id(common::to_id(*record_type->getDecl())); + } + else if (const auto *tsp = + base.getType()->getAs(); + tsp != nullptr) { + auto template_specialization_ptr = + build_template_instantiation(*tsp, {}); if (template_specialization_ptr) { cp.set_id(template_specialization_ptr->id()); } @@ -666,14 +668,19 @@ void translation_unit_visitor::process_template_specialization_children( } // Iterate over class template methods - for (auto const *decl_iterator : - clang::dyn_cast_or_null(cls)->decls()) { - auto const *method_template = - llvm::dyn_cast_or_null(decl_iterator); - if (method_template == nullptr) - continue; + if (const auto *cls_decl_context = + clang::dyn_cast_or_null(cls); + cls_decl_context != nullptr) { + for (auto const *decl_iterator : + clang::dyn_cast_or_null(cls)->decls()) { + auto const *method_template = + llvm::dyn_cast_or_null( + decl_iterator); + if (method_template == nullptr) + continue; - process_template_method(*method_template, c); + process_template_method(*method_template, c); + } } // Iterate over regular class fields @@ -728,14 +735,18 @@ void translation_unit_visitor::process_class_children( } // Iterate over class template methods - for (auto const *decl_iterator : - clang::dyn_cast_or_null(cls)->decls()) { - auto const *method_template = - llvm::dyn_cast_or_null(decl_iterator); - if (method_template == nullptr) - continue; + if (const auto *cls_decl_context = + clang::dyn_cast_or_null(cls); + cls_decl_context != nullptr) { + for (auto const *decl_iterator : cls_decl_context->decls()) { + auto const *method_template = + llvm::dyn_cast_or_null( + decl_iterator); + if (method_template == nullptr) + continue; - process_template_method(*method_template, c); + process_template_method(*method_template, c); + } } // Iterate over regular class fields @@ -896,20 +907,22 @@ bool translation_unit_visitor::find_relationships(const clang::QualType &type, relationships, relationship_t::kAggregation); } else if (type->isEnumeralType()) { - relationships.emplace_back( - common::to_id(*type->getAs()->getDecl()), - relationship_hint); + if (const auto *enum_type = type->getAs(); + enum_type != nullptr) { + relationships.emplace_back( + common::to_id(*enum_type->getDecl()), relationship_hint); + } } else if (type->isRecordType()) { const auto *type_instantiation_decl = type->getAs(); - if (type_instantiation_decl != nullptr) { - if (type_instantiation_decl->isTypeAlias()) - type_instantiation_decl = - type_instantiation_decl->getAliasedType() - ->getAs(); - } + // if (type_instantiation_decl != nullptr) { + // if (type_instantiation_decl->isTypeAlias()) + // type_instantiation_decl = + // type_instantiation_decl->getAliasedType() + // ->getAs(); + // } if (type_instantiation_decl != nullptr) { for (const auto &template_argument : *type_instantiation_decl) { @@ -938,12 +951,12 @@ bool translation_unit_visitor::find_relationships(const clang::QualType &type, clang::TemplateArgument::ArgKind::TemplateExpansion) { // pass } - else if (template_argument.getAsType() - ->getAs() != nullptr) { + else if (const auto *function_type = + template_argument.getAsType() + ->getAs(); + function_type != nullptr) { for (const auto ¶m_type : - template_argument.getAsType() - ->getAs() - ->param_types()) { + function_type->param_types()) { result = find_relationships(param_type, relationships, relationship_t::kDependency); } @@ -1018,11 +1031,11 @@ void translation_unit_visitor::process_function_parameter( if (underlying_type->isPointerType()) underlying_type = underlying_type->getPointeeType(); - if (underlying_type->getAs() != - nullptr) { - process_function_parameter_find_relationships_in_template(c, - template_parameter_names, - *underlying_type->getAs()); + if (const auto *tsp = + underlying_type->getAs(); + tsp != nullptr) { + process_function_parameter_find_relationships_in_template( + c, template_parameter_names, *tsp); } } @@ -1185,10 +1198,9 @@ void translation_unit_visitor::process_template_specialization_argument( // If this is a nested template type - add nested templates as // template arguments - if (arg.getAsType()->getAs() != - nullptr) { - const auto *nested_template_type = + if (const auto *nested_template_type = arg.getAsType()->getAs(); + nested_template_type != nullptr) { const auto nested_template_name = nested_template_type->getTemplateName() @@ -1198,8 +1210,7 @@ void translation_unit_visitor::process_template_specialization_argument( argument.set_name(nested_template_name); auto nested_template_instantiation = build_template_instantiation( - *arg.getAsType()->getAs(), - {&template_instantiation}); + *nested_template_type, {&template_instantiation}); argument.set_id(nested_template_instantiation->id()); @@ -1482,11 +1493,13 @@ std::unique_ptr translation_unit_visitor::build_template_instantiation( template_base_params{}; const auto *template_type_ptr = &template_type_decl; - if (template_type_decl.isTypeAlias() && - (template_type_decl.getAliasedType() - ->getAs() != nullptr)) - template_type_ptr = template_type_decl.getAliasedType() - ->getAs(); + + if (template_type_decl.isTypeAlias()) { + if (const auto *tsp = template_type_decl.getAliasedType() + ->getAs(); + tsp != nullptr) + template_type_ptr = tsp; + } const auto &template_type = *template_type_ptr; @@ -1696,7 +1709,7 @@ void translation_unit_visitor:: // the list of template params, from then this variable is true // and we can process following template parameters as belonging // to the variadic tuple - auto variadic_params = false; + [[maybe_unused]] auto variadic_params{false}; // In case any of the template arguments are base classes, add // them as parents of the current template instantiation class @@ -1743,12 +1756,14 @@ void translation_unit_visitor:: // If this is a nested template type - add nested templates as // template arguments - if (arg.getAsType()->getAs() != nullptr) { + if (const auto *function_type = + arg.getAsType()->getAs(); + function_type != nullptr) { - for (const auto ¶m_type : - arg.getAsType()->getAs()->param_types()) { - - if (param_type->getAs() == nullptr) + for (const auto ¶m_type : function_type->param_types()) { + const auto *param_record_type = + param_type->getAs(); + if (param_record_type == nullptr) continue; auto *classTemplateSpecialization = @@ -1759,8 +1774,7 @@ void translation_unit_visitor:: // Read arg info as needed. auto nested_template_instantiation = build_template_instantiation_from_class_template_specialization( - *classTemplateSpecialization, - *param_type->getAs(), + *classTemplateSpecialization, *param_record_type, diagram().should_include( full_template_specialization_name) ? std::make_optional(&template_instantiation) @@ -1769,9 +1783,6 @@ void translation_unit_visitor:: const auto nested_template_name = classTemplateSpecialization->getQualifiedNameAsString(); - auto [tinst_ns, tinst_name] = - cx::util::split_ns(nested_template_name); - if (nested_template_instantiation) { if (parent.has_value()) parent.value()->add_relationship( @@ -1789,25 +1800,22 @@ void translation_unit_visitor:: } } } - else if (arg.getAsType()->getAs() != - nullptr) { - const auto *nested_template_type = - arg.getAsType()->getAs(); + else if (const auto *nested_template_type = + arg.getAsType()->getAs(); + nested_template_type != nullptr) { const auto nested_template_name = nested_template_type->getTemplateName() .getAsTemplateDecl() ->getQualifiedNameAsString(); - auto [tinst_ns, tinst_name] = cx::util::split_ns(nested_template_name); - argument.set_name(nested_template_name); - auto nested_template_instantiation = build_template_instantiation( - *arg.getAsType()->getAs(), - diagram().should_include(full_template_specialization_name) - ? std::make_optional(&template_instantiation) - : parent); + auto nested_template_instantiation = + build_template_instantiation(*nested_template_type, + diagram().should_include(full_template_specialization_name) + ? std::make_optional(&template_instantiation) + : parent); argument.set_id(nested_template_instantiation->id()); @@ -1890,21 +1898,23 @@ void translation_unit_visitor:: argument.set_name( common::to_string(arg.getAsType(), template_decl->getASTContext())); - if ((arg.getAsType()->getAs() != nullptr) && - (arg.getAsType()->getAs()->getAsRecordDecl() != - nullptr)) { - argument.set_id(common::to_id(arg)); + if (const auto *record_type = arg.getAsType()->getAs(); + record_type != nullptr) { + if (const auto *record_type_decl = record_type->getAsRecordDecl(); + record_type_decl != nullptr) { + argument.set_id(common::to_id(arg)); - if (diagram().should_include(full_template_specialization_name)) { - // Add dependency relationship to the parent - // template - template_instantiation.add_relationship( - {relationship_t::kDependency, common::to_id(arg)}); + if (diagram().should_include(full_template_specialization_name)) { + // Add dependency relationship to the parent + // template + template_instantiation.add_relationship( + {relationship_t::kDependency, common::to_id(arg)}); + } } } - else if (arg.getAsType()->getAs() != nullptr) { - if (arg.getAsType()->getAs()->getAsTagDecl() != - nullptr) { + else if (const auto *enum_type = arg.getAsType()->getAs(); + enum_type != nullptr) { + if (enum_type->getAsTagDecl() != nullptr) { template_instantiation.add_relationship( {relationship_t::kDependency, common::to_id(arg)}); } @@ -2028,8 +2038,8 @@ void translation_unit_visitor::process_field( !field_type_is_template_template_parameter) { // Build the template instantiation for the field type - auto template_specialization_ptr = build_template_instantiation( - *field_type->getAs(), {&c}); + auto template_specialization_ptr = + build_template_instantiation(*template_field_type, {&c}); if (!field.skip_relationship() && template_specialization_ptr) { const auto &template_specialization = *template_specialization_ptr; diff --git a/src/common/clang_utils.cc b/src/common/clang_utils.cc index efc0038d..7be80898 100644 --- a/src/common/clang_utils.cc +++ b/src/common/clang_utils.cc @@ -171,7 +171,7 @@ std::string to_string(const clang::Expr *expr) clang::LangOptions lang_options; std::string result; llvm::raw_string_ostream ostream(result); - expr->printPretty(ostream, NULL, clang::PrintingPolicy(lang_options)); + expr->printPretty(ostream, nullptr, clang::PrintingPolicy(lang_options)); return result; } @@ -181,7 +181,7 @@ std::string to_string(const clang::Stmt *stmt) clang::LangOptions lang_options; std::string result; llvm::raw_string_ostream ostream(result); - stmt->printPretty(ostream, NULL, clang::PrintingPolicy(lang_options)); + stmt->printPretty(ostream, nullptr, clang::PrintingPolicy(lang_options)); return result; } @@ -284,15 +284,15 @@ template <> id_t to_id(const std::filesystem::path &file) template <> id_t to_id(const clang::TemplateArgument &template_argument) { if (template_argument.getKind() == clang::TemplateArgument::Type) { - if (template_argument.getAsType()->getAs() != nullptr) - return to_id(*template_argument.getAsType() - ->getAs() - ->getAsTagDecl()); - if (template_argument.getAsType()->getAs() != - nullptr) - return to_id(*template_argument.getAsType() - ->getAs() - ->getAsRecordDecl()); + if (const auto *enum_type = + template_argument.getAsType()->getAs(); + enum_type != nullptr) + return to_id(*enum_type->getAsTagDecl()); + + if (const auto *record_type = + template_argument.getAsType()->getAs(); + record_type != nullptr) + return to_id(*record_type->getAsRecordDecl()); } throw std::runtime_error("Cannot generate id for template argument"); diff --git a/src/common/model/diagram.h b/src/common/model/diagram.h index 96d0da9f..96cb0568 100644 --- a/src/common/model/diagram.h +++ b/src/common/model/diagram.h @@ -71,7 +71,7 @@ public: bool should_include(relationship_t r) const; bool should_include(access_t s) const; - virtual bool has_element(const diagram_element::id_t /*id*/) const + virtual bool has_element(const diagram_element::id_t /*id*/) const { return false; } diff --git a/src/common/visitor/comment/clang_visitor.cc b/src/common/visitor/comment/clang_visitor.cc index 97e0c5fa..5ec6de9d 100644 --- a/src/common/visitor/comment/clang_visitor.cc +++ b/src/common/visitor/comment/clang_visitor.cc @@ -160,6 +160,9 @@ void clang_visitor::visit_param_command( std::string description; + if (command == nullptr) + return; + const auto name = command->getParamNameAsWritten().str(); for (const auto *it = command->child_begin(); it != command->child_end(); @@ -192,6 +195,9 @@ void clang_visitor::visit_tparam_command( std::string description; + if (command == nullptr) + return; + const auto name = command->getParamNameAsWritten().str(); for (const auto *it = command->child_begin(); it != command->child_end(); @@ -220,12 +226,16 @@ void clang_visitor::visit_paragraph( using clang::comments::Comment; using clang::comments::TextComment; + if (paragraph == nullptr) + return; + for (const auto *text_it = paragraph->child_begin(); text_it != paragraph->child_end(); ++text_it) { - if ((*text_it)->getCommentKind() == Comment::TextCommentKind) { + if ((*text_it)->getCommentKind() == Comment::TextCommentKind && + clang::dyn_cast(*text_it) != nullptr) { // Merge paragraph lines into a single string - text += clang::dyn_cast((*text_it))->getText(); + text += clang::dyn_cast(*text_it)->getText(); text += "\n"; } } diff --git a/src/package_diagram/visitor/translation_unit_visitor.cc b/src/package_diagram/visitor/translation_unit_visitor.cc index 7b85dea8..bf4e0b72 100644 --- a/src/package_diagram/visitor/translation_unit_visitor.cc +++ b/src/package_diagram/visitor/translation_unit_visitor.cc @@ -28,7 +28,6 @@ namespace clanguml::package_diagram::visitor { -using clanguml::class_diagram::model::type_alias; using clanguml::common::model::access_t; using clanguml::common::model::namespace_; using clanguml::common::model::package; @@ -198,15 +197,19 @@ void translation_unit_visitor::process_class_children( } } - // Iterate over class template methods - for (auto const *decl_iterator : - clang::dyn_cast_or_null(&cls)->decls()) { - auto const *method_template = - llvm::dyn_cast_or_null(decl_iterator); - if (method_template == nullptr) - continue; + if (const auto *decl_context = + clang::dyn_cast_or_null(&cls); + decl_context != nullptr) { + // Iterate over class template methods + for (auto const *decl_iterator : decl_context->decls()) { + auto const *method_template = + llvm::dyn_cast_or_null( + decl_iterator); + if (method_template == nullptr) + continue; - process_template_method(*method_template, relationships); + process_template_method(*method_template, relationships); + } } // Iterate over regular class fields @@ -333,18 +336,14 @@ bool translation_unit_visitor::find_relationships(const clang::QualType &type, relationships, relationship_t::kAggregation); } else if (type->isEnumeralType()) { - relationships.emplace_back( - common::to_id(*type->getAs()), relationship_hint); + if (const auto *enum_type = type->getAs(); + enum_type != nullptr) { + relationships.emplace_back( + common::to_id(*enum_type), relationship_hint); + } } else if (const auto *template_specialization_type = type->getAs()) { - if (template_specialization_type != nullptr) { - if (template_specialization_type->isTypeAlias()) - template_specialization_type = - template_specialization_type->getAliasedType() - ->getAs(); - } - if (template_specialization_type != nullptr) { for (const auto &template_argument : *template_specialization_type) { @@ -373,12 +372,12 @@ bool translation_unit_visitor::find_relationships(const clang::QualType &type, clang::TemplateArgument::ArgKind::TemplateExpansion) { // pass } - else if (template_argument.getAsType() - ->getAs() != nullptr) { + else if (const auto *function_type = + template_argument.getAsType() + ->getAs(); + function_type != nullptr) { for (const auto ¶m_type : - template_argument.getAsType() - ->getAs() - ->param_types()) { + function_type->param_types()) { result = find_relationships(param_type, relationships, relationship_t::kDependency); } diff --git a/src/sequence_diagram/visitor/translation_unit_visitor.cc b/src/sequence_diagram/visitor/translation_unit_visitor.cc index 0d8c17a7..98bb1c1f 100644 --- a/src/sequence_diagram/visitor/translation_unit_visitor.cc +++ b/src/sequence_diagram/visitor/translation_unit_visitor.cc @@ -1030,6 +1030,9 @@ bool translation_unit_visitor::process_class_template_method_call_expression( clang::dyn_cast_or_null( expr->getCallee()); + if (dependent_member_callee == nullptr) + return false; + if (is_callee_valid_template_specialization(dependent_member_callee)) { const auto *template_declaration = dependent_member_callee->getBaseType() @@ -1172,21 +1175,21 @@ bool translation_unit_visitor::process_unresolved_lookup_call_expression( bool translation_unit_visitor::is_callee_valid_template_specialization( const clang::CXXDependentScopeMemberExpr *dependent_member_expr) const { - const bool base_type_is_not_null = - !dependent_member_expr->getBaseType().isNull(); + if (dependent_member_expr == nullptr) + return false; - const bool base_type_is_specialization_type = - dependent_member_expr->getBaseType() - ->getAs() != nullptr; + if (dependent_member_expr->getBaseType().isNull()) + return false; - const bool base_type_is_not_pointer_type = - base_type_is_specialization_type && - !dependent_member_expr->getBaseType() - ->getAs() - ->isPointerType(); + const auto *tst = dependent_member_expr->getBaseType() + ->getAs(); - return (base_type_is_not_null && base_type_is_specialization_type && - base_type_is_not_pointer_type); + if (tst == nullptr) + return false; + + return !(dependent_member_expr->getBaseType() + ->getAs() + ->isPointerType()); } bool translation_unit_visitor::is_smart_pointer( @@ -1554,18 +1557,15 @@ void translation_unit_visitor:: if (arg.getAsType()->getAs() != nullptr) { // TODO } - else if (arg.getAsType()->getAs() != - nullptr) { - const auto *nested_template_type = - arg.getAsType()->getAs(); + else if (const auto *nested_template_type = + arg.getAsType()->getAs(); + nested_template_type != nullptr) { const auto nested_template_name = nested_template_type->getTemplateName() .getAsTemplateDecl() ->getQualifiedNameAsString(); - auto [tinst_ns, tinst_name] = cx::util::split_ns(nested_template_name); - argument.set_name(nested_template_name); // Check if this template should be simplified (e.g. system @@ -1640,10 +1640,9 @@ void translation_unit_visitor::process_template_specialization_argument( // If this is a nested template type - add nested templates as // template arguments - if (arg.getAsType()->getAs() != - nullptr) { - const auto *nested_template_type = + if (const auto *nested_template_type = arg.getAsType()->getAs(); + nested_template_type != nullptr) { const auto nested_template_name = nested_template_type->getTemplateName() @@ -1653,8 +1652,7 @@ void translation_unit_visitor::process_template_specialization_argument( argument.set_name(nested_template_name); auto nested_template_instantiation = build_template_instantiation( - *arg.getAsType()->getAs(), - &template_instantiation); + *nested_template_type, &template_instantiation); argument.set_id(nested_template_instantiation->id());