From 162b02bcb480abf8dc7fdd0a4d51637d0fcc1e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 25 May 2007 06:51:18 +0000 Subject: [PATCH] CheckOther: Added files. This will contain checks that don't fit in the other checking files. --- CheckOther.cpp | 159 +++++++++++++++++++++++++++++++++++++++++++++++++ CheckOther.h | 25 ++++++++ 2 files changed, 184 insertions(+) create mode 100644 CheckOther.cpp create mode 100644 CheckOther.h diff --git a/CheckOther.cpp b/CheckOther.cpp new file mode 100644 index 000000000..8f7def91b --- /dev/null +++ b/CheckOther.cpp @@ -0,0 +1,159 @@ +//--------------------------------------------------------------------------- +#include "CheckOther.h" +#include "Tokenize.h" +#include "CommonCheck.h" +#include +//--------------------------------------------------------------------------- + + + + +//--------------------------------------------------------------------------- +// Warning on C-Style casts.. p = (kalle *)foo; +//--------------------------------------------------------------------------- + +void WarningOldStylePointerCast() +{ + for (TOKEN *tok = tokens; tok; tok = tok->next) + { + // Old style pointer casting.. + if (!match(tok, "( type * ) var")) + continue; + + // Is "type" a class? + const char *pattern[] = {"class","",NULL}; + pattern[1] = getstr(tok, 1); + if (!findtoken(tokens, pattern)) + continue; + + std::ostringstream ostr; + ostr << FileLine(tok) << ": C-style pointer casting"; + ReportErr(ostr.str()); + } +} + + + + +//--------------------------------------------------------------------------- +// Use standard function "isdigit" instead +//--------------------------------------------------------------------------- + +void WarningIsDigit() +{ + for (TOKEN *tok = tokens; tok; tok = tok->next) + { + bool err = false; + err |= match(tok, "var >= '0' && var <= '9'"); + err |= match(tok, "* var >= '0' && * var <= '9'"); + err |= match(tok, "( var >= '0' ) && ( var <= '9' )"); + err |= match(tok, "( * var >= '0' ) && ( * var <= '9' )"); + if (err) + { + std::ostringstream ostr; + ostr << FileLine(tok) << ": The condition can be simplified; use 'isdigit'"; + ReportErr(ostr.str()); + } + } +} +//--------------------------------------------------------------------------- + + +//--------------------------------------------------------------------------- +// Redundant code.. +//--------------------------------------------------------------------------- + +void WarningRedundantCode() +{ + + // if (p) delete p + for (TOKEN *tok = tokens; tok; tok = tok->next) + { + if (strcmp(tok->str,"if")) + continue; + + const char *varname1 = NULL; + TOKEN *tok2 = NULL; + + if (match(tok,"if ( var )")) + { + varname1 = getstr(tok, 2); + tok2 = gettok(tok, 4); + } + else if (match(tok,"if ( var != NULL )")) + { + varname1 = getstr(tok, 2); + tok2 = gettok(tok, 6); + } + + if (varname1==NULL || tok2==NULL) + continue; + + bool err = false; + if (match(tok2,"delete var ;")) + err = (strcmp(getstr(tok2,1),varname1)==0); + else if (match(tok2,"{ delete var ; }")) + err = (strcmp(getstr(tok2,2),varname1)==0); + else if (match(tok2,"delete [ ] var ;")) + err = (strcmp(getstr(tok2,1),varname1)==0); + else if (match(tok2,"{ delete [ ] var ; }")) + err = (strcmp(getstr(tok2,2),varname1)==0); + else if (match(tok2,"free ( var )")) + err = (strcmp(getstr(tok2,2),varname1)==0); + else if (match(tok2,"{ free ( var ) ; }")) + err = (strcmp(getstr(tok2,3),varname1)==0); + + if (err) + { + std::ostringstream ostr; + ostr << FileLine(tok) << ": Redundant condition. It is safe to deallocate a NULL pointer"; + ReportErr(ostr.str()); + } + } + + + // TODO + // if (haystack.find(needle) != haystack.end()) + // haystack.remove(needle); + +} +//--------------------------------------------------------------------------- + + + + +//--------------------------------------------------------------------------- +// if (condition); +//--------------------------------------------------------------------------- + +void WarningIf() +{ + for (TOKEN *tok = tokens; tok; tok = tok->next) + { + if (strcmp(tok->str,"if")==0) + { + int parlevel = 0; + for (TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next) + { + if (tok2->str[0]=='(') + parlevel++; + else if (tok2->str[0]==')') + { + parlevel--; + if (parlevel<=0) + { + if (strcmp(getstr(tok2,1), ";") == 0) + { + std::ostringstream ostr; + ostr << FileLine(tok) << ": Found \"if (condition);\""; + ReportErr(ostr.str()); + } + break; + } + } + } + } + } +} +//--------------------------------------------------------------------------- + diff --git a/CheckOther.h b/CheckOther.h new file mode 100644 index 000000000..e4397452f --- /dev/null +++ b/CheckOther.h @@ -0,0 +1,25 @@ +//--------------------------------------------------------------------------- +#ifndef CheckOtherH +#define CheckOtherH +//--------------------------------------------------------------------------- + + +// Casting +void WarningOldStylePointerCast(); + +// Use standard functions instead +void WarningIsDigit(); + +// Redundant code +void WarningRedundantCode(); + +// Warning upon: if (condition); +void WarningIf(); + +// Using dangerous functions +void WarningDangerousFunctions(); + + +//--------------------------------------------------------------------------- +#endif +