Clang-tidy-11 fixes for utility
authorPaul Bauer <paul.bauer.q@gmail.com>
Fri, 5 Feb 2021 15:58:27 +0000 (15:58 +0000)
committerArtem Zhmurov <zhmurov@gmail.com>
Fri, 5 Feb 2021 15:58:27 +0000 (15:58 +0000)
Fixes have been missing from previous fixes.

Refs #3897

19 files changed:
src/gromacs/utility/bitmask.h
src/gromacs/utility/cstringutil.cpp
src/gromacs/utility/futil.cpp
src/gromacs/utility/inmemoryserializer.cpp
src/gromacs/utility/int64_to_int.cpp
src/gromacs/utility/keyvaluetree.cpp
src/gromacs/utility/keyvaluetreeserializer.cpp
src/gromacs/utility/niceheader.cpp
src/gromacs/utility/path.cpp
src/gromacs/utility/physicalnodecommunicator.cpp
src/gromacs/utility/pleasecite.cpp
src/gromacs/utility/smalloc.cpp
src/gromacs/utility/strconvert.cpp
src/gromacs/utility/strdb.cpp
src/gromacs/utility/stringutil.cpp
src/gromacs/utility/tests/bitmask.h
src/gromacs/utility/tests/enumerationhelpers.cpp
src/gromacs/utility/tests/mdmodulenotification-impl.cpp
src/gromacs/utility/txtdump.cpp

index eddec13736eee67b1ff75ff45ddb445e5d63e339..6d4cb52ca482299b666b02be7dda2ee85d89cb4e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2014,2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2014,2018,2019,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.
@@ -157,9 +157,8 @@ inline static bool bitmask_is_set(gmx_bitmask_t m, int b)
 
 inline static bool bitmask_is_disjoint(gmx_bitmask_t a, gmx_bitmask_t b)
 {
-    int  i;
     bool r = true;
-    for (i = 0; i < BITMASK_ALEN; i++)
+    for (int i = 0; i < BITMASK_ALEN; i++)
     {
         r = r && ((a[i] & b[i]) == 0U);
     }
@@ -168,9 +167,8 @@ inline static bool bitmask_is_disjoint(gmx_bitmask_t a, gmx_bitmask_t b)
 
 inline static bool bitmask_is_equal(gmx_bitmask_t a, gmx_bitmask_t b)
 {
-    int  i;
     bool r = true;
-    for (i = 0; i < BITMASK_ALEN; i++)
+    for (int i = 0; i < BITMASK_ALEN; i++)
     {
         r = r && (a[i] == b[i]);
     }
@@ -179,9 +177,8 @@ inline static bool bitmask_is_equal(gmx_bitmask_t a, gmx_bitmask_t b)
 
 inline static bool bitmask_is_zero(gmx_bitmask_t m)
 {
-    int  i;
     bool r = true;
-    for (i = 0; i < BITMASK_ALEN; i++)
+    for (int i = 0; i < BITMASK_ALEN; i++)
     {
         r = r && (m[i] == 0U);
     }
@@ -190,8 +187,7 @@ inline static bool bitmask_is_zero(gmx_bitmask_t m)
 
 inline static void bitmask_union(gmx_bitmask_t* a, gmx_bitmask_t b)
 {
-    int i;
-    for (i = 0; i < BITMASK_ALEN; i++)
+    for (int i = 0; i < BITMASK_ALEN; i++)
     {
         (*a)[i] |= b[i];
     }
index 0d1a14a7d1aca504e356c683e51b299e2c709044..161fb7396eecb30393f43d60d54e165c253017cb 100644 (file)
@@ -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.
 
 int continuing(char* s)
 {
-    int sl;
     assert(s);
 
     rtrim(s);
-    sl = strlen(s);
+    int sl = strlen(s);
     if ((sl > 0) && (s[sl - 1] == CONTINUE))
     {
         s[sl - 1] = 0;
@@ -78,7 +77,7 @@ int continuing(char* s)
 
 char* fgets2(char* line, int n, FILE* stream)
 {
-    char* c;
+    char* c = nullptr;
     if (fgets(line, n, stream) == nullptr)
     {
         return nullptr;
@@ -113,7 +112,7 @@ char* fgets2(char* line, int n, FILE* stream)
 
 void strip_comment(char* line)
 {
-    char* c;
+    char* c = nullptr;
 
     if (!line)
     {
@@ -129,9 +128,7 @@ void strip_comment(char* line)
 
 void upstring(char* str)
 {
-    int i;
-
-    for (i = 0; (i < static_cast<int>(strlen(str))); i++)
+    for (int i = 0; (i < static_cast<int>(strlen(str))); i++)
     {
         str[i] = toupper(str[i]);
     }
@@ -139,21 +136,20 @@ void upstring(char* str)
 
 void ltrim(char* str)
 {
-    int i, c;
-
     if (nullptr == str)
     {
         return;
     }
 
-    c = 0;
+    int c = 0;
     while (('\0' != str[c]) && isspace(str[c]))
     {
         c++;
     }
     if (c > 0)
     {
-        for (i = c; ('\0' != str[i]); i++)
+        int i = c;
+        for (; ('\0' != str[i]); i++)
         {
             str[i - c] = str[i];
         }
@@ -163,14 +159,12 @@ void ltrim(char* str)
 
 void rtrim(char* str)
 {
-    int nul;
-
     if (nullptr == str)
     {
         return;
     }
 
-    nul = strlen(str) - 1;
+    int nul = strlen(str) - 1;
     while ((nul > 0) && ((str[nul] == ' ') || (str[nul] == '\t')))
     {
         str[nul] = '\0';
@@ -186,7 +180,7 @@ void trim(char* str)
 
 int gmx_strcasecmp_min(const char* str1, const char* str2)
 {
-    char ch1, ch2;
+    char ch1 = 0, ch2 = 0;
 
     do
     {
@@ -209,11 +203,10 @@ int gmx_strcasecmp_min(const char* str1, const char* str2)
 
 int gmx_strncasecmp_min(const char* str1, const char* str2, int n)
 {
-    char  ch1, ch2;
-    char *stri1, *stri2;
+    char ch1 = 0, ch2 = 0;
 
-    stri1 = const_cast<char*>(str1);
-    stri2 = const_cast<char*>(str2);
+    const char* stri1 = str1;
+    const char* stri2 = str2;
     do
     {
         do
@@ -235,7 +228,7 @@ int gmx_strncasecmp_min(const char* str1, const char* str2, int n)
 
 int gmx_strcasecmp(const char* str1, const char* str2)
 {
-    char ch1, ch2;
+    char ch1 = 0, ch2 = 0;
 
     do
     {
@@ -251,7 +244,7 @@ int gmx_strcasecmp(const char* str1, const char* str2)
 
 int gmx_strncasecmp(const char* str1, const char* str2, int n)
 {
-    char ch1, ch2;
+    char ch1 = 0, ch2 = 0;
 
     if (n == 0)
     {
@@ -273,7 +266,7 @@ int gmx_strncasecmp(const char* str1, const char* str2, int n)
 
 char* gmx_strdup(const char* src)
 {
-    char* dest;
+    char* dest = nullptr;
 
     auto length = strlen(src) + 1;
     snew(dest, length);
@@ -284,10 +277,9 @@ char* gmx_strdup(const char* src)
 
 char* gmx_strndup(const char* src, int n)
 {
-    int   len;
-    char* dest;
+    char* dest = nullptr;
 
-    len = strlen(src);
+    int len = strlen(src);
     if (len > n)
     {
         len = n;
@@ -306,7 +298,7 @@ const unsigned int gmx_string_hash_init = 5381;
 
 unsigned int gmx_string_fullhash_func(const char* s, unsigned int hash_init)
 {
-    int c;
+    int c = 0;
 
     while ((c = (*s++)) != '\0')
     {
@@ -317,7 +309,7 @@ unsigned int gmx_string_fullhash_func(const char* s, unsigned int hash_init)
 
 unsigned int gmx_string_hash_func(const char* s, unsigned int hash_init)
 {
-    int c;
+    int c = 0;
 
     while ((c = toupper(*s++)) != '\0')
     {
@@ -366,9 +358,8 @@ int gmx_wcmatch(const char* pattern, const char* str)
                  * since we have processed them above. */
                 if (*pattern == *str)
                 {
-                    int rc;
                     /* Match the suffix, and return if a match or an error */
-                    rc = gmx_wcmatch(pattern, str);
+                    int rc = gmx_wcmatch(pattern, str);
                     if (rc != GMX_NO_WCMATCH)
                     {
                         return rc;
@@ -395,9 +386,7 @@ int gmx_wcmatch(const char* pattern, const char* str)
 
 char* wrap_lines(const char* buf, int line_width, int indent, gmx_bool bIndentFirst)
 {
-    char*    b2;
-    int      i, i0, i2, j, b2len, lspace = 0, l2space = 0;
-    gmx_bool bFirst, bFitsOnLine;
+    int i = 0;
 
     /* characters are copied from buf to b2 with possible spaces changed
      * into newlines and extra space added for indentation.
@@ -411,10 +400,11 @@ char* wrap_lines(const char* buf, int line_width, int indent, gmx_bool bIndentFi
      * the current line (where it also won't fit, but looks better)
      */
 
-    b2    = nullptr;
-    b2len = strlen(buf) + 1 + indent;
+    char* b2    = nullptr;
+    int   b2len = strlen(buf) + 1 + indent;
     snew(b2, b2len);
-    i0 = i2 = 0;
+    int i0 = 0;
+    int i2 = 0;
     if (bIndentFirst)
     {
         for (i2 = 0; (i2 < indent); i2++)
@@ -422,10 +412,11 @@ char* wrap_lines(const char* buf, int line_width, int indent, gmx_bool bIndentFi
             b2[i2] = ' ';
         }
     }
-    bFirst = TRUE;
+    bool bFirst = true;
     do
     {
-        l2space = -1;
+        int lspace  = 0;
+        int l2space = -1;
         /* find the last space before end of line */
         for (i = i0; ((i - i0 < line_width) || (l2space == -1)) && (buf[i]); i++)
         {
@@ -443,7 +434,7 @@ char* wrap_lines(const char* buf, int line_width, int indent, gmx_bool bIndentFi
                 b2len += indent;
                 srenew(b2, b2len);
                 /* add indentation after the newline */
-                for (j = 0; (j < indent); j++)
+                for (int j = 0; (j < indent); j++)
                 {
                     b2[i2++] = ' ';
                 }
@@ -458,7 +449,7 @@ char* wrap_lines(const char* buf, int line_width, int indent, gmx_bool bIndentFi
         if (buf[i])
         {
             /* check if one word does not fit on the line */
-            bFitsOnLine = (i - i0 <= line_width);
+            bool bFitsOnLine = (i - i0 <= line_width);
             /* reset line counters to just after the space */
             i0 = lspace + 1;
             i2 = l2space + 1;
@@ -477,7 +468,7 @@ char* wrap_lines(const char* buf, int line_width, int indent, gmx_bool bIndentFi
                     }
                     b2len += indent;
                     srenew(b2, b2len);
-                    for (j = 0; (j < indent); j++)
+                    for (int j = 0; (j < indent); j++)
                     {
                         b2[i2++] = ' ';
                     }
index 1c5385d8de35278c9523c70f1e43796362f84b4c..95a0eaa04c249a7a982f9b8914a73a637db50200 100644 (file)
@@ -183,12 +183,11 @@ static int pclose(FILE* /* fp */)
 
 int gmx_ffclose(FILE* fp)
 {
-    t_pstack *ps = nullptr, *tmp = nullptr;
-    int       ret = 0;
+    int ret = 0;
 
     Lock pstackLock(pstack_mutex);
 
-    ps = pstack;
+    t_pstack* ps = pstack;
     if (ps == nullptr)
     {
         if (fp != nullptr)
@@ -217,8 +216,8 @@ int gmx_ffclose(FILE* fp)
             {
                 ret = pclose(ps->prev->fp);
             }
-            tmp      = ps->prev;
-            ps->prev = ps->prev->prev;
+            t_pstack* tmp = ps->prev;
+            ps->prev      = ps->prev->prev;
             sfree(tmp);
         }
         else
@@ -332,13 +331,11 @@ static FILE* gunzip(const std::string& fn, const char* mode)
 
 gmx_bool gmx_fexist(const std::string& fname)
 {
-    FILE* test = nullptr;
-
     if (fname.empty())
     {
         return FALSE;
     }
-    test = fopen(fname.c_str(), "r");
+    FILE* test = fopen(fname.c_str(), "r");
     if (test == nullptr)
     {
 /*Windows doesn't allow fopen of directory - so we need to check this seperately */
@@ -410,9 +407,7 @@ void make_backup(const std::string& name)
 
 FILE* gmx_ffopen(const std::string& file, const char* mode)
 {
-    FILE*    ff    = nullptr;
-    gmx_bool bRead = 0;
-    int      bs    = 0;
+    FILE* ff = nullptr;
 
     if (file.empty())
     {
@@ -424,7 +419,7 @@ FILE* gmx_ffopen(const std::string& file, const char* mode)
         make_backup(file);
     }
 
-    bRead = (mode[0] == 'r' && mode[1] != '+');
+    bool bRead = (mode[0] == 'r' && mode[1] != '+');
     if (!bRead || gmx_fexist(file))
     {
         if ((ff = fopen(file.c_str(), mode)) == nullptr)
@@ -438,14 +433,7 @@ FILE* gmx_ffopen(const std::string& file, const char* mode)
         if (bUnbuffered || ((bufsize = getenv("GMX_LOG_BUFFER")) != nullptr))
         {
             /* Check whether to use completely unbuffered */
-            if (bUnbuffered)
-            {
-                bs = 0;
-            }
-            else
-            {
-                bs = strtol(bufsize, nullptr, 10);
-            }
+            const int bs = bUnbuffered ? 0 : strtol(bufsize, nullptr, 10);
             if (bs <= 0)
             {
                 setbuf(ff, nullptr);
@@ -546,16 +534,15 @@ static int makeTemporaryFilename(char* buf)
      * since windows doesnt support it we have to separate the cases.
      * 20090307: mktemp deprecated, use iso c++ _mktemp instead.
      */
-    int fd = 0;
 #if GMX_NATIVE_WINDOWS
     _mktemp(buf);
     if (buf == NULL)
     {
         gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf, strerror(errno));
     }
-    fd = 0;
+    int fd = 0;
 #else
-    fd = mkstemp(buf);
+    int fd = mkstemp(buf);
 
     if (fd < 0)
     {
@@ -641,9 +628,7 @@ int gmx_file_copy(const char* oldname, const char* newname, gmx_bool copy_if_emp
 
     while (!feof(in.get()))
     {
-        size_t nread = 0;
-
-        nread = fread(buf.data(), sizeof(char), FILECOPY_BUFSIZE, in.get());
+        size_t nread = fread(buf.data(), sizeof(char), FILECOPY_BUFSIZE, in.get());
         if (nread > 0)
         {
             size_t ret = 0;
@@ -677,16 +662,14 @@ int gmx_fsync(FILE* fp)
     int rc = 0;
 
     {
-        int fn = 0;
-
         /* get the file number */
 #if HAVE_FILENO
-        fn = fileno(fp);
+        int fn = fileno(fp);
 #elif HAVE__FILENO
-        fn = _fileno(fp);
+        int fn = _fileno(fp);
 #else
         GMX_UNUSED_VALUE(fp);
-        fn = -1;
+        int fn = -1;
 #endif
 
         /* do the actual fsync */
index 832077e9da5c354dc7f182c8d5825e16b7b96ee3..cdfc6ad9d26004f89c8388c8ca9d013c4e46ebdf 100644 (file)
@@ -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.
@@ -276,7 +276,7 @@ public:
     }
     void doString(std::string* value)
     {
-        uint64_t size;
+        uint64_t size = 0;
         doValue<uint64_t>(&size);
         *value = std::string(&buffer_[pos_], size);
         pos_ += size;
index 0955138e627352132967d741571a24d15511a830..21b835dcbeda75bcc6f7be7748ad6558d86796d9 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2015,2017,2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2015,2017,2018,2019,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.
@@ -42,9 +42,7 @@
 
 int int64_to_int(int64_t step, const char* warn)
 {
-    int i;
-
-    i = static_cast<int>(step);
+    int i = static_cast<int>(step);
 
     if (warn != nullptr && (static_cast<int64_t>(i) != step))
     {
index 24fa2b3fd4845e5323f91a667bfc352da10c5ac0..774af0a3667c7acddd9caab4c0e248baf8de89f9 100644 (file)
@@ -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.
@@ -240,7 +240,7 @@ private:
         }
     }
 
-    bool areSimpleValuesOfSameTypeEqual(const KeyValueTreeValue& value1, const KeyValueTreeValue& value2)
+    bool areSimpleValuesOfSameTypeEqual(const KeyValueTreeValue& value1, const KeyValueTreeValue& value2) const
     {
         GMX_ASSERT(value1.type() == value2.type(), "Caller should ensure that types are equal");
         if (value1.isType<bool>())
index 5cb8db913dc4bc9cd69273cbb4dca8c71733f986..949799fdd44c7db3d57263d7b457702a3ebf40da 100644 (file)
@@ -41,6 +41,7 @@
 #include "gromacs/utility/iserializer.h"
 #include "gromacs/utility/keyvaluetree.h"
 #include "gromacs/utility/keyvaluetreebuilder.h"
+#include "math.h"
 
 namespace gmx
 {
@@ -103,7 +104,7 @@ struct SerializationTraits<KeyValueTreeObject>
     }
     static void deserializeObject(KeyValueTreeObjectBuilder* builder, ISerializer* serializer)
     {
-        int         count;
+        int         count = 0;
         std::string key;
         serializer->doInt(&count);
         for (int i = 0; i < count; ++i)
@@ -129,7 +130,7 @@ struct SerializationTraits<KeyValueTreeArray>
     static void deserialize(KeyValueTreeValueBuilder* value, ISerializer* serializer)
     {
         KeyValueTreeArrayBuilder builder(value->createArray());
-        int                      count;
+        int                      count = 0;
         serializer->doInt(&count);
         for (int i = 0; i < count; ++i)
         {
@@ -159,7 +160,7 @@ struct SerializationTraits<bool>
     static void serialize(bool value, ISerializer* serializer) { serializer->doBool(&value); }
     static void deserialize(KeyValueTreeValueBuilder* builder, ISerializer* serializer)
     {
-        bool value;
+        bool value = false;
         serializer->doBool(&value);
         builder->setValue<bool>(value);
     }
@@ -171,7 +172,7 @@ struct SerializationTraits<int>
     static void serialize(int value, ISerializer* serializer) { serializer->doInt(&value); }
     static void deserialize(KeyValueTreeValueBuilder* builder, ISerializer* serializer)
     {
-        int value;
+        int value = 0;
         serializer->doInt(&value);
         builder->setValue<int>(value);
     }
@@ -183,7 +184,7 @@ struct SerializationTraits<int64_t>
     static void serialize(int64_t value, ISerializer* serializer) { serializer->doInt64(&value); }
     static void deserialize(KeyValueTreeValueBuilder* builder, ISerializer* serializer)
     {
-        int64_t value;
+        int64_t value = 0;
         serializer->doInt64(&value);
         builder->setValue<int64_t>(value);
     }
@@ -195,7 +196,7 @@ struct SerializationTraits<float>
     static void serialize(float value, ISerializer* serializer) { serializer->doFloat(&value); }
     static void deserialize(KeyValueTreeValueBuilder* builder, ISerializer* serializer)
     {
-        float value;
+        float value = 0;
         serializer->doFloat(&value);
         builder->setValue<float>(value);
     }
@@ -207,7 +208,7 @@ struct SerializationTraits<double>
     static void serialize(double value, ISerializer* serializer) { serializer->doDouble(&value); }
     static void deserialize(KeyValueTreeValueBuilder* builder, ISerializer* serializer)
     {
-        double value;
+        double value = 0;
         serializer->doDouble(&value);
         builder->setValue<double>(value);
     }
@@ -263,7 +264,7 @@ void ValueSerializer::serialize(const KeyValueTreeValue& value, ISerializer* ser
 
 KeyValueTreeValue ValueSerializer::deserialize(ISerializer* serializer)
 {
-    unsigned char typeTag;
+    unsigned char typeTag = 0;
     serializer->doUChar(&typeTag);
     auto iter = s_deserializers.find(typeTag);
     GMX_RELEASE_ASSERT(iter != s_deserializers.end(), "Unknown type tag for deserializization");
index 333b9f5c02ba615f4aeb6387b3df659d494d1d8b..f54ff08f8d811d772704f91f514ed40b29edfa3e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2017,2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2017,2018,2019,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.
@@ -52,7 +52,6 @@ namespace gmx
 
 void niceHeader(TextWriter* writer, const char* fn, char commentChar)
 {
-    int  uid;
     char userbuf[256];
     char hostbuf[256];
 
@@ -60,7 +59,7 @@ void niceHeader(TextWriter* writer, const char* fn, char commentChar)
     writer->writeLine(formatString("%c", commentChar));
     writer->writeLine(formatString("%c\tFile '%s' was generated", commentChar, fn ? fn : "unknown"));
 
-    uid = gmx_getuid();
+    int uid = gmx_getuid();
     gmx_getusername(userbuf, 256);
     gmx_gethostname(hostbuf, 256);
 
index 374e2f5111242d8a4d0a1086b471af58001e32da..5185d5268d849e5a3da3d8f9d591bc5424ec416e 100644 (file)
@@ -2,7 +2,7 @@
  * This file is part of the GROMACS molecular simulation package.
  *
  * Copyright (c) 2011-2018, 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.
@@ -429,8 +429,8 @@ std::string Path::getWorkingDirectory()
 
 void Path::splitPathEnvironment(const std::string& pathEnv, std::vector<std::string>* result)
 {
-    size_t prevPos = 0;
-    size_t separator;
+    size_t prevPos   = 0;
+    size_t separator = 0;
     do
     {
         separator = pathEnv.find(cPathSeparator, prevPos);
@@ -462,7 +462,7 @@ std::string Path::resolveSymlinks(const std::string& path)
     std::string result(path);
 #if !GMX_NATIVE_WINDOWS
     char buf[GMX_PATH_MAX];
-    int  length;
+    int  length = 0;
     while ((length = readlink(result.c_str(), buf, sizeof(buf) - 1)) > 0)
     {
         buf[length] = '\0';
index 827a9bf301fbd3810d0684b6ebc7fb52796a1186..8d9866dcd9a3c4ce497573199d88c4902cd724da 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,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.
@@ -69,15 +69,15 @@ void MPI_Comm_free_wrapper(MPI_Comm* comm)
 PhysicalNodeCommunicator::PhysicalNodeCommunicator(MPI_Comm world, int physicalNodeId)
 {
 #if GMX_MPI
-    int isInitialized;
+    int isInitialized = 0;
     MPI_Initialized(&isInitialized);
     if (isInitialized)
     {
-        int sizeOfWorld;
+        int sizeOfWorld = 0;
         MPI_Comm_size(world, &sizeOfWorld);
         if (sizeOfWorld > 1)
         {
-            int rankWithinWorld;
+            int rankWithinWorld = 0;
             MPI_Comm_rank(world, &rankWithinWorld);
             MPI_Comm_split(world, physicalNodeId, rankWithinWorld, &comm_);
             auto ptr = MPI_Comm_ptr(&comm_);
index 0d49d451b7661075fd269d0dfc356efd96e6846c..65bfe5c7dbd9cd54f01c7db13e23bf7fc5b8bd97 100644 (file)
@@ -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.
@@ -57,6 +57,8 @@ typedef struct
     const char* pages;
 } t_citerec;
 
+static constexpr int sc_lineWidth = 79;
+
 void please_cite(FILE* fp, const char* key)
 {
     static const t_citerec citedb[] = {
@@ -558,24 +560,20 @@ void please_cite(FILE* fp, const char* key)
     };
 #define NSTR static_cast<int>(asize(citedb))
 
-    int   index;
-    char* author;
-    char* title;
-#define LINE_WIDTH 79
-
     if (fp == nullptr)
     {
         return;
     }
 
-    for (index = 0; index < NSTR && (strcmp(citedb[index].key, key) != 0); index++) {}
+    int index = 0;
+    for (; index < NSTR && (strcmp(citedb[index].key, key) != 0); index++) {}
 
     fprintf(fp, "\n++++ PLEASE READ AND CITE THE FOLLOWING REFERENCE ++++\n");
     if (index < NSTR)
     {
         /* Insert newlines */
-        author = wrap_lines(citedb[index].author, LINE_WIDTH, 0, FALSE);
-        title  = wrap_lines(citedb[index].title, LINE_WIDTH, 0, FALSE);
+        char* author = wrap_lines(citedb[index].author, sc_lineWidth, 0, FALSE);
+        char* title  = wrap_lines(citedb[index].title, sc_lineWidth, 0, FALSE);
         fprintf(fp,
                 "%s\n%s\n%s %d (%d) pp. %s\n",
                 author,
@@ -611,7 +609,7 @@ void writeSourceDoi(FILE* fp)
         return;
     }
     gmx::TextLineWrapper wrapper;
-    wrapper.settings().setLineLength(LINE_WIDTH);
+    wrapper.settings().setLineLength(sc_lineWidth);
     wrapper.settings().setFirstLineIndent(0);
     const std::string doiString = wrapper.wrapToString(gmxDOI());
 
index 79bf1010a6ce964e7a96dc073fa258f00df2e3b3..5e504aba944d9369ea8cc5739388e534907bf3a7 100644 (file)
@@ -66,9 +66,8 @@ static std::mutex g_overAllocMutex;
 
 void* save_malloc(const char* name, const char* file, int line, size_t size)
 {
-    void* p;
+    void* p = nullptr;
 
-    p = nullptr;
     if (size == 0)
     {
         p = nullptr;
@@ -95,9 +94,8 @@ void* save_malloc(const char* name, const char* file, int line, size_t size)
 
 void* save_calloc(const char* name, const char* file, int line, size_t nelem, size_t elsize)
 {
-    void* p;
+    void* p = nullptr;
 
-    p = nullptr;
     if ((nelem == 0) || (elsize == 0))
     {
         p = nullptr;
@@ -154,10 +152,9 @@ void* save_calloc(const char* name, const char* file, int line, size_t nelem, si
 
 void* save_realloc(const char* name, const char* file, int line, void* ptr, size_t nelem, size_t elsize)
 {
-    void*  p;
+    void*  p    = nullptr;
     size_t size = nelem * elsize;
 
-    p = nullptr;
     if (size == 0)
     {
         save_free(name, file, line, ptr);
@@ -217,7 +214,7 @@ void save_free(const char gmx_unused* name, const char gmx_unused* file, int gmx
  * the necessary alignment. */
 void* save_malloc_aligned(const char* name, const char* file, int line, size_t nelem, size_t elsize, size_t alignment)
 {
-    void* p;
+    void* p = nullptr;
 
     if (alignment == 0)
     {
index 44ee5e9f15095f20458eb667f87181f3d3cb998e..c2fe14b3df1a7fe2740b4fcbb6fce8087ebea953 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2016,2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2016,2018,2019,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.
@@ -75,9 +75,9 @@ bool boolFromString(const char* value)
 
 int intFromString(const char* str)
 {
-    errno = 0;
-    char*          endptr;
-    const long int value = std::strtol(str, &endptr, 10);
+    errno                 = 0;
+    char*          endptr = nullptr;
+    const long int value  = std::strtol(str, &endptr, 10);
     if (errno == ERANGE || value < std::numeric_limits<int>::min()
         || value > std::numeric_limits<int>::max())
     {
@@ -93,9 +93,9 @@ int intFromString(const char* str)
 
 int64_t int64FromString(const char* str)
 {
-    errno = 0;
-    char*         endptr;
-    const int64_t value = str_to_int64_t(str, &endptr);
+    errno                = 0;
+    char*         endptr = nullptr;
+    const int64_t value  = str_to_int64_t(str, &endptr);
     if (errno == ERANGE)
     {
         GMX_THROW(InvalidInputError("Invalid value: '" + std::string(str)
@@ -110,9 +110,9 @@ int64_t int64FromString(const char* str)
 
 float floatFromString(const char* str)
 {
-    errno = 0;
-    char*        endptr;
-    const double value = std::strtod(str, &endptr);
+    errno               = 0;
+    char*        endptr = nullptr;
+    const double value  = std::strtod(str, &endptr);
     if (errno == ERANGE || value < -std::numeric_limits<float>::max()
         || value > std::numeric_limits<float>::max())
     {
@@ -128,9 +128,9 @@ float floatFromString(const char* str)
 
 double doubleFromString(const char* str)
 {
-    errno = 0;
-    char*        endptr;
-    const double value = std::strtod(str, &endptr);
+    errno               = 0;
+    char*        endptr = nullptr;
+    const double value  = std::strtod(str, &endptr);
     if (errno == ERANGE)
     {
         GMX_THROW(InvalidInputError("Invalid value: '" + std::string(str)
index 8704b16055befeb19480d0a3ad0a8a1012913b9a..b4a2fd9727a5480b5cabd5089e70a442a455d28a 100644 (file)
@@ -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.
@@ -50,8 +50,8 @@
 
 gmx_bool get_a_line(FILE* fp, char line[], int n)
 {
-    char* line0;
-    char* dum;
+    char* line0 = nullptr;
+    char* dum   = nullptr;
 
     snew(line0, n + 1);
 
@@ -113,10 +113,8 @@ gmx_bool get_header(char line[], char* header)
 
 int search_str(int nstr, char** str, char* key)
 {
-    int i;
-
     /* Linear search */
-    for (i = 0; (i < nstr); i++)
+    for (int i = 0; (i < nstr); i++)
     {
         if (gmx_strcasecmp(str[i], key) == 0)
         {
@@ -129,12 +127,11 @@ int search_str(int nstr, char** str, char* key)
 
 static int fget_lines(FILE* in, const char* db, char*** strings)
 {
-    char** ptr;
+    char** ptr = nullptr;
     char   buf[STRLEN];
-    int    i, nstr;
-    char*  pret;
+    int    nstr = 0;
 
-    pret = fgets(buf, STRLEN, in);
+    char* pret = fgets(buf, STRLEN, in);
     if (pret == nullptr || sscanf(buf, "%d", &nstr) != 1)
     {
         gmx_warning("File is empty");
@@ -143,7 +140,7 @@ static int fget_lines(FILE* in, const char* db, char*** strings)
         return 0;
     }
     snew(ptr, nstr);
-    for (i = 0; (i < nstr); i++)
+    for (int i = 0; (i < nstr); i++)
     {
         if (fgets2(buf, STRLEN, in) == nullptr)
         {
index e5dbb9241e1e9b0dd7336672a466a2fa4897b44c..c818320e2ec82ccd32352676a10b01a4dd34bd62 100644 (file)
@@ -2,7 +2,7 @@
  * This file is part of the GROMACS molecular simulation package.
  *
  * Copyright (c) 2011-2018, 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.
@@ -200,11 +200,11 @@ std::vector<std::string> splitString(const std::string& str)
 std::vector<std::string> splitDelimitedString(const std::string& str, char delim)
 {
     std::vector<std::string> result;
-    size_t                   currPos = 0;
-    const size_t             len     = str.length();
+    const size_t             len = str.length();
     if (len > 0)
     {
-        size_t nextDelim;
+        size_t nextDelim = 0;
+        size_t currPos   = 0;
         do
         {
             nextDelim = str.find(delim, currPos);
index 2cba18b9c946b9dc856b77a929bec92d465695ad..19cd95b33cc155a71a2bd8f11023675f2c1ac7c8 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2014,2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2014,2018,2019,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.
@@ -60,7 +60,7 @@ class BITMASK_CLASSNAME(BITMASK_SIZE) : public ::testing::TestWithParam<int>
 
 BITMASK_TEST_P(SetAndClear) //NOLINT(misc-definitions-in-headers)
 {
-    gmx_bitmask_t m;
+    gmx_bitmask_t m; //NOLINT(cppcoreguidelines-init-variables)
     int           i = GetParam();
     bitmask_clear(&m);
     EXPECT_TRUE(bitmask_is_zero(m));
@@ -76,7 +76,7 @@ BITMASK_TEST_P(SetAndClear) //NOLINT(misc-definitions-in-headers)
 
 BITMASK_TEST_P(InitBit) //NOLINT(misc-definitions-in-headers)
 {
-    gmx_bitmask_t m1, m2;
+    gmx_bitmask_t m1, m2; //NOLINT(cppcoreguidelines-init-variables)
     int           i = GetParam();
     bitmask_init_bit(&m1, i);
     bitmask_clear(&m2);
@@ -87,7 +87,7 @@ BITMASK_TEST_P(InitBit) //NOLINT(misc-definitions-in-headers)
 
 BITMASK_TEST_P(InitLowBits) //NOLINT(misc-definitions-in-headers)
 {
-    gmx_bitmask_t m;
+    gmx_bitmask_t m; //NOLINT(cppcoreguidelines-init-variables)
     int           i = GetParam();
     bitmask_init_low_bits(&m, i);
     for (int j = 0; j < BITMASK_SIZE; j++)
@@ -98,7 +98,7 @@ BITMASK_TEST_P(InitLowBits) //NOLINT(misc-definitions-in-headers)
 
 BITMASK_TEST_P(Disjoint) //NOLINT(misc-definitions-in-headers)
 {
-    gmx_bitmask_t m1, m2;
+    gmx_bitmask_t m1, m2; //NOLINT(cppcoreguidelines-init-variables)
     int           i = GetParam();
     bitmask_init_bit(&m1, i);
     bitmask_init_bit(&m2, i);
@@ -109,7 +109,7 @@ BITMASK_TEST_P(Disjoint) //NOLINT(misc-definitions-in-headers)
 
 BITMASK_TEST_P(Union) //NOLINT(misc-definitions-in-headers)
 {
-    gmx_bitmask_t m1, m2;
+    gmx_bitmask_t m1, m2; //NOLINT(cppcoreguidelines-init-variables)
     int           i = GetParam();
     int           j = (i + BITMASK_SIZE / 2) % BITMASK_SIZE;
     bitmask_init_bit(&m1, i);
@@ -133,7 +133,7 @@ BITMASK_TEST_P(Union) //NOLINT(misc-definitions-in-headers)
 }
 BITMASK_TEST_P(ToHex) //NOLINT(misc-definitions-in-headers)
 {
-    gmx_bitmask_t m;
+    gmx_bitmask_t m; //NOLINT(cppcoreguidelines-init-variables)
     bitmask_clear(&m);
     bitmask_set_bit(&m, BITMASK_SIZE - 1);
     EXPECT_EQ(to_hex_string(m), "8" + std::string(BITMASK_SIZE / 4 - 1, '0'));
index 9f7da6257d29fe71765529e54c46c3c15522a247..cd54329df1052934a8436c8afdcf9c1774ddad9e 100644 (file)
@@ -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.
@@ -124,7 +124,7 @@ TEST(EnumerationHelpersTest, EnumerationArrayWorks)
     }
 
     // Incrementing iterators works
-    auto x = std::begin(fooStrings);
+    const auto* x = std::begin(fooStrings);
     EXPECT_EQ(*x, "Bar");
     ++x;
     EXPECT_EQ(*x, "Baz");
index 5107ed92a9e95f71ac59e27666d1d0ff45e70dd2..033a5ab11b4562e2745fdb42ffd7cb52d4178f32 100644 (file)
@@ -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.
@@ -63,7 +63,7 @@ class EventACallee final
 public:
     void callback(EventA /*a*/) { notifiedEventA_ = true; }
 
-    bool notifiedEventA() { return notifiedEventA_; }
+    bool notifiedEventA() const { return notifiedEventA_; }
 
 private:
     bool notifiedEventA_ = false;
@@ -74,7 +74,7 @@ class EventBCallee final
 public:
     void callback(EventB* /* bPointer */) { notifiedEventB_ = true; }
 
-    bool notifiedEventB() { return notifiedEventB_; }
+    bool notifiedEventB() const { return notifiedEventB_; }
 
 private:
     bool notifiedEventB_ = false;
@@ -87,8 +87,8 @@ public:
 
     void callback(EventA /* a */) { notifiedEventA_ = true; }
 
-    bool notifiedEventB() { return notifiedEventB_; }
-    bool notifiedEventA() { return notifiedEventA_; }
+    bool notifiedEventB() const { return notifiedEventB_; }
+    bool notifiedEventA() const { return notifiedEventA_; }
 
 private:
     bool notifiedEventB_ = false;
index 1198fa9dde26af610e9b14d43f7390e569daa86f..92ae68c6fe3c20a2f2f348dd8119663bd53971ce 100644 (file)
@@ -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.
@@ -48,9 +48,7 @@
 
 int pr_indent(FILE* fp, int n)
 {
-    int i;
-
-    for (i = 0; i < n; i++)
+    for (int i = 0; i < n; i++)
     {
         fprintf(fp, " ");
     }
@@ -93,13 +91,11 @@ int pr_title_nxn(FILE* fp, int indent, const char* title, int n1, int n2)
 
 void pr_reals(FILE* fp, int indent, const char* title, const real* vec, int n)
 {
-    int i;
-
     if (available(fp, vec, indent, title))
     {
         pr_indent(fp, indent);
         fprintf(fp, "%s:\t", title);
-        for (i = 0; i < n; i++)
+        for (int i = 0; i < n; i++)
         {
             fprintf(fp, "  %10g", vec[i]);
         }
@@ -109,13 +105,11 @@ void pr_reals(FILE* fp, int indent, const char* title, const real* vec, int n)
 
 void pr_doubles(FILE* fp, int indent, const char* title, const double* vec, int n)
 {
-    int i;
-
     if (available(fp, vec, indent, title))
     {
         pr_indent(fp, indent);
         fprintf(fp, "%s:\t", title);
-        for (i = 0; i < n; i++)
+        for (int i = 0; i < n; i++)
         {
             fprintf(fp, "  %10g", vec[i]);
         }
@@ -125,28 +119,18 @@ void pr_doubles(FILE* fp, int indent, const char* title, const double* vec, int
 
 void pr_reals_of_dim(FILE* fp, int indent, const char* title, const real* vec, int n, int dim)
 {
-    int         i, j;
     const char* fshort = "%12.5e";
     const char* flong  = "%15.8e";
-    const char* format;
-
-    if (getenv("GMX_PRINT_LONGFORMAT") != nullptr)
-    {
-        format = flong;
-    }
-    else
-    {
-        format = fshort;
-    }
+    const char* format = (getenv("GMX_PRINT_LONGFORMAT") != nullptr) ? flong : fshort;
 
     if (available(fp, vec, indent, title))
     {
         indent = pr_title_nxn(fp, indent, title, n, dim);
-        for (i = 0; i < n; i++)
+        for (int i = 0; i < n; i++)
         {
             pr_indent(fp, indent);
             fprintf(fp, "%s[%5d]={", title, i);
-            for (j = 0; j < dim; j++)
+            for (int j = 0; j < dim; j++)
             {
                 if (j != 0)
                 {
@@ -193,12 +177,10 @@ void pr_str(FILE* fp, int indent, const char* title, const char* s)
 
 void pr_strings(FILE* fp, int indent, const char* title, char*** nm, 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\"}\n", title, bShowNumbers ? i : -1, *(nm[i]));