Fix clang-tidy warning after upgrading to clang-tidy-15

This commit is contained in:
Bartek Kryza
2023-03-02 00:33:28 +01:00
parent 884e021b9a
commit 464d80eca3
25 changed files with 114 additions and 78 deletions

View File

@@ -7,7 +7,9 @@ Checks: >-
-android*,
-bugprone-branch-clone,
-bugprone-exception-escape,
-bugprone-easily-swappable-parameters,
-clang-analyzer-alpha.*,
-clang-analyzer-core.StackAddressEscape,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-macro-usage,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
@@ -16,6 +18,7 @@ Checks: >-
-cppcoreguidelines-non-private-member-variables-in-classes,
-cert-env33-c,
-cert-err58-cpp,
-cert-dcl58-cpp,
-fuchsia*,
-hicpp-no-array-decay,
-hicpp-special-member-functions,
@@ -30,17 +33,21 @@ Checks: >-
-llvm-namespace-comment,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
-misc-const-correctness,
-modernize-use-nodiscard,
-modernize-use-trailing-return-type,
-modernize-concat-nested-namespaces,
-mpi*,
-objc*,
-openmp*,
-readability-inconsistent-declaration-parameter-name,
-readability-identifier-length,
-readability-identifier-naming,
-readability-redundant-smartptr-get,
-readability-convert-member-functions-to-static,
-readability-function-cognitive-complexity,
-readability-const-return-type,
-readability-simplify-boolean-expr,
-darwin*,
-zircon*
WarningsAsErrors: '*'

View File

@@ -126,7 +126,7 @@ int class_::calculate_template_specialization_match(
{
int res{};
std::string left = name_and_ns();
const std::string left = name_and_ns();
// TODO: handle variadic templates
if ((name_and_ns() != full_name) ||
(templates().size() != other.templates().size())) {

View File

@@ -32,6 +32,8 @@ public:
class_element(
common::model::access_t scope, std::string name, std::string type);
virtual ~class_element() = default;
common::model::access_t access() const;
std::string name() const;
std::string type() const;

View File

@@ -28,7 +28,7 @@ public:
class_member(common::model::access_t access, const std::string &name,
const std::string &type);
virtual ~class_member() = default;
~class_member() override = default;
bool is_relationship() const;
void is_relationship(bool is_relationship);

View File

@@ -34,7 +34,7 @@ public:
class_method(common::model::access_t access, const std::string &name,
const std::string &type);
virtual ~class_method() = default;
~class_method() override = default;
bool is_pure_virtual() const;
void is_pure_virtual(bool is_pure_virtual);

View File

@@ -207,11 +207,11 @@ bool translation_unit_visitor::VisitClassTemplateSpecializationDecl(
// Process template specialization bases
process_class_bases(cls, template_specialization);
if (get_ast_local_id(cls->getSpecializedTemplate()->getID()).has_value())
const auto maybe_id =
get_ast_local_id(cls->getSpecializedTemplate()->getID());
if (maybe_id.has_value())
template_specialization.add_relationship(
{relationship_t::kInstantiation,
get_ast_local_id(cls->getSpecializedTemplate()->getID())
.value()});
{relationship_t::kInstantiation, maybe_id.value()});
if (diagram_.should_include(template_specialization)) {
const auto name = template_specialization.full_name(false);
@@ -615,10 +615,11 @@ void translation_unit_visitor::process_concept_specialization_relationships(
const auto cpt_name = cpt->getNameAsString();
if (!get_ast_local_id(cpt->getID()))
const auto maybe_id = get_ast_local_id(cpt->getID());
if (!maybe_id)
return;
auto target_id = get_ast_local_id(cpt->getID()).value();
const auto target_id = maybe_id.value();
std::vector<std::string> constrained_template_params;
@@ -2529,14 +2530,15 @@ bool translation_unit_visitor::build_template_instantiation_add_base_classes(
}
}
if (add_template_argument_as_base_class && ct.id()) {
const auto maybe_id = ct.id();
if (add_template_argument_as_base_class && maybe_id) {
LOG_DBG("Adding template argument as base class '{}'",
ct.to_string({}, false));
class_parent cp;
cp.set_access(access_t::kPublic);
cp.set_name(ct.to_string({}, false));
cp.set_id(ct.id().value());
cp.set_id(maybe_id.value());
tinst.add_parent(std::move(cp));
}
@@ -2735,24 +2737,24 @@ void translation_unit_visitor::resolve_local_to_global_ids()
for (const auto &cls : diagram().classes()) {
for (auto &rel : cls.get().relationships()) {
if (rel.type() == relationship_t::kInstantiation) {
const auto maybe_local_id = rel.destination();
if (get_ast_local_id(maybe_local_id)) {
const auto maybe_id = get_ast_local_id(rel.destination());
if (maybe_id) {
LOG_DBG("= Resolved instantiation destination from local "
"id {} to global id {}",
maybe_local_id, *get_ast_local_id(maybe_local_id));
rel.set_destination(*get_ast_local_id(maybe_local_id));
rel.destination(), *maybe_id);
rel.set_destination(*maybe_id);
}
}
}
}
for (const auto &cpt : diagram().concepts()) {
for (auto &rel : cpt.get().relationships()) {
const auto maybe_local_id = rel.destination();
if (get_ast_local_id(maybe_local_id)) {
const auto maybe_id = get_ast_local_id(rel.destination());
if (maybe_id) {
LOG_DBG("= Resolved instantiation destination from local "
"id {} to global id {}",
maybe_local_id, *get_ast_local_id(maybe_local_id));
rel.set_destination(*get_ast_local_id(maybe_local_id));
rel.destination(), *maybe_id);
rel.set_destination(*maybe_id);
}
}
}

View File

@@ -184,7 +184,7 @@ std::string to_string(const clang::RecordType &type,
std::string to_string(const clang::Expr *expr)
{
clang::LangOptions lang_options;
const clang::LangOptions lang_options;
std::string result;
llvm::raw_string_ostream ostream(result);
expr->printPretty(ostream, nullptr, clang::PrintingPolicy(lang_options));
@@ -194,7 +194,7 @@ std::string to_string(const clang::Expr *expr)
std::string to_string(const clang::Stmt *stmt)
{
clang::LangOptions lang_options;
const clang::LangOptions lang_options;
std::string result;
llvm::raw_string_ostream ostream(result);
stmt->printPretty(ostream, nullptr, clang::PrintingPolicy(lang_options));
@@ -254,7 +254,7 @@ std::string get_source_text_raw(
std::string get_source_text(
clang::SourceRange range, const clang::SourceManager &sm)
{
clang::LangOptions lo;
const clang::LangOptions lo;
auto start_loc = sm.getSpellingLoc(range.getBegin());
auto last_token_loc = sm.getSpellingLoc(range.getEnd());
@@ -386,8 +386,7 @@ std::vector<common::model::template_parameter> parse_unexposed_template_params(
nested_params_str, ns_resolve, depth + 1);
if (nested_params.empty())
nested_params.emplace_back(
template_parameter{nested_params_str});
nested_params.emplace_back(nested_params_str);
it = bracket_match_end - 1;
}

View File

@@ -27,7 +27,6 @@ template <typename T> class nested_element_stack {
public:
nested_element_stack(bool is_flat)
: is_flat_{is_flat}
, current_level_{0}
{
current_level_groups_.push_back({});
}
@@ -66,7 +65,7 @@ public:
private:
bool is_flat_;
uint32_t current_level_;
uint32_t current_level_{0};
std::vector<std::map<std::string, std::vector<T *>>> current_level_groups_;
};

View File

@@ -212,8 +212,9 @@ inja::json generator<C, D>::element_context(const E &e) const
ctx["element"]["source"]["line"] = e.line();
}
if (e.comment().has_value()) {
ctx["element"]["comment"] = e.comment().value();
const auto maybe_comment = e.comment();
if (maybe_comment) {
ctx["element"]["comment"] = maybe_comment.value();
}
return ctx;

View File

@@ -492,12 +492,12 @@ void diagram_filter::init_filters(const config::diagram &c)
std::vector<std::string> dependencies;
for (auto &&path : c.include().dependants) {
std::filesystem::path dep_path{path};
const std::filesystem::path dep_path{path};
dependants.emplace_back(dep_path.lexically_normal().string());
}
for (auto &&path : c.include().dependencies) {
std::filesystem::path dep_path{path};
const std::filesystem::path dep_path{path};
dependencies.emplace_back(dep_path.lexically_normal().string());
}

View File

@@ -45,8 +45,8 @@ inja::json element::context() const
ctx["alias"] = alias();
ctx["full_name"] = full_name(false);
ctx["namespace"] = get_namespace().to_string();
if (comment().has_value()) {
ctx["comment"] = comment().value();
if (const auto maybe_comment = comment(); maybe_comment.has_value()) {
ctx["comment"] = maybe_comment.value();
}
return ctx;

View File

@@ -45,7 +45,10 @@ public:
std::copy(begin, end, std::back_inserter(path_));
}
path(const path &right) { path_ = right.path_; }
path(const path &right)
: path_{right.path_}
{
}
path &operator=(const path &right) = default;

View File

@@ -148,18 +148,21 @@ std::string template_parameter::to_string(
res += namespace_{type()}.relative_to(using_namespace).to_string();
}
if (concept_constraint()) {
const auto &maybe_concept_constraint = concept_constraint();
if (maybe_concept_constraint) {
if (!relative)
res += namespace_{concept_constraint().value()}.to_string();
res += namespace_{maybe_concept_constraint.value()}.to_string();
else
res += namespace_{concept_constraint().value()}
res += namespace_{maybe_concept_constraint.value()}
.relative_to(using_namespace)
.to_string();
}
if (!name().empty()) {
if (!type().empty() || concept_constraint())
if (!type().empty() || maybe_concept_constraint)
res += " ";
if (!relative)
res += namespace_{name()}.to_string();
else
@@ -169,6 +172,7 @@ std::string template_parameter::to_string(
// Render nested template params
if (!template_params_.empty()) {
std::vector<std::string> params;
params.reserve(template_params_.size());
for (const auto &template_param : template_params_) {
params.push_back(
template_param.to_string(using_namespace, relative));
@@ -197,8 +201,9 @@ bool template_parameter::find_nested_relationships(
// If this type argument should be included in the relationship
// just add it and skip recursion (e.g. this is a user defined type)
if (should_include(name())) {
if (id()) {
nested_relationships.emplace_back(id().value(), hint);
const auto maybe_id = id();
if (maybe_id) {
nested_relationships.emplace_back(maybe_id.value(), hint);
added_aggregation_relationship =
(hint == common::model::relationship_t::kAggregation);
}
@@ -207,11 +212,10 @@ bool template_parameter::find_nested_relationships(
// interested what is stored inside it
else {
for (const auto &template_argument : template_params()) {
if (should_include(template_argument.name()) &&
template_argument.id()) {
const auto maybe_id = template_argument.id();
if (should_include(template_argument.name()) && maybe_id) {
nested_relationships.emplace_back(
template_argument.id().value(), hint);
nested_relationships.emplace_back(maybe_id.value(), hint);
added_aggregation_relationship =
(hint == common::model::relationship_t::kAggregation);

View File

@@ -27,7 +27,6 @@ std::ostream &template_trait::render_template_params(std::ostream &ostr,
if (!templates_.empty()) {
std::vector<std::string> tnames;
std::vector<std::string> tnames_simplified;
std::transform(templates_.cbegin(), templates_.cend(),
std::back_inserter(tnames),

View File

@@ -33,23 +33,38 @@ public:
optional_ref() = default;
optional_ref(T *value) { value_ = value; }
optional_ref(T *value)
: value_{value}
{
}
optional_ref(const T *value) { value_ = value; }
optional_ref(const T *value)
: value_{value}
{
}
optional_ref(T &value) { value_ = &value; }
optional_ref(T &value)
: value_{&value}
{
}
optional_ref(const T &value) { value_ = &value; }
optional_ref(const T &value)
: value_{&value}
{
}
optional_ref(optional_ref &right) { value_ = right.get(); }
optional_ref(optional_ref &right)
: value_{right.get()}
{
}
template <typename V,
typename = std::enable_if<
std::is_base_of_v<optional_type, typename V::optional_type> ||
std::is_same_v<optional_type, typename V::optional_type>>>
optional_ref(const V &t)
: value_{t.get()}
{
value_ = t.get();
}
template <typename V,
@@ -57,16 +72,16 @@ public:
std::is_base_of_v<optional_type, typename V::optional_type> ||
std::is_same_v<optional_type, typename V::optional_type>>>
optional_ref(V &&t)
: value_{t.get()}
{
value_ = t.get();
t.reset();
}
template <typename V,
typename = std::enable_if<std::is_base_of_v<optional_type, V>>>
optional_ref(const std::reference_wrapper<V> &t)
: value_{&t.get()}
{
value_ = &t.get();
}
optional_ref &operator=(const optional_ref &right)

View File

@@ -53,6 +53,8 @@ public:
explicit translation_unit_visitor(
clang::SourceManager &sm, const clanguml::config::diagram &config);
virtual ~translation_unit_visitor() = default;
/**
* @brief Get clang::SourceManager
* @return Reference to @link clang::SourceManager used by this translation

View File

@@ -185,7 +185,7 @@ std::vector<std::shared_ptr<decorator>> parse(
documentation_block, "\\" + clanguml_tag, "@" + clanguml_tag);
documentation_block = util::trim(documentation_block);
std::string_view block_view{documentation_block};
const std::string_view block_view{documentation_block};
auto pos = block_view.find("@" + clanguml_tag + "{");
while (pos < documentation_block.size()) {

View File

@@ -47,6 +47,8 @@ public:
clanguml::include_diagram::model::diagram &diagram,
const clanguml::config::include_diagram &config);
~include_visitor() override = default;
#if LLVM_VERSION_MAJOR > 14
void InclusionDirective(clang::SourceLocation hash_loc,
const clang::Token &include_tok, clang::StringRef file_name,

View File

@@ -49,7 +49,6 @@ backward::SignalHandling sh; // NOLINT
#endif
using namespace clanguml;
using config::config;
/**
* Print the program version and basic information

View File

@@ -27,12 +27,10 @@
namespace clanguml::package_diagram::visitor {
using clanguml::common::model::access_t;
using clanguml::common::model::namespace_;
using clanguml::common::model::package;
using clanguml::common::model::relationship;
using clanguml::common::model::relationship_t;
using clanguml::package_diagram::model::diagram;
translation_unit_visitor::translation_unit_visitor(clang::SourceManager &sm,
clanguml::package_diagram::model::diagram &diagram,

View File

@@ -45,6 +45,8 @@ public:
clanguml::package_diagram::model::diagram &diagram,
const clanguml::config::package_diagram &config);
~translation_unit_visitor() override = default;
virtual bool VisitNamespaceDecl(clang::NamespaceDecl *ns);
virtual bool VisitCXXRecordDecl(clang::CXXRecordDecl *cls);

View File

@@ -265,7 +265,7 @@ std::string function_template::message_name(message_render_mode mode) const
std::ostringstream s;
render_template_params(s, using_namespace(), true);
std::string template_params = s.str();
const std::string template_params = s.str();
if (mode == message_render_mode::no_arguments) {
return fmt::format(

View File

@@ -1098,9 +1098,10 @@ bool translation_unit_visitor::process_class_template_method_call_expression(
m.set_message_name(
dependent_member_callee->getMember().getAsString());
if (get_unique_id(template_declaration->getID()))
diagram().add_active_participant(
get_unique_id(template_declaration->getID()).value());
if (const auto maybe_id =
get_unique_id(template_declaration->getID());
maybe_id.has_value())
diagram().add_active_participant(maybe_id.value());
}
}
else {
@@ -1137,12 +1138,13 @@ bool translation_unit_visitor::process_function_call_expression(
std::unique_ptr<model::function_template> f_ptr;
if (!get_unique_id(callee_function->getID()).has_value()) {
const auto maybe_id = get_unique_id(callee_function->getID());
if (!maybe_id.has_value()) {
// This is hopefully not an interesting call...
m.set_to(callee_function->getID());
}
else {
m.set_to(get_unique_id(callee_function->getID()).value());
m.set_to(maybe_id.value());
}
m.set_message_name(callee_name.substr(0, callee_name.size() - 2));
@@ -1168,10 +1170,11 @@ bool translation_unit_visitor::process_unresolved_lookup_call_expression(
const auto *ftd =
clang::dyn_cast_or_null<clang::FunctionTemplateDecl>(decl);
if (!get_unique_id(ftd->getID()).has_value())
const auto maybe_id = get_unique_id(ftd->getID());
if (!maybe_id.has_value())
m.set_to(ftd->getID());
else {
m.set_to(get_unique_id(ftd->getID()).value());
m.set_to(maybe_id.value());
}
break;
@@ -1748,14 +1751,11 @@ void translation_unit_visitor::process_template_specialization_argument(
}
else if ((arg.getAsType()->getAsCXXRecordDecl() != nullptr) &&
arg.getAsType()->getAsCXXRecordDecl()->isLambda()) {
if (get_unique_id(arg.getAsType()->getAsCXXRecordDecl()->getID())
.has_value()) {
argument.set_name(get_participant(
get_unique_id(
arg.getAsType()->getAsCXXRecordDecl()->getID())
.value())
.value()
.full_name(false));
const auto maybe_id =
get_unique_id(arg.getAsType()->getAsCXXRecordDecl()->getID());
if (maybe_id.has_value()) {
argument.set_name(
get_participant(maybe_id.value()).value().full_name(false));
}
else {
const auto type_name =

View File

@@ -42,6 +42,8 @@ public:
clanguml::sequence_diagram::model::diagram &diagram,
const clanguml::config::sequence_diagram &config);
~translation_unit_visitor() override = default;
bool shouldVisitTemplateInstantiations();
bool VisitCallExpr(clang::CallExpr *expr);

View File

@@ -53,7 +53,7 @@ std::string get_process_output(const std::string &command)
std::string result;
#if defined(__linux) || defined(__unix) || defined(__APPLE__)
std::unique_ptr<FILE, decltype(&pclose)> pipe(
const std::unique_ptr<FILE, decltype(&pclose)> pipe(
popen(command.c_str(), "r"), pclose);
#elif defined(_WIN32)
std::unique_ptr<FILE, decltype(&_pclose)> pipe(
@@ -147,13 +147,13 @@ std::string get_git_toplevel_dir()
std::string ltrim(const std::string &s)
{
size_t start = s.find_first_not_of(WHITESPACE);
const size_t start = s.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : s.substr(start);
}
std::string rtrim(const std::string &s)
{
size_t end = s.find_last_not_of(WHITESPACE);
const size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
@@ -210,7 +210,7 @@ std::string abbreviate(const std::string &s, const unsigned int max_length)
bool find_element_alias(
const std::string &input, std::tuple<std::string, size_t, size_t> &result)
{
std::regex alias_regex(R"((@A\([^\).]+\)))");
const std::regex alias_regex(R"((@A\([^\).]+\)))");
auto alias_it =
std::sregex_iterator(input.begin(), input.end(), alias_regex);
@@ -219,8 +219,8 @@ bool find_element_alias(
if (alias_it == end_it)
return false;
std::smatch match = *alias_it;
std::string alias = match.str().substr(3, match.str().size() - 4);
const std::smatch &match = *alias_it;
const std::string alias = match.str().substr(3, match.str().size() - 4);
std::get<0>(result) = alias;
std::get<1>(result) = match.position();