diff --git a/Statements.cpp b/Statements.cpp index 065124c76..c2dca55ad 100644 --- a/Statements.cpp +++ b/Statements.cpp @@ -359,110 +359,116 @@ void CreateStatementList() if (Debug) { - std::list::const_iterator it; - for (it = Statements.begin(); it != Statements.end(); it++) + OutputStatementList( std::cout ); + } +} + + +void OutputStatementList( std::ostream &ostr ) +{ + std::list::const_iterator it; + for (it = Statements.begin(); it != Statements.end(); it++) + { + STATEMENT s = *it; + // ostr << it->Token->linenr << " : "; + switch (s.Type) { - STATEMENT s = *it; - std::cout << it->Token->linenr << " : "; - switch (s.Type) - { - case STATEMENT::OBRACE: - std::cout << "{"; - break; + case STATEMENT::OBRACE: + ostr << "{"; + break; - case STATEMENT::EBRACE: - std::cout << "}"; - break; + case STATEMENT::EBRACE: + ostr << "}"; + break; - case STATEMENT::DECL: - std::cout << "decl " << VariableNames[s.VarIndex]; - break; + case STATEMENT::DECL: + ostr << "decl " << VariableNames[s.VarIndex]; + break; - case STATEMENT::ASSIGN: - std::cout << "assign " << VariableNames[s.VarIndex]; - break; + case STATEMENT::ASSIGN: + ostr << "assign " << VariableNames[s.VarIndex]; + break; - case STATEMENT::MALLOC: - std::cout << "malloc " << VariableNames[s.VarIndex]; - break; + case STATEMENT::MALLOC: + ostr << "malloc " << VariableNames[s.VarIndex]; + break; - case STATEMENT::FREE: - std::cout << "free " << VariableNames[s.VarIndex]; - break; + case STATEMENT::FREE: + ostr << "free " << VariableNames[s.VarIndex]; + break; - case STATEMENT::NEW: - std::cout << "new " << VariableNames[s.VarIndex]; - break; + case STATEMENT::NEW: + ostr << "new " << VariableNames[s.VarIndex]; + break; - case STATEMENT::NEWARRAY: - std::cout << "new[] " << VariableNames[s.VarIndex]; - break; + case STATEMENT::NEWARRAY: + ostr << "new[] " << VariableNames[s.VarIndex]; + break; - case STATEMENT::DELETE: - std::cout << "delete " << VariableNames[s.VarIndex]; - break; + case STATEMENT::DELETE: + ostr << "delete " << VariableNames[s.VarIndex]; + break; - case STATEMENT::DELETEARRAY: - std::cout << "delete[] " << VariableNames[s.VarIndex]; - break; + case STATEMENT::DELETEARRAY: + ostr << "delete[] " << VariableNames[s.VarIndex]; + break; - case STATEMENT::USE: - std::cout << "use " << VariableNames[s.VarIndex]; - break; + case STATEMENT::USE: + ostr << "use " << VariableNames[s.VarIndex]; + break; - case STATEMENT::LOOP: - std::cout << "loop"; - break; + case STATEMENT::LOOP: + ostr << "loop"; + break; - case STATEMENT::ENDLOOP: - std::cout << "endloop"; - break; + case STATEMENT::ENDLOOP: + ostr << "endloop"; + break; - case STATEMENT::SWITCH: - std::cout << "switch"; - break; + case STATEMENT::SWITCH: + ostr << "switch"; + break; - case STATEMENT::ENDSWITCH: - std::cout << "endswitch"; - break; + case STATEMENT::ENDSWITCH: + ostr << "endswitch"; + break; - case STATEMENT::IF: - std::cout << "if"; - break; + case STATEMENT::IF: + ostr << "if"; + break; - case STATEMENT::ELSEIF: - std::cout << "elseif"; - break; + case STATEMENT::ELSEIF: + ostr << "elseif"; + break; - case STATEMENT::ELSE: - std::cout << "else"; - break; + case STATEMENT::ELSE: + ostr << "else"; + break; - case STATEMENT::ENDIF: - std::cout << "endif"; - break; + case STATEMENT::ENDIF: + ostr << "endif"; + break; - case STATEMENT::RETURN: - std::cout << "return " << VariableNames[s.VarIndex]; - break; + case STATEMENT::RETURN: + ostr << "return " << VariableNames[s.VarIndex]; + break; - case STATEMENT::CONTINUE: - std::cout << "continue"; - break; + case STATEMENT::CONTINUE: + ostr << "continue"; + break; - case STATEMENT::BREAK: - std::cout << "break"; - break; + case STATEMENT::BREAK: + ostr << "break"; + break; - default: - std::cout << "ERROR. Unknown code!!"; - break; - } - std::cout << "\n"; + default: + ostr << "ERROR. Unknown code!!"; + break; } + ostr << "\n"; } } //--------------------------------------------------------------------------- diff --git a/Statements.h b/Statements.h index 4a53a5151..f6e2bacbb 100644 --- a/Statements.h +++ b/Statements.h @@ -32,6 +32,8 @@ extern std::list Statements; void CreateStatementList(); +void OutputStatementList( std::ostream &ostr ); + //--------------------------------------------------------------------------- #endif //--------------------------------------------------------------------------- diff --git a/internaltesting/testassign.cpp b/internaltesting/testassign.cpp deleted file mode 100644 index e4096baa5..000000000 --- a/internaltesting/testassign.cpp +++ /dev/null @@ -1,11 +0,0 @@ - - -void f() -{ - a = 1; - b[2] = 3; - c[4][5].min = 6; - d.min = 7; -} - - diff --git a/internaltesting/testassign.out b/internaltesting/testassign.out deleted file mode 100644 index 9c3fcd9ff..000000000 --- a/internaltesting/testassign.out +++ /dev/null @@ -1,6 +0,0 @@ -4 : { -5 : assign a -6 : assign b[2] -7 : assign c[4][5].min -8 : assign d.min -9 : } diff --git a/internaltesting/testdecl.cpp b/internaltesting/testdecl.cpp deleted file mode 100644 index 02093a360..000000000 --- a/internaltesting/testdecl.cpp +++ /dev/null @@ -1,14 +0,0 @@ - - -void f() -{ - int a; - int b = 2, c, *d = NULL; - int e = g(p1,p2); - char str[10]; - - return a; - delete a; -} - - diff --git a/internaltesting/testdecl.out b/internaltesting/testdecl.out deleted file mode 100644 index c3a59f52d..000000000 --- a/internaltesting/testdecl.out +++ /dev/null @@ -1,16 +0,0 @@ -4 : { -5 : decl a -6 : decl b -6 : assign b -6 : decl c -6 : decl d -6 : assign d -6 : use NULL -7 : decl e -7 : assign e -7 : use p1 -7 : use p2 -8 : decl str -10 : return a -11 : delete a -12 : } diff --git a/internaltesting/testif.cpp b/internaltesting/testif.cpp deleted file mode 100644 index 1cc014f95..000000000 --- a/internaltesting/testif.cpp +++ /dev/null @@ -1,17 +0,0 @@ - - -void f() -{ - if (ab) - { - - } - else if (cd) - { - - } - else - { - - } -} \ No newline at end of file diff --git a/internaltesting/testif.out b/internaltesting/testif.out deleted file mode 100644 index 83a5f6ad2..000000000 --- a/internaltesting/testif.out +++ /dev/null @@ -1,16 +0,0 @@ -4 : { -5 : if -5 : use ab -5 : endif -6 : { -8 : } -9 : elseif -9 : use cd -9 : endif -10 : { -12 : } -13 : else -13 : endif -14 : { -16 : } -17 : } diff --git a/internaltesting/testloop.cpp b/internaltesting/testloop.cpp deleted file mode 100644 index e31daae35..000000000 --- a/internaltesting/testloop.cpp +++ /dev/null @@ -1,13 +0,0 @@ - - -void f() -{ - for (int i = 0; i < j; i++) - { - if (condition) - continue; - - break; - } -} - diff --git a/internaltesting/testloop.out b/internaltesting/testloop.out deleted file mode 100644 index 32d61ce98..000000000 --- a/internaltesting/testloop.out +++ /dev/null @@ -1,16 +0,0 @@ -4 : { -5 : loop -5 : assign i -5 : use i -5 : use i -5 : use j -5 : use i -5 : endloop -6 : { -7 : if -7 : use condition -8 : continue -7 : endif -10 : break -11 : } -12 : } diff --git a/internaltesting/testnew.cpp b/internaltesting/testnew.cpp deleted file mode 100644 index 2e8a8d3b6..000000000 --- a/internaltesting/testnew.cpp +++ /dev/null @@ -1,10 +0,0 @@ - - -void f() -{ - a = new char[10]; - fred = new Fred; - fred = new Fred(); -} - - diff --git a/internaltesting/testnew.out b/internaltesting/testnew.out deleted file mode 100644 index 5b9b63975..000000000 --- a/internaltesting/testnew.out +++ /dev/null @@ -1,7 +0,0 @@ -4 : { -5 : new[] a -5 : use char[10] -6 : new fred -6 : use Fred -7 : new fred -8 : } diff --git a/internaltesting/testuse.cpp b/internaltesting/testuse.cpp deleted file mode 100644 index 5802f250b..000000000 --- a/internaltesting/testuse.cpp +++ /dev/null @@ -1,8 +0,0 @@ - -void f() -{ - a = b; - c = func(d,e); -} - - diff --git a/internaltesting/testuse.out b/internaltesting/testuse.out deleted file mode 100644 index fb1a2160c..000000000 --- a/internaltesting/testuse.out +++ /dev/null @@ -1,7 +0,0 @@ -3 : { -4 : assign a -4 : use b -5 : assign c -5 : use d -5 : use e -6 : } diff --git a/tests.cpp b/tests.cpp index 532e97cba..0fd67497f 100644 --- a/tests.cpp +++ b/tests.cpp @@ -4,6 +4,7 @@ //--------------------------------------------------------------------------- #include "tokenize.h" // <- Tokenizer +#include "Statements.h" #include "CommonCheck.h" #include "CheckBufferOverrun.h" @@ -20,6 +21,7 @@ bool CheckCodingStyle = false; //--------------------------------------------------------------------------- static unsigned int FailCount, SuccessCount; //--------------------------------------------------------------------------- +static void internal_statementlist(); static void buffer_overrun(); static void constructors(); static void operator_eq(); @@ -31,6 +33,7 @@ static void memleak_in_class(); int main() { Files.push_back( std::string("test.cpp") ); + internal_statementlist(); buffer_overrun(); constructors(); operator_eq(); @@ -80,6 +83,179 @@ static void check(void (chk)(), } //--------------------------------------------------------------------------- +static void statementlist() +{ + CreateStatementList(); + OutputStatementList( errout ); +} + +static void internal_statementlist() +{ + const char code1[] = "void f()\n" + "{\n" + " a = 1;\n" + " b[2] = 3;\n" + " c[4][5].min = 6;\n" + " d.min = 7;\n" + "}\n"; + + const char sl1[] = "{\n" + "assign a\n" + "assign b[2]\n" + "assign c[4][5].min\n" + "assign d.min\n" + "}\n"; + + check( statementlist, __LINE__, code1, sl1 ); + + + + + + const char code2[] = "void f()\n" + "{\n" + " int a;\n" + " int b = 2, c, *d = NULL;\n" + " int e = g(p1,p2);\n" + " char str[10];\n" + " return a;\n" + " delete a;\n" + "}\n"; + + const char sl2[] = "{\n" + "decl a\n" + "decl b\n" + "assign b\n" + "decl c\n" + "decl d\n" + "assign d\n" + "use NULL\n" + "decl e\n" + "assign e\n" + "use p1\n" + "use p2\n" + "decl str\n" + "return a\n" + "delete a\n" + "}\n"; + + check( statementlist, __LINE__, code2, sl2 ); + + + + + + + const char code3[] = "void f()\n" + "{\n" + " if (ab)\n" + " {\n" + " }\n" + " else if (cd)\n" + " {\n" + " }\n" + " else\n" + " {\n" + " }\n" + "}\n"; + + const char sl3[] = "{\n" + "if\n" + "use ab\n" + "endif\n" + "{\n" + "}\n" + "elseif\n" + "use cd\n" + "endif\n" + "{\n" + "}\n" + "else\n" + "endif\n" + "{\n" + "}\n" + "}\n"; + + check( statementlist, __LINE__, code3, sl3 ); + + + + + + + const char code4[] = "void f()\n" + "{\n" + " for (int i = 0; i < j; i++)\n" + " {\n" + " if (condition)\n" + " continue;\n" + " break;\n" + " }\n" + "}\n"; + + const char sl4[] = "{\n" + "loop\n" + "assign i\n" + "use i\n" + "use i\n" + "use j\n" + "use i\n" + "endloop\n" + "{\n" + "if\n" + "use condition\n" + "continue\n" + "endif\n" + "break\n" + "}\n" + "}\n"; + + check( statementlist, __LINE__, code4, sl4 ); + + + + +/* TODO: This should work + + const char code5[] = "void f()\n" + "{\n" + " a = new char[10];\n" + " fred = new Fred;\n" + " fred = new Fred();\n" + "}\n"; + + const char sl5[] = "{\n" + "new[] a\n" + "use char[10]\n" + "new fred\n" + "use Fred\n" + "new fred\n" + "}\n"; + + check( statementlist, __LINE__, code5, sl5 ); + +*/ + + + const char code6[] = "void f()\n" + "{\n" + " a = b;\n" + " c = func(d,e);\n" + "}\n"; + + const char sl6[] = "{\n" + "assign a\n" + "use b\n" + "assign c\n" + "use d\n" + "use e\n" + "}\n"; + + check( statementlist, __LINE__, code6, sl6 ); + +} +//--------------------------------------------------------------------------- + static void buffer_overrun() { // test1: numeric array index