tests: Refactoring

This commit is contained in:
Daniel Marjamäki 2008-08-11 17:23:34 +00:00
parent 4aa51fa973
commit 953693ac2e
1 changed files with 605 additions and 588 deletions

285
tests.cpp
View File

@ -126,72 +126,74 @@ static void buffer_overrun()
// test7: unknown string length // test7: unknown string length
// test8: struct member.. // test8: struct member..
const char test1[] = "void f()\n" const char *code;
code = "void f()\n"
"{\n" "{\n"
" char str[0x10];\n" " char str[0x10];\n"
" str[15] = 0;\n" " str[15] = 0;\n"
" str[16] = 0;\n" " str[16] = 0;\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test1, "[test.cpp:5]: Array index out of bounds\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:5]: Array index out of bounds\n" );
const char test2[] = "void f()\n" code = "void f()\n"
"{\n" "{\n"
" int val[50];\n" " int val[50];\n"
" for (i = 0; i < 100; i++)\n" " for (i = 0; i < 100; i++)\n"
" sum += val[i];\n" " sum += val[i];\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test2, "[test.cpp:5]: Buffer overrun\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:5]: Buffer overrun\n" );
code = "const int SIZE = 10;\n"
const char test3[] = "void f()\n"
"{\n"
" if (ab)\n"
" {\n"
" char str[50];\n"
" }\n"
" if (ab)\n"
" {\n"
" char str[50];\n"
" }\n"
"}\n";
check( CheckBufferOverrun, __LINE__, test3, "" );
const char test4[] = "void f()\n"
"{\n"
" char str[3];\n"
" strcpy(str, \"abc\");\n"
"}\n";
check( CheckBufferOverrun, __LINE__, test4, "[test.cpp:4]: Buffer overrun\n" );
const char test5[] = "const int SIZE = 10;\n"
"void f()\n" "void f()\n"
"{\n" "{\n"
" int i[SIZE];\n" " int i[SIZE];\n"
" i[SIZE] = 0;\n" " i[SIZE] = 0;\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test5, "[test.cpp:5]: Array index out of bounds\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:5]: Array index out of bounds\n" );
code = "void f()\n"
"{\n"
" if (ab)\n"
" {\n"
" char str[50];\n"
" }\n"
" if (ab)\n"
" {\n"
" char str[50];\n"
" }\n"
"}\n";
check( CheckBufferOverrun, __LINE__, code, "" );
code = "void f()\n"
"{\n"
" char str[3];\n"
" strcpy(str, \"abc\");\n"
"}\n";
check( CheckBufferOverrun, __LINE__, code, "[test.cpp:4]: Buffer overrun\n" );
const char test6[] = "void f()\n"
code = "void f()\n"
"{\n" "{\n"
" int i[10];\n" " int i[10];\n"
" i[ sizeof(i) - 1 ] = 0;\n" " i[ sizeof(i) - 1 ] = 0;\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test6, "[test.cpp:4]: Array index out of bounds\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:4]: Array index out of bounds\n" );
const char test7[] = "void f1(char *str)\n" code = "void f1(char *str)\n"
"{\n" "{\n"
" strcpy(buf,str);\n" " strcpy(buf,str);\n"
"}\n" "}\n"
@ -208,10 +210,10 @@ static void buffer_overrun()
" strcpy(buf, str);\n" " strcpy(buf, str);\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test7, "" ); check( CheckBufferOverrun, __LINE__, code, "" );
const char test8[] = "struct ABC\n" code = "struct ABC\n"
"{\n" "{\n"
" char str[10];\n" " char str[10];\n"
"};\n" "};\n"
@ -221,10 +223,10 @@ static void buffer_overrun()
" struct ABC abc;\n" " struct ABC abc;\n"
" abc.str[10] = 0;\n" " abc.str[10] = 0;\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test8, "[test.cpp:9]: Array index out of bounds\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:9]: Array index out of bounds\n" );
const char test9[] = "const int SIZE = 10;\n" code = "const int SIZE = 10;\n"
"\n" "\n"
"struct ABC\n" "struct ABC\n"
"{\n" "{\n"
@ -236,11 +238,11 @@ static void buffer_overrun()
" struct ABC abc;\n" " struct ABC abc;\n"
" abc.str[SIZE] = 0;\n" " abc.str[SIZE] = 0;\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test9, "[test.cpp:11]: Array index out of bounds\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:11]: Array index out of bounds\n" );
const char test10[] = "struct ABC\n" code = "struct ABC\n"
"{\n" "{\n"
" char str[10];\n" " char str[10];\n"
"};\n" "};\n"
@ -249,11 +251,11 @@ static void buffer_overrun()
"{\n" "{\n"
" abc->str[10] = 0;\n" " abc->str[10] = 0;\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test10, "[test.cpp:8]: Array index out of bounds\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:8]: Array index out of bounds\n" );
const char test11[] = "struct ABC\n" code = "struct ABC\n"
"{\n" "{\n"
" char str[5];\n" " char str[5];\n"
"};\n" "};\n"
@ -262,11 +264,11 @@ static void buffer_overrun()
"{\n" "{\n"
" strcpy( abc->str, \"abcdef\" );\n" " strcpy( abc->str, \"abcdef\" );\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test11, "[test.cpp:8]: Buffer overrun\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:8]: Buffer overrun\n" );
const char test12[] = "static void memclr( char *data )\n" code = "static void memclr( char *data )\n"
"{\n" "{\n"
" data[10] = 0;\n" " data[10] = 0;\n"
"}\n" "}\n"
@ -276,10 +278,10 @@ static void buffer_overrun()
" char str[5];\n" " char str[5];\n"
" memclr( str ); // ERROR\n" " memclr( str ); // ERROR\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test12, "[test.cpp:9] -> [test.cpp:3]: Array index out of bounds\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:9] -> [test.cpp:3]: Array index out of bounds\n" );
const char test13[] = "struct ABC\n" code = "struct ABC\n"
"{\n" "{\n"
" char str[10];\n" " char str[10];\n"
"};\n" "};\n"
@ -293,11 +295,11 @@ static void buffer_overrun()
"{\n" "{\n"
" memclr(abc->str);\n" " memclr(abc->str);\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test13, "[test.cpp:13] -> [test.cpp:8]: Array index out of bounds\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:13] -> [test.cpp:8]: Array index out of bounds\n" );
const char test14[] = "class ABC\n" code = "class ABC\n"
"{\n" "{\n"
"public:\n" "public:\n"
" ABC();\n" " ABC();\n"
@ -312,16 +314,16 @@ static void buffer_overrun()
" abc->str[10] = 0;\n" " abc->str[10] = 0;\n"
" }\n" " }\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test14, "[test.cpp:12]: Array index out of bounds\n" ); check( CheckBufferOverrun, __LINE__, code, "[test.cpp:12]: Array index out of bounds\n" );
const char test15[] = "static void f()\n" code = "static void f()\n"
"{\n" "{\n"
" char data[1];\n" " char data[1];\n"
" return abc.data[1];\n" " return abc.data[1];\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test15, "" ); check( CheckBufferOverrun, __LINE__, code, "" );
@ -329,7 +331,7 @@ static void buffer_overrun()
// TODO // TODO
/* /*
const char test11[] = "static void memclr( char *data, const int bytes )\n" code = "static void memclr( char *data, const int bytes )\n"
"{\n" "{\n"
" for (int i = 0; i < bytes; i++)\n" " for (int i = 0; i < bytes; i++)\n"
" data[i] = 0;\n" " data[i] = 0;\n"
@ -342,7 +344,7 @@ static void buffer_overrun()
" memclr( str+1, 5 ); // ERROR\n" " memclr( str+1, 5 ); // ERROR\n"
" memclr( str, 6 ); // ERROR\n" " memclr( str, 6 ); // ERROR\n"
"}\n"; "}\n";
check( CheckBufferOverrun, __LINE__, test11, "" ); check( CheckBufferOverrun, __LINE__, code, "" );
*/ */
@ -372,27 +374,29 @@ static void constructors()
// Test3: Uninitialized variable // Test3: Uninitialized variable
// Test4: multiple constructors, uninitialized variable // Test4: multiple constructors, uninitialized variable
const char test1[] = "class Fred\n" const char *code;
code = "class Fred\n"
"{\n" "{\n"
"public:\n" "public:\n"
" int i;\n" " int i;\n"
"};\n"; "};\n";
check( CheckConstructors, __LINE__, test1, "[test.cpp:1] The class 'Fred' has no constructor\n" ); check( CheckConstructors, __LINE__, code, "[test.cpp:1] The class 'Fred' has no constructor\n" );
const char test2[] = "class Fred\n" code = "class Fred\n"
"{\n" "{\n"
"public:\n" "public:\n"
" Fred() { }\n" " Fred() { }\n"
" int i;\n" " int i;\n"
"};\n"; "};\n";
check( CheckConstructors, __LINE__, test2, "[test.cpp:4] Uninitialized member variable 'Fred::i'\n" ); check( CheckConstructors, __LINE__, code, "[test.cpp:4] Uninitialized member variable 'Fred::i'\n" );
const char test3[] = "class Fred\n" code = "class Fred\n"
"{\n" "{\n"
"public:\n" "public:\n"
" Fred();\n" " Fred();\n"
@ -400,10 +404,10 @@ static void constructors()
"};\n" "};\n"
"Fred::Fred()\n" "Fred::Fred()\n"
"{ }\n"; "{ }\n";
check( CheckConstructors, __LINE__, test3, "[test.cpp:7] Uninitialized member variable 'Fred::i'\n" ); check( CheckConstructors, __LINE__, code, "[test.cpp:7] Uninitialized member variable 'Fred::i'\n" );
const char test4[] = "class Fred\n" code = "class Fred\n"
"{\n" "{\n"
"public:\n" "public:\n"
" Fred();\n" " Fred();\n"
@ -416,19 +420,21 @@ static void constructors()
"{\n" "{\n"
" i = _i;\n" " i = _i;\n"
"}\n"; "}\n";
check( CheckConstructors, __LINE__, test4, "[test.cpp:8] Uninitialized member variable 'Fred::i'\n" ); check( CheckConstructors, __LINE__, code, "[test.cpp:8] Uninitialized member variable 'Fred::i'\n" );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static void operator_eq() static void operator_eq()
{ {
const char test1[] = "class Fred\n" const char *code;
code = "class Fred\n"
"{\n" "{\n"
"public:\n" "public:\n"
" void operator=(const int &value);\n" " void operator=(const int &value);\n"
"};\n"; "};\n";
check( CheckOperatorEq1, __LINE__, test1, "[test.cpp:4]: 'operator=' should return something\n" ); check( CheckOperatorEq1, __LINE__, code, "[test.cpp:4]: 'operator=' should return something\n" );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -445,27 +451,30 @@ static void memleak_in_function()
// test8: check all execution paths // test8: check all execution paths
// test9: mismatching allocation / deallocation // test9: mismatching allocation / deallocation
const char test1[] = "void f()\n"
const char *code;
code = "void f()\n"
"{\n" "{\n"
" int *a = new int[10];\n" " int *a = new int[10];\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test1, "[test.cpp:3]: Memory leak: a\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:3]: Memory leak: a\n" );
const char test2[] = "Fred *NewFred()\n" code = "Fred *NewFred()\n"
"{\n" "{\n"
" Fred *f = new Fred;\n" " Fred *f = new Fred;\n"
" return f;\n" " return f;\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test2, "" ); check( CheckMemoryLeak, __LINE__, code, "" );
/* TODO /* TODO
const char test3[] = "void f()\n" code = "void f()\n"
"{\n" "{\n"
" Fred *fred;\n" " Fred *fred;\n"
" if (somecondition)\n" " if (somecondition)\n"
@ -478,11 +487,11 @@ static void memleak_in_function()
" }\n" " }\n"
" delete fred;\n" " delete fred;\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test3, "" ); check( CheckMemoryLeak, __LINE__, code, "" );
*/ */
const char test4[] = "void f()\n" code = "void f()\n"
"{\n" "{\n"
" for (int i = 0; i < j; i++)\n" " for (int i = 0; i < j; i++)\n"
" {\n" " {\n"
@ -492,13 +501,13 @@ static void memleak_in_function()
" free(str);\n" " free(str);\n"
" }\n" " }\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test4, "[test.cpp:7]: Memory leak: str\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:7]: Memory leak: str\n" );
const char test5[] = "void f()\n" code = "void f()\n"
"{\n" "{\n"
" char *str = strdup(\"hello\");\n" " char *str = strdup(\"hello\");\n"
" while (condition)\n" " while (condition)\n"
@ -508,12 +517,12 @@ static void memleak_in_function()
" }\n" " }\n"
" free(str);\n" " free(str);\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test5, "" ); check( CheckMemoryLeak, __LINE__, code, "" );
const char test6[] = "void f()\n" code = "void f()\n"
"{\n" "{\n"
" char *str = strdup(\"hello\");\n" " char *str = strdup(\"hello\");\n"
" if (a==b)\n" " if (a==b)\n"
@ -522,11 +531,11 @@ static void memleak_in_function()
" }\n" " }\n"
" free(str);\n" " free(str);\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test6, "[test.cpp:6]: Memory leak: str\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:6]: Memory leak: str\n" );
const char test7[] = "void f()\n" code = "void f()\n"
"{\n" "{\n"
" char *str = strdup(\"hello\");\n" " char *str = strdup(\"hello\");\n"
" if (a==b)\n" " if (a==b)\n"
@ -535,11 +544,11 @@ static void memleak_in_function()
" return;\n" " return;\n"
" }\n" " }\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test7, "[test.cpp:3]: Memory leak: str\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:3]: Memory leak: str\n" );
const char test8[] = "void f()\n" code = "void f()\n"
"{\n" "{\n"
" char *str = new char[10];\n" " char *str = new char[10];\n"
" if (a==b)\n" " if (a==b)\n"
@ -549,59 +558,59 @@ static void memleak_in_function()
" }\n" " }\n"
" delete [] str;\n" " delete [] str;\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test8, "" ); check( CheckMemoryLeak, __LINE__, code, "" );
const char test9[] = "void f()\n" code = "void f()\n"
"{\n" "{\n"
" int *a = new int[10];\n" " int *a = new int[10];\n"
" free(a);\n" " free(a);\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test9, "[test.cpp:3]: Mismatching allocation and deallocation: a\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:3]: Mismatching allocation and deallocation: a\n" );
const char test10[] = "static void f()\n" code = "static void f()\n"
"{\n" "{\n"
" struct acpi_object_list *obj_list;\n" " struct acpi_object_list *obj_list;\n"
" obj_list = kmalloc(sizeof(struct acpi_object_list), GFP_KERNEL);\n" " obj_list = kmalloc(sizeof(struct acpi_object_list), GFP_KERNEL);\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test10, "[test.cpp:3]: Memory leak: obj_list\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:3]: Memory leak: obj_list\n" );
const char test11[] = "static char *f()\n" code = "static char *f()\n"
"{\n" "{\n"
" char *s = new char[100];\n" " char *s = new char[100];\n"
" return s;\n" " return s;\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test11, "" ); check( CheckMemoryLeak, __LINE__, code, "" );
const char test12[] = "static char *f()\n" code = "static char *f()\n"
"{\n" "{\n"
" Fred *fred = new Fred;\n" " Fred *fred = new Fred;\n"
" free( fred->Name );\n" " free( fred->Name );\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test12, "[test.cpp:3]: Memory leak: fred\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:3]: Memory leak: fred\n" );
const char test13[] = "static char *f()\n" code = "static char *f()\n"
"{\n" "{\n"
" Fred *fred = new Fred;\n" " Fred *fred = new Fred;\n"
" // fred is deleted automaticly\n" " // fred is deleted automaticly\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test13, "" ); check( CheckMemoryLeak, __LINE__, code, "" );
/* TODO /* TODO
const char test14[] = "struct Fred\n" code = "struct Fred\n"
"{\n" "{\n"
" char *str;\n" " char *str;\n"
"}\n" "}\n"
@ -611,20 +620,20 @@ static void memleak_in_function()
" Fred f;\n" " Fred f;\n"
" f.str = strdup(\"aa\");\n" " f.str = strdup(\"aa\");\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test14, "[test.cpp:9]: Memory leak: f.str\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:9]: Memory leak: f.str\n" );
*/ */
const char test15[] = "static char *f()\n" code = "static char *f()\n"
"{\n" "{\n"
" char *s = new char[100];\n" " char *s = new char[100];\n"
" return (char *)s;\n" " return (char *)s;\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test15, "" ); check( CheckMemoryLeak, __LINE__, code, "" );
const char test16[] = "static char *f()\n" code = "static char *f()\n"
"{\n" "{\n"
" char *s = new char[100];\n" " char *s = new char[100];\n"
" if ( a == b )\n" " if ( a == b )\n"
@ -633,29 +642,29 @@ static void memleak_in_function()
" }\n" " }\n"
" return NULL;\n" " return NULL;\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test16, "[test.cpp:8]: Memory leak: s\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Memory leak: s\n" );
const char test17[] = "static void f()\n" code = "static void f()\n"
"{\n" "{\n"
" char *str = strdup(\"hello\");\n" " char *str = strdup(\"hello\");\n"
" char *str2 = (char *)str;\n" " char *str2 = (char *)str;\n"
" free(str2);\n" " free(str2);\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test17, "" ); check( CheckMemoryLeak, __LINE__, code, "" );
const char test18[] = "static void f()\n" code = "static void f()\n"
"{\n" "{\n"
" char *str;\n" " char *str;\n"
" if ((str = (char *)malloc(123,33)) == NULL)\n" " if ((str = (char *)malloc(123,33)) == NULL)\n"
" return;\n" " return;\n"
" free(str);\n" " free(str);\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test18, "" ); check( CheckMemoryLeak, __LINE__, code, "" );
const char test19[] = "struct abc\n" code = "struct abc\n"
"{\n" "{\n"
" int a;\n" " int a;\n"
" int b;\n" " int b;\n"
@ -667,10 +676,10 @@ static void memleak_in_function()
" struct abc *abc1 = new abc;\n" " struct abc *abc1 = new abc;\n"
" p = &abc1->a;\n" " p = &abc1->a;\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test19, "" ); check( CheckMemoryLeak, __LINE__, code, "" );
const char test20[] = "static char *dmalloc()\n" code = "static char *dmalloc()\n"
"{\n" "{\n"
" char *p = new char[100];\n" " char *p = new char[100];\n"
" return p;\n" " return p;\n"
@ -679,10 +688,10 @@ static void memleak_in_function()
"{\n" "{\n"
" char *p = dmalloc();\n" " char *p = dmalloc();\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test20, "[test.cpp:8]: Memory leak: p\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Memory leak: p\n" );
const char test21[] = "static char *dmalloc()\n" code = "static char *dmalloc()\n"
"{\n" "{\n"
" char *p = new char[100];\n" " char *p = new char[100];\n"
" return p;\n" " return p;\n"
@ -692,14 +701,17 @@ static void memleak_in_function()
" char *p = dmalloc();\n" " char *p = dmalloc();\n"
" delete p;\n" " delete p;\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test21, "[test.cpp:8]: Mismatching allocation and deallocation: p\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Mismatching allocation and deallocation: p\n" );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static void memleak_in_class() static void memleak_in_class()
{ {
const char test1[] = "class Fred\n"
const char *code;
code = "class Fred\n"
"{\n" "{\n"
"private:\n" "private:\n"
" char *str1;\n" " char *str1;\n"
@ -720,12 +732,12 @@ static void memleak_in_class()
" delete [] str2;\n" " delete [] str2;\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test1, "[test.cpp:1]: Memory leak: Fred::str1\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:1]: Memory leak: Fred::str1\n" );
const char test2[] = "class Fred\n" code = "class Fred\n"
"{\n" "{\n"
"private:\n" "private:\n"
" char *str1;\n" " char *str1;\n"
@ -744,13 +756,13 @@ static void memleak_in_class()
" free(str1);\n" " free(str1);\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test2, "[test.cpp:17]: Mismatching allocation and deallocation: Fred::str1\n" ); check( CheckMemoryLeak, __LINE__, code, "[test.cpp:17]: Mismatching allocation and deallocation: Fred::str1\n" );
/* TODO /* TODO
const char test3[] = "class Fred\n" code = "class Fred\n"
"{\n" "{\n"
"private:\n" "private:\n"
" char *str;\n" " char *str;\n"
@ -775,7 +787,7 @@ static void memleak_in_class()
" str = strdup(s);\n" " str = strdup(s);\n"
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test3, "Memory leak for 'Fred::str'\n" ); check( CheckMemoryLeak, __LINE__, code, "Memory leak for 'Fred::str'\n" );
*/ */
@ -785,31 +797,36 @@ static void memleak_in_class()
static void division() static void division()
{ {
const char test1[] = "void f()\n" const char *code;
code = "void f()\n"
"{\n" "{\n"
" int ivar = -2;\n" " int ivar = -2;\n"
" unsigned int uvar = 2;\n" " unsigned int uvar = 2;\n"
" return ivar / uvar;\n" " return ivar / uvar;\n"
"}\n"; "}\n";
check( CheckUnsignedDivision, __LINE__, test1, "[test.cpp:5]: If the result is negative it will be wrong because an operand is unsigned.\n" ); check( CheckUnsignedDivision, __LINE__, code, "[test.cpp:5]: If the result is negative it will be wrong because an operand is unsigned.\n" );
const char test2[] = "void f()\n" code = "void f()\n"
"{\n" "{\n"
" int ivar = -2;\n" " int ivar = -2;\n"
" unsigned int uvar = 2;\n" " unsigned int uvar = 2;\n"
" return uvar / ivar;\n" " return uvar / ivar;\n"
"}\n"; "}\n";
check( CheckUnsignedDivision, __LINE__, test2, "[test.cpp:5]: If the result is negative it will be wrong because an operand is unsigned.\n" ); check( CheckUnsignedDivision, __LINE__, code, "[test.cpp:5]: If the result is negative it will be wrong because an operand is unsigned.\n" );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static void variable_scope() static void variable_scope()
{ {
const char *code;
/* TODO /* TODO
// Unused private member variable... // Unused private member variable...
const char test1[] = "class Fred\n" code = "class Fred\n"
"{\n" "{\n"
"private:\n" "private:\n"
" int i;\n" " int i;\n"
@ -823,7 +840,7 @@ static void variable_scope()
*/ */
// Scope of variable.. // Scope of variable..
const char test2[] = "void f()\n" code = "void f()\n"
"{\n" "{\n"
" int i;\n" " int i;\n"
" if (abc)\n" " if (abc)\n"
@ -831,21 +848,21 @@ static void variable_scope()
" i = 1;\n" " i = 1;\n"
" }\n" " }\n"
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test2, "[test.cpp:3] The scope of the variable 'i' can be limited\n" ); check( CheckVariableScope, __LINE__, code, "[test.cpp:3] The scope of the variable 'i' can be limited\n" );
const char test3[] = "static void DeleteNextToken(TOKEN *tok)\n" code = "static void DeleteNextToken(TOKEN *tok)\n"
"{\n" "{\n"
" TOKEN *next = tok->next;\n" " TOKEN *next = tok->next;\n"
" tok->next = next->next;\n" " tok->next = next->next;\n"
" free(next->str);\n" " free(next->str);\n"
" delete next;\n" " delete next;\n"
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test3, "" ); check( CheckVariableScope, __LINE__, code, "" );
const char test4[] = "static void f()\n" code = "static void f()\n"
"{\n" "{\n"
" bool special = false;\n" " bool special = false;\n"
" do\n" " do\n"
@ -858,11 +875,11 @@ static void variable_scope()
" }\n" " }\n"
" while (special || c != \'\"\');\n" " while (special || c != \'\"\');\n"
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test4, "" ); check( CheckVariableScope, __LINE__, code, "" );
const char test5[] = "static void f()\n" code = "static void f()\n"
"{\n" "{\n"
" int i = 0;\n" " int i = 0;\n"
" {\n" " {\n"
@ -872,26 +889,26 @@ static void variable_scope()
" i+5;\n" " i+5;\n"
" }\n" " }\n"
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test5, "" ); check( CheckVariableScope, __LINE__, code, "" );
const char test6[] = "static void f()\n" code = "static void f()\n"
"{\n" "{\n"
"#define F1(x, y, z) (z ^ (x & (y ^ z)))\n" "#define F1(x, y, z) (z ^ (x & (y ^ z)))\n"
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test6, "" ); check( CheckVariableScope, __LINE__, code, "" );
const char test7[] = "struct a\n" code = "struct a\n"
"{\n" "{\n"
" int x;\n" " int x;\n"
" int y;\n" " int y;\n"
"};\n"; "};\n";
check( CheckVariableScope, __LINE__, test7, "" ); check( CheckVariableScope, __LINE__, code, "" );
const char test8[] = "static void f()\n" code = "static void f()\n"
"{\n" "{\n"
" struct\n" " struct\n"
" {\n" " {\n"
@ -899,10 +916,10 @@ static void variable_scope()
" int y;\n" " int y;\n"
" } fred;\n" " } fred;\n"
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test8, "" ); check( CheckVariableScope, __LINE__, code, "" );
const char test9[] = "static void f()\n" code = "static void f()\n"
"{\n" "{\n"
" int i;\n" " int i;\n"
" while (abc)\n" " while (abc)\n"
@ -917,12 +934,12 @@ static void variable_scope()
" }\n" " }\n"
" }\n" " }\n"
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test9, "" ); check( CheckVariableScope, __LINE__, code, "" );
const char test10[] = "static void f()\n" code = "static void f()\n"
"{\n" "{\n"
" TPoint p1;\n" " TPoint p1;\n"
" for (i=0;i<10;i++)\n" " for (i=0;i<10;i++)\n"
@ -930,7 +947,7 @@ static void variable_scope()
" p1=point(i,i);\n" " p1=point(i,i);\n"
" }\n" " }\n"
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test10, "" ); check( CheckVariableScope, __LINE__, code, "" );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------