Skip to content

Commit d96fb57

Browse files
committed
CLI: Added --environment flag
1 parent 1a35241 commit d96fb57

10 files changed

Lines changed: 224 additions & 158 deletions

File tree

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ ifndef PREFIX
7575
endif
7676

7777
ifndef INCLUDE_FOR_LIB
78-
INCLUDE_FOR_LIB=-Ilib
78+
INCLUDE_FOR_LIB=-Ilib -Iexternals -Iexternals/tinyxml
7979
endif
8080

8181
ifndef INCLUDE_FOR_CLI
@@ -120,6 +120,7 @@ LIBOBJ = $(SRCDIR)/check64bit.o \
120120
$(SRCDIR)/checkunusedfunctions.o \
121121
$(SRCDIR)/checkunusedvar.o \
122122
$(SRCDIR)/cppcheck.o \
123+
$(SRCDIR)/environment.o \
123124
$(SRCDIR)/errorlogger.o \
124125
$(SRCDIR)/executionpath.o \
125126
$(SRCDIR)/mathlib.o \
@@ -285,7 +286,7 @@ $(SRCDIR)/checknullpointer.o: lib/checknullpointer.cpp lib/checknullpointer.h li
285286
$(SRCDIR)/checkobsoletefunctions.o: lib/checkobsoletefunctions.cpp lib/checkobsoletefunctions.h lib/config.h lib/check.h lib/token.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/standards.h lib/symboldatabase.h lib/mathlib.h
286287
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $(SRCDIR)/checkobsoletefunctions.o $(SRCDIR)/checkobsoletefunctions.cpp
287288

288-
$(SRCDIR)/checkother.o: lib/checkother.cpp lib/checkother.h lib/config.h lib/check.h lib/token.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/standards.h lib/mathlib.h lib/symboldatabase.h
289+
$(SRCDIR)/checkother.o: lib/checkother.cpp lib/checkother.h lib/config.h lib/check.h lib/token.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/standards.h lib/mathlib.h lib/symboldatabase.h lib/templatesimplifier.h
289290
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $(SRCDIR)/checkother.o $(SRCDIR)/checkother.cpp
290291

291292
$(SRCDIR)/checkpostfixoperator.o: lib/checkpostfixoperator.cpp lib/checkpostfixoperator.h lib/config.h lib/check.h lib/token.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/standards.h lib/symboldatabase.h lib/mathlib.h
@@ -309,6 +310,9 @@ $(SRCDIR)/checkunusedvar.o: lib/checkunusedvar.cpp lib/checkunusedvar.h lib/conf
309310
$(SRCDIR)/cppcheck.o: lib/cppcheck.cpp lib/cppcheck.h lib/config.h lib/settings.h lib/suppressions.h lib/standards.h lib/errorlogger.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/preprocessor.h lib/path.h lib/timer.h
310311
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $(SRCDIR)/cppcheck.o $(SRCDIR)/cppcheck.cpp
311312

313+
$(SRCDIR)/environment.o: lib/environment.cpp lib/environment.h
314+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $(SRCDIR)/environment.o $(SRCDIR)/environment.cpp
315+
312316
$(SRCDIR)/errorlogger.o: lib/errorlogger.cpp lib/errorlogger.h lib/config.h lib/suppressions.h lib/path.h lib/cppcheck.h lib/settings.h lib/standards.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/tokenlist.h
313317
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $(SRCDIR)/errorlogger.o $(SRCDIR)/errorlogger.cpp
314318

cli/cmdlineparser.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
328328
}
329329
}
330330

331+
// --environment
332+
else if (std::strncmp(argv[i], "--environment=", 14) == 0) {
333+
if (!_settings->environment.load(argv[i]+14)) {
334+
PrintMessage("cppcheck: Failed to load environment file '" + std::string(argv[i]+14) + "'");
335+
return false;
336+
}
337+
}
338+
331339
// --error-exitcode=1
332340
else if (std::strncmp(argv[i], "--error-exitcode=", 17) == 0) {
333341
std::string temp = argv[i]+17;

lib/checkleakautovar.cpp

Lines changed: 43 additions & 143 deletions
Large diffs are not rendered by default.

lib/checkleakautovar.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
class CPPCHECKLIB VarInfo {
3030
public:
31-
std::map<unsigned int, std::string> alloctype;
31+
std::map<unsigned int, int> alloctype;
3232
std::map<unsigned int, std::string> possibleUsage;
3333
std::set<unsigned int> conditionalAlloc;
3434
std::set<unsigned int> referenced;
@@ -81,20 +81,11 @@ class CPPCHECKLIB CheckLeakAutoVar : public Check {
8181
/** @brief Run checks against the simplified token list */
8282
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
8383
CheckLeakAutoVar checkLeakAutoVar(tokenizer, settings, errorLogger);
84-
checkLeakAutoVar.parseConfigurationFile("cppcheck.cfg");
8584
checkLeakAutoVar.check();
8685
}
8786

8887
private:
8988

90-
std::map<std::string,std::string> cfgalloc;
91-
std::map<std::string,std::string> cfgdealloc;
92-
std::set<std::string> cfgignore;
93-
std::set<std::string> cfguse;
94-
std::set<std::string> cfgnoreturn;
95-
96-
void parseConfigurationFile(const std::string &filename);
97-
9889
/** check for leaks in all scopes */
9990
void check();
10091

@@ -104,15 +95,15 @@ class CPPCHECKLIB CheckLeakAutoVar : public Check {
10495
std::set<unsigned int> notzero);
10596

10697
/** parse function call */
107-
void functionCall(const Token *tok, VarInfo *varInfo, const std::string &dealloc);
98+
void functionCall(const Token *tok, VarInfo *varInfo, const int dealloc);
10899

109100
/** return. either "return" or end of variable scope is seen */
110101
void ret(const Token *tok, const VarInfo &varInfo);
111102

112103
/** if variable is allocated then there is a leak */
113104
void leakIfAllocated(const Token *vartok, const VarInfo &varInfo);
114105

115-
void leakError(const Token* tok, const std::string &varname, const std::string &type);
106+
void leakError(const Token* tok, const std::string &varname, int type);
116107
void mismatchError(const Token* tok, const std::string &varname);
117108
void deallocUseError(const Token *tok, const std::string &varname);
118109
void deallocReturnError(const Token *tok, const std::string &varname);

lib/environment.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "environment.h"
20+
#include <tinyxml2.h>
21+
22+
Environment::Environment() : allocid(0)
23+
{
24+
while (!ismemory(++allocid));
25+
_alloc["malloc"] = allocid;
26+
_alloc["calloc"] = allocid;
27+
_alloc["strdup"] = allocid;
28+
_alloc["strndup"] = allocid;
29+
_dealloc["free"] = allocid;
30+
31+
while (!isresource(++allocid));
32+
_alloc["fopen"] = allocid;
33+
_dealloc["fclose"] = allocid;
34+
}
35+
36+
Environment::Environment(const Environment &env) : use(env.use), ignore(env.ignore), noreturn(env.noreturn), allocid(env.allocid), _alloc(env._alloc), _dealloc(env._dealloc)
37+
{
38+
39+
}
40+
41+
Environment::~Environment() { }
42+
43+
bool Environment::load(const char path[])
44+
{
45+
tinyxml2::XMLDocument doc;
46+
47+
const tinyxml2::XMLError error = doc.LoadFile(path);
48+
if (error != tinyxml2::XML_NO_ERROR)
49+
return false;
50+
51+
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
52+
if (strcmp(rootnode->Value(),"def") != 0)
53+
return false;
54+
55+
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
56+
if (strcmp(node->Value(),"memory")==0) {
57+
while (!ismemory(++allocid));
58+
for (const tinyxml2::XMLElement *memorynode = node->FirstChildElement(); memorynode; memorynode = memorynode->NextSiblingElement()) {
59+
if (strcmp(memorynode->Value(),"alloc")==0)
60+
_alloc[node->GetText()] = allocid;
61+
else if (strcmp(memorynode->Value(),"dealloc")==0)
62+
_dealloc[node->GetText()] = allocid;
63+
else if (strcmp(node->Value(),"use")==0)
64+
use.insert(node->GetText());
65+
else
66+
return false;
67+
}
68+
69+
}
70+
71+
else if (strcmp(node->Value(),"ignore")==0)
72+
ignore.insert(node->GetText());
73+
else if (strcmp(node->Value(),"noreturn")==0)
74+
noreturn.insert(node->GetText());
75+
else
76+
return false;
77+
}
78+
return true;
79+
}

lib/environment.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef ENVIRONMENT_H
20+
#define ENVIRONMENT_H
21+
22+
#include <map>
23+
#include <set>
24+
#include <string>
25+
26+
/// @addtogroup Core
27+
/// @{
28+
29+
/**
30+
* @brief Environment definitions handling
31+
*/
32+
class Environment {
33+
public:
34+
Environment();
35+
Environment(const Environment &);
36+
~Environment();
37+
38+
bool load(const char path[]);
39+
40+
/** get allocation id for function (by name) */
41+
int alloc(const std::string &name) const {
42+
return getid(_alloc, name);
43+
}
44+
45+
/** get deallocation id for function (by name) */
46+
int dealloc(const std::string &name) const {
47+
return getid(_dealloc, name);
48+
}
49+
50+
/** is allocation type memory? */
51+
static bool ismemory(int id) {
52+
return ((id > 0) && ((id & 1) == 0));
53+
}
54+
55+
/** is allocation type resource? */
56+
static bool isresource(int id) {
57+
return ((id > 0) && ((id & 1) == 1));
58+
}
59+
60+
std::set<std::string> use;
61+
std::set<std::string> ignore;
62+
std::set<std::string> noreturn;
63+
64+
private:
65+
int allocid;
66+
std::map<std::string, int> _alloc; // allocation functions
67+
std::map<std::string, int> _dealloc; // deallocation functions
68+
69+
int getid(const std::map<std::string,int> &data, const std::string &name) const {
70+
const std::map<std::string,int>::const_iterator it = data.find(name);
71+
return (it == data.end()) ? 0 : it->second;
72+
}
73+
};
74+
75+
/// @}
76+
77+
#endif // PATH_H_INCLUDED

lib/lib.pri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ HEADERS += $${BASEPATH}check.h \
2727
$${BASEPATH}checkunusedfunctions.h \
2828
$${BASEPATH}checkunusedvar.h \
2929
$${BASEPATH}cppcheck.h \
30+
$${BASEPATH}environment.h \
3031
$${BASEPATH}errorlogger.h \
3132
$${BASEPATH}executionpath.h \
3233
$${BASEPATH}mathlib.h \
@@ -66,6 +67,7 @@ SOURCES += $${BASEPATH}check64bit.cpp \
6667
$${BASEPATH}checkunusedfunctions.cpp \
6768
$${BASEPATH}checkunusedvar.cpp \
6869
$${BASEPATH}cppcheck.cpp \
70+
$${BASEPATH}environment.cpp \
6971
$${BASEPATH}errorlogger.cpp \
7072
$${BASEPATH}executionpath.cpp \
7173
$${BASEPATH}mathlib.cpp \

lib/settings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string>
2525
#include <set>
2626
#include "config.h"
27+
#include "environment.h"
2728
#include "suppressions.h"
2829
#include "standards.h"
2930

@@ -174,6 +175,9 @@ class CPPCHECKLIB Settings {
174175
/** @brief --report-progress */
175176
bool reportProgress;
176177

178+
/** Environment (--environment) */
179+
Environment environment;
180+
177181
/** Rule */
178182
class CPPCHECKLIB Rule {
179183
public:

test/testleakautovar.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class TestLeakAutoVar : public TestFixture {
119119
// Check for leaks..
120120
CheckLeakAutoVar c;
121121
settings.experimental = true;
122+
settings.addEnabled("information");
122123
c.runSimplifiedChecks(&tokenizer, &settings, this);
123124
}
124125

tools/dmake.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ int main(int argc, char **argv)
321321

322322
makeConditionalVariable(fout, "CXX", "g++");
323323
makeConditionalVariable(fout, "PREFIX", "/usr");
324-
makeConditionalVariable(fout, "INCLUDE_FOR_LIB", "-Ilib");
324+
makeConditionalVariable(fout, "INCLUDE_FOR_LIB", "-Ilib -Iexternals -Iexternals/tinyxml");
325325
makeConditionalVariable(fout, "INCLUDE_FOR_CLI", "-Ilib -Iexternals -Iexternals/tinyxml");
326326
makeConditionalVariable(fout, "INCLUDE_FOR_TEST", "-Ilib -Icli -Iexternals -Iexternals/tinyxml");
327327

0 commit comments

Comments
 (0)