Fixing ticket #35 (Get rid of #ifdefs in our code where possible)

This commit is contained in:
Reijo Tomperi 2009-01-23 20:25:13 +00:00
parent ca56520c29
commit 8d6f41397a
11 changed files with 52 additions and 85 deletions

View File

@ -30,7 +30,7 @@
#include <cstring> #include <cstring>
#include <stdlib.h> // <- strtoul #include <cstdlib> // <- strtoul
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -26,14 +26,8 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <cstring>
#include <algorithm> #include <algorithm>
#ifdef __BORLANDC__
#include <ctype.h>
#include <mem.h>
#endif
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckClass::CheckClass(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger) CheckClass::CheckClass(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger)
@ -101,11 +95,7 @@ struct CheckClass::VAR *CheckClass::ClassChecking_GetVarList(const Token *tok1)
// If the varname was set in one of the two if-block above, create a entry for this variable.. // If the varname was set in one of the two if-block above, create a entry for this variable..
if (varname) if (varname)
{ {
struct VAR *var = new VAR; struct VAR *var = new VAR(varname, false, varlist);
memset(var, 0, sizeof(struct VAR));
var->name = varname;
var->init = false;
var->next = varlist;
varlist = var; varlist = var;
} }
} }

View File

@ -46,6 +46,13 @@ public:
private: private:
struct VAR struct VAR
{ {
VAR(const char *name = 0, bool init = false, struct VAR *next = 0)
{
this->name = name;
this->init = init;
this->next = next;
}
const char *name; const char *name;
bool init; bool init;
struct VAR *next; struct VAR *next;

View File

@ -21,6 +21,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "checkheaders.h" #include "checkheaders.h"
#include "tokenize.h" #include "tokenize.h"
#include "filelister.h"
#include <algorithm> #include <algorithm>
#include <list> #include <list>
@ -94,7 +95,7 @@ void CheckHeaders::WarningIncludeHeader()
const char *includefile = includetok->next()->aaaa(); const char *includefile = includetok->next()->aaaa();
while (hfile < _tokenizer->getFiles()->size()) while (hfile < _tokenizer->getFiles()->size())
{ {
if (Tokenizer::SameFileName(_tokenizer->getFiles()->at(hfile).c_str(), includefile)) if (FileLister::SameFileName(_tokenizer->getFiles()->at(hfile).c_str(), includefile))
break; break;
++hfile; ++hfile;
} }

View File

@ -19,23 +19,12 @@
#include "checkmemoryleak.h" #include "checkmemoryleak.h"
#include "errormessage.h" #include "errormessage.h"
#include <stdlib.h> // free
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#ifdef __BORLANDC__
#include <mem.h> // <- memset
#else
#include <string.h>
#endif
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckMemoryLeakClass::CheckMemoryLeakClass(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger) CheckMemoryLeakClass::CheckMemoryLeakClass(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger)

View File

@ -24,7 +24,7 @@
#include <list> #include <list>
#include <map> #include <map>
#include <sstream> #include <sstream>
#include <stdlib.h> // <- atoi #include <cstdlib> // <- atoi
#include <cstring> #include <cstring>
#include <cctype> #include <cctype>
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -217,4 +217,20 @@ void FileLister::RecursiveAddFiles(std::vector<std::string> &filenames, const st
//---------------------------------------------------------------------------
bool FileLister::SameFileName(const char fname1[], const char fname2[])
{
#ifdef __linux__
return bool(strcmp(fname1, fname2) == 0);
#endif
#ifdef __GNUC__
return bool(strcasecmp(fname1, fname2) == 0);
#endif
#ifdef __BORLANDC__
return bool(stricmp(fname1, fname2) == 0);
#endif
#ifdef _MSC_VER
return bool(_stricmp(fname1, fname2) == 0);
#endif
}

View File

@ -41,6 +41,7 @@ class FileLister
public: public:
static void RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive); static void RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive);
static std::string simplifyPath(const char *originalPath); static std::string simplifyPath(const char *originalPath);
static bool SameFileName(const char fname1[], const char fname2[]);
private: private:
static bool AcceptFile(const std::string &filename); static bool AcceptFile(const std::string &filename);
}; };

View File

@ -23,16 +23,10 @@
#include "token.h" #include "token.h"
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <cstdlib>
#ifdef __BORLANDC__
#include <ctype>
#include <stdlib.h> // exit
#endif
Preprocessor::Preprocessor() Preprocessor::Preprocessor()
{ {
@ -477,8 +471,8 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
// std::string line; // std::string line;
std::string path = filename; std::string path = filename;
path.erase(1 + path.find_last_of("\\/")); path.erase(1 + path.find_last_of("\\/"));
std::string::size_type pos = 0; std::string::size_type pos = 0;
std::map<std::string,bool> handledFiles; std::map<std::string, bool> handledFiles;
while ((pos = code.find("#include", pos)) != std::string::npos) while ((pos = code.find("#include", pos)) != std::string::npos)
{ {
// Accept only includes that are at the start of a line // Accept only includes that are at the start of a line
@ -497,16 +491,16 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
filename = getHeaderFileName(filename); filename = getHeaderFileName(filename);
if (filename.length() == 0) if (filename.length() == 0)
continue; continue;
if( handledFiles.find( filename ) != handledFiles.end() ) if (handledFiles.find(filename) != handledFiles.end())
{ {
// We have processed this file already once, skip // We have processed this file already once, skip
// it this time to avoid ethernal loop. // it this time to avoid ethernal loop.
continue; continue;
} }
handledFiles[ filename ] = true; handledFiles[ filename ] = true;
// filename contains now a file name e.g. "menu.h" // filename contains now a file name e.g. "menu.h"
std::string processedFile; std::string processedFile;
for (std::list<std::string>::const_iterator iter = includePaths.begin(); iter != includePaths.end(); ++iter) for (std::list<std::string>::const_iterator iter = includePaths.begin(); iter != includePaths.end(); ++iter)
@ -514,7 +508,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
std::ifstream fin; std::ifstream fin;
fin.open((*iter + filename).c_str()); fin.open((*iter + filename).c_str());
if (fin.is_open()) if (fin.is_open())
{ {
filename = *iter + filename; filename = *iter + filename;
processedFile = Preprocessor::read(fin); processedFile = Preprocessor::read(fin);
break; break;
@ -527,7 +521,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
std::ifstream fin(filename.c_str()); std::ifstream fin(filename.c_str());
processedFile = Preprocessor::read(fin); processedFile = Preprocessor::read(fin);
} }
if (processedFile.length() > 0) if (processedFile.length() > 0)
{ {
// Replace all tabs with spaces.. // Replace all tabs with spaces..
@ -540,10 +534,10 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
// Remove space characters that are after or before new line character // Remove space characters that are after or before new line character
processedFile = removeSpaceNearNL(processedFile); processedFile = removeSpaceNearNL(processedFile);
processedFile = "#file \"" + filename + "\"\n" + processedFile + "\n#endfile"; processedFile = "#file \"" + filename + "\"\n" + processedFile + "\n#endfile";
code.insert(pos, processedFile); code.insert(pos, processedFile);
} }
} }
} }
class Macro class Macro

View File

@ -22,10 +22,7 @@
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <cctype>
#ifdef __BORLANDC__
#include <ctype.h> // isalpha, isdigit
#endif
Token::Token() : Token::Token() :
_str(""), _str(""),
@ -51,8 +48,8 @@ void Token::str(const char s[])
_str = s; _str = s;
std::free(_cstr); std::free(_cstr);
_cstr = strdup(s); _cstr = strdup(s);
_isName = bool(_str[0] == '_' || isalpha(_str[0])); _isName = bool(_str[0] == '_' || std::isalpha(_str[0]));
_isNumber = bool(isdigit(_str[0]) != 0); _isNumber = bool(std::isdigit(_str[0]) != 0);
if (_str == "true" || _str == "false") if (_str == "true" || _str == "false")
_isBoolean = true; _isBoolean = true;
else else

View File

@ -20,29 +20,17 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "tokenize.h" #include "tokenize.h"
//---------------------------------------------------------------------------
#include "filelister.h" #include "filelister.h"
#include <locale> #include <locale>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <list> #include <list>
#include <algorithm> #include <algorithm>
#include <stdlib.h> // <- strtoul #include <cstdlib>
#include <stdio.h>
#ifdef __BORLANDC__
#include <ctype.h>
#include <mem.h>
#endif
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -297,7 +285,7 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[])
fileIndexes.push_back(FileIndex); fileIndexes.push_back(FileIndex);
for (unsigned int i = 0; i < _files.size(); i++) for (unsigned int i = 0; i < _files.size(); i++)
{ {
if (SameFileName(_files[i].c_str(), line.c_str())) if (FileLister::SameFileName(_files[i].c_str(), line.c_str()))
{ {
// Use this index // Use this index
foundOurfile = true; foundOurfile = true;
@ -1528,22 +1516,6 @@ std::string Tokenizer::fileLine(const Token *tok) const
return ostr.str(); return ostr.str();
} }
//---------------------------------------------------------------------------
bool Tokenizer::SameFileName(const char fname1[], const char fname2[])
{
#ifdef __linux__
return bool(strcmp(fname1, fname2) == 0);
#endif
#ifdef __GNUC__
return bool(strcasecmp(fname1, fname2) == 0);
#endif
#ifdef __BORLANDC__
return bool(stricmp(fname1, fname2) == 0);
#endif
#ifdef _MSC_VER
return bool(_stricmp(fname1, fname2) == 0);
#endif
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------