[Avida-SVN] r2405 - development/source/script

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Sat Mar 1 09:01:27 PST 2008


Author: brysonda
Date: 2008-03-01 12:01:27 -0500 (Sat, 01 Mar 2008)
New Revision: 2405

Modified:
   development/source/script/AvidaScript.h
   development/source/script/cSemanticASTVisitor.cc
   development/source/script/cSemanticASTVisitor.h
   development/source/script/cSymbolTable.cc
   development/source/script/cSymbolTable.h
Log:
AS: Unreachable code warning.  No return from non-void function warning.

Modified: development/source/script/AvidaScript.h
===================================================================
--- development/source/script/AvidaScript.h	2008-03-01 03:31:14 UTC (rev 2404)
+++ development/source/script/AvidaScript.h	2008-03-01 17:01:27 UTC (rev 2405)
@@ -113,6 +113,7 @@
 typedef enum eASSemanticErrors {
   AS_SEMANTIC_WARN_LOSS_OF_PRECISION,
   AS_SEMANTIC_WARN_NO_DIMENSIONS,
+  AS_SEMANTIC_WARN_NO_RETURN,
   AS_SEMANTIC_WARN_UNREACHABLE,
   AS_SEMANTIC_WARN__LAST,
   

Modified: development/source/script/cSemanticASTVisitor.cc
===================================================================
--- development/source/script/cSemanticASTVisitor.cc	2008-03-01 03:31:14 UTC (rev 2404)
+++ development/source/script/cSemanticASTVisitor.cc	2008-03-01 17:01:27 UTC (rev 2405)
@@ -92,7 +92,7 @@
 {
   node.GetExpression()->Accept(*this);
   checkCast(m_parent_scope->GetFunctionRType(m_fun_id), node.GetExpression()->GetType());
-  // @TODO - mark scope as containing return
+  m_cur_symtbl->SetScopeReturn();
 }
 
 
@@ -100,10 +100,17 @@
 {
   tListIterator<cASTNode> it = node.Iterator();
   
+  bool has_return = false;
+  bool warn_unreach = false;
   cASTNode* stmt = NULL;
   while ((stmt = it.Next())) {
+    m_fun_def = false;
     stmt->Accept(*this);
-    // @TODO - check for unreachable statements
+    if (!has_return && m_cur_symtbl->ScopeHasReturn()) has_return = true;
+    else if (has_return && !m_fun_def && !warn_unreach) {
+      reportError(AS_SEMANTIC_WARN_UNREACHABLE, stmt->GetFilePosition(),  __LINE__);
+      warn_unreach = true;
+    }
   }
 }
 
@@ -262,6 +269,8 @@
     if (!m_parent_scope->GetFunctionDefinition(fun_id)) {
       node.GetCode()->Accept(*this);
       
+      if (node.GetType() != TYPE(VOID) && !m_cur_symtbl->ScopeHasReturn()) SEMANTIC_WARNING(NO_RETURN);
+      
       // Move the code to the symbol table entry
       m_parent_scope->SetFunctionDefinition(fun_id, node.GetCode());
       node.SetCode(NULL);
@@ -279,6 +288,8 @@
   m_cur_symtbl = m_parent_scope;
   m_parent_scope = prev.parent_scope;
   m_fun_id = prev.fun_id;
+  
+  m_fun_def = true;
 }
 
 
@@ -686,6 +697,9 @@
     case AS_SEMANTIC_WARN_NO_DIMENSIONS:
       std::cerr << "no dimensions specified" << ERR_ENDL;
       break;
+    case AS_SEMANTIC_WARN_NO_RETURN:
+      std::cerr << "control reaches the end of non-void function" << ERR_ENDL;
+      break;
     case AS_SEMANTIC_WARN_UNREACHABLE:
       std::cerr << "unreachable statement(s)" << ERR_ENDL;
       break;

Modified: development/source/script/cSemanticASTVisitor.h
===================================================================
--- development/source/script/cSemanticASTVisitor.h	2008-03-01 03:31:14 UTC (rev 2404)
+++ development/source/script/cSemanticASTVisitor.h	2008-03-01 17:01:27 UTC (rev 2405)
@@ -57,6 +57,7 @@
 
   
   bool m_success;
+  bool m_fun_def;
 
   
   cSemanticASTVisitor(); // @not_implemented

Modified: development/source/script/cSymbolTable.cc
===================================================================
--- development/source/script/cSymbolTable.cc	2008-03-01 03:31:14 UTC (rev 2404)
+++ development/source/script/cSymbolTable.cc	2008-03-01 17:01:27 UTC (rev 2405)
@@ -73,6 +73,7 @@
     }
   }
 
+  m_return = false;
   m_scope--;
 }
 

Modified: development/source/script/cSymbolTable.h
===================================================================
--- development/source/script/cSymbolTable.h	2008-03-01 03:31:14 UTC (rev 2404)
+++ development/source/script/cSymbolTable.h	2008-03-01 17:01:27 UTC (rev 2405)
@@ -73,13 +73,15 @@
   int m_scope;
   int m_deactivate_cycle;
   
+  bool m_return;
   
+  
   cSymbolTable(const cSymbolTable&); // @not_implemented
   cSymbolTable& operator=(const cSymbolTable&); // @not_implemented
   
   
 public:
-  cSymbolTable() : m_scope(0), m_deactivate_cycle(0) { ; }
+  cSymbolTable() : m_scope(0), m_deactivate_cycle(0), m_return(false) { ; }
   ~cSymbolTable();
 
   
@@ -96,6 +98,9 @@
   void PopScope();
   inline int GetScope() const { return m_scope; }
   
+  inline void SetScopeReturn() { m_return = true; }
+  inline bool ScopeHasReturn() const { return m_return; }
+  
   inline ASType_t GetVariableType(int var_id) const { return m_sym_tbl[var_id]->type; }
 
   inline const cString& GetFunctionName(int fun_id) const { return m_fun_tbl[fun_id]->name; }




More information about the Avida-cvs mailing list