Handle buggy source code gracefully (do not bork when non-array variable is used as an array. e.g: int_var[42]:= 33)
authormjsousa
Sat, 10 Aug 2013 09:10:06 +0100
changeset 830 6f45ec6ed011
parent 829 6e39eea5f5d0
child 831 691e1cc38d3a
Handle buggy source code gracefully (do not bork when non-array variable is used as an array. e.g: int_var[42]:= 33)
absyntax_utils/array_dimension_iterator.cc
absyntax_utils/search_varfb_instance_type.cc
--- a/absyntax_utils/array_dimension_iterator.cc	Wed Aug 07 10:34:57 2013 +0100
+++ b/absyntax_utils/array_dimension_iterator.cc	Sat Aug 10 09:10:06 2013 +0100
@@ -82,12 +82,17 @@
  */
 array_dimension_iterator_c::array_dimension_iterator_c(symbol_c *symbol) {
   /* do some consistency check... */
+  /* NOTE: We comment out the consistency check so the compiler does not bork when it encounters buggy source code.
+   *        e.g. Code that handles a non array variable as an array!
+   *               VAR  v1, v2: int; END_VAR
+   *               v1 := v2[33, 45];
+   *       The above error will be caught by the datatype checking algorithms!
+   */
   array_specification_c* array_spec = dynamic_cast<array_specification_c*>(symbol);
-
-  if (NULL == array_spec) ERROR;
+  // if (NULL == array_spec) ERROR;
 
   /* OK. Now initialize this object... */
-  this->array_specification = symbol;
+  this->array_specification = array_spec; // Set to array_spec and not symbol => will be NULL if not an array_specification_c* !!
   reset();
 }
 
@@ -101,9 +106,9 @@
  * Returns the subrange symbol!
  */
 subrange_c *array_dimension_iterator_c::next(void) {
+  if (NULL == array_specification) return NULL; /* The source code probably has a bug which will be caught somewhere else! */
   void *res = array_specification->accept(*this);
-  if (res == NULL) 
-    return NULL;
+  if (NULL == res)                 return NULL;
 
   return current_array_dimension;
 }
--- a/absyntax_utils/search_varfb_instance_type.cc	Wed Aug 07 10:34:57 2013 +0100
+++ b/absyntax_utils/search_varfb_instance_type.cc	Sat Aug 10 09:10:06 2013 +0100
@@ -209,7 +209,8 @@
  * structure_type_declaration_c, but for now, let's leave it in...
  */
 void *search_varfb_instance_type_c::visit(structure_type_declaration_c *symbol) {
-  if (NULL == current_field_selector) ERROR;
+  if (NULL == current_field_selector) return NULL; // the source code has a datatype consistency bug that will be caught later!!
+  
   symbol->structure_specification->accept(*this);
   return NULL;
   /* NOTE: structure_specification will point to either a
@@ -228,7 +229,7 @@
  * initialized_structure_c, but for now, let's leave it in...
  */
 void *search_varfb_instance_type_c::visit(initialized_structure_c *symbol)	{
-  if (NULL != current_field_selector) ERROR;
+  if (NULL == current_field_selector) return NULL; // the source code has a datatype consistency bug that will be caught later!!
   
   /* recursively find out the data type of current_field_selector... */
   symbol->structure_type_name->accept(*this);
@@ -239,7 +240,7 @@
 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
 /* structure_element_declaration_list structure_element_declaration ';' */
 void *search_varfb_instance_type_c::visit(structure_element_declaration_list_c *symbol)	{
-  if (NULL == current_field_selector) ERROR;
+  if (NULL == current_field_selector) return NULL; // the source code has a datatype consistency bug that will be caught later!!
 
   /* now search the structure declaration */
   for(int i = 0; i < symbol->n; i++) {
@@ -251,7 +252,7 @@
 
 /*  structure_element_name ':' spec_init */
 void *search_varfb_instance_type_c::visit(structure_element_declaration_c *symbol) {
-  if (NULL == current_field_selector) ERROR;
+  if (NULL == current_field_selector) return NULL; // the source code has a datatype consistency bug that will be caught later!!
 
   if (compare_identifiers(symbol->structure_element_name, current_field_selector) == 0) {
     /* found the type of the element we were looking for! */
@@ -357,7 +358,7 @@
 /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
 // SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
 void *search_varfb_instance_type_c::visit(function_block_declaration_c *symbol) {
-  if (NULL == current_field_selector) ERROR;
+  if (NULL == current_field_selector) return NULL; // the source code has a datatype consistency bug that will be caught later!!
 
   /* now search the function block declaration for the variable... */
   /* If not found, these pointers will all be set to NULL!! */
@@ -378,7 +379,7 @@
 // SYM_REF2(initial_step_c, step_name, action_association_list)
 /* NOTE: this method may be called from visit(structured_variable_c *symbol) method| */
 void *search_varfb_instance_type_c::visit(initial_step_c *symbol) {
-  if (NULL == current_field_selector) ERROR;
+  if (NULL == current_field_selector) return NULL; // the source code has a datatype consistency bug that will be caught later!!
 
   identifier_c T("T");
   identifier_c X("X");