Skip to content

Commit 7f1fe97

Browse files
committed
extracted AddonInfo into separate file / minor AddonInfo interface cleanup
1 parent 74ad724 commit 7f1fe97

File tree

6 files changed

+300
-237
lines changed

6 files changed

+300
-237
lines changed

Makefile

Lines changed: 123 additions & 119 deletions
Large diffs are not rendered by default.

lib/addoninfo.cpp

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2023 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 "addoninfo.h"
20+
21+
#include "path.h"
22+
#include "utils.h"
23+
24+
#include <fstream>
25+
#include <sstream>
26+
#include <string>
27+
28+
#define PICOJSON_USE_INT64
29+
#include <picojson.h>
30+
31+
static std::string getFullPath(const std::string &fileName, const std::string &exename) {
32+
if (Path::fileExists(fileName))
33+
return fileName;
34+
35+
const std::string exepath = Path::getPathFromFilename(exename);
36+
if (Path::fileExists(exepath + fileName))
37+
return exepath + fileName;
38+
if (Path::fileExists(exepath + "addons/" + fileName))
39+
return exepath + "addons/" + fileName;
40+
41+
#ifdef FILESDIR
42+
if (Path::fileExists(FILESDIR + ("/" + fileName)))
43+
return FILESDIR + ("/" + fileName);
44+
if (Path::fileExists(FILESDIR + ("/addons/" + fileName)))
45+
return FILESDIR + ("/addons/" + fileName);
46+
#endif
47+
return "";
48+
}
49+
50+
static std::string parseAddonInfo(AddonInfo& addoninfo, const picojson::value &json, const std::string &fileName, const std::string &exename) {
51+
const std::string& json_error = picojson::get_last_error();
52+
if (!json_error.empty()) {
53+
return "Loading " + fileName + " failed. " + json_error;
54+
}
55+
if (!json.is<picojson::object>())
56+
return "Loading " + fileName + " failed. Bad json.";
57+
picojson::object obj = json.get<picojson::object>();
58+
if (obj.count("args")) {
59+
if (!obj["args"].is<picojson::array>())
60+
return "Loading " + fileName + " failed. args must be array.";
61+
for (const picojson::value &v : obj["args"].get<picojson::array>())
62+
addoninfo.args += " " + v.get<std::string>();
63+
}
64+
65+
if (obj.count("ctu")) {
66+
// ctu is specified in the config file
67+
if (!obj["ctu"].is<bool>())
68+
return "Loading " + fileName + " failed. ctu must be boolean.";
69+
addoninfo.ctu = obj["ctu"].get<bool>();
70+
} else {
71+
addoninfo.ctu = false;
72+
}
73+
74+
if (obj.count("python")) {
75+
// Python was defined in the config file
76+
if (obj["python"].is<picojson::array>()) {
77+
return "Loading " + fileName +" failed. python must not be an array.";
78+
}
79+
addoninfo.python = obj["python"].get<std::string>();
80+
} else {
81+
addoninfo.python = "";
82+
}
83+
84+
if (obj.count("executable")) {
85+
if (!obj["executable"].is<std::string>())
86+
return "Loading " + fileName + " failed. executable must be a string.";
87+
addoninfo.executable = getFullPath(obj["executable"].get<std::string>(), fileName);
88+
return "";
89+
}
90+
91+
return addoninfo.getAddonInfo(obj["script"].get<std::string>(), exename);
92+
}
93+
94+
std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::string &exename) {
95+
if (fileName[0] == '{') {
96+
std::istringstream in(fileName);
97+
picojson::value json;
98+
in >> json;
99+
return parseAddonInfo(*this, json, fileName, exename);
100+
}
101+
if (fileName.find('.') == std::string::npos)
102+
return getAddonInfo(fileName + ".py", exename);
103+
104+
if (endsWith(fileName, ".py")) {
105+
scriptFile = getFullPath(fileName, exename);
106+
if (scriptFile.empty())
107+
return "Did not find addon " + fileName;
108+
109+
std::string::size_type pos1 = scriptFile.rfind('/');
110+
if (pos1 == std::string::npos)
111+
pos1 = 0;
112+
else
113+
pos1++;
114+
std::string::size_type pos2 = scriptFile.rfind('.');
115+
if (pos2 < pos1)
116+
pos2 = std::string::npos;
117+
name = scriptFile.substr(pos1, pos2 - pos1);
118+
119+
runScript = getFullPath("runaddon.py", exename);
120+
121+
return "";
122+
}
123+
124+
if (!endsWith(fileName, ".json"))
125+
return "Failed to open addon " + fileName;
126+
127+
std::ifstream fin(fileName);
128+
if (!fin.is_open())
129+
return "Failed to open " + fileName;
130+
picojson::value json;
131+
fin >> json;
132+
return parseAddonInfo(*this, json, fileName, exename);
133+
}

lib/addoninfo.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2023 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 addonInfoH
20+
#define addonInfoH
21+
22+
#include <string>
23+
24+
struct AddonInfo {
25+
std::string name;
26+
std::string scriptFile; // addon script
27+
std::string executable; // addon executable
28+
std::string args; // special extra arguments
29+
std::string python; // script interpreter
30+
bool ctu = false;
31+
std::string runScript;
32+
33+
std::string getAddonInfo(const std::string &fileName, const std::string &exename);
34+
};
35+
36+
#endif // addonInfoH

lib/cppcheck.cpp

Lines changed: 2 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18+
1819
#include "cppcheck.h"
1920

21+
#include "addoninfo.h"
2022
#include "check.h"
2123
#include "checkunusedfunctions.h"
2224
#include "clangimport.h"
@@ -105,122 +107,6 @@ namespace {
105107
};
106108
}
107109

108-
namespace {
109-
struct AddonInfo {
110-
std::string name;
111-
std::string scriptFile; // addon script
112-
std::string executable; // addon executable
113-
std::string args; // special extra arguments
114-
std::string python; // script interpreter
115-
bool ctu = false;
116-
std::string runScript;
117-
118-
static std::string getFullPath(const std::string &fileName, const std::string &exename) {
119-
if (Path::isFile(fileName))
120-
return fileName;
121-
122-
const std::string exepath = Path::getPathFromFilename(exename);
123-
if (Path::isFile(exepath + fileName))
124-
return exepath + fileName;
125-
if (Path::isFile(exepath + "addons/" + fileName))
126-
return exepath + "addons/" + fileName;
127-
128-
#ifdef FILESDIR
129-
if (Path::isFile(FILESDIR + ("/" + fileName)))
130-
return FILESDIR + ("/" + fileName);
131-
if (Path::isFile(FILESDIR + ("/addons/" + fileName)))
132-
return FILESDIR + ("/addons/" + fileName);
133-
#endif
134-
return "";
135-
}
136-
137-
std::string parseAddonInfo(const picojson::value &json, const std::string &fileName, const std::string &exename) {
138-
const std::string& json_error = picojson::get_last_error();
139-
if (!json_error.empty()) {
140-
return "Loading " + fileName + " failed. " + json_error;
141-
}
142-
if (!json.is<picojson::object>())
143-
return "Loading " + fileName + " failed. Bad json.";
144-
picojson::object obj = json.get<picojson::object>();
145-
if (obj.count("args")) {
146-
if (!obj["args"].is<picojson::array>())
147-
return "Loading " + fileName + " failed. args must be array.";
148-
for (const picojson::value &v : obj["args"].get<picojson::array>())
149-
args += " " + v.get<std::string>();
150-
}
151-
152-
if (obj.count("ctu")) {
153-
// ctu is specified in the config file
154-
if (!obj["ctu"].is<bool>())
155-
return "Loading " + fileName + " failed. ctu must be boolean.";
156-
ctu = obj["ctu"].get<bool>();
157-
} else {
158-
ctu = false;
159-
}
160-
161-
if (obj.count("python")) {
162-
// Python was defined in the config file
163-
if (obj["python"].is<picojson::array>()) {
164-
return "Loading " + fileName +" failed. python must not be an array.";
165-
}
166-
python = obj["python"].get<std::string>();
167-
} else {
168-
python = "";
169-
}
170-
171-
if (obj.count("executable")) {
172-
if (!obj["executable"].is<std::string>())
173-
return "Loading " + fileName + " failed. executable must be a string.";
174-
executable = getFullPath(obj["executable"].get<std::string>(), fileName);
175-
return "";
176-
}
177-
178-
return getAddonInfo(obj["script"].get<std::string>(), exename);
179-
}
180-
181-
std::string getAddonInfo(const std::string &fileName, const std::string &exename) {
182-
if (fileName[0] == '{') {
183-
std::istringstream in(fileName);
184-
picojson::value json;
185-
in >> json;
186-
return parseAddonInfo(json, fileName, exename);
187-
}
188-
if (fileName.find('.') == std::string::npos)
189-
return getAddonInfo(fileName + ".py", exename);
190-
191-
if (endsWith(fileName, ".py")) {
192-
scriptFile = getFullPath(fileName, exename);
193-
if (scriptFile.empty())
194-
return "Did not find addon " + fileName;
195-
196-
std::string::size_type pos1 = scriptFile.rfind('/');
197-
if (pos1 == std::string::npos)
198-
pos1 = 0;
199-
else
200-
pos1++;
201-
std::string::size_type pos2 = scriptFile.rfind('.');
202-
if (pos2 < pos1)
203-
pos2 = std::string::npos;
204-
name = scriptFile.substr(pos1, pos2 - pos1);
205-
206-
runScript = getFullPath("runaddon.py", exename);
207-
208-
return "";
209-
}
210-
211-
if (!endsWith(fileName, ".json"))
212-
return "Failed to open addon " + fileName;
213-
214-
std::ifstream fin(fileName);
215-
if (!fin.is_open())
216-
return "Failed to open " + fileName;
217-
picojson::value json;
218-
fin >> json;
219-
return parseAddonInfo(json, fileName, exename);
220-
}
221-
};
222-
}
223-
224110
static std::string cmdFileName(std::string f)
225111
{
226112
f = Path::toNativeSeparators(f);

lib/cppcheck.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<ItemGroup Label="SourceFiles">
3737
<ClCompile Include="..\externals\simplecpp\simplecpp.cpp" />
3838
<ClCompile Include="..\externals\tinyxml2\tinyxml2.cpp" />
39+
<ClCompile Include="addoninfo.cpp" />
3940
<ClCompile Include="analyzerinfo.cpp" />
4041
<ClCompile Include="astutils.cpp" />
4142
<ClCompile Include="check.cpp">
@@ -109,6 +110,7 @@
109110
<ItemGroup Label="HeaderFiles">
110111
<ClInclude Include="..\externals\simplecpp\simplecpp.h" />
111112
<ClInclude Include="..\externals\tinyxml2\tinyxml2.h" />
113+
<ClInclude Include="addoninfo.h" />
112114
<ClInclude Include="analyzer.h" />
113115
<ClInclude Include="analyzerinfo.h" />
114116
<ClInclude Include="astutils.h" />

lib/lib.pri

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
include($$PWD/pcrerules.pri)
44
include($$PWD/../externals/externals.pri)
55
INCLUDEPATH += $$PWD
6-
HEADERS += $${PWD}/analyzer.h \
6+
HEADERS += $${PWD}/addoninfo.h \
7+
$${PWD}/analyzer.h \
78
$${PWD}/analyzerinfo.h \
89
$${PWD}/astutils.h \
910
$${PWD}/calculate.h \
@@ -74,7 +75,8 @@ HEADERS += $${PWD}/analyzer.h \
7475
$${PWD}/version.h \
7576
$${PWD}/vfvalue.h
7677

77-
SOURCES += $${PWD}/analyzerinfo.cpp \
78+
SOURCES += $${PWD}/addoninfo.cpp \
79+
$${PWD}/analyzerinfo.cpp \
7880
$${PWD}/astutils.cpp \
7981
$${PWD}/check.cpp \
8082
$${PWD}/check64bit.cpp \

0 commit comments

Comments
 (0)