Skip to content

Commit 1a2aaa6

Browse files
committed
Library: If load from current path fails, try to load 'default' configuration from cppcheck-executable path. Allow that '.cfg' extension is not used. Allow that multiple configurations are provided (comma separated).
1 parent 1dc8b17 commit 1a2aaa6

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
482482

483483
// --library
484484
else if (std::strncmp(argv[i], "--library=", 10) == 0) {
485-
if (!_settings->library.load(argv[i]+10)) {
486-
PrintMessage("cppcheck: Failed to load library file '" + std::string(argv[i]+10) + "'");
485+
if (!_settings->library.load(argv[0], argv[i]+10)) {
486+
PrintMessage("cppcheck: Failed to load library configuration file '" + std::string(argv[i]+10) + "'");
487487
return false;
488488
}
489489
}

lib/library.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,39 @@ Library::Library(const Library &lib) :
4848

4949
Library::~Library() { }
5050

51-
bool Library::load(const char path[])
51+
bool Library::load(const char exename[], const char path[])
5252
{
5353
tinyxml2::XMLDocument doc;
5454

55-
const tinyxml2::XMLError error = doc.LoadFile(path);
56-
if (error != tinyxml2::XML_NO_ERROR)
55+
if (std::strchr(path,',') != NULL) {
56+
bool ret = true;
57+
std::string p(path);
58+
while (p.find(",") != std::string::npos) {
59+
const std::string::size_type pos = p.find(",");
60+
ret &= load(exename, p.substr(0,pos).c_str());
61+
p = p.substr(pos+1);
62+
}
63+
if (!p.empty())
64+
ret &= load(exename, p.c_str());
65+
return ret;
66+
}
67+
68+
FILE *fp = fopen(path, "rt");
69+
if (fp == NULL) {
70+
std::string fullpath(exename);
71+
if (fullpath.find_first_of("/\\") != std::string::npos)
72+
fullpath = fullpath.substr(0, 1U + fullpath.find_last_of("/\\"));
73+
fullpath += path;
74+
fp = fopen(fullpath.c_str(), "rt");
75+
if (fp == NULL) {
76+
fullpath += ".cfg";
77+
fp = fopen(fullpath.c_str(), "rt");
78+
if (fp == NULL)
79+
return false;
80+
}
81+
}
82+
83+
if (doc.LoadFile(fp) != tinyxml2::XML_NO_ERROR)
5784
return false;
5885

5986
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();

lib/library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CPPCHECKLIB Library {
3636
Library(const Library &);
3737
~Library();
3838

39-
bool load(const char path[]);
39+
bool load(const char exename[], const char path[]);
4040

4141
/** get allocation id for function (by name) */
4242
int alloc(const std::string &name) const {

0 commit comments

Comments
 (0)