};
/* 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)
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 char* ptr;
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;
}
* 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))
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::string& include : *includes)
{
if (strcmp(include.c_str(), includePath) == 0)
{
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_define& define : *defines)
{
if (define.name == name)
{
/* 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_cpp* cpp = new gmx_cpp;
*handle = cpp;
if (definesFromParent)
}
else
{
- cpp->defines = std::make_shared < std::vector < t_define>>();
+ cpp->defines = std::make_shared<std::vector<t_define>>();
}
if (includesFromParent)
}
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
if (strstr(cppopts[i], "-D") == cppopts[i])
{
/* If the option contains a =, split it into name and value. */
- char *ptr = strchr(cppopts[i], '=');
+ char* ptr = 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
{
else
{
/* If not, check all the paths given with -I. */
- for (const std::string &include : *cpp->includes)
+ for (const std::string& include : *cpp->includes)
{
std::string buf = include + "/" + filenm;
if (gmx_fexist(buf))
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");
switch (errno)
{
case EINVAL:
- default:
- return eCPP_UNKNOWN;
+ default: return eCPP_UNKNOWN;
}
}
return eCPP_OK;
/* 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");
return eCPP_SYNTAX;
}
bool found = false;
- for (const t_define &define : *handle->defines)
+ for (const t_define& define : *handle->defines)
{
if (define.name == dval)
{
}
/* #endif statement */
- if (dname == "endif")
+ if (dname == "endif")
{
if (ifdefs.empty())
{
{
if (len == -1)
{
- i0 = i1+1;
+ i0 = i1 + 1;
len = 0;
}
else
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;
return eCPP_SYNTAX;
}
/* Split it into name and value. */
- const char *ptr = dval.c_str();
+ const char* ptr = dval.c_str();
while ((*ptr != '\0') && !isspace(*ptr))
{
ptr++;
}
/* #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)
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_t* handlep, int n, char buf[])
{
- gmx_cpp_t handle = *handlep;
- int status;
- bool bEOF;
+ gmx_cpp_t handle = *handlep;
+ int status;
+ bool bEOF;
if (!handle)
{
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.
return eCPP_EOF;
}
cpp_close_file(handlep);
- *handlep = handle->parent;
+ *handlep = handle->parent;
delete handle;
return cpp_read_line(handlep, n, 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_define& define : *handle->defines)
{
if (!define.def.empty())
{
int nn = 0;
- const char *ptr = buf;
+ const char* ptr = buf;
while ((ptr = strstrw(ptr, define.name.c_str())) != nullptr)
{
nn++;
}
root->unmatched_defines.erase(define.name);
- std::string name;
- const char *ptr = buf;
- const char *ptr2;
+ std::string name;
+ const char* ptr = buf;
+ const char* ptr2;
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),
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_t* handlep)
{
return (*handlep)->line_nr;
}
/* Close the file! Return integer status. */
-int cpp_close_file(gmx_cpp_t *handlep)
+int cpp_close_file(gmx_cpp_t* handlep)
{
gmx_cpp_t handle = *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_define& define : *(*handlep)->defines)
{
if (define.name == defineName)
{
/* 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))
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_cpp& handle)
{
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;
}