From 501a64211968de13968a8e0a6a308effeba569eb Mon Sep 17 00:00:00 2001 From: Paul Bauer Date: Mon, 15 Feb 2021 09:45:02 +0000 Subject: [PATCH] Fix clang-tidy-11 for topology module Part of updates for clang-tidy changes to require variable initialization. --- src/gromacs/topology/atomprop.cpp | 14 +- src/gromacs/topology/atoms.cpp | 46 ++-- src/gromacs/topology/block.cpp | 37 ++- src/gromacs/topology/forcefieldparameters.cpp | 23 +- src/gromacs/topology/idef.cpp | 17 +- src/gromacs/topology/index.cpp | 256 +++++++----------- src/gromacs/topology/invblock.cpp | 20 +- src/gromacs/topology/mtop_lookup.h | 16 +- src/gromacs/topology/mtop_util.cpp | 74 ++--- src/gromacs/topology/symtab.cpp | 61 ++--- src/gromacs/topology/tests/symtab.cpp | 22 +- src/gromacs/topology/topology.cpp | 28 +- src/gromacs/topology/topsort.cpp | 34 +-- 13 files changed, 261 insertions(+), 387 deletions(-) diff --git a/src/gromacs/topology/atomprop.cpp b/src/gromacs/topology/atomprop.cpp index 4874044330..4516c324e0 100644 --- a/src/gromacs/topology/atomprop.cpp +++ b/src/gromacs/topology/atomprop.cpp @@ -4,7 +4,7 @@ * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team. - * Copyright (c) 2019,2020, by the GROMACS development team, led by + * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -237,8 +237,8 @@ static void addProperty(AtomProperty* ap, real propValue, int line) { - bool bExact; - int j = findPropertyIndex(ap, restype, residueName, atomName, &bExact); + bool bExact = false; + int j = findPropertyIndex(ap, restype, residueName, atomName, &bExact); if (!bExact) { @@ -296,7 +296,7 @@ static void readProperty(AtomProperty* ap, ResidueType* restype, double factor) while (get_a_line(fp.get(), line, STRLEN)) { line_no++; - double pp; + double pp = 0.0; if (sscanf(line, "%31s %31s %20lf", resnm, atomnm, &pp) == 3) { pp *= factor; @@ -391,9 +391,8 @@ bool AtomProperties::setAtomProperty(int eprop, const std::string& atomName, real* value) { - int j; std::string tmpAtomName, tmpResidueName; - gmx_bool bExact; + bool bExact = false; if (setProperties(prop(eprop), restype(), eprop, impl_->bWarned)) { @@ -410,7 +409,8 @@ bool AtomProperties::setAtomProperty(int eprop, { tmpAtomName = atomName; } - j = findPropertyIndex(&(impl_->prop[eprop]), &impl_->restype, residueName, tmpAtomName, &bExact); + const int j = findPropertyIndex( + &(impl_->prop[eprop]), &impl_->restype, residueName, tmpAtomName, &bExact); if (eprop == epropVDW && !impl_->bWarnVDW) { diff --git a/src/gromacs/topology/atoms.cpp b/src/gromacs/topology/atoms.cpp index 4da6464a03..3615ed3eab 100644 --- a/src/gromacs/topology/atoms.cpp +++ b/src/gromacs/topology/atoms.cpp @@ -4,7 +4,7 @@ * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team. - * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -101,8 +101,6 @@ void done_atomtypes(t_atomtypes* atype) void add_t_atoms(t_atoms* atoms, int natom_extra, int nres_extra) { - int i; - if (natom_extra > 0) { srenew(atoms->atomname, atoms->nr + natom_extra); @@ -119,7 +117,7 @@ void add_t_atoms(t_atoms* atoms, int natom_extra, int nres_extra) { srenew(atoms->atomtypeB, atoms->nr + natom_extra); } - for (i = atoms->nr; (i < atoms->nr + natom_extra); i++) + for (int i = atoms->nr; (i < atoms->nr + natom_extra); i++) { atoms->atomname[i] = nullptr; memset(&atoms->atom[i], 0, sizeof(atoms->atom[i])); @@ -141,7 +139,7 @@ void add_t_atoms(t_atoms* atoms, int natom_extra, int nres_extra) if (nres_extra > 0) { srenew(atoms->resinfo, atoms->nres + nres_extra); - for (i = atoms->nres; (i < atoms->nres + nres_extra); i++) + for (int i = atoms->nres; (i < atoms->nres + nres_extra); i++) { std::memset(&atoms->resinfo[i], 0, sizeof(atoms->resinfo[i])); } @@ -187,8 +185,7 @@ void gmx_pdbinfo_init_default(t_pdbinfo* pdbinfo) t_atoms* copy_t_atoms(const t_atoms* src) { - t_atoms* dst; - int i; + t_atoms* dst = nullptr; snew(dst, 1); init_t_atoms(dst, src->nr, (nullptr != src->pdbinfo)); @@ -205,7 +202,7 @@ t_atoms* copy_t_atoms(const t_atoms* src) { snew(dst->atomtypeB, src->nr); } - for (i = 0; (i < src->nr); i++) + for (int i = 0; (i < src->nr); i++) { dst->atom[i] = src->atom[i]; if (nullptr != src->pdbinfo) @@ -231,7 +228,7 @@ t_atoms* copy_t_atoms(const t_atoms* src) dst->havePdbInfo = src->havePdbInfo; dst->haveType = src->haveType; dst->nres = src->nres; - for (i = 0; (i < src->nres); i++) + for (int i = 0; (i < src->nres); i++) { dst->resinfo[i] = src->resinfo[i]; } @@ -247,25 +244,21 @@ void t_atoms_set_resinfo(t_atoms* atoms, int chainnum, char chainid) { - t_resinfo* ri; - - ri = &atoms->resinfo[atoms->atom[atom_ind].resind]; - ri->name = put_symtab(symtab, resname); - ri->rtp = nullptr; - ri->nr = resnr; - ri->ic = ic; - ri->chainnum = chainnum; - ri->chainid = chainid; + t_resinfo* ri = &atoms->resinfo[atoms->atom[atom_ind].resind]; + ri->name = put_symtab(symtab, resname); + ri->rtp = nullptr; + ri->nr = resnr; + ri->ic = ic; + ri->chainnum = chainnum; + ri->chainid = chainid; } static void pr_atom(FILE* fp, int indent, const char* title, const t_atom* atom, int n) { - int i; - if (available(fp, atom, indent, title)) { indent = pr_title_n(fp, indent, title, n); - for (i = 0; i < n; i++) + for (int i = 0; i < n; i++) { pr_indent(fp, indent); fprintf(fp, @@ -288,12 +281,10 @@ static void pr_atom(FILE* fp, int indent, const char* title, const t_atom* atom, static void pr_strings2(FILE* fp, int indent, const char* title, char*** nm, char*** nmB, int n, gmx_bool bShowNumbers) { - int i; - if (available(fp, nm, indent, title)) { indent = pr_title_n(fp, indent, title, n); - for (i = 0; i < n; i++) + for (int i = 0; i < n; i++) { pr_indent(fp, indent); fprintf(fp, "%s[%d]={name=\"%s\",nameB=\"%s\"}\n", title, bShowNumbers ? i : -1, *(nm[i]), *(nmB[i])); @@ -303,12 +294,10 @@ static void pr_strings2(FILE* fp, int indent, const char* title, char*** nm, cha static void pr_resinfo(FILE* fp, int indent, const char* title, const t_resinfo* resinfo, int n, gmx_bool bShowNumbers) { - int i; - if (available(fp, resinfo, indent, title)) { indent = pr_title_n(fp, indent, title, n); - for (i = 0; i < n; i++) + for (int i = 0; i < n; i++) { pr_indent(fp, indent); fprintf(fp, @@ -337,11 +326,10 @@ void pr_atoms(FILE* fp, int indent, const char* title, const t_atoms* atoms, gmx void pr_atomtypes(FILE* fp, int indent, const char* title, const t_atomtypes* atomtypes, gmx_bool bShowNumbers) { - int i; if (available(fp, atomtypes, indent, title)) { indent = pr_title(fp, indent, title); - for (i = 0; i < atomtypes->nr; i++) + for (int i = 0; i < atomtypes->nr; i++) { pr_indent(fp, indent); fprintf(fp, "atomtype[%3d]={atomnumber=%4d}\n", bShowNumbers ? i : -1, atomtypes->atomnumber[i]); diff --git a/src/gromacs/topology/block.cpp b/src/gromacs/topology/block.cpp index b31d5e2c06..8ffa830c50 100644 --- a/src/gromacs/topology/block.cpp +++ b/src/gromacs/topology/block.cpp @@ -4,7 +4,7 @@ * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team. - * Copyright (c) 2019,2020, by the GROMACS development team, led by + * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -104,7 +104,7 @@ void init_blocka_null(t_blocka* block) t_blocka* new_blocka() { - t_blocka* block; + t_blocka* block = nullptr; snew(block, 1); snew(block->index, 1); @@ -235,17 +235,15 @@ static int pr_listoflists_title(FILE* fp, int indent, const char* title, const g static void low_pr_blocka(FILE* fp, int indent, const char* title, const t_blocka* block, gmx_bool bShowNumbers) { - int i; - if (available(fp, block, indent, title)) { indent = pr_blocka_title(fp, indent, title, block); - for (i = 0; i <= block->nr; i++) + for (int i = 0; i <= block->nr; i++) { pr_indent(fp, indent + INDENT); fprintf(fp, "%s->index[%d]=%d\n", title, bShowNumbers ? i : -1, block->index[i]); } - for (i = 0; i < block->nra; i++) + for (int i = 0; i < block->nra; i++) { pr_indent(fp, indent + INDENT); fprintf(fp, "%s->a[%d]=%d\n", title, bShowNumbers ? i : -1, block->a[i]); @@ -255,19 +253,17 @@ static void low_pr_blocka(FILE* fp, int indent, const char* title, const t_block void pr_block(FILE* fp, int indent, const char* title, const t_block* block, gmx_bool bShowNumbers) { - int i, start; - if (available(fp, block, indent, title)) { - indent = pr_block_title(fp, indent, title, block); - start = 0; + indent = pr_block_title(fp, indent, title, block); + int start = 0; if (block->index[start] != 0) { fprintf(fp, "block->index[%d] should be 0\n", start); } else { - for (i = 0; i < block->nr; i++) + for (int i = 0; i < block->nr; i++) { int end = block->index[i + 1]; pr_indent(fp, indent); @@ -292,23 +288,24 @@ void pr_block(FILE* fp, int indent, const char* title, const t_block* block, gmx void pr_blocka(FILE* fp, int indent, const char* title, const t_blocka* block, gmx_bool bShowNumbers) { - int i, j, ok, size, start, end; + bool ok = false; if (available(fp, block, indent, title)) { - indent = pr_blocka_title(fp, indent, title, block); - start = 0; - end = start; - if ((ok = static_cast(block->index[start] == 0)) == 0) + indent = pr_blocka_title(fp, indent, title, block); + int start = 0; + int end = start; + ok = (block->index[start] == 0); + if (!ok) { fprintf(fp, "block->index[%d] should be 0\n", start); } else { - for (i = 0; i < block->nr; i++) + for (int i = 0; i < block->nr; i++) { - end = block->index[i + 1]; - size = pr_indent(fp, indent); + end = block->index[i + 1]; + int size = pr_indent(fp, indent); if (end <= start) { size += fprintf(fp, "%s[%d]={", title, i); @@ -322,7 +319,7 @@ void pr_blocka(FILE* fp, int indent, const char* title, const t_blocka* block, g bShowNumbers ? start : -1, bShowNumbers ? end - 1 : -1); } - for (j = start; j < end; j++) + for (int j = start; j < end; j++) { if (j > start) { diff --git a/src/gromacs/topology/forcefieldparameters.cpp b/src/gromacs/topology/forcefieldparameters.cpp index 24c72ca9ad..d15cdfc087 100644 --- a/src/gromacs/topology/forcefieldparameters.cpp +++ b/src/gromacs/topology/forcefieldparameters.cpp @@ -4,7 +4,7 @@ * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. * Copyright (c) 2013,2014,2015,2016,2018 by the GROMACS development team. - * Copyright (c) 2019,2020, by the GROMACS development team, led by + * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -44,18 +44,9 @@ static void pr_cmap(FILE* fp, int indent, const char* title, const gmx_cmap_t* cmap_grid, gmx_bool bShowNumbers) { - int j, nelem; - real dx, idx; + const real dx = cmap_grid->grid_spacing != 0 ? 360.0 / cmap_grid->grid_spacing : 0; - if (cmap_grid->grid_spacing != 0) - { - dx = 360.0 / cmap_grid->grid_spacing; - } - else - { - dx = 0; - } - nelem = cmap_grid->grid_spacing * cmap_grid->grid_spacing; + const int nelem = cmap_grid->grid_spacing * cmap_grid->grid_spacing; if (available(fp, cmap_grid, indent, title)) { @@ -63,12 +54,12 @@ static void pr_cmap(FILE* fp, int indent, const char* title, const gmx_cmap_t* c for (gmx::index i = 0; i < gmx::ssize(cmap_grid->cmapdata); i++) { - idx = -180.0; + real idx = -180.0; fprintf(fp, "%8s %8s %8s %8s\n", "V", "dVdx", "dVdy", "d2dV"); fprintf(fp, "grid[%3zd]={\n", bShowNumbers ? i : -1); - for (j = 0; j < nelem; j++) + for (int j = 0; j < nelem; j++) { if ((j % cmap_grid->grid_spacing) == 0) { @@ -88,14 +79,12 @@ static void pr_cmap(FILE* fp, int indent, const char* title, const gmx_cmap_t* c void pr_ffparams(FILE* fp, int indent, const char* title, const gmx_ffparams_t* ffparams, gmx_bool bShowNumbers) { - int i; - indent = pr_title(fp, indent, title); pr_indent(fp, indent); fprintf(fp, "atnr=%d\n", ffparams->atnr); pr_indent(fp, indent); fprintf(fp, "ntypes=%d\n", ffparams->numTypes()); - for (i = 0; i < ffparams->numTypes(); i++) + for (int i = 0; i < ffparams->numTypes(); i++) { pr_indent(fp, indent + INDENT); fprintf(fp, diff --git a/src/gromacs/topology/idef.cpp b/src/gromacs/topology/idef.cpp index 086fd8d29f..9d33f271a6 100644 --- a/src/gromacs/topology/idef.cpp +++ b/src/gromacs/topology/idef.cpp @@ -403,8 +403,6 @@ static void printIlist(FILE* fp, gmx_bool bShowParameters, const t_iparams* iparams) { - int i, j, k, type, ftype; - indent = pr_title(fp, indent, title); pr_indent(fp, indent); fprintf(fp, "nr: %d\n", ilist.size()); @@ -412,18 +410,19 @@ static void printIlist(FILE* fp, { pr_indent(fp, indent); fprintf(fp, "iatoms:\n"); - for (i = j = 0; i < ilist.size();) + int j = 0; + for (int i = 0; i < ilist.size();) { pr_indent(fp, indent + INDENT); - type = ilist.iatoms[i]; - ftype = functype[type]; + const int type = ilist.iatoms[i]; + const int ftype = functype[type]; if (bShowNumbers) { fprintf(fp, "%d type=%d ", j, type); } j++; printf("(%s)", interaction_function[ftype].name); - for (k = 0; k < interaction_function[ftype].nratoms; k++) + for (int k = 0; k < interaction_function[ftype].nratoms; k++) { fprintf(fp, " %3d", ilist.iatoms[i + 1 + k]); } @@ -452,8 +451,6 @@ void pr_ilist(FILE* fp, void pr_idef(FILE* fp, int indent, const char* title, const t_idef* idef, gmx_bool bShowNumbers, gmx_bool bShowParameters) { - int i, j; - if (available(fp, idef, indent, title)) { indent = pr_title(fp, indent, title); @@ -461,7 +458,7 @@ void pr_idef(FILE* fp, int indent, const char* title, const t_idef* idef, gmx_bo fprintf(fp, "atnr=%d\n", idef->atnr); pr_indent(fp, indent); fprintf(fp, "ntypes=%d\n", idef->ntypes); - for (i = 0; i < idef->ntypes; i++) + for (int i = 0; i < idef->ntypes; i++) { pr_indent(fp, indent + INDENT); fprintf(fp, @@ -472,7 +469,7 @@ void pr_idef(FILE* fp, int indent, const char* title, const t_idef* idef, gmx_bo } pr_real(fp, indent, "fudgeQQ", idef->fudgeQQ); - for (j = 0; (j < F_NRE); j++) + for (int j = 0; (j < F_NRE); j++) { printIlist(fp, indent, diff --git a/src/gromacs/topology/index.cpp b/src/gromacs/topology/index.cpp index bb2d495e49..6d671af2b5 100644 --- a/src/gromacs/topology/index.cpp +++ b/src/gromacs/topology/index.cpp @@ -4,7 +4,7 @@ * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team. - * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -45,6 +45,7 @@ #include #include +#include #include "gromacs/topology/atoms.h" #include "gromacs/topology/block.h" @@ -59,7 +60,7 @@ static gmx_bool gmx_ask_yesno(gmx_bool bASK) { - char c; + char c = 0; if (bASK) { @@ -78,15 +79,12 @@ static gmx_bool gmx_ask_yesno(gmx_bool bASK) void write_index(const char* outf, t_blocka* b, char** gnames, gmx_bool bDuplicate, int natoms) { - FILE* out; - int i, j, k; - - out = gmx_ffopen(outf, "w"); + FILE* out = gmx_ffopen(outf, "w"); /* fprintf(out,"%5d %5d\n",b->nr,b->nra); */ - for (i = 0; (i < b->nr); i++) + for (int i = 0; (i < b->nr); i++) { fprintf(out, "[ %s ]", gnames[i]); - for (k = 0, j = b->index[i]; j < b->index[i + 1]; j++, k++) + for (int k = 0, j = b->index[i]; j < b->index[i + 1]; j++, k++) { const char sep = (k % 15 == 0 ? '\n' : ' '); fprintf(out, "%c%4d", sep, b->a[j] + 1); @@ -98,10 +96,10 @@ void write_index(const char* outf, t_blocka* b, char** gnames, gmx_bool bDuplica if (bDuplicate) { fprintf(stderr, "Duplicating the whole system with an atom offset of %d atoms.\n", natoms); - for (i = 0; (i < b->nr); i++) + for (int i = 0; (i < b->nr); i++) { fprintf(out, "[ %s_copy ]", gnames[i]); - for (k = 0, j = b->index[i]; j < b->index[i + 1]; j++, k++) + for (int k = 0, j = b->index[i]; j < b->index[i + 1]; j++, k++) { const char sep = (k % 15 == 0 ? '\n' : ' '); fprintf(out, "%c%4d", sep, b->a[j] + 1 + natoms); @@ -228,12 +226,10 @@ static void analyse_other(gmx::ArrayRef restype, gmx_bool bASK, gmx_bool bVerb) { - restp_t* restp = nullptr; - char** attp = nullptr; - char * rname, *aname; - int i, resind, natp, nrestp = 0; + std::vector restp; + int i = 0; - for (i = 0; (i < atoms->nres); i++) + for (; (i < atoms->nres); i++) { if (gmx_strcasecmp(restype[i].c_str(), "Protein") && gmx_strcasecmp(restype[i].c_str(), "DNA") && gmx_strcasecmp(restype[i].c_str(), "RNA") @@ -252,38 +248,32 @@ static void analyse_other(gmx::ArrayRef restype, } for (int k = 0; (k < atoms->nr); k++) { - resind = atoms->atom[k].resind; - rname = *atoms->resinfo[resind].name; + int resind = atoms->atom[k].resind; + const char* rname = *atoms->resinfo[resind].name; if (gmx_strcasecmp(restype[resind].c_str(), "Protein") && gmx_strcasecmp(restype[resind].c_str(), "DNA") && gmx_strcasecmp(restype[resind].c_str(), "RNA") && gmx_strcasecmp(restype[resind].c_str(), "Water")) { - int l; - for (l = 0; (l < nrestp); l++) - { - assert(restp); - if (strcmp(restp[l].rname, rname) == 0) - { - break; - } - } - if (l == nrestp) + auto found = std::find_if(restp.begin(), restp.end(), [rname](const auto& entry) { + return strcmp(entry.rname, rname) == 0; + }); + if (found == restp.end()) { - srenew(restp, nrestp + 1); - restp[nrestp].rname = gmx_strdup(rname); - restp[nrestp].bNeg = FALSE; - restp[nrestp].gname = gmx_strdup(rname); - nrestp++; + restp.emplace_back(); + auto& last = restp.back(); + last.rname = gmx_strdup(rname); + last.bNeg = false; + last.gname = gmx_strdup(rname); } } } - for (int i = 0; (i < nrestp); i++) + for (int i = 0; (i < gmx::ssize(restp)); i++) { std::vector aid; for (int j = 0; (j < atoms->nr); j++) { - rname = *atoms->resinfo[atoms->atom[j].resind].name; + const char* rname = *atoms->resinfo[atoms->atom[j].resind].name; if ((strcmp(restp[i].rname, rname) == 0 && !restp[i].bNeg) || (strcmp(restp[i].rname, rname) != 0 && restp[i].bNeg)) { @@ -297,32 +287,27 @@ static void analyse_other(gmx::ArrayRef restype, fflush(stdout); if (gmx_ask_yesno(bASK)) { - natp = 0; + std::vector attp; for (size_t k = 0; (k < aid.size()); k++) { - aname = *atoms->atomname[aid[k]]; - int l; - for (l = 0; (l < natp); l++) - { - if (strcmp(aname, attp[l]) == 0) - { - break; - } - } - if (l == natp) + const char* aname = *atoms->atomname[aid[k]]; + auto found = std::find_if(attp.begin(), attp.end(), [aname](const char* entry) { + return strcmp(aname, entry) == 0; + }); + if (found == attp.end()) { - srenew(attp, ++natp); - attp[natp - 1] = aname; + attp.emplace_back(aname); } } - if (natp > 1) + if (attp.size() > 1) { + const int natp = attp.size(); for (int l = 0; (l < natp); l++) { std::vector aaid; for (size_t k = 0; (k < aid.size()); k++) { - aname = *atoms->atomname[aid[k]]; + const char* aname = *atoms->atomname[aid[k]]; if (strcmp(aname, attp[l]) == 0) { aaid.push_back(aid[k]); @@ -331,14 +316,11 @@ static void analyse_other(gmx::ArrayRef restype, add_grp(gb, gn, aaid, attp[l]); } } - sfree(attp); - attp = nullptr; } } sfree(restp[i].rname); sfree(restp[i].gname); } - sfree(restp); } } @@ -401,11 +383,7 @@ static void analyse_prot(gmx::ArrayRef restype, }; const int num_index_groups = asize(constructing_data); - int n, j; - int npres; - gmx_bool match; - char ndx_name[STRLEN], *atnm; - int i; + char ndx_name[STRLEN]; if (bVerb) { @@ -414,8 +392,8 @@ static void analyse_prot(gmx::ArrayRef restype, std::vector aid; /* calculate the number of protein residues */ - npres = 0; - for (i = 0; (i < atoms->nres); i++) + int npres = 0; + for (int i = 0; (i < atoms->nres); i++) { if (0 == gmx_strcasecmp(restype[i].c_str(), "Protein")) { @@ -423,17 +401,17 @@ static void analyse_prot(gmx::ArrayRef restype, } } /* find matching or complement atoms */ - for (i = 0; (i < num_index_groups); i++) + for (int i = 0; (i < num_index_groups); i++) { - for (n = 0; (n < atoms->nr); n++) + for (int n = 0; (n < atoms->nr); n++) { if (0 == gmx_strcasecmp(restype[atoms->atom[n].resind].c_str(), "Protein")) { - match = FALSE; - for (j = 0; (j < constructing_data[i].num_defining_atomnames); j++) + bool match = false; + for (int j = 0; (j < constructing_data[i].num_defining_atomnames); j++) { /* skip digits at beginning of atomname, e.g. 1H */ - atnm = *atoms->atomname[n]; + char* atnm = *atoms->atomname[n]; while (isdigit(atnm[0])) { atnm++; @@ -442,7 +420,7 @@ static void analyse_prot(gmx::ArrayRef restype, { if (0 == gmx_strcasecmp(constructing_data[i].defining_atomnames[j], atnm)) { - match = TRUE; + match = true; } } else @@ -452,7 +430,7 @@ static void analyse_prot(gmx::ArrayRef restype, atnm, strlen(constructing_data[i].defining_atomnames[j]))) { - match = TRUE; + match = true; } } } @@ -473,26 +451,25 @@ static void analyse_prot(gmx::ArrayRef restype, if (bASK) { - for (i = 0; (i < num_index_groups); i++) + for (int i = 0; (i < num_index_groups); i++) { printf("Split %12s into %5d residues (y/n) ? ", constructing_data[i].group_name, npres); if (gmx_ask_yesno(bASK)) { - int resind; aid.clear(); - for (n = 0; ((atoms->atom[n].resind < npres) && (n < atoms->nr));) + for (int n = 0; ((atoms->atom[n].resind < npres) && (n < atoms->nr));) { - resind = atoms->atom[n].resind; + int resind = atoms->atom[n].resind; for (; ((atoms->atom[n].resind == resind) && (n < atoms->nr)); n++) { - match = FALSE; - for (j = 0; (j < constructing_data[i].num_defining_atomnames); j++) + bool match = false; + for (int j = 0; (j < constructing_data[i].num_defining_atomnames); j++) { if (0 == gmx_strcasecmp(constructing_data[i].defining_atomnames[j], *atoms->atomname[n])) { - match = TRUE; + match = true; } } if (constructing_data[i].bTakeComplement != match) @@ -503,8 +480,7 @@ static void analyse_prot(gmx::ArrayRef restype, /* copy the residuename to the tail of the groupname */ if (!aid.empty()) { - t_resinfo* ri; - ri = &atoms->resinfo[resind]; + t_resinfo* ri = &atoms->resinfo[resind]; sprintf(ndx_name, "%s_%s%d%c", constructing_data[i].group_name, @@ -576,21 +552,13 @@ static void analyse_prot(gmx::ArrayRef restype, void analyse(const t_atoms* atoms, t_blocka* gb, char*** gn, gmx_bool bASK, gmx_bool bVerb) { - char* resnm; - int i; - int iwater, iion; - int nwater, nion; - if (bVerb) { printf("Analysing residue names:\n"); } /* Create system group, every single atom */ std::vector aid(atoms->nr); - for (i = 0; i < atoms->nr; i++) - { - aid[i] = i; - } + std::iota(aid.begin(), aid.end(), 0); add_grp(gb, gn, aid, "System"); /* For every residue, get a pointer to the residue type name */ @@ -600,15 +568,13 @@ void analyse(const t_atoms* atoms, t_blocka* gb, char*** gn, gmx_bool bASK, gmx_ std::vector previousTypename; if (atoms->nres > 0) { - int i = 0; - - resnm = *atoms->resinfo[i].name; + const char* resnm = *atoms->resinfo[0].name; restype.emplace_back(rt.typeOfNamedDatabaseResidue(resnm)); previousTypename.push_back(restype.back()); - for (i = 1; i < atoms->nres; i++) + for (int i = 1; i < atoms->nres; i++) { - resnm = *atoms->resinfo[i].name; + const char* resnm = *atoms->resinfo[i].name; restype.emplace_back(rt.typeOfNamedDatabaseResidue(resnm)); /* Note that this does not lead to a N*N loop, but N*K, where @@ -633,7 +599,7 @@ void analyse(const t_atoms* atoms, t_blocka* gb, char*** gn, gmx_bool bASK, gmx_ for (gmx::index k = 0; k < gmx::ssize(previousTypename); k++) { - aid = mk_aid(atoms, restype, previousTypename[k], TRUE); + std::vector aid = mk_aid(atoms, restype, previousTypename[k], TRUE); /* Check for special types to do fancy stuff with */ @@ -643,7 +609,7 @@ void analyse(const t_atoms* atoms, t_blocka* gb, char*** gn, gmx_bool bASK, gmx_ analyse_prot(restype, atoms, gb, gn, bASK, bVerb); /* Create a Non-Protein group */ - aid = mk_aid(atoms, restype, "Protein", FALSE); + std::vector aid = mk_aid(atoms, restype, "Protein", FALSE); if ((!aid.empty()) && (gmx::ssize(aid) < atoms->nr)) { add_grp(gb, gn, aid, "non-Protein"); @@ -657,7 +623,7 @@ void analyse(const t_atoms* atoms, t_blocka* gb, char*** gn, gmx_bool bASK, gmx_ /* Solvent, create a negated group too */ - aid = mk_aid(atoms, restype, "Water", FALSE); + std::vector aid = mk_aid(atoms, restype, "Water", FALSE); if ((!aid.empty()) && (gmx::ssize(aid) < atoms->nr)) { add_grp(gb, gn, aid, "non-Water"); @@ -673,12 +639,12 @@ void analyse(const t_atoms* atoms, t_blocka* gb, char*** gn, gmx_bool bASK, gmx_ /* Create a merged water_and_ions group */ - iwater = -1; - iion = -1; - nwater = 0; - nion = 0; + int iwater = -1; + int iion = -1; + int nwater = 0; + int nion = 0; - for (i = 0; i < gb->nr; i++) + for (int i = 0; i < gb->nr; i++) { if (!gmx_strcasecmp((*gn)[i], "Water")) { @@ -700,14 +666,14 @@ void analyse(const t_atoms* atoms, t_blocka* gb, char*** gn, gmx_bool bASK, gmx_ srenew(gb->a, gb->nra + nwater + nion); if (nwater > 0) { - for (i = gb->index[iwater]; i < gb->index[iwater + 1]; i++) + for (int i = gb->index[iwater]; i < gb->index[iwater + 1]; i++) { gb->a[gb->nra++] = gb->a[i]; } } if (nion > 0) { - for (i = gb->index[iion]; i < gb->index[iion + 1]; i++) + for (int i = gb->index[iion]; i < gb->index[iion + 1]; i++) { gb->a[gb->nra++] = gb->a[i]; } @@ -720,9 +686,7 @@ void analyse(const t_atoms* atoms, t_blocka* gb, char*** gn, gmx_bool bASK, gmx_ void check_index(const char* gname, int n, int index[], const char* traj, int natoms) { - int i; - - for (i = 0; i < n; i++) + for (int i = 0; i < n; i++) { if (index[i] >= natoms) { @@ -747,20 +711,17 @@ void check_index(const char* gname, int n, int index[], const char* traj, int na t_blocka* init_index(const char* gfile, char*** grpname) { - FILE* in; - t_blocka* b; - int maxentries; - int i, j; - char line[STRLEN], *pt, str[STRLEN]; + t_blocka* b = nullptr; + char line[STRLEN], str[STRLEN]; - in = gmx_ffopen(gfile, "r"); + FILE* in = gmx_ffopen(gfile, "r"); snew(b, 1); - b->nr = 0; - b->index = nullptr; - b->nra = 0; - b->a = nullptr; - *grpname = nullptr; - maxentries = 0; + b->nr = 0; + b->index = nullptr; + b->nra = 0; + b->a = nullptr; + *grpname = nullptr; + int maxentries = 0; while (get_a_line(in, line, STRLEN)) { if (get_header(line, str)) @@ -781,10 +742,10 @@ t_blocka* init_index(const char* gfile, char*** grpname) { gmx_fatal(FARGS, "The first header of your indexfile is invalid"); } - pt = line; + char* pt = line; while (sscanf(pt, "%s", str) == 1) { - i = b->index[b->nr]; + int i = b->index[b->nr]; if (i >= maxentries) { maxentries += 1024; @@ -800,10 +761,10 @@ t_blocka* init_index(const char* gfile, char*** grpname) } gmx_ffclose(in); - for (i = 0; (i < b->nr); i++) + for (int i = 0; (i < b->nr); i++) { assert(b->a != nullptr); // for clang analyzer - for (j = b->index[i]; (j < b->index[i + 1]); j++) + for (int j = b->index[i]; (j < b->index[i + 1]); j++) { if (b->a[j] < 0) { @@ -817,9 +778,7 @@ t_blocka* init_index(const char* gfile, char*** grpname) static void minstring(char* str) { - int i; - - for (i = 0; (i < static_cast(strlen(str))); i++) + for (int i = 0; (i < static_cast(strlen(str))); i++) { if (str[i] == '-') { @@ -830,21 +789,19 @@ static void minstring(char* str) int find_group(const char* s, int ngrps, char** grpname) { - int aa, i, n; - char string[STRLEN]; - gmx_bool bMultiple; - bMultiple = FALSE; - n = strlen(s); - aa = -1; + char string[STRLEN]; + bool bMultiple = false; + const int n = strlen(s); + int aa = -1; /* first look for whole name match */ { - for (i = 0; i < ngrps; i++) + for (int i = 0; i < ngrps; i++) { if (gmx_strcasecmp_min(s, grpname[i]) == 0) { if (aa != -1) { - bMultiple = TRUE; + bMultiple = true; } aa = i; } @@ -853,7 +810,7 @@ int find_group(const char* s, int ngrps, char** grpname) /* second look for first string match */ if (aa == -1) { - for (i = 0; i < ngrps; i++) + for (int i = 0; i < ngrps; i++) { if (gmx_strncasecmp_min(s, grpname[i], n) == 0) { @@ -873,7 +830,7 @@ int find_group(const char* s, int ngrps, char** grpname) key[STRLEN - 1] = '\0'; upstring(key); minstring(key); - for (i = 0; i < ngrps; i++) + for (int i = 0; i < ngrps; i++) { strncpy(string, grpname[i], STRLEN - 1); upstring(string); @@ -898,10 +855,10 @@ int find_group(const char* s, int ngrps, char** grpname) static int qgroup(int* a, int ngrps, char** grpname) { - char s[STRLEN]; - int aa; - gmx_bool bInRange; - char* end; + char s[STRLEN]; + int aa = 0; + bool bInRange = false; + char* end = nullptr; do { @@ -933,18 +890,17 @@ static int qgroup(int* a, int ngrps, char** grpname) static void rd_groups(t_blocka* grps, char** grpname, char* gnames[], int ngrps, int isize[], int* index[], int grpnr[]) { - int i, j, gnr1; - if (grps->nr == 0) { gmx_fatal(FARGS, "Error: no groups in indexfile"); } - for (i = 0; (i < grps->nr); i++) + for (int i = 0; (i < grps->nr); i++) { fprintf(stderr, "Group %5d (%15s) has %5d elements\n", i, grpname[i], grps->index[i + 1] - grps->index[i]); } - for (i = 0; (i < ngrps); i++) + for (int i = 0; (i < ngrps); i++) { + int gnr1 = 0; if (grps->nr > 1) { do @@ -964,7 +920,7 @@ rd_groups(t_blocka* grps, char** grpname, char* gnames[], int ngrps, int isize[] gnames[i] = gmx_strdup(grpname[gnr1]); isize[i] = grps->index[gnr1 + 1] - grps->index[gnr1]; snew(index[i], isize[i]); - for (j = 0; (j < isize[i]); j++) + for (int j = 0; (j < isize[i]); j++) { index[i][j] = grps->a[grps->index[gnr1] + j]; } @@ -973,16 +929,15 @@ rd_groups(t_blocka* grps, char** grpname, char* gnames[], int ngrps, int isize[] void rd_index(const char* statfile, int ngrps, int isize[], int* index[], char* grpnames[]) { - char** gnames; - t_blocka* grps; - int* grpnr; + char** gnames = nullptr; + int* grpnr = nullptr; snew(grpnr, ngrps); if (!statfile) { gmx_fatal(FARGS, "No index file specified"); } - grps = init_index(statfile, &gnames); + t_blocka* grps = init_index(statfile, &gnames); rd_groups(grps, gnames, grpnames, ngrps, isize, index, grpnr); for (int i = 0; i < grps->nr; i++) { @@ -996,9 +951,9 @@ void rd_index(const char* statfile, int ngrps, int isize[], int* index[], char* void get_index(const t_atoms* atoms, const char* fnm, int ngrps, int isize[], int* index[], char* grpnames[]) { - char*** gnames; - t_blocka* grps = nullptr; - int* grpnr; + char*** gnames = nullptr; + t_blocka* grps = nullptr; + int* grpnr = nullptr; snew(grpnr, ngrps); snew(gnames, 1); @@ -1031,13 +986,12 @@ void get_index(const t_atoms* atoms, const char* fnm, int ngrps, int isize[], in t_cluster_ndx* cluster_index(FILE* fplog, const char* ndx) { - t_cluster_ndx* c; - int i; + t_cluster_ndx* c = nullptr; snew(c, 1); c->clust = init_index(ndx, &c->grpname); c->maxframe = -1; - for (i = 0; (i < c->clust->nra); i++) + for (int i = 0; (i < c->clust->nra); i++) { c->maxframe = std::max(c->maxframe, c->clust->a[i]); } @@ -1049,7 +1003,7 @@ t_cluster_ndx* cluster_index(FILE* fplog, const char* ndx) if (debug) { pr_blocka(debug, 0, "clust", c->clust, TRUE); - for (i = 0; (i < c->clust->nra); i++) + for (int i = 0; (i < c->clust->nra); i++) { if ((c->clust->a[i] < 0) || (c->clust->a[i] > c->maxframe)) { diff --git a/src/gromacs/topology/invblock.cpp b/src/gromacs/topology/invblock.cpp index d50e75abea..ccd4c3f41a 100644 --- a/src/gromacs/topology/invblock.cpp +++ b/src/gromacs/topology/invblock.cpp @@ -3,7 +3,7 @@ * * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. - * Copyright (c) 2010,2014,2015,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2010,2014,2015,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -45,18 +45,17 @@ int* make_invblock(const t_block* block, int nr) { - int i, j; - int* invblock; + int* invblock = nullptr; snew(invblock, nr + 1); /* Mark unused numbers */ - for (i = 0; i <= nr; i++) + for (int i = 0; i <= nr; i++) { invblock[i] = -1; } - for (i = 0; (i < block->nr); i++) + for (int i = 0; (i < block->nr); i++) { - for (j = block->index[i]; (j < block->index[i + 1]); j++) + for (int j = block->index[i]; (j < block->index[i + 1]); j++) { if (invblock[j] == -1) { @@ -78,18 +77,17 @@ int* make_invblock(const t_block* block, int nr) int* make_invblocka(const t_blocka* block, int nr) { - int i, j; - int* invblock; + int* invblock = nullptr; snew(invblock, nr + 1); /* Mark unused numbers */ - for (i = 0; i <= nr; i++) + for (int i = 0; i <= nr; i++) { invblock[i] = -1; } - for (i = 0; (i < block->nr); i++) + for (int i = 0; (i < block->nr); i++) { - for (j = block->index[i]; (j < block->index[i + 1]); j++) + for (int j = block->index[i]; (j < block->index[i + 1]); j++) { if (invblock[block->a[j]] == -1) { diff --git a/src/gromacs/topology/mtop_lookup.h b/src/gromacs/topology/mtop_lookup.h index 8416952059..f0395eb75e 100644 --- a/src/gromacs/topology/mtop_lookup.h +++ b/src/gromacs/topology/mtop_lookup.h @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2016,2017,2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2016,2017,2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -88,7 +88,7 @@ static inline void mtopGetMolblockIndex(const gmx_mtop_t* mtop, int molBlock0 = -1; int molBlock1 = mtop->molblock.size(); - int globalAtomStart; + int globalAtomStart = 0; while (TRUE) { globalAtomStart = mtop->moleculeBlockIndices[*moleculeBlock].globalAtomStart; @@ -134,7 +134,7 @@ static inline void mtopGetMolblockIndex(const gmx_mtop_t* mtop, */ static inline int mtopGetMoleculeIndex(const gmx_mtop_t* mtop, int globalAtomIndex, int* moleculeBlock) { - int localMoleculeIndex; + int localMoleculeIndex = 0; mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock, &localMoleculeIndex, nullptr); return mtop->moleculeBlockIndices[*moleculeBlock].moleculeIndexStart + localMoleculeIndex; @@ -154,7 +154,7 @@ static inline int mtopGetMoleculeIndex(const gmx_mtop_t* mtop, int globalAtomInd */ static inline const t_atom& mtopGetAtomParameters(const gmx_mtop_t* mtop, int globalAtomIndex, int* moleculeBlock) { - int atomIndexInMolecule; + int atomIndexInMolecule = 0; mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock, nullptr, &atomIndexInMolecule); const gmx_moltype_t& moltype = mtop->moltype[mtop->molblock[*moleculeBlock].type]; return moltype.atoms.atom[atomIndexInMolecule]; @@ -206,8 +206,8 @@ static inline void mtopGetAtomAndResidueName(const gmx_mtop_t* mtop, const char** residueName, int* globalResidueIndex) { - int moleculeIndex; - int atomIndexInMolecule; + int moleculeIndex = 0; + int atomIndexInMolecule = 0; mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock, &moleculeIndex, &atomIndexInMolecule); const gmx_molblock_t& molb = mtop->molblock[*moleculeBlock]; @@ -268,7 +268,7 @@ static inline void mtopGetAtomAndResidueName(const gmx_mtop_t& mtop, */ static inline const t_resinfo& mtopGetResidueInfo(const gmx_mtop_t* mtop, int globalAtomIndex, int* moleculeBlock) { - int atomIndexInMolecule; + int atomIndexInMolecule = 0; mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock, nullptr, &atomIndexInMolecule); const gmx_moltype_t& moltype = mtop->moltype[mtop->molblock[*moleculeBlock].type]; const int resind = moltype.atoms.atom[atomIndexInMolecule].resind; @@ -289,7 +289,7 @@ static inline const t_resinfo& mtopGetResidueInfo(const gmx_mtop_t* mtop, int gl */ static inline const t_pdbinfo& mtopGetAtomPdbInfo(const gmx_mtop_t* mtop, int globalAtomIndex, int* moleculeBlock) { - int atomIndexInMolecule; + int atomIndexInMolecule = 0; mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock, nullptr, &atomIndexInMolecule); const gmx_moltype_t& moltype = mtop->moltype[mtop->molblock[*moleculeBlock].type]; GMX_ASSERT(moltype.atoms.havePdbInfo, "PDB information not present when requested"); diff --git a/src/gromacs/topology/mtop_util.cpp b/src/gromacs/topology/mtop_util.cpp index 0433952b68..b159337037 100644 --- a/src/gromacs/topology/mtop_util.cpp +++ b/src/gromacs/topology/mtop_util.cpp @@ -3,7 +3,7 @@ * * Copyright (c) 2008,2009,2010, The GROMACS development team. * Copyright (c) 2012,2013,2014,2015,2016 The GROMACS development team. - * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -68,15 +68,7 @@ void gmx_mtop_count_atomtypes(const gmx_mtop_t* mtop, int state, int typecount[] const t_atoms& atoms = mtop->moltype[molb.type].atoms; for (int i = 0; i < atoms.nr; ++i) { - int tpi; - if (state == 0) - { - tpi = atoms.atom[i].type; - } - else - { - tpi = atoms.atom[i].typeB; - } + const int tpi = (state == 0) ? atoms.atom[i].type : atoms.atom[i].typeB; typecount[tpi] += molb.nmol; } } @@ -202,7 +194,7 @@ typedef struct gmx_mtop_atomloop_block gmx_mtop_atomloop_block_t gmx_mtop_atomloop_block_init(const gmx_mtop_t* mtop) { - struct gmx_mtop_atomloop_block* aloop; + struct gmx_mtop_atomloop_block* aloop = nullptr; snew(aloop, 1); @@ -254,7 +246,7 @@ typedef struct gmx_mtop_ilistloop gmx_mtop_ilistloop_t gmx_mtop_ilistloop_init(const gmx_mtop_t* mtop) { - struct gmx_mtop_ilistloop* iloop; + struct gmx_mtop_ilistloop* iloop = nullptr; snew(iloop, 1); @@ -308,12 +300,10 @@ typedef struct gmx_mtop_ilistloop_all int gmx_mtop_ftype_count(const gmx_mtop_t* mtop, int ftype) { - gmx_mtop_ilistloop_t iloop; - int n, nmol; - - n = 0; + int nmol = 0; + int n = 0; - iloop = gmx_mtop_ilistloop_init(mtop); + gmx_mtop_ilistloop_t iloop = gmx_mtop_ilistloop_init(mtop); while (const InteractionLists* il = gmx_mtop_ilistloop_next(iloop, &nmol)) { n += nmol * (*il)[ftype].size() / (1 + NRAL(ftype)); @@ -337,7 +327,7 @@ int gmx_mtop_interaction_count(const gmx_mtop_t& mtop, const int unsigned if_fla int n = 0; gmx_mtop_ilistloop_t iloop = gmx_mtop_ilistloop_init(mtop); - int nmol; + int nmol = 0; while (const InteractionLists* il = gmx_mtop_ilistloop_next(iloop, &nmol)) { for (int ftype = 0; ftype < F_NRE; ftype++) @@ -381,7 +371,7 @@ std::array gmx_mtop_particletype_count(const gmx_mtop_t& mtop) static void atomcat(t_atoms* dest, const t_atoms* src, int copies, int maxres_renum, int* maxresnr) { - int i, j, l, size; + int i = 0, j = 0, l = 0, size = 0; int srcnr = src->nr; int destnr = dest->nr; @@ -513,19 +503,17 @@ t_atoms gmx_mtop_global_atoms(const gmx_mtop_t* mtop) static void ilistcat(int ftype, InteractionList* dest, const InteractionList& src, int copies, int dnum, int snum) { - int nral, c, i, a; - - nral = NRAL(ftype); + const int nral = NRAL(ftype); size_t destIndex = dest->iatoms.size(); dest->iatoms.resize(dest->iatoms.size() + copies * src.size()); - for (c = 0; c < copies; c++) + for (int c = 0; c < copies; c++) { - for (i = 0; i < src.size();) + for (int i = 0; i < src.size();) { dest->iatoms[destIndex++] = src.iatoms[i++]; - for (a = 0; a < nral; a++) + for (int a = 0; a < nral; a++) { dest->iatoms[destIndex++] = dnum + src.iatoms[i++]; } @@ -536,19 +524,17 @@ static void ilistcat(int ftype, InteractionList* dest, const InteractionList& sr static void ilistcat(int ftype, t_ilist* dest, const InteractionList& src, int copies, int dnum, int snum) { - int nral, c, i, a; - - nral = NRAL(ftype); + const int nral = NRAL(ftype); dest->nalloc = dest->nr + copies * src.size(); srenew(dest->iatoms, dest->nalloc); - for (c = 0; c < copies; c++) + for (int c = 0; c < copies; c++) { - for (i = 0; i < src.size();) + for (int i = 0; i < src.size();) { dest->iatoms[dest->nr++] = src.iatoms[i++]; - for (a = 0; a < nral; a++) + for (int a = 0; a < nral; a++) { dest->iatoms[dest->nr++] = dnum + src.iatoms[i++]; } @@ -580,18 +566,15 @@ static void resizeIParams(t_iparams** iparams, const int newSize) template static void set_posres_params(IdefType* idef, const gmx_molblock_t* molb, int i0, int a_offset) { - int i1, i, a_molb; - t_iparams* ip; - auto* il = &idef->il[F_POSRES]; - i1 = il->size() / 2; + int i1 = il->size() / 2; resizeIParams(&idef->iparams_posres, i1); - for (i = i0; i < i1; i++) + for (int i = i0; i < i1; i++) { - ip = &idef->iparams_posres[i]; + t_iparams* ip = &idef->iparams_posres[i]; /* Copy the force constants */ - *ip = getIparams(*idef, il->iatoms[i * 2]); - a_molb = il->iatoms[i * 2 + 1] - a_offset; + *ip = getIparams(*idef, il->iatoms[i * 2]); + int a_molb = il->iatoms[i * 2 + 1] - a_offset; if (molb->posres_xA.empty()) { gmx_incons("Position restraint coordinates are missing"); @@ -619,18 +602,15 @@ static void set_posres_params(IdefType* idef, const gmx_molblock_t* molb, int i0 template static void set_fbposres_params(IdefType* idef, const gmx_molblock_t* molb, int i0, int a_offset) { - int i1, i, a_molb; - t_iparams* ip; - auto* il = &idef->il[F_FBPOSRES]; - i1 = il->size() / 2; + int i1 = il->size() / 2; resizeIParams(&idef->iparams_fbposres, i1); - for (i = i0; i < i1; i++) + for (int i = i0; i < i1; i++) { - ip = &idef->iparams_fbposres[i]; + t_iparams* ip = &idef->iparams_fbposres[i]; /* Copy the force constants */ - *ip = getIparams(*idef, il->iatoms[i * 2]); - a_molb = il->iatoms[i * 2 + 1] - a_offset; + *ip = getIparams(*idef, il->iatoms[i * 2]); + int a_molb = il->iatoms[i * 2 + 1] - a_offset; if (molb->posres_xA.empty()) { gmx_incons("Position restraint coordinates are missing"); diff --git a/src/gromacs/topology/symtab.cpp b/src/gromacs/topology/symtab.cpp index c6e56a5ec1..84ae546e07 100644 --- a/src/gromacs/topology/symtab.cpp +++ b/src/gromacs/topology/symtab.cpp @@ -4,7 +4,7 @@ * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team. - * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -185,7 +185,7 @@ constexpr int c_maxBufSize = 5; */ static char* trim_string(const char* s, char* out, int maxlen) { - int len, i; + int len = 0, i = 0; if (strlen(s) > static_cast(maxlen - 1)) { @@ -214,11 +214,8 @@ static char* trim_string(const char* s, char* out, int maxlen) int lookup_symtab(t_symtab* symtab, char** name) { - int base; - t_symbuf* symbuf; - - base = 0; - symbuf = symtab->symbuf; + int base = 0; + t_symbuf* symbuf = symtab->symbuf; while (symbuf != nullptr) { const int index = name - symbuf->buf; @@ -237,9 +234,7 @@ int lookup_symtab(t_symtab* symtab, char** name) char** get_symtab_handle(t_symtab* symtab, int name) { - t_symbuf* symbuf; - - symbuf = symtab->symbuf; + t_symbuf* symbuf = symtab->symbuf; while (symbuf != nullptr) { if (name < symbuf->bufsize) @@ -258,7 +253,7 @@ char** get_symtab_handle(t_symtab* symtab, int name) //! Returns a new initialized entry into the symtab linked list. static t_symbuf* new_symbuf() { - t_symbuf* symbuf; + t_symbuf* symbuf = nullptr; snew(symbuf, 1); symbuf->bufsize = c_maxBufSize; @@ -277,19 +272,17 @@ static t_symbuf* new_symbuf() */ static char** enter_buf(t_symtab* symtab, char* name) { - int i; - t_symbuf* symbuf; - gmx_bool bCont; + bool bCont = false; if (symtab->symbuf == nullptr) { symtab->symbuf = new_symbuf(); } - symbuf = symtab->symbuf; + t_symbuf* symbuf = symtab->symbuf; do { - for (i = 0; (i < symbuf->bufsize); i++) + for (int i = 0; (i < symbuf->bufsize); i++) { if (symbuf->buf[i] == nullptr) { @@ -340,7 +333,7 @@ void close_symtab(t_symtab gmx_unused* symtab) {} // std::list>> for t_symtab. t_symtab* duplicateSymtab(const t_symtab* symtab) { - t_symtab* copySymtab; + t_symtab* copySymtab = nullptr; snew(copySymtab, 1); open_symtab(copySymtab); t_symbuf* symbuf = symtab->symbuf; @@ -373,21 +366,19 @@ t_symtab* duplicateSymtab(const t_symtab* symtab) void done_symtab(t_symtab* symtab) { - int i; - t_symbuf *symbuf, *freeptr; - close_symtab(symtab); - symbuf = symtab->symbuf; + t_symbuf* symbuf = symtab->symbuf; while (symbuf != nullptr) { - for (i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++) + int i = 0; + for (; (i < symbuf->bufsize) && (i < symtab->nr); i++) { sfree(symbuf->buf[i]); } symtab->nr -= i; sfree(symbuf->buf); - freeptr = symbuf; - symbuf = symbuf->next; + t_symbuf* freeptr = symbuf; + symbuf = symbuf->next; sfree(freeptr); } symtab->symbuf = nullptr; @@ -399,15 +390,13 @@ void done_symtab(t_symtab* symtab) void free_symtab(t_symtab* symtab) { - t_symbuf *symbuf, *freeptr; - close_symtab(symtab); - symbuf = symtab->symbuf; + t_symbuf* symbuf = symtab->symbuf; while (symbuf != nullptr) { symtab->nr -= std::min(symbuf->bufsize, symtab->nr); - freeptr = symbuf; - symbuf = symbuf->next; + t_symbuf* freeptr = symbuf; + symbuf = symbuf->next; sfree(freeptr); } symtab->symbuf = nullptr; @@ -419,18 +408,16 @@ void free_symtab(t_symtab* symtab) void pr_symtab(FILE* fp, int indent, const char* title, t_symtab* symtab) { - int i, j, nr; - t_symbuf* symbuf; - if (available(fp, symtab, indent, title)) { - indent = pr_title_n(fp, indent, title, symtab->nr); - i = 0; - nr = symtab->nr; - symbuf = symtab->symbuf; + indent = pr_title_n(fp, indent, title, symtab->nr); + int i = 0; + int nr = symtab->nr; + t_symbuf* symbuf = symtab->symbuf; while (symbuf != nullptr) { - for (j = 0; (j < symbuf->bufsize) && (j < nr); j++) + int j = 0; + for (; (j < symbuf->bufsize) && (j < nr); j++) { pr_indent(fp, indent); (void)fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, symbuf->buf[j]); diff --git a/src/gromacs/topology/tests/symtab.cpp b/src/gromacs/topology/tests/symtab.cpp index f9a0d67d0e..1b8bcddb8e 100644 --- a/src/gromacs/topology/tests/symtab.cpp +++ b/src/gromacs/topology/tests/symtab.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2019,2020, by the GROMACS development team, led by + * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -448,8 +448,8 @@ void LegacySymtabTest::dumpSymtab() int pos = 0; while (symbuf != nullptr) { - int i; - for (i = 0; (i < symbuf->bufsize) && (i < nr); i++) + int i = 0; + for (; (i < symbuf->bufsize) && (i < nr); i++) { symtabDump.emplace_back(formatString("Symtab[%d]=\"%s\"", pos++, symbuf->buf[i])); } @@ -515,7 +515,7 @@ TEST_F(LegacySymtabTest, EmptyOnOpen) TEST_F(LegacySymtabTest, AddSingleEntry) { - auto fooSymbol = put_symtab(symtab(), "Foo"); + auto* fooSymbol = put_symtab(symtab(), "Foo"); ASSERT_EQ(1, symtab()->nr); compareSymtabLookupAndHandle(symtab(), fooSymbol); EXPECT_STREQ("Foo", *fooSymbol); @@ -523,8 +523,8 @@ TEST_F(LegacySymtabTest, AddSingleEntry) TEST_F(LegacySymtabTest, AddTwoDistinctEntries) { - auto fooSymbol = put_symtab(symtab(), "Foo"); - auto barSymbol = put_symtab(symtab(), "Bar"); + auto* fooSymbol = put_symtab(symtab(), "Foo"); + auto* barSymbol = put_symtab(symtab(), "Bar"); ASSERT_EQ(2, symtab()->nr); compareSymtabLookupAndHandle(symtab(), fooSymbol); @@ -538,8 +538,8 @@ TEST_F(LegacySymtabTest, AddTwoDistinctEntries) TEST_F(LegacySymtabTest, TryToAddDuplicates) { - auto fooSymbol = put_symtab(symtab(), "Foo"); - auto barSymbol = put_symtab(symtab(), "Bar"); + auto* fooSymbol = put_symtab(symtab(), "Foo"); + auto* barSymbol = put_symtab(symtab(), "Bar"); ASSERT_EQ(2, symtab()->nr); compareSymtabLookupAndHandle(symtab(), fooSymbol); @@ -551,7 +551,7 @@ TEST_F(LegacySymtabTest, TryToAddDuplicates) EXPECT_STREQ("Bar", *barSymbol); // Insert a duplicate element - auto anotherFooSymbol = put_symtab(symtab(), "Foo"); + auto* anotherFooSymbol = put_symtab(symtab(), "Foo"); ASSERT_EQ(2, symtab()->nr); // Check for correct post-conditions @@ -582,7 +582,7 @@ TEST_F(LegacySymtabTest, AddLargeNumberOfEntries) compareSymtabLookupAndHandle(symtab(), symbolsAdded[i]); } // Add something unrelated and check that indices still work afterward. - auto foobarSymbol = put_symtab(symtab(), "foobar"); + auto* foobarSymbol = put_symtab(symtab(), "foobar"); ASSERT_EQ(numStringsToAdd + 1, symtab()->nr); for (int i = 0; i < numStringsToAdd; ++i) { @@ -608,7 +608,7 @@ TEST_F(LegacySymtabTest, NoDuplicatesInLargeTable) ASSERT_EQ(halfOfStringsToAdd, symtab()->nr); // We now try to mess around in the symtab. - auto bazSymbol = put_symtab(symtab(), "baz"); + auto* bazSymbol = put_symtab(symtab(), "baz"); ASSERT_EQ(halfOfStringsToAdd + 1, symtab()->nr); compareSymtabLookupAndHandle(symtab(), bazSymbol); diff --git a/src/gromacs/topology/topology.cpp b/src/gromacs/topology/topology.cpp index 807eb7de23..fc859513fd 100644 --- a/src/gromacs/topology/topology.cpp +++ b/src/gromacs/topology/topology.cpp @@ -361,14 +361,12 @@ static void pr_moltype(FILE* fp, gmx_bool bShowNumbers, gmx_bool bShowParameters) { - int j; - indent = pr_title_n(fp, indent, title, n); pr_indent(fp, indent); fprintf(fp, "name=\"%s\"\n", *(molt->name)); pr_atoms(fp, indent, "atoms", &(molt->atoms), bShowNumbers); pr_listoflists(fp, indent, "excls", &molt->excls, bShowNumbers); - for (j = 0; (j < F_NRE); j++) + for (int j = 0; (j < F_NRE); j++) { pr_ilist(fp, indent, @@ -465,11 +463,8 @@ static void cmp_iparm(FILE* fp, real relativeTolerance, real absoluteTolerance) { - int i; - gmx_bool bDiff; - - bDiff = FALSE; - for (i = 0; i < MAXFORCEPARAM && !bDiff; i++) + bool bDiff = false; + for (int i = 0; i < MAXFORCEPARAM && !bDiff; i++) { bDiff = !equal_real(ip1.generic.buf[i], ip2.generic.buf[i], relativeTolerance, absoluteTolerance); } @@ -484,13 +479,10 @@ static void cmp_iparm(FILE* fp, static void cmp_iparm_AB(FILE* fp, const char* s, t_functype ft, const t_iparams& ip1, real relativeTolerance, real absoluteTolerance) { - int nrfpA, nrfpB, p0, i; - gmx_bool bDiff; - /* Normally the first parameter is perturbable */ - p0 = 0; - nrfpA = interaction_function[ft].nrfpA; - nrfpB = interaction_function[ft].nrfpB; + int p0 = 0; + int nrfpA = interaction_function[ft].nrfpA; + int nrfpB = interaction_function[ft].nrfpB; if (ft == F_PDIHS) { nrfpB = 2; @@ -501,8 +493,8 @@ static void cmp_iparm_AB(FILE* fp, const char* s, t_functype ft, const t_iparams p0 = 1; nrfpB = 1; } - bDiff = FALSE; - for (i = 0; i < nrfpB && !bDiff; i++) + bool bDiff = false; + for (int i = 0; i < nrfpB && !bDiff; i++) { bDiff = !equal_real( ip1.generic.buf[p0 + i], ip1.generic.buf[nrfpA + i], relativeTolerance, absoluteTolerance); @@ -531,11 +523,9 @@ static void cmp_cmap(FILE* fp, const gmx_cmap_t* cmap1, const gmx_cmap_t* cmap2, { for (size_t g = 0; g < cmap1->cmapdata.size(); g++) { - int i; - fprintf(fp, "comparing cmap %zu\n", g); - for (i = 0; i < 4 * cmap1->grid_spacing * cmap1->grid_spacing; i++) + for (int i = 0; i < 4 * cmap1->grid_spacing * cmap1->grid_spacing; i++) { cmp_real(fp, "", i, cmap1->cmapdata[g].cmap[i], cmap2->cmapdata[g].cmap[i], relativeTolerance, absoluteTolerance); } diff --git a/src/gromacs/topology/topsort.cpp b/src/gromacs/topology/topsort.cpp index 81900500c8..8270ca0307 100644 --- a/src/gromacs/topology/topsort.cpp +++ b/src/gromacs/topology/topsort.cpp @@ -4,7 +4,7 @@ * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2008, The GROMACS development team. * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team. - * Copyright (c) 2019,2020, by the GROMACS development team, led by + * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -49,14 +49,12 @@ static gmx_bool ip_pert(int ftype, const t_iparams* ip) { - gmx_bool bPert; - int i; - if (NRFPB(ftype) == 0) { return FALSE; } + bool bPert = false; switch (ftype) { case F_BONDS: @@ -88,7 +86,7 @@ static gmx_bool ip_pert(int ftype, const t_iparams* ip) break; case F_RBDIHS: bPert = FALSE; - for (i = 0; i < NR_RBDIHS; i++) + for (int i = 0; i < NR_RBDIHS; i++) { if (ip->rbdihs.rbcA[i] != ip->rbdihs.rbcB[i]) { @@ -102,7 +100,7 @@ static gmx_bool ip_pert(int ftype, const t_iparams* ip) case F_TABDIHS: bPert = (ip->tab.kA != ip->tab.kB); break; case F_POSRES: bPert = FALSE; - for (i = 0; i < DIM; i++) + for (int i = 0; i < DIM; i++) { if (ip->posres.pos0A[i] != ip->posres.pos0B[i] || ip->posres.fcA[i] != ip->posres.fcB[i]) { @@ -180,10 +178,6 @@ gmx_bool gmx_mtop_bondeds_free_energy(const gmx_mtop_t* mtop) void gmx_sort_ilist_fe(InteractionDefinitions* idef, const real* qA, const real* qB) { - int ftype, nral, i, ic, ib, a; - t_iatom* iabuf; - int iabuf_nalloc; - if (qB == nullptr) { qB = qA; @@ -191,19 +185,19 @@ void gmx_sort_ilist_fe(InteractionDefinitions* idef, const real* qA, const real* bool havePerturbedInteractions = false; - iabuf_nalloc = 0; - iabuf = nullptr; + int iabuf_nalloc = 0; + t_iatom* iabuf = nullptr; - for (ftype = 0; ftype < F_NRE; ftype++) + for (int ftype = 0; ftype < F_NRE; ftype++) { if (interaction_function[ftype].flags & IF_BOND) { InteractionList* ilist = &idef->il[ftype]; int* iatoms = ilist->iatoms.data(); - nral = NRAL(ftype); - ic = 0; - ib = 0; - i = 0; + const int nral = NRAL(ftype); + int ic = 0; + int ib = 0; + int i = 0; while (i < ilist->size()) { /* Check if this interaction is perturbed */ @@ -215,7 +209,7 @@ void gmx_sort_ilist_fe(InteractionDefinitions* idef, const real* qA, const real* iabuf_nalloc = over_alloc_large(ib + 1 + nral); srenew(iabuf, iabuf_nalloc); } - for (a = 0; a < 1 + nral; a++) + for (int a = 0; a < 1 + nral; a++) { iabuf[ib++] = iatoms[i++]; } @@ -225,7 +219,7 @@ void gmx_sort_ilist_fe(InteractionDefinitions* idef, const real* qA, const real* else { /* Copy in place */ - for (a = 0; a < 1 + nral; a++) + for (int a = 0; a < 1 + nral; a++) { iatoms[ic++] = iatoms[i++]; } @@ -235,7 +229,7 @@ void gmx_sort_ilist_fe(InteractionDefinitions* idef, const real* qA, const real* idef->numNonperturbedInteractions[ftype] = ic; /* Copy the buffer with perturbed interactions to the ilist */ - for (a = 0; a < ib; a++) + for (int a = 0; a < ib; a++) { iatoms[ic++] = iabuf[a]; } -- 2.22.0