From: Roland Schulz Date: Wed, 19 Sep 2018 19:57:15 +0000 (-0700) Subject: Fix clang-tidy warnings for clang 7 X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=efaaccd4e92949920a4661c6bfb5250105eb88be;p=alexxy%2Fgromacs.git Fix clang-tidy warnings for clang 7 Disable clang-analyzer because we run analyzer as seperate job. Disable cppcoreguidelines-avoid-goto because it is easy to spot in code-review and has very rare good uses. Change-Id: Ibfeb8ba6b14012b0079e7eef15282e0e2b6538fc --- diff --git a/admin/builds/pre-submit-matrix.txt b/admin/builds/pre-submit-matrix.txt index 82c377d47a..b0127e8a7f 100644 --- a/admin/builds/pre-submit-matrix.txt +++ b/admin/builds/pre-submit-matrix.txt @@ -62,7 +62,7 @@ clang-7 no-openmp asan cmake-3.11.4 # Test newest cmake at time of release # Test with clang-tidy (without OpenMP, cmake>=3.6) -clang-6 no-openmp cmake-3.12.1 tidy +clang-7 no-openmp cmake-3.12.1 tidy # Test oldest supported MSVC on Windows # Test newest supported MSVC on Windows diff --git a/docs/dev-manual/tools.rst b/docs/dev-manual/tools.rst index 979c381c90..401a094109 100644 --- a/docs/dev-manual/tools.rst +++ b/docs/dev-manual/tools.rst @@ -82,10 +82,10 @@ unit testing (CTest) regression tests clang-tidy - `clang-tidy `_ + `clang-tidy `_ is used for static code analysis. clang-tidy is easy to install. It is contained in the llvm binary `package `_. Only - version 6.0.* with libstdc++<7 or libc++ is supported. Others might miss tests or give false positives. + version 7.0.* with libstdc++<7 or libc++ is supported. Others might miss tests or give false positives. It is run automatically on Jenkins for each commit. Many checks have fixes which can automatically be applied. To run it, the build has to be configured with ``cmake -DGMX_CLANG_TIDY=ON -DGMX_OPENMP=no -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=on``. diff --git a/src/.clang-tidy b/src/.clang-tidy index 21b88ea026..20be0b77f7 100644 --- a/src/.clang-tidy +++ b/src/.clang-tidy @@ -1,4 +1,4 @@ -Checks: clang-diagnostic-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.strcpy, +Checks: clang-diagnostic-*,-clang-analyzer-*,-clang-analyzer-security.insecureAPI.strcpy, bugprone-*,misc-*,readability-*,performance-*,mpi-*, -readability-inconsistent-declaration-parameter-name, -readability-function-size,-readability-else-after-return, @@ -9,6 +9,7 @@ Checks: clang-diagnostic-*,clang-analyzer-*,-clang-analyzer-security.insecureAP modernize-redundant-void-arg,modernize-use-bool-literals, cppcoreguidelines-*,-cppcoreguidelines-pro-*,-cppcoreguidelines-owning-memory, -cppcoreguidelines-no-malloc,-cppcoreguidelines-special-member-functions, + -cppcoreguidelines-avoid-goto, google-*,-google-build-using-namespace,-google-explicit-constructor, -google-readability-function-size,-google-readability-todo,-google-runtime-int HeaderFilterRegex: .* diff --git a/src/.clang-tidy.new.code b/src/.clang-tidy.new.code index 5558050fd1..e495372d9b 100644 --- a/src/.clang-tidy.new.code +++ b/src/.clang-tidy.new.code @@ -1,4 +1,4 @@ -Checks: clang-diagnostic-*,clang-analyzer-*, +Checks: clang-diagnostic-*,-clang-analyzer-*, bugprone-*,misc-*,readability-*,performance-*,mpi-*,-misc-incorrect-roundings, -readability-else-after-return, modernize-use-nullptr,modernize-use-emplace, @@ -12,6 +12,7 @@ Checks: clang-diagnostic-*,clang-analyzer-*, -cppcoreguidelines-pro-bounds-constant-array-index, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, -cppcoreguidelines-pro-bounds-pointer-arithmetic, + -cppcoreguidelines-avoid-goto, google-*,-google-build-using-namespace,-google-explicit-constructor, -google-readability-todo, -google-runtime-int diff --git a/src/gromacs/analysisdata/modules/plot.cpp b/src/gromacs/analysisdata/modules/plot.cpp index 03659c2b14..25eccc9bd4 100644 --- a/src/gromacs/analysisdata/modules/plot.cpp +++ b/src/gromacs/analysisdata/modules/plot.cpp @@ -336,7 +336,7 @@ AbstractPlotModule::dataStarted(AbstractAnalysisData * /* data */) else { time_unit_t time_unit - = static_cast(impl_->settings_.timeUnit() + 1); // NOLINT(misc-misplaced-widening-cast) + = static_cast(impl_->settings_.timeUnit() + 1); // NOLINT(bugprone-misplaced-widening-cast) xvg_format_t xvg_format = (impl_->settings_.plotFormat() > 0 ? static_cast(impl_->settings_.plotFormat()) diff --git a/src/gromacs/awh/grid.cpp b/src/gromacs/awh/grid.cpp index 7b6492791f..737b31f4f2 100644 --- a/src/gromacs/awh/grid.cpp +++ b/src/gromacs/awh/grid.cpp @@ -209,12 +209,10 @@ void linearArrayIndexToMultiDim(int indexLinear, int numDimensions, const awh_iv int stride = 1; /* Workaround for bug in clang */ -#ifndef __clang_analyzer__ for (int k = d + 1; k < numDimensions; k++) { stride *= numPointsDim[k]; } -#endif indexMulti[d] = indexLinear/stride; indexLinear -= indexMulti[d]*stride; diff --git a/src/gromacs/awh/tests/bias.cpp b/src/gromacs/awh/tests/bias.cpp index 033ad06d22..84fa8bf22d 100644 --- a/src/gromacs/awh/tests/bias.cpp +++ b/src/gromacs/awh/tests/bias.cpp @@ -266,9 +266,9 @@ TEST_P(BiasTest, ForcesBiasPmf) * In taking this deviation we lose a lot of precision, so we should * compare against k*max(coord) instead of the instantaneous force. */ - const double kCoordMax = bias.dimParams()[0].k*coordMaxValue; + const double kCoordMax = bias.dimParams()[0].k*coordMaxValue; - const double ulpTol = 10; + constexpr int ulpTol = 10; checker.checkSequence(props.begin(), props.end(), "Properties"); checker.setDefaultTolerance(absoluteTolerance(kCoordMax*GMX_DOUBLE_EPS*ulpTol)); diff --git a/src/gromacs/commandline/pargs.cpp b/src/gromacs/commandline/pargs.cpp index 87a602ca65..0804bbb7ba 100644 --- a/src/gromacs/commandline/pargs.cpp +++ b/src/gromacs/commandline/pargs.cpp @@ -566,7 +566,7 @@ gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags, /* set program name, command line, and default values for output options */ output_env_init(oenv, gmx::getProgramContext(), - static_cast(timeUnit + 1), bView, // NOLINT(misc-misplaced-widening-cast) + static_cast(timeUnit + 1), bView, // NOLINT(bugprone-misplaced-widening-cast) static_cast(xvgFormat + 1), 0); /* Extract Time info from arguments */ diff --git a/src/gromacs/domdec/domdec_topology.cpp b/src/gromacs/domdec/domdec_topology.cpp index b86d93d23e..04966c882f 100644 --- a/src/gromacs/domdec/domdec_topology.cpp +++ b/src/gromacs/domdec/domdec_topology.cpp @@ -699,7 +699,7 @@ static gmx_reverse_top_t make_reverse_top(const gmx_mtop_t *mtop, gmx_bool bFE, atoms_global.nr = mtop->natoms; atoms_global.atom = nullptr; /* Only used with virtual sites */ - GMX_RELEASE_ASSERT(mtop->intermolecular_ilist.get(), "We should have an ilist when intermolecular interactions are on"); + GMX_RELEASE_ASSERT(mtop->intermolecular_ilist, "We should have an ilist when intermolecular interactions are on"); *nint += make_reverse_ilist(*mtop->intermolecular_ilist, @@ -2281,7 +2281,7 @@ t_blocka *make_charge_group_links(const gmx_mtop_t *mtop, gmx_domdec_t *dd, atoms.nr = mtop->natoms; atoms.atom = nullptr; - GMX_RELEASE_ASSERT(mtop->intermolecular_ilist.get(), "We should have an ilist when intermolecular interactions are on"); + GMX_RELEASE_ASSERT(mtop->intermolecular_ilist, "We should have an ilist when intermolecular interactions are on"); make_reverse_ilist(*mtop->intermolecular_ilist, &atoms, @@ -2702,7 +2702,7 @@ void dd_bonded_cg_distance(const gmx::MDLogger &mdlog, gmx_fatal(FARGS, "The combination of intermolecular interactions, charge groups and domain decomposition is not supported. Use cutoff-scheme=Verlet (which removes the charge groups) or run without domain decomposition."); } - GMX_RELEASE_ASSERT(mtop->intermolecular_ilist.get(), "We should have an ilist when intermolecular interactions are on"); + GMX_RELEASE_ASSERT(mtop->intermolecular_ilist, "We should have an ilist when intermolecular interactions are on"); bonded_distance_intermol(*mtop->intermolecular_ilist, bBCheck, diff --git a/src/gromacs/fileio/matio.cpp b/src/gromacs/fileio/matio.cpp index 25876f8081..129ae67d63 100644 --- a/src/gromacs/fileio/matio.cpp +++ b/src/gromacs/fileio/matio.cpp @@ -47,6 +47,7 @@ #include #include "gromacs/fileio/gmxfio.h" +#include "gromacs/math/functions.h" #include "gromacs/math/utilities.h" #include "gromacs/utility/binaryinformation.h" #include "gromacs/utility/cstringutil.h" @@ -879,7 +880,7 @@ static void write_xpm_data(FILE *out, int n_x, int n_y, real **mat, fprintf(out, "\""); for (i = 0; (i < n_x); i++) { - c = std::round((mat[i][j]-lo)*invlevel); + c = gmx::roundToInt((mat[i][j]-lo)*invlevel); if (c < 0) { c = 0; @@ -929,11 +930,11 @@ static void write_xpm_data3(FILE *out, int n_x, int n_y, real **mat, { if (mat[i][j] >= mid) { - c = nmid+std::round((mat[i][j]-mid)*invlev_hi); + c = nmid+gmx::roundToInt((mat[i][j]-mid)*invlev_hi); } else if (mat[i][j] >= lo) { - c = std::round((mat[i][j]-lo)*invlev_lo); + c = gmx::roundToInt((mat[i][j]-lo)*invlev_lo); } else { @@ -989,7 +990,7 @@ static void write_xpm_data_split(FILE *out, int n_x, int n_y, real **mat, { if (i < j) { - c = nlevel_bot+std::round((mat[i][j]-lo_top)*invlev_top); + c = nlevel_bot+gmx::roundToInt((mat[i][j]-lo_top)*invlev_top); if ((c < nlevel_bot) || (c >= nlevel_bot+nlevel_top)) { gmx_fatal(FARGS, "Range checking i = %d, j = %d, c = %d, bot = %d, top = %d matrix[i,j] = %f", i, j, c, nlevel_bot, nlevel_top, mat[i][j]); @@ -997,7 +998,7 @@ static void write_xpm_data_split(FILE *out, int n_x, int n_y, real **mat, } else if (i > j) { - c = std::round((mat[i][j]-lo_bot)*invlev_bot); + c = gmx::roundToInt((mat[i][j]-lo_bot)*invlev_bot); if ((c < 0) || (c >= nlevel_bot+nlevel_bot)) { gmx_fatal(FARGS, "Range checking i = %d, j = %d, c = %d, bot = %d, top = %d matrix[i,j] = %f", i, j, c, nlevel_bot, nlevel_top, mat[i][j]); diff --git a/src/gromacs/fileio/pdbio.cpp b/src/gromacs/fileio/pdbio.cpp index aa070856c6..48dc48d246 100644 --- a/src/gromacs/fileio/pdbio.cpp +++ b/src/gromacs/fileio/pdbio.cpp @@ -588,7 +588,7 @@ void get_pdb_atomnumber(const t_atoms *atoms, gmx_atomprop_t aps) anm_copy[2] = nc; if (gmx_atomprop_query(aps, epropElement, "???", anm_copy, &eval)) { - atomnumber = std::round(eval); + atomnumber = gmx::roundToInt(eval); atomNumberSet = true; } else @@ -596,7 +596,7 @@ void get_pdb_atomnumber(const t_atoms *atoms, gmx_atomprop_t aps) anm_copy[1] = nc; if (gmx_atomprop_query(aps, epropElement, "???", anm_copy, &eval)) { - atomnumber = std::round(eval); + atomnumber = gmx::roundToInt(eval); atomNumberSet = true; } } @@ -612,7 +612,7 @@ void get_pdb_atomnumber(const t_atoms *atoms, gmx_atomprop_t aps) anm_copy[1] = nc; if (gmx_atomprop_query(aps, epropElement, "???", anm_copy, &eval)) { - atomnumber = std::round(eval); + atomnumber = gmx::roundToInt(eval); atomNumberSet = true; } } diff --git a/src/gromacs/gmxana/gmx_dipoles.cpp b/src/gromacs/gmxana/gmx_dipoles.cpp index 5fe85b58a2..095aa2da37 100644 --- a/src/gromacs/gmxana/gmx_dipoles.cpp +++ b/src/gromacs/gmxana/gmx_dipoles.cpp @@ -137,7 +137,7 @@ static void done_gkrbin(t_gkrbin **gb) static void add2gkr(t_gkrbin *gb, real r, real cosa, real phi) { - int cy, index = std::round(r/gb->spacing); + int cy, index = gmx::roundToInt(r/gb->spacing); real alpha; if (index < gb->nelem) diff --git a/src/gromacs/gmxana/gmx_genion.cpp b/src/gromacs/gmxana/gmx_genion.cpp index 871d020255..1190caa175 100644 --- a/src/gromacs/gmxana/gmx_genion.cpp +++ b/src/gromacs/gmxana/gmx_genion.cpp @@ -430,14 +430,14 @@ int gmx_genion(int argc, char *argv[]) { qtot += atoms.atom[i].q; } - iqtot = std::round(qtot); + iqtot = gmx::roundToInt(qtot); if (conc > 0) { /* Compute number of ions to be added */ vol = det(box); - nsalt = std::round(conc*vol*AVOGADRO/1e24); + nsalt = gmx::roundToInt(conc*vol*AVOGADRO/1e24); p_num = abs(nsalt*n_q); n_num = abs(nsalt*p_q); } diff --git a/src/gromacs/gmxana/gmx_msd.cpp b/src/gromacs/gmxana/gmx_msd.cpp index 8c54f617e6..ec7feb678f 100644 --- a/src/gromacs/gmxana/gmx_msd.cpp +++ b/src/gromacs/gmxana/gmx_msd.cpp @@ -884,7 +884,7 @@ static void do_corr(const char *trx_file, const char *ndx_file, const char *msd_ char **grpname; int i, i0, i1, j, N, nat_trx; real *DD, *SigmaD, a, a2, b, r, chi2; - rvec *x; + rvec *x = nullptr; matrix box; int *gnx_com = nullptr; /* the COM removal group size */ int **index_com = nullptr; /* the COM removal group atom indices */ diff --git a/src/gromacs/gmxana/gmx_trjcat.cpp b/src/gromacs/gmxana/gmx_trjcat.cpp index 9d5ed554e2..144892a765 100644 --- a/src/gromacs/gmxana/gmx_trjcat.cpp +++ b/src/gromacs/gmxana/gmx_trjcat.cpp @@ -370,7 +370,7 @@ static void do_demux(gmx::ArrayRef inFiles, } for (gmx::index i = 0; i < inFiles.size(); i++) { - int j = std::round(value[i][k]); + int j = gmx::roundToInt(value[i][k]); range_check(j, 0, inFiles.size()); if (bSet[j]) { diff --git a/src/gromacs/gmxana/pp2shift.cpp b/src/gromacs/gmxana/pp2shift.cpp index da2589d653..b05ff8c2d1 100644 --- a/src/gromacs/gmxana/pp2shift.cpp +++ b/src/gromacs/gmxana/pp2shift.cpp @@ -160,7 +160,7 @@ static t_shiftdata *read_shifts(const char *fn) { gmx_fatal(FARGS, "Error reading from file %s", fn); } - + GMX_ASSERT(nx > 0, ""); sd->nx = nx; sd->ny = ny; sd->dx = nx/(2*M_PI); diff --git a/src/gromacs/gmxlib/nonbonded/.clang-tidy b/src/gromacs/gmxlib/nonbonded/.clang-tidy index de0482b721..2a87d8112c 100644 --- a/src/gromacs/gmxlib/nonbonded/.clang-tidy +++ b/src/gromacs/gmxlib/nonbonded/.clang-tidy @@ -1 +1 @@ -Checks: -* +Checks: -modernize-* diff --git a/src/gromacs/gmxpreprocess/pdb2top.cpp b/src/gromacs/gmxpreprocess/pdb2top.cpp index b44f0666f0..8ed41d9f45 100644 --- a/src/gromacs/gmxpreprocess/pdb2top.cpp +++ b/src/gromacs/gmxpreprocess/pdb2top.cpp @@ -125,7 +125,7 @@ bool is_int(double x) { x = -x; } - ix = std::round(x); + ix = gmx::roundToInt(x); return (fabs(x-ix) < tol); } diff --git a/src/gromacs/hardware/identifyavx512fmaunits.cpp b/src/gromacs/hardware/identifyavx512fmaunits.cpp index 8eabb5784f..a5a3b6dc75 100644 --- a/src/gromacs/hardware/identifyavx512fmaunits.cpp +++ b/src/gromacs/hardware/identifyavx512fmaunits.cpp @@ -227,7 +227,7 @@ timeFmaOnlyLoop(uint64_t loopCount) bool checkDualAvx512FmaUnits() { - uint64_t timeFmaAndShuf = 1e9; // Large value + uint64_t timeFmaAndShuf = static_cast(1e9); // Large value // Make sure the CPU is in AVX512 mode by executing a fairly long loop. // Use the return value to make sure it is not optimized away. Later invocations diff --git a/src/gromacs/listed-forces/pairs.cpp b/src/gromacs/listed-forces/pairs.cpp index c48d05afe7..8de6b30d4a 100644 --- a/src/gromacs/listed-forces/pairs.cpp +++ b/src/gromacs/listed-forces/pairs.cpp @@ -108,7 +108,7 @@ evaluate_single(real r2, real tabscale, const real *vftab, real tableStride, ntab = static_cast(rtab); eps = rtab-ntab; eps2 = eps*eps; - ntab = tableStride*ntab; + ntab = static_cast(tableStride*ntab); /* Electrostatics */ Y = vftab[ntab]; F = vftab[ntab+1]; @@ -264,7 +264,7 @@ free_energy_evaluate_single(real r2, real sc_r_power, real alpha_coul, ntab = static_cast(rtab); eps = rtab-ntab; eps2 = eps*eps; - ntab = tableStride*ntab; + ntab = static_cast(tableStride*ntab); /* Electrostatics */ Y = vftab[ntab]; F = vftab[ntab+1]; diff --git a/src/gromacs/listed-forces/position-restraints.cpp b/src/gromacs/listed-forces/position-restraints.cpp index 280bcd4aeb..0fa2a2fed8 100644 --- a/src/gromacs/listed-forces/position-restraints.cpp +++ b/src/gromacs/listed-forces/position-restraints.cpp @@ -199,7 +199,7 @@ real fbposres(int nbonds, gmx_bool bInvert; npbcdim = ePBC2npbcdim(ePBC); - + GMX_ASSERT((ePBC == epbcNONE) == (npbcdim == 0), ""); if (refcoord_scaling == erscCOM) { clear_rvec(com_sc); @@ -329,7 +329,7 @@ real posres(int nbonds, rvec comA_sc, comB_sc, rdist, dpdl, dx; npbcdim = ePBC2npbcdim(ePBC); - + GMX_ASSERT((ePBC == epbcNONE) == (npbcdim == 0), ""); if (refcoord_scaling == erscCOM) { clear_rvec(comA_sc); diff --git a/src/gromacs/math/functions.h b/src/gromacs/math/functions.h index 18eac01194..ddb3f6541f 100644 --- a/src/gromacs/math/functions.h +++ b/src/gromacs/math/functions.h @@ -474,22 +474,22 @@ constexpr int64_t exactDiv(int64_t a, int64_t b) */ static inline int roundToInt(float x) { - return rintf(x); + return static_cast(rintf(x)); } //! Round double to int static inline int roundToInt(double x) { - return rint(x); + return static_cast(rint(x)); } //! Round float to int64_t static inline int64_t roundToInt64(float x) { - return rintf(x); + return static_cast(rintf(x)); } //! Round double to int64_t static inline int64_t roundToInt64(double x) { - return rint(x); + return static_cast(rint(x)); } } // namespace gmx diff --git a/src/gromacs/mdlib/coupling.cpp b/src/gromacs/mdlib/coupling.cpp index 190053851e..c076938743 100644 --- a/src/gromacs/mdlib/coupling.cpp +++ b/src/gromacs/mdlib/coupling.cpp @@ -1265,7 +1265,7 @@ static real energyNoseHoover(const t_inputrec *ir, const t_state *state, const t const double *ivxi = &state->nosehoover_vxi[i*nh]; const double *iQinv = &(MassQ->Qinv[i*nh]); - int nd = ir->opts.nrdf[i]; + int nd = static_cast(ir->opts.nrdf[i]); real reft = std::max(ir->opts.ref_t[i], 0); real kT = BOLTZ * reft; @@ -1287,7 +1287,7 @@ static real energyNoseHoover(const t_inputrec *ir, const t_state *state, const t } else { - ndj = 1.0; + ndj = 1; } energy += ndj*ixi[j]*kT; } diff --git a/src/gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_gpu_ref.cpp b/src/gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_gpu_ref.cpp index eb170467be..e7f805b370 100644 --- a/src/gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_gpu_ref.cpp +++ b/src/gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_gpu_ref.cpp @@ -282,7 +282,7 @@ nbnxn_kernel_gpu_ref(const nbnxn_pairlist_t *nbl, { r = rsq*rinv; rt = r*iconst->tabq_scale; - n0 = rt; + n0 = static_cast(rt); eps = rt - n0; fexcl = (1 - eps)*Ftab[n0] + eps*Ftab[n0+1]; diff --git a/src/gromacs/pulling/pullutil.cpp b/src/gromacs/pulling/pullutil.cpp index faf588b424..5395ca0595 100644 --- a/src/gromacs/pulling/pullutil.cpp +++ b/src/gromacs/pulling/pullutil.cpp @@ -706,7 +706,7 @@ void pull_calc_coms(const t_commrec *cr, } pullAllReduce(cr, comm, pull->group.size()*3*DIM, - static_cast(comm->comBuffer.data()[0])); + static_cast(comm->comBuffer[0])); for (size_t g = 0; g < pull->group.size(); g++) { diff --git a/src/gromacs/selection/params.cpp b/src/gromacs/selection/params.cpp index 52bbf6b81c..a2a313eab1 100644 --- a/src/gromacs/selection/params.cpp +++ b/src/gromacs/selection/params.cpp @@ -379,7 +379,7 @@ parse_values_range(const SelectionParserValueList &values, /* Store the values */ if (param->flags & SPAR_VARNUM) { - dataGuard.release(); + (void)dataGuard.release(); param->val.nr = n; if (param->val.type == INT_VALUE) { diff --git a/src/gromacs/selection/selelem.cpp b/src/gromacs/selection/selelem.cpp index 5c998412ee..ab0112b7fb 100644 --- a/src/gromacs/selection/selelem.cpp +++ b/src/gromacs/selection/selelem.cpp @@ -49,6 +49,7 @@ #include "gromacs/selection/position.h" #include "gromacs/selection/selectionenums.h" #include "gromacs/utility/exceptions.h" +#include "gromacs/utility/fatalerror.h" #include "gromacs/utility/gmxassert.h" #include "gromacs/utility/smalloc.h" #include "gromacs/utility/stringutil.h" @@ -265,7 +266,7 @@ void SelectionTreeElement::mempoolReserve(int count) break; default: - GMX_THROW(gmx::InternalError("Memory pooling not implemented for requested type")); + gmx_incons("Memory pooling not implemented for requested type"); } } @@ -291,7 +292,7 @@ void SelectionTreeElement::mempoolRelease() break; default: - GMX_THROW(gmx::InternalError("Memory pooling not implemented for requested type")); + gmx_incons("Memory pooling not implemented for requested type"); } } diff --git a/src/gromacs/simd/scalar/scalar.h b/src/gromacs/simd/scalar/scalar.h index c9a5c65220..df588dbb6f 100644 --- a/src/gromacs/simd/scalar/scalar.h +++ b/src/gromacs/simd/scalar/scalar.h @@ -442,7 +442,7 @@ blend(float a, float b, bool sel) static inline std::int32_t cvtR2I(float a) { - return std::round(a); + return static_cast(std::round(a)); }; /*! \brief Truncate single precision floating point to integer. @@ -457,7 +457,7 @@ cvtR2I(float a) static inline std::int32_t cvttR2I(float a) { - return std::trunc(a); + return static_cast(std::trunc(a)); }; /*! \brief Return integer. @@ -844,7 +844,7 @@ blend(double a, double b, bool sel) static inline std::int32_t cvtR2I(double a) { - return std::round(a); + return static_cast(std::round(a)); }; /*! \brief Truncate single precision doubleing point to integer. @@ -859,7 +859,7 @@ cvtR2I(double a) static inline std::int32_t cvttR2I(double a) { - return std::trunc(a); + return static_cast(std::trunc(a)); }; // We do not have a separate cvtI2R for double, since that would require diff --git a/src/gromacs/tables/cubicsplinetable.cpp b/src/gromacs/tables/cubicsplinetable.cpp index e49007e506..5609052df9 100644 --- a/src/gromacs/tables/cubicsplinetable.cpp +++ b/src/gromacs/tables/cubicsplinetable.cpp @@ -146,7 +146,7 @@ fillSingleCubicSplineTableData(const std::function &function, double spacing, std::vector *yfghTableData) { - int endIndex = range.second / spacing + 2; + int endIndex = static_cast(range.second / spacing + 2); yfghTableData->resize(4*endIndex); @@ -227,7 +227,7 @@ fillSingleCubicSplineTableData(ArrayRef function, double spacing, std::vector *yfghTableData) { - int endIndex = range.second / spacing + 2; + int endIndex = static_cast(range.second / spacing + 2); std::vector tmpFunction(endIndex); std::vector tmpDerivative(endIndex); @@ -241,7 +241,7 @@ fillSingleCubicSplineTableData(ArrayRef function, { double x = i * spacing; double xtab = x / inputSpacing; - int index = xtab; + int index = static_cast(xtab); double eps = xtab - index; if (range.first > 0 && i == 0) @@ -329,7 +329,7 @@ CubicSplineTable::CubicSplineTable(std::initializer_list &functi std::vector *functionTableData, std::vector *derivativeTableData) { - std::size_t endIndex = range.second / spacing + 2; + std::size_t endIndex = static_cast(range.second / spacing + 2); functionTableData->resize(endIndex); derivativeTableData->resize(endIndex); @@ -162,7 +162,7 @@ fillSingleQuadraticSplineTableData(ArrayRef functi std::vector *functionTableData, std::vector *derivativeTableData) { - std::size_t endIndex = range.second / spacing + 2; + std::size_t endIndex = static_cast(range.second / spacing + 2); functionTableData->resize(endIndex); derivativeTableData->resize(endIndex); @@ -190,7 +190,7 @@ fillSingleQuadraticSplineTableData(ArrayRef functi { // Step 1: Interpolate the function value at x from input table. double inputXTab = x / inputSpacing; - int inputIndex = inputXTab; + int inputIndex = static_cast(inputXTab); double inputEps = inputXTab - inputIndex; // Linear interpolation of input derivative and third derivative @@ -288,7 +288,7 @@ QuadraticSplineTable::QuadraticSplineTable(std::initializer_list func double inputSpacing, const std::pair &range) { - std::size_t firstIndex = range.first / inputSpacing; - std::size_t lastIndex = range.second / inputSpacing; + std::size_t firstIndex = static_cast(range.first / inputSpacing); + std::size_t lastIndex = static_cast(range.second / inputSpacing); bool isConsistent = true; std::size_t minFail = lastIndex; std::size_t maxFail = firstIndex; @@ -214,8 +214,8 @@ findSmallestQuotientOfFunctionAndSecondDerivative(ArrayRef const std::pair &range) { - std::size_t firstIndex = range.first / inputSpacing; - std::size_t lastIndex = range.second / inputSpacing; + std::size_t firstIndex = static_cast(range.first / inputSpacing); + std::size_t lastIndex = static_cast(range.second / inputSpacing); double minQuotient = GMX_REAL_MAX; for (std::size_t i = firstIndex + 1; (i + 1) < lastIndex; i++) @@ -292,8 +292,8 @@ findSmallestQuotientOfFunctionAndThirdDerivative(ArrayRef f const std::pair &range) { - std::size_t firstIndex = range.first / inputSpacing; - std::size_t lastIndex = range.second / inputSpacing; + std::size_t firstIndex = static_cast(range.first / inputSpacing); + std::size_t lastIndex = static_cast(range.second / inputSpacing); double minQuotient = GMX_REAL_MAX; for (std::size_t i = firstIndex + 2; (i + 2) < lastIndex; i++) diff --git a/src/gromacs/topology/atomprop.cpp b/src/gromacs/topology/atomprop.cpp index 5761699e8b..a2512322da 100644 --- a/src/gromacs/topology/atomprop.cpp +++ b/src/gromacs/topology/atomprop.cpp @@ -43,6 +43,7 @@ #include #include +#include "gromacs/math/functions.h" #include "gromacs/math/utilities.h" #include "gromacs/topology/residuetypes.h" #include "gromacs/utility/cstringutil.h" @@ -421,7 +422,7 @@ int gmx_atomprop_atomnumber(gmx_atomprop_t aps, const char *elem) { if (gmx_strcasecmp(ap->prop[epropElement].atomnm[i], elem) == 0) { - return std::round(ap->prop[epropElement].value[i]); + return gmx::roundToInt(ap->prop[epropElement].value[i]); } } return -1; diff --git a/src/gromacs/trajectoryanalysis/runnercommon.cpp b/src/gromacs/trajectoryanalysis/runnercommon.cpp index f7323a8d87..3341d04096 100644 --- a/src/gromacs/trajectoryanalysis/runnercommon.cpp +++ b/src/gromacs/trajectoryanalysis/runnercommon.cpp @@ -203,7 +203,7 @@ TrajectoryAnalysisRunnerCommon::Impl::initFirstFrame() return; } time_unit_t time_unit - = static_cast(settings_.timeUnit() + 1); // NOLINT(misc-misplaced-widening-cast) + = static_cast(settings_.timeUnit() + 1); // NOLINT(bugprone-misplaced-widening-cast) output_env_init(&oenv_, getProgramContext(), time_unit, FALSE, exvgNONE, 0); int frflags = settings_.frflags(); diff --git a/src/gromacs/utility/bitmask.h b/src/gromacs/utility/bitmask.h index 604081299b..1b3fe93f82 100644 --- a/src/gromacs/utility/bitmask.h +++ b/src/gromacs/utility/bitmask.h @@ -147,7 +147,7 @@ inline static void bitmask_init_low_bits(gmx_bitmask_t* m, int b) { memset(m->data(), 255, b/64*8); (*m)[b/64] = (static_cast(1) << (b%64)) - 1; - memset(&(m->data())[b/64+1], 0, (BITMASK_ALEN-b/64-1)*8); + memset(&(*m)[b/64+1], 0, (BITMASK_ALEN-b/64-1)*8); } inline static bool bitmask_is_set(gmx_bitmask_t m, int b) diff --git a/src/gromacs/utility/futil.cpp b/src/gromacs/utility/futil.cpp index 8ea8138b54..a15b7bc4e9 100644 --- a/src/gromacs/utility/futil.cpp +++ b/src/gromacs/utility/futil.cpp @@ -660,36 +660,33 @@ int gmx_file_rename(const char *oldname, const char *newname) int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_empty) { -/* the full copy buffer size: */ -#define FILECOPY_BUFSIZE (1<<16) - FILE *in = nullptr; - FILE *out = nullptr; - char *buf; - - snew(buf, FILECOPY_BUFSIZE); - - in = fopen(oldname, "rb"); + gmx::FilePtr in(fopen(oldname, "rb")); if (!in) { - goto error; + return 1; } /* If we don't copy when empty, we postpone opening the file until we're actually ready to write. */ + gmx::FilePtr out; if (copy_if_empty) { - out = fopen(newname, "wb"); + out.reset(fopen(newname, "wb")); if (!out) { - goto error; + return 1; } } - while (!feof(in)) + /* the full copy buffer size: */ + constexpr int FILECOPY_BUFSIZE = 1<<16; + std::vector buf(FILECOPY_BUFSIZE); + + while (!feof(in.get())) { size_t nread; - nread = fread(buf, sizeof(char), FILECOPY_BUFSIZE, in); + nread = fread(buf.data(), sizeof(char), FILECOPY_BUFSIZE, in.get()); if (nread > 0) { size_t ret; @@ -697,39 +694,24 @@ int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_emp { /* so this is where we open when copy_if_empty is false: here we know we read something. */ - out = fopen(newname, "wb"); + out.reset(fopen(newname, "wb")); if (!out) { - goto error; + return 1; } } - ret = fwrite(buf, sizeof(char), nread, out); + ret = fwrite(buf.data(), sizeof(char), nread, out.get()); if (ret != nread) { - goto error; + return 1; } } - if (ferror(in)) + if (ferror(in.get())) { - goto error; + return 1; } } - sfree(buf); - fclose(in); - fclose(out); return 0; -error: - sfree(buf); - if (in) - { - fclose(in); - } - if (out) - { - fclose(out); - } - return 1; -#undef FILECOPY_BUFSIZE } diff --git a/src/gromacs/utility/stringutil.cpp b/src/gromacs/utility/stringutil.cpp index f42a3f16d9..70196a130e 100644 --- a/src/gromacs/utility/stringutil.cpp +++ b/src/gromacs/utility/stringutil.cpp @@ -126,7 +126,7 @@ std::string stripString(const std::string &str) { ++start; } - while (start != end && std::isspace(*(end - 1))) + while (start != end && (std::isspace(*(end - 1)) != 0)) { --end; } diff --git a/src/programs/mdrun/tests/rerun.cpp b/src/programs/mdrun/tests/rerun.cpp index 5ee7fb5b10..c171ae573e 100644 --- a/src/programs/mdrun/tests/rerun.cpp +++ b/src/programs/mdrun/tests/rerun.cpp @@ -230,7 +230,7 @@ TEST_P(MdrunRerunTest, WithinTolerances) "no", "no"); // bd is much less reproducible in a rerun than the other integrators - double toleranceScaleFactor = (integrator == "bd") ? 2 : 1; + const int toleranceScaleFactor = (integrator == "bd") ? 2 : 1; EnergyTolerances energiesToMatch {{ { diff --git a/src/testutils/testasserts.h b/src/testutils/testasserts.h index 8990bd81f7..c4a2cce9ec 100644 --- a/src/testutils/testasserts.h +++ b/src/testutils/testasserts.h @@ -515,7 +515,8 @@ static inline FloatingPointTolerance defaultRealTolerance() static inline FloatingPointTolerance defaultFloatTolerance() { return relativeToleranceAsPrecisionDependentUlp - (1.0, detail::g_defaultUlpTolerance, detail::g_defaultUlpTolerance * (GMX_FLOAT_EPS / GMX_DOUBLE_EPS)); + (1.0, detail::g_defaultUlpTolerance, + static_cast(detail::g_defaultUlpTolerance * (GMX_FLOAT_EPS / GMX_DOUBLE_EPS))); } /*! \name Assertions for floating-point comparison