Browse Source

HPCC-11819 - Changes from code review
- Fixed Memory management issues
- Legacy code cleanup
- fixed a grammar issue regarding negative enum values
- Enhanced grammar to support enum syntax currently used by scapps
Signed-off-by: rpastrana <rodrigo.pastrana@lexisnexis.com>

rpastrana 11 years ago
parent
commit
69050a7517
3 changed files with 164 additions and 28 deletions
  1. 93 14
      tools/esdlcomp/esdlcomp.cpp
  2. 46 3
      tools/esdlcomp/esdlcomp.h
  3. 25 11
      tools/esdlcomp/esdlgram.y

+ 93 - 14
tools/esdlcomp/esdlcomp.cpp

@@ -45,8 +45,10 @@ extern int yyparse();
 //extern YY_BUFFER_STATE getCurrentBuffer();
 
 extern void yyrestart  (FILE * input_file );
-extern int yylex_destroy  (void);
+extern int  yylex_destroy  (void);
+//extern void yy_delete_buffer (YY_BUFFER_STATE  b );
 extern void yyInitESDLGlobals(ESDLcompiler * esdlcompiler);
+extern void yyCleanupESDLGlobals();
 
 extern ESDLcompiler * hcp;
 extern char *esp_def_export_tag;
@@ -418,12 +420,20 @@ ParamInfo::~ParamInfo()
         free(xsdtype);
     if (m_arrayImplType)
         delete m_arrayImplType;
+
     while (layouts)
     {
         LayoutInfo *l=layouts;
         layouts = l->next;
         delete l;
     }
+
+    while (tags)
+    {
+        MetaTagInfo *t=tags;
+        tags = t->next;
+        delete t;
+    }
 }
 
 char * ParamInfo::bytesize(int deref)
@@ -908,12 +918,15 @@ ApiInfo::ApiInfo(const char *n)
 
 ApiInfo::~ApiInfo()
 {
-    if (name)
-        free(name);
-    if (proc)
-        delete proc;
-    if (group)
-        free(group);
+    free (group);
+    free (name);
+
+    while (proc)
+    {
+        ProcInfo *pr=proc;
+        proc = pr->next;
+        delete pr;
+    }
 }
 
 //-------------------------------------------------------------------------------------------------------------
@@ -931,13 +944,15 @@ ModuleInfo::ModuleInfo(const char *n)
 
 ModuleInfo::~ModuleInfo()
 {
-    free(name);
     while (procs)
     {
-        ProcInfo *p=procs;
-        procs = p->next;
-        delete p;
+       ProcInfo *proc=procs;
+       procs = proc->next;
+       delete proc;
     }
+
+    free (name);
+    free (base);
 }
 
 //-------------------------------------------------------------------------------------------------------------
@@ -1125,6 +1140,71 @@ ESDLcompiler::~ESDLcompiler()
 {
     free(packagename);
     free(filename);
+
+    while (modules)
+    {
+        ModuleInfo *mo=modules;
+        modules = mo->next;
+        delete mo;
+    }
+
+    while (enums)
+    {
+        EnumInfo *en=enums;
+        enums = en->next;
+        delete en;
+    }
+
+    while (apis)
+    {
+        ApiInfo *api=apis;
+        apis = api->next;
+        delete api;
+    }
+
+    while (servs)
+    {
+        EspServInfo *ser=servs;
+        servs = ser->next;
+        delete ser;
+    }
+
+
+
+    while (includes)
+    {
+        IncludeInfo *in=includes;
+        includes = in->next;
+        delete in;
+    }
+
+    while (methods)
+    {
+        EspMethodInfo *meth=methods;
+        methods = meth->next;
+        delete meth;
+    }
+
+    while (versions)
+    {
+        VersionInfo *mes=versions;
+        versions = mes->next;
+        delete mes;
+    }
+
+    while (msgs)
+    {
+        EspMessageInfo *mi=msgs;
+        msgs = mi->next;
+        delete mi;
+    }
+
+    while (includes)
+    {
+        IncludeInfo *in=includes;
+        includes = in->next;
+        delete in;
+    }
 }
 
 void ESDLcompiler::Process()
@@ -1143,9 +1223,11 @@ void ESDLcompiler::Process()
     }
     write_esxdl();
 
+   // yy_delete_buffer (getCurrentBuffer() );
     fclose(yyin);
     close(esxdlo);
 
+    yyCleanupESDLGlobals();
     yylex_destroy();
 }
 
@@ -1193,8 +1275,5 @@ void ESDLcompiler::write_esxdl()
     gOutfile = -1;
 }
 
-//-------------------------------------------------------------------------------------------------------------
-// class EnumInfo
-
 // end
 //-------------------------------------------------------------------------------------------------------------

+ 46 - 3
tools/esdlcomp/esdlcomp.h

@@ -258,6 +258,7 @@ public:
 
     void release()
     {
+
         if (mttype_==mt_string && str_val_!=NULL)
             free(str_val_);
 
@@ -623,6 +624,7 @@ public:
     ModuleInfo(const char *n);
     ~ModuleInfo();
 
+
     char        *name;
     char        *base;
     int          version;
@@ -658,10 +660,11 @@ public:
     ApiInfo(const char *grp="");
     ~ApiInfo();
 
+
    void write_header_method();
    void write_clarion_include_method();
 
-    char            *group;
+    char        *group;
     char        *name;
     ProcInfo    *proc;
     ApiInfo  *next;
@@ -687,13 +690,13 @@ public:
 class VersionInfo
 {
 public:
-    //VersionInfo(const char *version_name_, double version_value_) : version_name(version_name_), version_value(version_value_)
     VersionInfo(const char *version_name_, double version_value_)
     {
         version_name.append(version_name_);
         version_value = version_value_;
         next=NULL;
     };
+
     ~VersionInfo(){}
 
    void write_esxdl()
@@ -740,10 +743,16 @@ public:
     ~EnumInfo()
     {
         free(name);
+        while (vals)
+        {
+            EnumValInfo *va=vals;
+            vals = va->next;
+            delete va;
+        }
     }
 
     char             *name;
-    EnumValInfo  *vals;
+    EnumValInfo      *vals;
     EnumInfo         *next;
 };
 
@@ -935,6 +944,28 @@ public:
     MetaTagInfo     *tags;
     EspMessageInfo  *next;
 
+    ~EspMessageInfo()
+    {
+        while (attrs_)
+        {
+            ParamInfo *attr=attrs_;
+            attrs_ = attr->next;
+            delete attr;
+        }
+
+        while (tags)
+        {
+            MetaTagInfo *attr=tags;
+            tags = attr->next;
+            delete attr;
+        }
+        delete tags;
+        free (name_);
+        free (base_);
+        free (parent);
+        free (xsdgrouptype);
+    }
+
 private:
     ParamInfo       *attrs_;
     espm_type       espm_type_;
@@ -981,6 +1012,18 @@ public:
         next=NULL;
     }
 
+    ~EspMethodInfo()
+    {
+        fprintf(stderr, "~EspMethodInfo(%s)\n", name_);
+
+        while (tags)
+        {
+            MetaTagInfo *p=tags;
+            tags = p->next;
+            delete p;
+        }
+    }
+
     const char *getName(){return name_;}
     void setName(const char *name)
     {

+ 25 - 11
tools/esdlcomp/esdlgram.y

@@ -361,6 +361,10 @@ EnumList
  ;
 
 EnumItemDef
+ : EspMetaData EnumItemBare
+ ;
+
+EnumItemBare
  : ID '(' EnumConstValue ')'
  {
     CurParam = new ParamInfo;
@@ -830,7 +834,7 @@ EnumDef
  }
  | ID '=' '-' INTEGER_CONST
  {
-    EnumValue = - $3.getInt();
+    EnumValue = - $4.getInt();
     CurEnumVal = new EnumValInfo($1.getName(),EnumValue);
     EnumValue++;
  }
@@ -876,6 +880,12 @@ EspVersionDef
     hcp->versions = new VersionInfo($3.getName(), $5.getDouble());
     hcp->versions->next = prev;
  }
+ | ESPVERSIONDEF '(' ID ',' INTEGER_CONST ')' ';'
+ {
+    VersionInfo *prev = hcp->versions;
+    hcp->versions = new VersionInfo($3.getName(), $5.getInt());
+    hcp->versions->next = prev;
+ }
  ;
 
 ApiStart
@@ -1574,24 +1584,24 @@ void AddEnum()
    }
 }
 
-void yyInitESDLGlobals(ESDLcompiler * esdlcompiler)
+void yyCleanupESDLGlobals()
 {
-    hcp = esdlcompiler;
+//These global pointers, have been assigned to the esdlcompiler 'hcp' member vars
+//The compiler is responsible for releasing memory allocated.
+
+    hcp = NULL;
     CurModule=NULL;
     CurProc=NULL;
     CurParam=NULL;
     CurLayout=NULL;
     CurEnum=NULL;
     CurEnumVal=NULL;
-    EnumValue = 0;
     CurApi=NULL;
-    CurInclude=NULL;
-
+    CurInclude = NULL;
     CurEspMessage=NULL;
     CurService=NULL;
     CurMethod=NULL;
     CurMetaTags=NULL;
-
     LastModule=NULL;
     LastProc=NULL;
     LastParam=NULL;
@@ -1600,19 +1610,23 @@ void yyInitESDLGlobals(ESDLcompiler * esdlcompiler)
     LastEnumVal=NULL;
     LastApi=NULL;
     LastInclude=NULL;
-
     LastEspMessage=NULL;
     LastService=NULL;
     LastMethod=NULL;
+}
+
+void yyInitESDLGlobals(ESDLcompiler * esdlcompiler)
+{
+    hcp = esdlcompiler;
 
+    EnumValue = 0;
     esp_def_export_tag=NULL;
 
-    unsigned linenum=1;
-    unsigned errnum=0;
+    linenum=1;
+    errnum=0;
     nCommentStartLine = -1;
 }
 
-
 extern char *yytext;
 void yyerror(const char *s)
 {