Skip to content

Commit 7e12d19

Browse files
committed
Fixup: Rework mangle_name
1 parent e04b887 commit 7e12d19

File tree

2 files changed

+49
-67
lines changed

2 files changed

+49
-67
lines changed

src/tools/registry/gen_inc.c

+48-63
Original file line numberDiff line numberDiff line change
@@ -700,16 +700,12 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
700700
ezxml_t nmlrecs_xml, nmlopt_xml;
701701

702702
const char *const_core;
703-
const char *nmlrecname, *nmlrecindef, *nmlrecinsub;
704-
const char *nmloptname, *nmlopttype, *nmloptval, *nmloptunits, *nmloptdesc, *nmloptposvals, *nmloptindef;
703+
const char *original_nmlrecname, *nmlrecindef, *nmlrecinsub;
704+
const char *original_nmloptname, *nmlopttype, *nmloptval, *nmloptunits, *nmloptdesc, *nmloptposvals, *nmloptindef;
705705

706-
#ifdef MPAS_CAM_DYCORE
707706
// Fortran variable names have a length limit of 63 characters. + 1 for the terminating null character.
708-
char new_nmlrecname[64];
709-
char new_nmloptname[64];
710-
const char *old_nmlrecname;
711-
const char *old_nmloptname;
712-
#endif
707+
char nmlrecname[64];
708+
char nmloptname[64];
713709

714710
char pool_name[1024];
715711
char core_string[1024];
@@ -755,14 +751,9 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
755751

756752
// Parse Namelist Records
757753
for (nmlrecs_xml = ezxml_child(registry, "nml_record"); nmlrecs_xml; nmlrecs_xml = nmlrecs_xml->next){
758-
#ifdef MPAS_CAM_DYCORE
759-
old_nmlrecname = ezxml_attr(nmlrecs_xml, "name");
760-
transform_name(new_nmlrecname, sizeof(new_nmlrecname), old_nmlrecname);
754+
original_nmlrecname = ezxml_attr(nmlrecs_xml, "name");
755+
mangle_name(nmlrecname, sizeof(nmlrecname), original_nmlrecname);
761756

762-
nmlrecname = new_nmlrecname;
763-
#else
764-
nmlrecname = ezxml_attr(nmlrecs_xml, "name");
765-
#endif
766757
nmlrecindef = ezxml_attr(nmlrecs_xml, "in_defaults");
767758
nmlrecinsub = ezxml_attr(nmlrecs_xml, "in_subpool");
768759

@@ -796,14 +787,9 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
796787

797788
// Define variable definitions prior to reading the namelist in.
798789
for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){
799-
#ifdef MPAS_CAM_DYCORE
800-
old_nmloptname = ezxml_attr(nmlopt_xml, "name");
801-
transform_name(new_nmloptname, sizeof(new_nmloptname), old_nmloptname);
790+
original_nmloptname = ezxml_attr(nmlopt_xml, "name");
791+
mangle_name(nmloptname, sizeof(nmloptname), original_nmloptname);
802792

803-
nmloptname = new_nmloptname;
804-
#else
805-
nmloptname = ezxml_attr(nmlopt_xml, "name");
806-
#endif
807793
nmlopttype = ezxml_attr(nmlopt_xml, "type");
808794
nmloptval = ezxml_attr(nmlopt_xml, "default_value");
809795
nmloptunits = ezxml_attr(nmlopt_xml, "units");
@@ -835,14 +821,9 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
835821
// Define the namelist block, to read the namelist record in.
836822
fortprintf(fd, " namelist /%s/ &\n", nmlrecname);
837823
for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){
838-
#ifdef MPAS_CAM_DYCORE
839-
old_nmloptname = ezxml_attr(nmlopt_xml, "name");
840-
transform_name(new_nmloptname, sizeof(new_nmloptname), old_nmloptname);
824+
original_nmloptname = ezxml_attr(nmlopt_xml, "name");
825+
mangle_name(nmloptname, sizeof(nmloptname), original_nmloptname);
841826

842-
nmloptname = new_nmloptname;
843-
#else
844-
nmloptname = ezxml_attr(nmlopt_xml, "name");
845-
#endif
846827
if(nmlopt_xml->next){
847828
fortprintf(fd, " %s, &\n", nmloptname);
848829
} else {
@@ -873,14 +854,9 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
873854
// Define broadcast calls for namelist values.
874855
fortprintf(fd, " if (ierr <= 0) then\n");
875856
for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){
876-
#ifdef MPAS_CAM_DYCORE
877-
old_nmloptname = ezxml_attr(nmlopt_xml, "name");
878-
transform_name(new_nmloptname, sizeof(new_nmloptname), old_nmloptname);
857+
original_nmloptname = ezxml_attr(nmlopt_xml, "name");
858+
mangle_name(nmloptname, sizeof(nmloptname), original_nmloptname);
879859

880-
nmloptname = new_nmloptname;
881-
#else
882-
nmloptname = ezxml_attr(nmlopt_xml, "name");
883-
#endif
884860
nmlopttype = ezxml_attr(nmlopt_xml, "type");
885861

886862
if(strncmp(nmlopttype, "real", 1024) == 0){
@@ -898,14 +874,9 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
898874
fortprintf(fd, " call mpas_log_write(' The following values will be used for variables in this record:')\n");
899875
fortprintf(fd, " call mpas_log_write(' ')\n");
900876
for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){
901-
#ifdef MPAS_CAM_DYCORE
902-
old_nmloptname = ezxml_attr(nmlopt_xml, "name");
903-
transform_name(new_nmloptname, sizeof(new_nmloptname), old_nmloptname);
877+
original_nmloptname = ezxml_attr(nmlopt_xml, "name");
878+
mangle_name(nmloptname, sizeof(nmloptname), original_nmloptname);
904879

905-
nmloptname = new_nmloptname;
906-
#else
907-
nmloptname = ezxml_attr(nmlopt_xml, "name");
908-
#endif
909880
nmlopttype = ezxml_attr(nmlopt_xml, "type");
910881

911882
if (strncmp(nmlopttype, "character", 1024) == 0) {
@@ -932,21 +903,12 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
932903
fortprintf(fd, "\n");
933904

934905
for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){
935-
#ifdef MPAS_CAM_DYCORE
936-
old_nmloptname = ezxml_attr(nmlopt_xml, "name");
937-
transform_name(new_nmloptname, sizeof(new_nmloptname), old_nmloptname);
906+
original_nmloptname = ezxml_attr(nmlopt_xml, "name");
907+
mangle_name(nmloptname, sizeof(nmloptname), original_nmloptname);
938908

939-
nmloptname = new_nmloptname;
940-
941-
// Keep namelist options to their original names in MPAS pools for compatibility reasons.
942-
fortprintf(fd, " call mpas_pool_add_config(%s, '%s', %s)\n", pool_name, old_nmloptname, nmloptname);
943-
fortprintf(fcg, " call mpas_pool_get_config(configPool, '%s', %s)\n", old_nmloptname, nmloptname);
944-
#else
945-
nmloptname = ezxml_attr(nmlopt_xml, "name");
946-
947-
fortprintf(fd, " call mpas_pool_add_config(%s, '%s', %s)\n", pool_name, nmloptname, nmloptname);
948-
fortprintf(fcg, " call mpas_pool_get_config(configPool, '%s', %s)\n", nmloptname, nmloptname);
949-
#endif
909+
// Always keep namelist options to their original names in MPAS pools for compatibility reasons.
910+
fortprintf(fd, " call mpas_pool_add_config(%s, '%s', %s)\n", pool_name, original_nmloptname, nmloptname);
911+
fortprintf(fcg, " call mpas_pool_get_config(configPool, '%s', %s)\n", original_nmloptname, nmloptname);
950912
}
951913
fortprintf(fd, "\n");
952914
fortprintf(fcg, "\n");
@@ -2592,15 +2554,36 @@ int parse_structs_from_registry(ezxml_t registry)/*{{{*/
25922554
}/*}}}*/
25932555

25942556

2595-
#ifdef MPAS_CAM_DYCORE
2596-
// Perform transformations for namelist group and option names.
2597-
void transform_name(char *new_name, const size_t new_name_size, const char *old_name)
2557+
/**
2558+
* mangle_name
2559+
*
2560+
* Perform name mangling for MPAS namelist groups and options, as appropriate, depending on the containing
2561+
* host model.
2562+
*
2563+
* When MPAS is used as a dynamical core in a host model (e.g., CAM/CAM-SIMA), it needs to share
2564+
* the namelist file with other model components. As a result, MPAS namelist groups and options may not
2565+
* be easily recognizable at first sight. With the `MPAS_CAM_DYCORE` macro being defined, this function
2566+
* adds a unique identifier to each MPAS namelist group and option name by performing the following
2567+
* transformations:
2568+
*
2569+
* 1. Leading "config_" is removed recursively from the name. Case-insensitive.
2570+
* 2. Leading "mpas_" is removed recursively from the name. Case-insensitive.
2571+
* 3. Prepend "mpas_" to the name.
2572+
*
2573+
* By doing so, it is now easier to distinguish MPAS namelist groups and options from host model ones.
2574+
* The possibility of name collisions with host model ones is also resolved once and for all.
2575+
*
2576+
* For stand-alone MPAS, where the `MPAS_CAM_DYCORE` macro is not defined, this function just returns
2577+
* the name as is.
2578+
*/
2579+
void mangle_name(char *new_name, const size_t new_name_size, const char *old_name)
25982580
{
2581+
if (!new_name || !old_name || new_name_size == 0) return;
2582+
2583+
#ifdef MPAS_CAM_DYCORE
25992584
const char *const new_prefix = "mpas_";
26002585
const char *const old_prefix = "config_";
26012586

2602-
if (!new_name || !old_name || new_name_size == 0) return;
2603-
26042587
// Remove all leading whitespaces by moving pointer forward.
26052588
while (*old_name != '\0' && isspace((unsigned char) *old_name)) old_name++;
26062589

@@ -2616,5 +2599,7 @@ void transform_name(char *new_name, const size_t new_name_size, const char *old_
26162599
// Remove all trailing whitespaces by zeroing (nulling) out.
26172600
new_name += strlen(new_name) - 1;
26182601
while (*new_name != '\0' && isspace((unsigned char) *new_name)) *new_name-- = '\0';
2619-
}
2602+
#else
2603+
snprintf(new_name, new_name_size, "%s", old_name);
26202604
#endif
2605+
}

src/tools/registry/gen_inc.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,4 @@ int push_attributes(ezxml_t currentPosition);
3838
int merge_structs_and_var_arrays(ezxml_t currentPosition);
3939
int merge_streams(ezxml_t registry);
4040
int parse_structs_from_registry(ezxml_t registry);
41-
42-
#ifdef MPAS_CAM_DYCORE
43-
void transform_name(char *new_name, const size_t new_name_size, const char *old_name);
44-
#endif
41+
void mangle_name(char *new_name, const size_t new_name_size, const char *old_name);

0 commit comments

Comments
 (0)