From 21f3f51ea2a719c9e57bdaf31c34b385c0c4efd4 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 2 Aug 2019 10:13:52 -0400 Subject: [PATCH] better error messages on accessing invalid IDs in variable expressions --- src/variable.cpp | 65 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/src/variable.cpp b/src/variable.cpp index 3fffa11d5e..1093ce9066 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include "universe.h" #include "atom.h" #include "update.h" @@ -1302,8 +1303,12 @@ double Variable::evaluate(char *str, Tree **tree, int ivar) if (word[0] == 'C') lowercase = 0; int icompute = modify->find_compute(word+2); - if (icompute < 0) - print_var_error(FLERR,"Invalid compute ID in variable formula",ivar); + if (icompute < 0) { + std::string mesg = "Invalid compute ID '"; + mesg += (word+2); + mesg += "' in variable formula"; + print_var_error(FLERR,mesg.c_str(),ivar); + } Compute *compute = modify->compute[icompute]; // parse zero or one or two trailing brackets @@ -1604,9 +1609,10 @@ double Variable::evaluate(char *str, Tree **tree, int ivar) int ifix = modify->find_fix(word+2); if (ifix < 0) { - char msg[128]; - snprintf(msg,128,"Invalid fix ID '%s' in variable formula",word+2); - print_var_error(FLERR,msg,ivar); + std::string mesg = "Invalid fix ID '"; + mesg += (word+2); + mesg += "' in variable formula"; + print_var_error(FLERR,mesg.c_str(),ivar); } Fix *fix = modify->fix[ifix]; @@ -3792,8 +3798,12 @@ int Variable::group_function(char *word, char *contents, Tree **tree, // group to operate on int igroup = group->find(args[0]); - if (igroup == -1) - print_var_error(FLERR,"Group ID in variable formula does not exist",ivar); + if (igroup == -1) { + std::string mesg = "Group ID '"; + mesg += args[0]; + mesg += "' in variable formula does not exist"; + print_var_error(FLERR,mesg.c_str(),ivar); + } // match word to group function @@ -4001,8 +4011,12 @@ int Variable::group_function(char *word, char *contents, Tree **tree, int Variable::region_function(char *id, int ivar) { int iregion = domain->find_region(id); - if (iregion == -1) - print_var_error(FLERR,"Region ID in variable formula does not exist",ivar); + if (iregion == -1) { + std::string mesg = "Region ID '"; + mesg += id; + mesg += "' in variable formula does not exist"; + print_var_error(FLERR,mesg.c_str(),ivar); + } // init region in case sub-regions have been deleted @@ -4080,9 +4094,10 @@ int Variable::special_function(char *word, char *contents, Tree **tree, int icompute = modify->find_compute(&args[0][2]); if (icompute < 0) { - char msg[128]; - snprintf(msg,128,"Invalid compute ID '%s' in variable formula",word+2); - print_var_error(FLERR,msg,ivar); + std::string mesg = "Invalid compute ID '"; + mesg += (args[0]+2); + mesg += "' in variable formula"; + print_var_error(FLERR,mesg.c_str(),ivar); } compute = modify->compute[icompute]; if (index == 0 && compute->vector_flag) { @@ -4123,13 +4138,20 @@ int Variable::special_function(char *word, char *contents, Tree **tree, } else index = 0; int ifix = modify->find_fix(&args[0][2]); - if (ifix < 0) - print_var_error(FLERR,"Invalid fix ID in variable formula",ivar); + if (ifix < 0) { + std::string mesg = "Invalid fix ID '"; + mesg += (args[0]+2); + mesg += "' in variable formula"; + print_var_error(FLERR,mesg.c_str(),ivar); + } fix = modify->fix[ifix]; if (index == 0 && fix->vector_flag) { - if (update->whichflag > 0 && update->ntimestep % fix->global_freq) - print_var_error(FLERR,"Fix in variable not computed at " - "compatible time",ivar); + if (update->whichflag > 0 && update->ntimestep % fix->global_freq) { + std::string mesg = "Fix with ID '"; + mesg += (args[0]+2); + mesg += "' in variable formula not computed at compatible time"; + print_var_error(FLERR,mesg.c_str(),ivar); + } nvec = fix->size_vector; nstride = 1; } else if (index && fix->array_flag) { @@ -4336,9 +4358,12 @@ int Variable::special_function(char *word, char *contents, Tree **tree, print_var_error(FLERR,"Invalid special function in variable formula",ivar); int ivar = find(args[0]); - if (ivar < 0) - print_var_error(FLERR,"Variable ID in " - "variable formula does not exist",ivar); + if (ivar < 0) { + std::string mesg = "Variable ID '"; + mesg += args[0]; + mesg += "' in variable formula does not exist"; + print_var_error(FLERR,mesg.c_str(),ivar); + } // SCALARFILE has single current value, read next one // save value in tree or on argstack