Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
authormjsousa
Thu, 30 Apr 2015 20:54:04 +0100
changeset 1000 556b74055518
parent 999 dd50a82ae8da
child 1001 f6ed7dcfc53e
Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
absyntax/absyntax.hh
stage3/Makefile.am
stage3/case_elements_check.cc
stage3/case_elements_check.hh
stage3/stage3.cc
--- a/absyntax/absyntax.hh	Wed Apr 15 23:25:07 2015 +0100
+++ b/absyntax/absyntax.hh	Thu Apr 30 20:54:04 2015 +0100
@@ -148,6 +148,10 @@
     /* comparison operator */
     bool operator==(const const_value_c cv)
       {return ((_int64==cv._int64) && (_uint64==cv._uint64) && (_real64==cv._real64) && (_bool==cv._bool));}                                                     
+      
+    /* return true if at least one of the const values (int, real, ...) is a valid const value */
+    bool is_const(void) 
+      {return (_int64.is_valid() || _uint64.is_valid() || _real64.is_valid() || _bool.is_valid());}   
 };
 
 
--- a/stage3/Makefile.am	Wed Apr 15 23:25:07 2015 +0100
+++ b/stage3/Makefile.am	Thu Apr 30 20:54:04 2015 +0100
@@ -12,6 +12,7 @@
 	datatype_functions.cc \
 	lvalue_check.cc \
 	array_range_check.cc \
+	case_elements_check.cc \
         constant_folding.cc \
         declaration_check.cc \
         enum_declaration_check.cc \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage3/case_elements_check.cc	Thu Apr 30 20:54:04 2015 +0100
@@ -0,0 +1,249 @@
+/*
+ *  matiec - a compiler for the programming languages defined in IEC 61131-3
+ *
+ *  Copyright (C) 2015  Mario de Sousa (msousa@fe.up.pt)
+ *
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * This code is made available on the understanding that it will not be
+ * used in safety-critical situations without a full and competent review.
+ */
+
+/*
+ * An IEC 61131-3 compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+
+/*
+ * Case Options Checking:
+ *   - Check whether the options in a case statement are repeated, either directly, or in a range.
+ *       For example:
+ *         case var of
+ *           1: ...   <- OK
+ *           2: ...   <- OK
+ *           1: ...   <- OK (not an error), but produce a warning due to repeated '1'!
+ *           0..8: ...<- OK (not an error), but produce a warning cue to repeated '1' and '2'!
+ */
+
+
+#include "case_elements_check.hh"
+
+
+#define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order)   ? (symbol1) : (symbol2))
+#define  LAST_(symbol1, symbol2) (((symbol1)->last_order  > (symbol2)->last_order)    ? (symbol1) : (symbol2))
+
+#define STAGE3_ERROR(error_level, symbol1, symbol2, ...) {                                                                  \
+  if (current_display_error_level >= error_level) {                                                                         \
+    fprintf(stderr, "%s:%d-%d..%d-%d: error: ",                                                                             \
+            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
+                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
+    fprintf(stderr, __VA_ARGS__);                                                                                           \
+    fprintf(stderr, "\n");                                                                                                  \
+    error_count++;                                                                                                     \
+  }                                                                                                                         \
+}
+
+
+#define STAGE3_WARNING(symbol1, symbol2, ...) {                                                                             \
+    fprintf(stderr, "%s:%d-%d..%d-%d: warning: ",                                                                           \
+            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
+                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
+    fprintf(stderr, __VA_ARGS__);                                                                                           \
+    fprintf(stderr, "\n");                                                                                                  \
+    warning_found = true;                                                                                                   \
+}
+
+
+#define GET_CVALUE(dtype, symbol)             ((symbol)->const_value._##dtype.get())
+#define VALID_CVALUE(dtype, symbol)           ((symbol)->const_value._##dtype.is_valid())
+
+
+
+
+case_elements_check_c::case_elements_check_c(symbol_c *ignore) {
+  warning_found = false;
+  error_count = 0;
+  current_display_error_level = 0;
+}
+
+
+
+case_elements_check_c::~case_elements_check_c(void) {
+}
+
+
+
+int case_elements_check_c::get_error_count() {
+  return error_count;
+}
+
+
+
+
+/* compare two integer constants, and determins if s1 < s2 */
+static bool less_than(symbol_c *s1, symbol_c *s2) {
+  if (   (VALID_CVALUE( int64, s1))
+      && (VALID_CVALUE( int64, s2))
+      && (  GET_CVALUE( int64, s1) < GET_CVALUE( int64, s2)))
+    return true;
+  
+  if (   (VALID_CVALUE(uint64, s1))
+      && (VALID_CVALUE(uint64, s2))
+      && (  GET_CVALUE(uint64, s1) < GET_CVALUE(uint64, s2)))
+    return true;
+  
+  if (   (VALID_CVALUE( int64, s1))
+      && (VALID_CVALUE(uint64, s2))
+      && (  GET_CVALUE( int64, s1) < 0))
+    return true;
+  
+  return false;
+}
+
+
+
+void case_elements_check_c::check_subr_subr(symbol_c *s1, symbol_c *s2) {
+  subrange_c *sub1 = dynamic_cast<subrange_c *>(s1);
+  subrange_c *sub2 = dynamic_cast<subrange_c *>(s2);
+  
+  if ((NULL == sub1) || (NULL == sub2)) return;
+  symbol_c *l1 = sub1->lower_limit;
+  symbol_c *u1 = sub1->upper_limit;
+  symbol_c *l2 = sub2->lower_limit;
+  symbol_c *u2 = sub2->upper_limit;
+  
+  if (less_than(u1, l2))  return; // no overlap!
+  if (less_than(u2, l1))  return; // no overlap!
+
+  if (   (VALID_CVALUE( int64, l1) || (VALID_CVALUE(uint64, l1)))
+      && (VALID_CVALUE( int64, l2) || (VALID_CVALUE(uint64, l2)))
+      && (VALID_CVALUE( int64, u1) || (VALID_CVALUE(uint64, u1)))
+      && (VALID_CVALUE( int64, u2) || (VALID_CVALUE(uint64, u2))))
+    STAGE3_WARNING(s1, s2, "Elements in CASE options have overlapping ranges.");
+}
+
+
+
+
+void case_elements_check_c::check_subr_symb(symbol_c *s1, symbol_c *s2) {
+  subrange_c *subr = NULL;
+  symbol_c   *symb = NULL;
+  if ((subr = dynamic_cast<subrange_c *>(s1)) != NULL) {symb = s2;}
+  if ((subr = dynamic_cast<subrange_c *>(s2)) != NULL) {symb = s1;}
+  
+  if ((NULL == subr) || (NULL == symb)) return;
+  symbol_c   *lowl = subr->lower_limit;
+  symbol_c   *uppl = subr->upper_limit;
+  
+  if (   (VALID_CVALUE(int64, symb))
+      && (VALID_CVALUE(int64, lowl))
+      && (VALID_CVALUE(int64, uppl))
+      && (  GET_CVALUE(int64, symb) >= GET_CVALUE(int64, lowl))
+      && (  GET_CVALUE(int64, symb) <= GET_CVALUE(int64, uppl)))
+    {STAGE3_WARNING(s1, s2, "Element in CASE option falls within range of another element."); return;}
+    
+  if (   (VALID_CVALUE(uint64, symb))
+      && (VALID_CVALUE( int64, lowl))
+      && (VALID_CVALUE(uint64, uppl))
+      && (  GET_CVALUE( int64, lowl) < 0)
+      && (  GET_CVALUE(uint64, symb) <= GET_CVALUE(uint64, uppl)))
+    {STAGE3_WARNING(s1, s2, "Element in CASE option falls within range of another element."); return;}
+
+  if (   (VALID_CVALUE(uint64, symb))
+      && (VALID_CVALUE(uint64, lowl))
+      && (VALID_CVALUE(uint64, uppl))
+      && (  GET_CVALUE(uint64, symb) >= GET_CVALUE(uint64, lowl))
+      && (  GET_CVALUE(uint64, symb) <= GET_CVALUE(uint64, uppl)))
+    {STAGE3_WARNING(s1, s2, "Element in CASE option falls within range of another element."); return;}
+}
+
+
+
+
+#include <typeinfo>
+void case_elements_check_c::check_symb_symb(symbol_c *s1, symbol_c *s2) {
+  if (   (dynamic_cast<subrange_c *>(s1) != NULL)
+      || (dynamic_cast<subrange_c *>(s2) != NULL)) 
+    return; // only run this test if neither s1 nor s2 are subranges!
+  
+  if (   (s1->const_value.is_const() && s2->const_value.is_const() && (s1->const_value == s2->const_value))  // if const, then compare const values (using overloaded '==' operator!)
+      || (compare_identifiers(s1, s2) == 0))  // if token_c, compare tokens! (compare_identifiers() returns 0 when equal tokens!, -1 when either is not token_c)
+    STAGE3_WARNING(s1, s2, "Duplicate element found in CASE options.");
+}
+
+
+
+
+
+
+
+
+
+/***************************************/
+/* B.3 - Language ST (Structured Text) */
+/***************************************/
+/********************/
+/* B 3.2 Statements */
+/********************/
+/********************************/
+/* B 3.2.3 Selection Statements */
+/********************************/
+/* CASE expression OF case_element_list ELSE statement_list END_CASE */
+// SYM_REF3(case_statement_c, expression, case_element_list, statement_list)
+void *case_elements_check_c::visit(case_statement_c *symbol) {
+  std::vector<symbol_c *> case_elements_list_local = case_elements_list; // Required when source code contains CASE inside another CASE !
+
+  case_elements_list.clear();
+  symbol->case_element_list->accept(*this); // will fill up the case_elements_list with all the elements in the case!
+  
+  // OK, now check whether we have any overlappings...
+  std::vector<symbol_c *>::iterator s1 = case_elements_list.begin();
+  for (  ; s1 != case_elements_list.end(); s1++) {
+    std::vector<symbol_c *>::iterator s2 = s1;
+    s2++; // do not compare the value with itself!
+    for (; s2 != case_elements_list.end(); s2++) {
+      // Check for overlapping elements
+      check_subr_subr(*s1, *s2);
+      check_subr_symb(*s1, *s2);
+      check_symb_symb(*s2, *s1);
+    }
+  }
+  
+  case_elements_list = case_elements_list_local;
+  return NULL;
+}
+
+/* helper symbol for case_statement */
+// SYM_LIST(case_element_list_c)
+// void *case_elements_check_c::visit(case_element_list_c *symbol) // not needed! We inherit from iterator_visitor_c
+
+/*  case_list ':' statement_list */
+// SYM_REF2(case_element_c, case_list, statement_list)
+// void *case_elements_check_c::visit(case_element_c *symbol) // not needed! We inherit from iterator_visitor_c
+
+
+// SYM_LIST(case_list_c)
+void *case_elements_check_c::visit(case_list_c *symbol) {
+  for (int i = 0; i < symbol->n; i++)
+    case_elements_list.push_back(symbol->elements[i]);
+  return NULL;
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage3/case_elements_check.hh	Thu Apr 30 20:54:04 2015 +0100
@@ -0,0 +1,85 @@
+/*
+ *  matiec - a compiler for the programming languages defined in IEC 61131-3
+ *
+ *  Copyright (C) 2015  Mario de Sousa (msousa@fe.up.pt)
+ *
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * This code is made available on the understanding that it will not be
+ * used in safety-critical situations without a full and competent review.
+ */
+
+/*
+ * An IEC 61131-3 compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+
+/*
+ * Case Options Checking:
+ *   - Check whether the options in a case statement are repeated, either directly, or in a range.
+ *       For example:
+ *         case var of
+ *           1: ...   <- OK
+ *           2: ...   <- OK
+ *           1: ...   <- OK (not an error), but produce a warning!
+ *           0..8: ...<- OK (not an error), but produce a warning!
+ */
+
+#include "../absyntax_utils/absyntax_utils.hh"
+
+
+
+class case_elements_check_c: public iterator_visitor_c {
+
+  private:
+    bool warning_found;
+    int error_count;
+    int current_display_error_level;
+
+    std::vector<symbol_c *> case_elements_list;
+    void check_subr_subr(symbol_c *s1, symbol_c *s2);
+    void check_subr_symb(symbol_c *s1, symbol_c *s2);
+    void check_symb_symb(symbol_c *s1, symbol_c *s2);
+  
+
+  public:
+    case_elements_check_c(symbol_c *ignore);
+    virtual ~case_elements_check_c(void);
+    int get_error_count();
+
+    /***************************************/
+    /* B.3 - Language ST (Structured Text) */
+    /***************************************/
+    /********************/
+    /* B 3.2 Statements */
+    /********************/
+    /********************************/
+    /* B 3.2.3 Selection Statements */
+    /********************************/
+    void *visit(case_statement_c *symbol);
+    void *visit(case_list_c      *symbol);
+}; /* case_elements_check_c */
+
+
+
+
+
+
+
--- a/stage3/stage3.cc	Wed Apr 15 23:25:07 2015 +0100
+++ b/stage3/stage3.cc	Thu Apr 30 20:54:04 2015 +0100
@@ -41,6 +41,7 @@
 #include "print_datatypes_error.hh"
 #include "lvalue_check.hh"
 #include "array_range_check.hh"
+#include "case_elements_check.hh"
 #include "constant_folding.hh"
 #include "declaration_check.hh"
 #include "enum_declaration_check.hh"
@@ -126,6 +127,15 @@
 }
 
 
+/* Case options check assumes that constant folding has been completed!
+ * so be sure to call constant_folding() before calling this function!
+ */
+static int case_elements_check(symbol_c *tree_root){
+	case_elements_check_c case_elements_check(tree_root);
+	tree_root->accept(case_elements_check);
+	return case_elements_check.get_error_count();
+}
+
 
 /* Removing forward dependencies only makes sense when stage1_2 is run with the pre-parsing option.
  * This algorithm has no dependencies on other stage 3 algorithms.
@@ -154,6 +164,7 @@
 	error_count += type_safety(tree_root);
 	error_count += lvalue_check(tree_root);
 	error_count += array_range_check(tree_root);
+	error_count += case_elements_check(tree_root);
 	error_count += remove_forward_dependencies(tree_root, ordered_tree_root);
 	
 	if (error_count > 0) {