From 82410b345d54e136440913a1ff84d98b55abfead Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Wed, 3 May 2023 21:31:17 +0200 Subject: [PATCH] Fixed clang-tidy warnings --- .clang-tidy | 1 + src/class_diagram/visitor/template_builder.cc | 64 ++++++++++--------- src/class_diagram/visitor/template_builder.h | 2 +- src/common/clang_utils.cc | 31 +++++---- src/common/model/template_parameter.cc | 19 +++--- src/common/model/template_parameter.h | 2 +- src/common/visitor/ast_id_mapper.h | 2 +- 7 files changed, 65 insertions(+), 56 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 5aa17178..00c9eaae 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -48,6 +48,7 @@ Checks: >- -readability-function-cognitive-complexity, -readability-const-return-type, -readability-simplify-boolean-expr, + -readability-make-member-function-const, -darwin*, -zircon* WarningsAsErrors: '*' diff --git a/src/class_diagram/visitor/template_builder.cc b/src/class_diagram/visitor/template_builder.cc index ca4e3b23..c7785ed6 100644 --- a/src/class_diagram/visitor/template_builder.cc +++ b/src/class_diagram/visitor/template_builder.cc @@ -383,10 +383,12 @@ void template_builder::process_template_arguments( const auto *maybe_type_parm_decl = clang::dyn_cast( template_decl->getTemplateParameters()->getParam( - std::min(arg_index, - template_decl->getTemplateParameters()->size() - + std::min(arg_index, + static_cast( + template_decl->getTemplateParameters() + ->size()) - 1))); - if (maybe_type_parm_decl && + if (maybe_type_parm_decl != nullptr && maybe_type_parm_decl->hasDefaultArgument()) { continue; } @@ -528,10 +530,13 @@ clang::QualType template_builder::consume_context( ctx.pr = common::model::rpqualifier::kRValueReference; try_again = true; } - else if (type->isMemberFunctionPointerType()) { - const auto ref_qualifier = type->getPointeeType() - ->getAs() - ->getRefQualifier(); + else if (type->isMemberFunctionPointerType() && + type->getPointeeType()->getAs() != + nullptr) { + const auto ref_qualifier = + type->getPointeeType() // NOLINT + ->getAs() // NOLINT + ->getRefQualifier(); if (ref_qualifier == clang::RefQualifierKind::RQ_RValue) { ctx.pr = common::model::rpqualifier::kRValueReference; @@ -569,7 +574,7 @@ clang::QualType template_builder::consume_context( ctx.is_volatile = type.isVolatileQualified(); } - tp.push_context(std::move(ctx)); + tp.push_context(ctx); if (type->isMemberFunctionPointerType()) return type; @@ -587,7 +592,7 @@ template_parameter template_builder::process_type_argument( std::optional argument; if (type->getAs() != nullptr) { - type = type->getAs()->getNamedType(); + type = type->getAs()->getNamedType(); // NOLINT } auto type_name = common::to_string(type, &cls->getASTContext()); @@ -715,7 +720,7 @@ std::string map_type_parameter_to_template_parameter( return tp; } -} +} // namespace detail std::string map_type_parameter_to_template_parameter_name( const clang::Decl *decl, const std::string &type_parameter) @@ -784,13 +789,13 @@ std::vector template_builder::process_pack_argument( const clang::NamedDecl *cls, class_ &template_instantiation, const clang::TemplateDecl *base_template_decl, const clang::TemplateArgument &arg, size_t argument_index, - std::vector &argument) + std::vector & /*argument*/) { assert(arg.getKind() == clang::TemplateArgument::Pack); std::vector res; - int pack_argument_index = argument_index; + auto pack_argument_index = argument_index; for (const auto &a : arg.getPackAsArray()) { argument_process_dispatch(parent, cls, template_instantiation, @@ -900,13 +905,14 @@ std::optional template_builder::try_as_array( if (array_type->isDependentSizedArrayType() && array_type->getDependence() == clang::TypeDependence::DependentInstantiation) { - argument.add_template_param(template_parameter::make_template_type( - common::to_string(((clang::DependentSizedArrayType *)array_type) - ->getSizeExpr()))); + argument.add_template_param( + template_parameter::make_template_type(common::to_string( + ((clang::DependentSizedArrayType *)array_type) // NOLINT + ->getSizeExpr()))); } else if (array_type->isConstantArrayType()) { argument.add_template_param(template_parameter::make_argument( - std::to_string(((clang::ConstantArrayType *)array_type) + std::to_string(((clang::ConstantArrayType *)array_type) // NOLINT ->getSize() .getLimitedValue()))); } @@ -1037,23 +1043,23 @@ template_builder::try_as_template_specialization_type( } std::optional template_builder::try_as_template_parm_type( - const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + const clang::NamedDecl *cls, const clang::TemplateDecl * /*template_decl*/, clang::QualType &type) { auto is_variadic{false}; - auto type_parameter = + const auto *type_parameter = common::dereference(type)->getAs(); auto type_name = common::to_string(type, &cls->getASTContext()); if (type_parameter == nullptr) { - if (common::dereference(type)->getAs()) { + if (const auto *pet = + common::dereference(type)->getAs(); + pet != nullptr) { is_variadic = true; - type_parameter = common::dereference(type) - ->getAs() - ->getPattern() - ->getAs(); + type_parameter = + pet->getPattern()->getAs(); } } @@ -1076,7 +1082,7 @@ std::optional template_builder::try_as_template_parm_type( } std::optional template_builder::try_as_lambda( - const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + const clang::NamedDecl *cls, const clang::TemplateDecl * /*template_decl*/, clang::QualType &type) { auto type_name = common::to_string(type, &cls->getASTContext()); @@ -1095,9 +1101,9 @@ std::optional template_builder::try_as_lambda( std::optional template_builder::try_as_record_type( std::optional &parent, - const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + const clang::NamedDecl * /*cls*/, const clang::TemplateDecl *template_decl, clang::QualType &type, class_ &template_instantiation, - size_t argument_index) + size_t /*argument_index*/) { const auto *record_type = common::dereference(type)->getAs(); @@ -1152,8 +1158,8 @@ std::optional template_builder::try_as_record_type( } std::optional template_builder::try_as_enum_type( - std::optional &parent, - const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + std::optional & /*parent*/, + const clang::NamedDecl * /*cls*/, const clang::TemplateDecl *template_decl, clang::QualType &type, class_ &template_instantiation) { const auto *enum_type = type->getAs(); @@ -1177,7 +1183,7 @@ std::optional template_builder::try_as_enum_type( } std::optional template_builder::try_as_builtin_type( - std::optional &parent, + std::optional & /*parent*/, clang::QualType &type, const clang::TemplateDecl *template_decl) { const auto *builtin_type = type->getAs(); diff --git a/src/class_diagram/visitor/template_builder.h b/src/class_diagram/visitor/template_builder.h index 8e783566..9f34f699 100644 --- a/src/class_diagram/visitor/template_builder.h +++ b/src/class_diagram/visitor/template_builder.h @@ -182,4 +182,4 @@ private: clang::SourceManager &source_manager_; }; -} \ No newline at end of file +} // namespace clanguml::class_diagram::visitor \ No newline at end of file diff --git a/src/common/clang_utils.cc b/src/common/clang_utils.cc index 7c640a2f..fc9f2855 100644 --- a/src/common/clang_utils.cc +++ b/src/common/clang_utils.cc @@ -480,7 +480,10 @@ bool is_bracket(const std::string &b) return b == "(" || b == ")" || b == "[" || b == "]"; } -bool is_identifier_character(char c) { return std::isalnum(c) || c == '_'; } +bool is_identifier_character(char c) +{ + return std::isalnum(c) != 0 || c == '_'; +} bool is_identifier(const std::string &t) { @@ -509,7 +512,7 @@ bool is_keyword(const std::string &t) bool is_qualified_identifier(const std::string &t) { - return std::isalpha(t.at(0)) && + return std::isalpha(t.at(0)) != 0 && std::all_of(t.begin(), t.end(), [](const char c) { return is_identifier_character(c) || c == ':'; }); @@ -547,7 +550,7 @@ std::vector tokenize_unexposed_template_parameter( for (const auto &word : spaced_out) { if (is_qualified_identifier(word)) { if (word != "class" && word != "templated" && word != "struct") - result.push_back(word); + result.emplace_back(word); continue; } @@ -557,17 +560,17 @@ std::vector tokenize_unexposed_template_parameter( if (c == '(' || c == ')' || c == '[' || c == ']' || c == '<' || c == '>') { if (!tok.empty()) - result.push_back(tok); - result.push_back(std::string{c}); + result.emplace_back(tok); + result.emplace_back(std::string{c}); tok.clear(); } else if (c == ':') { if (!tok.empty() && tok != ":") { - result.push_back(tok); + result.emplace_back(tok); tok = ":"; } else if (tok == ":") { - result.push_back("::"); + result.emplace_back("::"); tok = ""; } else { @@ -576,30 +579,30 @@ std::vector tokenize_unexposed_template_parameter( } else if (c == ',') { if (!tok.empty()) { - result.push_back(tok); + result.emplace_back(tok); } - result.push_back(","); + result.emplace_back(","); tok.clear(); } else if (c == '*') { if (!tok.empty()) { - result.push_back(tok); + result.emplace_back(tok); } - result.push_back("*"); + result.emplace_back("*"); tok.clear(); } else if (c == '.') { // This can only be the case if we have a variadic template, // right? if (tok == "..") { - result.push_back("..."); + result.emplace_back("..."); tok.clear(); } else if (tok == ".") { tok = ".."; } else if (!tok.empty()) { - result.push_back(tok); + result.emplace_back(tok); tok = "."; } } @@ -612,7 +615,7 @@ std::vector tokenize_unexposed_template_parameter( if (!tok.empty()) { if (tok != "class" && tok != "typename" && word != "struct") - result.push_back(tok); + result.emplace_back(tok); tok.clear(); } } diff --git a/src/common/model/template_parameter.cc b/src/common/model/template_parameter.cc index faa66a56..34cc243f 100644 --- a/src/common/model/template_parameter.cc +++ b/src/common/model/template_parameter.cc @@ -28,9 +28,9 @@ std::string context::to_string() const { std::vector cv_qualifiers; if (is_const) - cv_qualifiers.push_back("const"); + cv_qualifiers.emplace_back("const"); if (is_volatile) - cv_qualifiers.push_back("volatile"); + cv_qualifiers.emplace_back("volatile"); auto res = fmt::format("{}", fmt::join(cv_qualifiers, " ")); @@ -303,7 +303,8 @@ int template_parameter::calculate_specialization_match( !base_template_parameter.deduced_context().empty() && util::starts_with( deduced_context(), base_template_parameter.deduced_context())) - res += base_template_parameter.deduced_context().size(); + res += static_cast( + base_template_parameter.deduced_context().size()); } return res; @@ -410,9 +411,8 @@ std::string template_parameter::to_string( if (skip_qualifiers) return unqualified; - else { - return util::join(" ", unqualified, deduced_context_str()); - } + + return util::join(" ", unqualified, deduced_context_str()); } if (is_member_pointer()) { @@ -433,9 +433,8 @@ std::string template_parameter::to_string( "{} ({}::*)({})", return_type, class_type, fmt::join(args, ",")); if (skip_qualifiers) return unqualified; - else { - return util::join(" ", unqualified, deduced_context_str()); - } + + return util::join(" ", unqualified, deduced_context_str()); } std::string res; @@ -627,7 +626,7 @@ bool template_parameter::is_data_pointer() const { return is_data_pointer_; } void template_parameter::is_array(bool a) { is_array_ = a; } bool template_parameter::is_array() const { return is_array_; } -void template_parameter::push_context(const context q) +void template_parameter::push_context(const context &q) { context_.push_front(q); } diff --git a/src/common/model/template_parameter.h b/src/common/model/template_parameter.h index 50e94479..7c6d0790 100644 --- a/src/common/model/template_parameter.h +++ b/src/common/model/template_parameter.h @@ -165,7 +165,7 @@ public: void is_array(bool a); bool is_array() const; - void push_context(const context q); + void push_context(const context &q); const std::deque &deduced_context() const; void deduced_context(const std::deque &c); diff --git a/src/common/visitor/ast_id_mapper.h b/src/common/visitor/ast_id_mapper.h index daedb141..306191f4 100644 --- a/src/common/visitor/ast_id_mapper.h +++ b/src/common/visitor/ast_id_mapper.h @@ -40,4 +40,4 @@ private: id_map_; }; -} // namespace clanguml::class_diagram::visitor \ No newline at end of file +} // namespace clanguml::common::visitor \ No newline at end of file