Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / gmxcpp.cpp
index d232b55dbf109e4f0aae681c6b3cc9999a114990..eaebc356dd76ff61532369fa0ee0a6b98e04fb72 100644 (file)
@@ -66,24 +66,28 @@ struct t_define
 };
 
 /* enum used for handling ifdefs */
-enum {
-    eifTRUE, eifFALSE, eifIGNORE, eifNR
+enum
+{
+    eifTRUE,
+    eifFALSE,
+    eifIGNORE,
+    eifNR
 };
 
 struct gmx_cpp
 {
-    std::shared_ptr < std::vector < t_define>>    defines;
-    std::shared_ptr < std::vector < std::string>> includes;
-    std::unordered_set<std::string> unmatched_defines;
-    FILE                           *fp = nullptr;
-    std::string                     path;
-    std::string                     cwd;
-    std::string                     fn;
-    std::string                     line;
-    int                             line_nr;
-    std::vector<int>                ifdefs;
-    struct gmx_cpp                 *child  = nullptr;
-    struct gmx_cpp                 *parent = nullptr;
+    std::shared_ptr<std::vector<t_define>>    defines;
+    std::shared_ptr<std::vector<std::string>> includes;
+    std::unordered_set<std::string>           unmatched_defines;
+    FILE*                                     fp = nullptr;
+    std::string                               path;
+    std::string                               cwd;
+    std::string                               fn;
+    std::string                               line;
+    int                                       line_nr;
+    std::vector<int>                          ifdefs;
+    struct gmx_cpp*                           child  = nullptr;
+    struct gmx_cpp*                           parent = nullptr;
 };
 
 static bool is_word_end(char c)
@@ -91,16 +95,15 @@ static bool is_word_end(char c)
     return !((isalnum(c) != 0) || c == '_');
 }
 
-static const char *strstrw(const char *buf, const char *word)
+static const char* strstrw(const char* buf, const char* word)
 {
-    const char *ptr;
+    const charptr;
 
     while ((ptr = strstr(buf, word)) != nullptr)
     {
         /* Check if we did not find part of a longer word */
-        if (ptr &&
-            is_word_end(ptr[strlen(word)]) &&
-            (((ptr > buf) && is_word_end(ptr[-1])) || (ptr == buf)))
+        if (ptr && is_word_end(ptr[strlen(word)])
+            && (((ptr > buf) && is_word_end(ptr[-1])) || (ptr == buf)))
         {
             return ptr;
         }
@@ -114,9 +117,7 @@ static const char *strstrw(const char *buf, const char *word)
  * returned in *name, and the remainder of the line after leading
  * whitespace, without trailing whitespace, is returned in *val
  */
-static bool find_directive(const char  *buf,
-                           std::string *name,
-                           std::string *val)
+static bool find_directive(const char* buf, std::string* name, std::string* val)
 {
     /* Skip initial whitespace */
     while (isspace(*buf))
@@ -173,13 +174,12 @@ static bool is_ifdeffed_out(gmx::ArrayRef<const int> ifdefs)
     return (!ifdefs.empty() && ifdefs.back() != eifTRUE);
 }
 
-static void add_include(std::vector<std::string> *includes,
-                        const char               *includePath)
+static void add_include(std::vector<std::string>* includes, const char* includePath)
 {
     GMX_RELEASE_ASSERT(includes, "Need valid includes");
     GMX_RELEASE_ASSERT(includePath, "Need a valid include path");
 
-    for (const std::string &include : *includes)
+    for (const std::stringinclude : *includes)
     {
         if (strcmp(include.c_str(), includePath) == 0)
         {
@@ -190,14 +190,12 @@ static void add_include(std::vector<std::string> *includes,
     includes->push_back(includePath);
 }
 
-static void add_define(std::vector<t_define> *defines,
-                       const std::string     &name,
-                       const char            *value)
+static void add_define(std::vector<t_define>* defines, const std::string& name, const char* value)
 {
     GMX_RELEASE_ASSERT(defines, "Need defines");
     GMX_RELEASE_ASSERT(value, "Need a value");
 
-    for (t_define &define : *defines)
+    for (t_definedefine : *defines)
     {
         if (define.name == name)
         {
@@ -211,15 +209,14 @@ static void add_define(std::vector<t_define> *defines,
 
 /* Open the file to be processed. The handle variable holds internal
    info for the cpp emulator. Return integer status */
-static int
-cpp_open_file(const char                                     *filenm,
-              gmx_cpp_t                                      *handle,
-              char                                          **cppopts,
-              std::shared_ptr < std::vector < t_define>>     *definesFromParent,
-              std::shared_ptr < std::vector < std::string>>  *includesFromParent)
+static int cpp_open_file(const char*                                filenm,
+                         gmx_cpp_t*                                 handle,
+                         char**                                     cppopts,
+                         std::shared_ptr<std::vector<t_define>>*    definesFromParent,
+                         std::shared_ptr<std::vector<std::string>>* includesFromParent)
 {
     // TODO: We should avoid new/delete, we should use Pimpl instead
-    gmx_cpp *cpp = new gmx_cpp;
+    gmx_cppcpp = new gmx_cpp;
     *handle      = cpp;
 
     if (definesFromParent)
@@ -228,7 +225,7 @@ cpp_open_file(const char                                     *filenm,
     }
     else
     {
-        cpp->defines = std::make_shared < std::vector < t_define>>();
+        cpp->defines = std::make_shared<std::vector<t_define>>();
     }
 
     if (includesFromParent)
@@ -237,7 +234,7 @@ cpp_open_file(const char                                     *filenm,
     }
     else
     {
-        cpp->includes = std::make_shared < std::vector < std::string>>();
+        cpp->includes = std::make_shared<std::vector<std::string>>();
     }
 
     /* First process options, they might be necessary for opening files
@@ -254,14 +251,13 @@ cpp_open_file(const char                                     *filenm,
             if (strstr(cppopts[i], "-D") == cppopts[i])
             {
                 /* If the option contains a =, split it into name and value. */
-                char *ptr = strchr(cppopts[i], '=');
+                charptr = strchr(cppopts[i], '=');
                 if (ptr)
                 {
                     std::string buf = cppopts[i] + 2;
                     buf.resize(ptr - cppopts[i] - 2);
                     add_define(cpp->defines.get(), buf, ptr + 1);
                     cpp->unmatched_defines.insert(buf);
-
                 }
                 else
                 {
@@ -281,7 +277,7 @@ cpp_open_file(const char                                     *filenm,
     else
     {
         /* If not, check all the paths given with -I. */
-        for (const std::string &include : *cpp->includes)
+        for (const std::stringinclude : *cpp->includes)
         {
             std::string buf = include + "/" + filenm;
             if (gmx_fexist(buf))
@@ -324,10 +320,10 @@ cpp_open_file(const char                                     *filenm,
         gmx_chdir(cpp->path.c_str());
     }
     cpp->line.clear();
-    cpp->line_nr  = 0;
+    cpp->line_nr = 0;
     cpp->ifdefs.clear();
-    cpp->child    = nullptr;
-    cpp->parent   = nullptr;
+    cpp->child  = nullptr;
+    cpp->parent = nullptr;
     if (cpp->fp == nullptr)
     {
         cpp->fp = fopen(cpp->fn.c_str(), "r");
@@ -337,8 +333,7 @@ cpp_open_file(const char                                     *filenm,
         switch (errno)
         {
             case EINVAL:
-            default:
-                return eCPP_UNKNOWN;
+            default: return eCPP_UNKNOWN;
         }
     }
     return eCPP_OK;
@@ -346,20 +341,17 @@ cpp_open_file(const char                                     *filenm,
 
 /* Open the file to be processed. The handle variable holds internal
    info for the cpp emulator. Return integer status */
-int cpp_open_file(const char *filenm, gmx_cpp_t *handle, char **cppopts)
+int cpp_open_file(const char* filenm, gmx_cpp_t* handle, char** cppopts)
 {
     return cpp_open_file(filenm, handle, cppopts, nullptr, nullptr);
 }
 
 /* Note that dval might be null, e.g. when handling a line like '#define */
-static int
-process_directive(gmx_cpp_t         *handlep,
-                  const std::string &dname,
-                  const std::string &dval)
+static int process_directive(gmx_cpp_t* handlep, const std::string& dname, const std::string& dval)
 {
-    gmx_cpp_t         handle = *handlep;
+    gmx_cpp_t handle = *handlep;
 
-    std::vector<int> &ifdefs = handle->ifdefs;
+    std::vector<int>ifdefs = handle->ifdefs;
 
     /* #ifdef or ifndef statement */
     bool bIfdef  = (dname == "ifdef");
@@ -378,7 +370,7 @@ process_directive(gmx_cpp_t         *handlep,
                 return eCPP_SYNTAX;
             }
             bool found = false;
-            for (const t_define &define : *handle->defines)
+            for (const t_definedefine : *handle->defines)
             {
                 if (define.name == dval)
                 {
@@ -425,7 +417,7 @@ process_directive(gmx_cpp_t         *handlep,
     }
 
     /* #endif statement */
-    if (dname ==  "endif")
+    if (dname == "endif")
     {
         if (ifdefs.empty())
         {
@@ -464,7 +456,7 @@ process_directive(gmx_cpp_t         *handlep,
             {
                 if (len == -1)
                 {
-                    i0  = i1+1;
+                    i0  = i1 + 1;
                     len = 0;
                 }
                 else
@@ -484,8 +476,8 @@ process_directive(gmx_cpp_t         *handlep,
         std::string inc_fn = dval.substr(i0, len);
 
         /* Open include file and store it as a child in the handle structure */
-        int status = cpp_open_file(inc_fn.c_str(), &(handle->child), nullptr,
-                                   &handle->defines, &handle->includes);
+        int status = cpp_open_file(inc_fn.c_str(), &(handle->child), nullptr, &handle->defines,
+                                   &handle->includes);
         if (status != eCPP_OK)
         {
             handle->child = nullptr;
@@ -506,7 +498,7 @@ process_directive(gmx_cpp_t         *handlep,
             return eCPP_SYNTAX;
         }
         /* Split it into name and value. */
-        const char *ptr = dval.c_str();
+        const charptr = dval.c_str();
         while ((*ptr != '\0') && !isspace(*ptr))
         {
             ptr++;
@@ -523,14 +515,14 @@ process_directive(gmx_cpp_t         *handlep,
     }
 
     /* #undef statement */
-    if (dname ==  "undef")
+    if (dname == "undef")
     {
         // A bare '#undef' is an invalid line
         if (dval.empty())
         {
             return eCPP_SYNTAX;
         }
-        std::vector<t_define> &defines = *handle->defines;
+        std::vector<t_define>defines = *handle->defines;
         for (size_t i = 0; i < defines.size(); i++)
         {
             if (defines[i].name == dval)
@@ -552,11 +544,11 @@ process_directive(gmx_cpp_t         *handlep,
    routine also does all the "intelligent" work like processing cpp
    directives and so on. Note that often the routine is called
    recursively and no cpp directives are printed. */
-int cpp_read_line(gmx_cpp_t *handlep, int n, char buf[])
+int cpp_read_line(gmx_cpp_thandlep, int n, char buf[])
 {
-    gmx_cpp_t   handle = *handlep;
-    int         status;
-    bool        bEOF;
+    gmx_cpp_t handle = *handlep;
+    int       status;
+    bool      bEOF;
 
     if (!handle)
     {
@@ -571,7 +563,7 @@ int cpp_read_line(gmx_cpp_t *handlep, int n, char buf[])
     if (!bEOF)
     {
         /* Read the actual line now. */
-        if (fgets2(buf, n-1, handle->fp) == nullptr)
+        if (fgets2(buf, n - 1, handle->fp) == nullptr)
         {
             /* Recheck EOF, since we could have been at the end before
              * the fgets2 call, but we need to read past the end to know.
@@ -594,7 +586,7 @@ int cpp_read_line(gmx_cpp_t *handlep, int n, char buf[])
             return eCPP_EOF;
         }
         cpp_close_file(handlep);
-        *handlep      = handle->parent;
+        *handlep = handle->parent;
         delete handle;
         return cpp_read_line(handlep, n, buf);
     }
@@ -630,12 +622,12 @@ int cpp_read_line(gmx_cpp_t *handlep, int n, char buf[])
        that we have to use a best fit algorithm, rather than first come
        first go. We do this by sorting the defines on length first, and
        then on alphabetical order. */
-    for (t_define &define : *handle->defines)
+    for (t_definedefine : *handle->defines)
     {
         if (!define.def.empty())
         {
             int         nn  = 0;
-            const char *ptr = buf;
+            const charptr = buf;
             while ((ptr = strstrw(ptr, define.name.c_str())) != nullptr)
             {
                 nn++;
@@ -651,14 +643,14 @@ int cpp_read_line(gmx_cpp_t *handlep, int n, char buf[])
                 }
                 root->unmatched_defines.erase(define.name);
 
-                std::string  name;
-                const char  *ptr = buf;
-                const char  *ptr2;
+                std::string name;
+                const charptr = buf;
+                const charptr2;
                 while ((ptr2 = strstrw(ptr, define.name.c_str())) != nullptr)
                 {
                     name.append(ptr, ptr2 - ptr);
                     name += define.def;
-                    ptr   = ptr2 + define.name.size();
+                    ptr = ptr2 + define.name.size();
                 }
                 name += ptr;
                 GMX_RELEASE_ASSERT(name.size() < static_cast<size_t>(n),
@@ -671,18 +663,18 @@ int cpp_read_line(gmx_cpp_t *handlep, int n, char buf[])
     return eCPP_OK;
 }
 
-const char *cpp_cur_file(const gmx_cpp_t *handlep)
+const char* cpp_cur_file(const gmx_cpp_t* handlep)
 {
     return (*handlep)->fn.c_str();
 }
 
-int cpp_cur_linenr(const gmx_cpp_t *handlep)
+int cpp_cur_linenr(const gmx_cpp_thandlep)
 {
     return (*handlep)->line_nr;
 }
 
 /* Close the file! Return integer status. */
-int cpp_close_file(gmx_cpp_t *handlep)
+int cpp_close_file(gmx_cpp_thandlep)
 {
     gmx_cpp_t handle = *handlep;
 
@@ -708,10 +700,9 @@ int cpp_close_file(gmx_cpp_t *handlep)
     return eCPP_OK;
 }
 
-const std::string *cpp_find_define(const gmx_cpp_t   *handlep,
-                                   const std::string &defineName)
+const std::string* cpp_find_define(const gmx_cpp_t* handlep, const std::string& defineName)
 {
-    for (const t_define &define : *(*handlep)->defines)
+    for (const t_definedefine : *(*handlep)->defines)
     {
         if (define.name == defineName)
         {
@@ -734,19 +725,24 @@ void cpp_done(gmx_cpp_t handle)
 
 /* Return a string containing the error message coresponding to status
    variable */
-char *cpp_error(gmx_cpp_t *handlep, int status)
+char* cpp_error(gmx_cpp_t* handlep, int status)
 {
     char        buf[256];
-    const char *ecpp[] = {
-        "OK", "File not found", "End of file", "Syntax error", "Interrupted",
-        "Invalid file handle", "Invalid delimiter for filename in #include statement",
-        "File not open", "Unknown error, perhaps your text file uses wrong line endings?", "Error status out of range"
-    };
+    const char* ecpp[] = { "OK",
+                           "File not found",
+                           "End of file",
+                           "Syntax error",
+                           "Interrupted",
+                           "Invalid file handle",
+                           "Invalid delimiter for filename in #include statement",
+                           "File not open",
+                           "Unknown error, perhaps your text file uses wrong line endings?",
+                           "Error status out of range" };
     gmx_cpp_t   handle = *handlep;
 
     if (!handle)
     {
-        return const_cast<char *>(ecpp[eCPP_INVALID_HANDLE]);
+        return const_cast<char*>(ecpp[eCPP_INVALID_HANDLE]);
     }
 
     if ((status < 0) || (status >= eCPP_NR))
@@ -754,28 +750,29 @@ char *cpp_error(gmx_cpp_t *handlep, int status)
         status = eCPP_NR;
     }
 
-    sprintf(buf, "%s - File %s, line %d\nLast line read:\n'%s'",
-            ecpp[status],
+    sprintf(buf, "%s - File %s, line %d\nLast line read:\n'%s'", ecpp[status],
             (handle && !handle->fn.empty()) ? handle->fn.c_str() : "unknown",
-            (handle) ? handle->line_nr : -1,
-            !handle->line.empty() ? handle->line.c_str() : "");
+            (handle) ? handle->line_nr : -1, !handle->line.empty() ? handle->line.c_str() : "");
 
     return gmx_strdup(buf);
 }
 
-std::string checkAndWarnForUnusedDefines(const gmx_cpp &handle)
+std::string checkAndWarnForUnusedDefines(const gmx_cpphandle)
 {
     std::string warning;
     if (!handle.unmatched_defines.empty())
     {
-        warning = "The following macros were defined in the 'define' mdp field with the -D prefix, but "
-            "were not used in the topology:\n";
-        for (auto &str : handle.unmatched_defines)
+        warning =
+                "The following macros were defined in the 'define' mdp field with the -D prefix, "
+                "but "
+                "were not used in the topology:\n";
+        for (auto& str : handle.unmatched_defines)
         {
             warning += ("    " + str + "\n");
         }
-        warning += "If you haven't made a spelling error, either use the macro you defined, "
-            "or don't define the macro";
+        warning +=
+                "If you haven't made a spelling error, either use the macro you defined, "
+                "or don't define the macro";
     }
     return warning;
 }