From 0c5e39a81386e64bf6dea460018e0a2f7a730373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 20 Jul 2013 17:12:56 +0200 Subject: [PATCH] Library: Added configuration file std.cfg that clients should load. --- cfg/std.cfg | 17 ++++++++++++++++ cli/cppcheckexecutor.cpp | 2 ++ gui/mainwindow.cpp | 3 +++ lib/library.cpp | 43 ++++++++++++++++++++-------------------- lib/library.h | 9 +++++++++ test/testleakautovar.cpp | 7 +++++++ 6 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 cfg/std.cfg diff --git a/cfg/std.cfg b/cfg/std.cfg new file mode 100644 index 000000000..5b7d4d415 --- /dev/null +++ b/cfg/std.cfg @@ -0,0 +1,17 @@ + + + + free + + malloc + calloc + strdup + strndup + + + + fclose + fopen + + + diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index fa4f31a47..236f74b05 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -152,6 +152,8 @@ int CppCheckExecutor::check(int argc, const char* const argv[]) Settings& settings = cppCheck.settings(); _settings = &settings; + settings.library.load(argv[0], "std"); + if (!parseFromArgs(&cppCheck, argc, argv)) { return EXIT_FAILURE; } diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 408127ba6..15e51c003 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -516,6 +516,9 @@ Settings MainWindow::GetCppcheckSettings() { Settings result; + const QString applicationFilePath = QCoreApplication::applicationFilePath(); + result.library.load(applicationFilePath.toLatin1(), "std"); + // If project file loaded, read settings from it if (mProject) { ProjectFile *pfile = mProject->GetProjectFile(); diff --git a/lib/library.cpp b/lib/library.cpp index c2451b6e9..904fc9100 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -17,20 +17,14 @@ */ #include "library.h" +#include "path.h" #include "tinyxml2.h" +#include +#include + Library::Library() : allocid(0) { - while (!ismemory(++allocid)); - _alloc["malloc"] = allocid; - _alloc["calloc"] = allocid; - _alloc["strdup"] = allocid; - _alloc["strndup"] = allocid; - _dealloc["free"] = allocid; - - while (!isresource(++allocid)); - _alloc["fopen"] = allocid; - _dealloc["fclose"] = allocid; } Library::Library(const Library &lib) : @@ -43,7 +37,6 @@ Library::Library(const Library &lib) : _dealloc(lib._dealloc), _noreturn(lib._noreturn) { - } Library::~Library() { } @@ -65,19 +58,27 @@ bool Library::load(const char exename[], const char path[]) return ret; } + // open file.. FILE *fp = fopen(path, "rt"); if (fp == NULL) { - std::string fullpath(exename); - if (fullpath.find_first_of("/\\") != std::string::npos) - fullpath = fullpath.substr(0, 1U + fullpath.find_last_of("/\\")); - fullpath += path; - fp = fopen(fullpath.c_str(), "rt"); - if (fp == NULL) { - fullpath += ".cfg"; - fp = fopen(fullpath.c_str(), "rt"); - if (fp == NULL) - return false; + // failed to open file.. is there no extension? + std::string fullfilename(path); + if (Path::getFilenameExtension(fullfilename) == "") { + fullfilename += ".cfg"; + fp = fopen(fullfilename.c_str(), "rt"); } + + if (fp==NULL) { + // Try to locate the library configuration in the installation folder.. + std::string temp = exename; + std::replace(temp.begin(), temp.end(), '\\', '/'); + const std::string installfolder = Path::getPathFromFilename(temp); + const std::string filename = installfolder + "cfg/" + fullfilename; + fp = fopen(filename.c_str(), "rt"); + } + + if (fp == NULL) + return false; } if (doc.LoadFile(fp) != tinyxml2::XML_NO_ERROR) diff --git a/lib/library.h b/lib/library.h index 043398edc..146aa51d2 100644 --- a/lib/library.h +++ b/lib/library.h @@ -48,6 +48,15 @@ public: return getid(_dealloc, name); } + /** set allocation id for function */ + void setalloc(const std::string &functionname, int id) { + _alloc[functionname] = id; + } + + void setdealloc(const std::string &functionname, int id) { + _dealloc[functionname] = id; + } + /** is allocation type memory? */ static bool ismemory(int id) { return ((id > 0) && ((id & 1) == 0)); diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 2f9d20107..d2945e97a 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -111,6 +111,13 @@ private: // Tokenize.. Settings settings; + int id = 0; + while (!settings.library.ismemory(++id)); + settings.library.setalloc("malloc",id); + settings.library.setdealloc("free",id); + while (!settings.library.isresource(++id)); + settings.library.setalloc("fopen",id); + settings.library.setdealloc("fclose",id); Tokenizer tokenizer(&settings, this); std::istringstream istr(code); tokenizer.tokenize(istr, "test.c");