Library: Added configuration file std.cfg that clients should load.

This commit is contained in:
Daniel Marjamäki 2013-07-20 17:12:56 +02:00
parent 941da42cfd
commit 0c5e39a813
6 changed files with 60 additions and 21 deletions

17
cfg/std.cfg Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0"?>
<def>
<memory>
<dealloc>free</dealloc>
<alloc init="false">malloc</alloc>
<alloc init="true">calloc</alloc>
<alloc init="true">strdup</alloc>
<alloc init="true">strndup</alloc>
</memory>
<resource>
<dealloc>fclose</dealloc>
<alloc init="false">fopen</alloc>
</resource>
</def>

View File

@ -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;
}

View File

@ -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();

View File

@ -17,20 +17,14 @@
*/
#include "library.h"
#include "path.h"
#include "tinyxml2.h"
#include <string>
#include <algorithm>
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)

View File

@ -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));

View File

@ -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");