Testing: Added warning messages.

This commit is contained in:
Daniel Marjamäki 2007-05-21 17:16:35 +00:00
parent 242f627db2
commit 6f69fefc24
11 changed files with 86 additions and 29 deletions

112
main.cpp
View File

@ -16,6 +16,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
std::vector<std::string> Files; std::vector<std::string> Files;
static bool Debug = false; static bool Debug = false;
static bool ShowWarnings = false;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
struct TOKEN struct TOKEN
{ {
@ -84,15 +85,27 @@ static void CppCheck(const char FileName[]);
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
Debug = (argc == 3 && strcmp(argv[1], "--debug")==0); const char *fname = NULL;
for (int i = 1; i < argc; i++)
if (argc == 1 || argc > 3)
{ {
std::cout << "Syntax:\n checkcode filename\n"; if (stricmp(argv[i],"--debug") == 0)
Debug = true;
else if (stricmp(argv[i],"-w") == 0)
ShowWarnings = true;
else
fname = argv[i];
}
if (!fname)
{
std::cout << "checkcode [-w] filename\n";
std::cout << "-w : enables extra warnings\n";
return 0; return 0;
} }
CppCheck(argv[argc - 1]); CppCheck(fname);
return 0; return 0;
} }
@ -119,10 +132,6 @@ static void CppCheck(const char FileName[])
// f << "[" << Files[tok->FileIndex] << ":" << tok->linenr << "]:" << tok->str << '\n'; // f << "[" << Files[tok->FileIndex] << ":" << tok->linenr << "]:" << tok->str << '\n';
//f.close(); //f.close();
// Check that all class constructors are ok.
// Temporarily inactivated to avoid any false positives
//CheckConstructors();
// Check that all private functions are called. // Check that all private functions are called.
// Temporarily inactivated to avoid any false positives // Temporarily inactivated to avoid any false positives
CheckUnusedPrivateFunctions(); CheckUnusedPrivateFunctions();
@ -132,28 +141,32 @@ static void CppCheck(const char FileName[])
CheckMemset(); CheckMemset();
// Warnings (Inactivated for now) // Warnings
/* if (ShowWarnings)
{
// Found implementation in header
WarningHeaderWithImplementation();
CheckOperatorEq1(); // Warning upon c-style pointer casts
const char *ext = strrchr(FileName, '.');
if (ext && stricmp(ext,".c"))
WarningOldStylePointerCast();
// Found implementation in header // Use standard functions instead
// Since this is not a bug I am not enabling it right now WarningIsDigit();
//WarningHeaderWithImplementation();
// Warning upon c-style pointer casts // Including header
// This is not very interesting. It should only be shown upon "-Wall" or similar //WarningIncludeHeader();
const char *ext = strrchr(FileName, '.');
if (ext && stricmp(ext,".c")) CheckOperatorEq1();
WarningOldStylePointerCast();
// Check that all class constructors are ok.
// Temporarily inactivated to avoid any false positives
//CheckConstructors();
}
// Use standard functions instead
WarningIsDigit();
// Including header
//WarningIncludeHeader();
*/
// if (a) delete a; // if (a) delete a;
WarningRedundantCode(); WarningRedundantCode();
@ -1510,7 +1523,22 @@ void CheckBufferOverrun()
strindex = getstr(tok2,2); strindex = getstr(tok2,2);
value = atoi(getstr(tok2,8)); value = atoi(getstr(tok2,8));
} }
if (strindex && value>size) else if (match(tok2,"for ( var = 0 ; var <= num ; var + + )"))
{
strindex = getstr(tok2,2);
value = 1 + atoi(getstr(tok2,8));
}
else if (match(tok2,"for ( var = 0 ; var < num ; + + var )"))
{
strindex = getstr(tok2,2);
value = atoi(getstr(tok2,8));
}
else if (match(tok2,"for ( var = 0 ; var <= num ; + + var )"))
{
strindex = getstr(tok2,2);
value = 1 + atoi(getstr(tok2,8));
}
if (strindex && value>(int)size)
{ {
TOKEN *tok3 = tok2; TOKEN *tok3 = tok2;
while (tok3 && strcmp(tok3->str,")")) while (tok3 && strcmp(tok3->str,")"))
@ -1534,7 +1562,33 @@ void CheckBufferOverrun()
} }
tok3 = tok3->next; tok3 = tok3->next;
} }
}
// Writing data into array..
if (match(tok2,"strcpy ( var , "))
{
int len = 0;
if (strcmp(getstr(tok2, 2), varname) == 0)
{
const char *str = getstr(tok2, 4);
if (str[0] == '\"')
{
while (*str)
{
if (*str=='\\')
str++;
str++;
len++;
}
}
}
if (len > 2 && len >= (int)size + 2)
{
std::ostringstream ostr;
ostr << FileLine(tok2) << ": Buffer overrun";
ReportErr(ostr.str());
}
} }
} }
} }
@ -2140,9 +2194,6 @@ void WarningIf()
void WarningDangerousFunctions() void WarningDangerousFunctions()
{ {
char str[10];
str[20] = 0;
for (TOKEN *tok = tokens; tok; tok = tok->next) for (TOKEN *tok = tokens; tok; tok = tok->next)
{ {
if (match(tok, "gets (")) if (match(tok, "gets ("))
@ -2160,3 +2211,6 @@ void WarningDangerousFunctions()
} }
} }
} }

0
testclass1/warn.msg Normal file
View File

0
testclass10/warn.msg Normal file
View File

0
testclass2/warn.msg Normal file
View File

0
testclass4/warn.msg Normal file
View File

0
testclass7/warn.msg Normal file
View File

0
testclass8/warn.msg Normal file
View File

1
testdelete1/warn.msg Normal file
View File

@ -0,0 +1 @@
[testdelete1\testdelete1.cpp:6]: Redundant condition. It is safe to deallocate a NULL pointer

2
testh1/warn.msg Normal file
View File

@ -0,0 +1,2 @@
[testh1\testh1.h:5]: Found implementation in header
[testh1\testh1.h:11]: Found implementation in header

0
testh2/warn.msg Normal file
View File

0
testh5/warn.msg Normal file
View File