forked from viva64/plog-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
277 lines (245 loc) · 9.64 KB
/
application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// 2006-2008 (c) Viva64.com Team
// 2008-2020 (c) OOO "Program Verification Systems"
#include <args.hxx>
#include "application.h"
#include "configparser.h"
#include "warning.h"
#include "outputfactory.h"
#include "logparserworker.h"
#include "utils.h"
#include <iostream>
#include <cstring>
#include <iterator>
namespace PlogConverter
{
Analyzer ParseEnabledAnalyzer(const std::string &str)
{
Analyzer res;
auto pos = str.find(':');
if (pos != std::string::npos)
{
Split(str.substr(pos + 1), ",", std::back_inserter(res.levels), [](const std::string &s) { return std::stoi(s); });
}
const std::string &name = str.substr(0, pos);
if (name == "GA")
res.type = AnalyzerType::General;
else if (name == "64")
res.type = AnalyzerType::Viva64;
else if (name == "OP")
res.type = AnalyzerType::Optimization;
else if (name == "CS")
res.type = AnalyzerType::CustomerSpecific;
else if (name == "MISRA")
res.type = AnalyzerType::Misra;
else
throw ConfigException("Wrong analyzer name: " + name);
return res;
}
void ParseEnabledAnalyzers(std::string str, std::vector<Analyzer>& analyzers)
{
analyzers.clear();
try
{
if (str == "all" || str == "ALL")
{
return;
}
ReplaceAll(str, "+", ";");
Split(str, ";", std::back_inserter(analyzers), &ParseEnabledAnalyzer);
}
catch (std::exception&)
{
throw ConfigException("Wrong analyzers format: " + str);
}
}
void Application::AddWorker(std::unique_ptr<IWorker> worker)
{
if (!worker)
{
throw std::invalid_argument("worker is nullptr");
}
m_workers.push_back(std::move(worker));
}
int Application::Exec(int argc, const char** argv)
{
try
{
#ifdef _WIN32
std::cout << AppName_Win << std::endl;
#else
std::cout << AppName_Default << std::endl;
#endif
std::cout << "Copyright (c) 2008-2020 OOO \"Program Verification Systems\"" << std::endl;
std::cout << AboutPVSStudio << std::endl;
SetCmdOptions(argc, argv);
SetConfigOptions(m_options.configFile);
if (m_workers.empty())
{
throw std::logic_error("No workers set, nothing to do");
}
if (std::find(m_options.codeMappings.cbegin(), m_options.codeMappings.cend(), SecurityCodeMapping::MISRA) != m_options.codeMappings.cend() &&
!m_options.enabledAnalyzers.empty() &&
std::find_if(m_options.enabledAnalyzers.cbegin(), m_options.enabledAnalyzers.cend(), [](const Analyzer& item) { return item.type == AnalyzerType::Misra; }) == m_options.enabledAnalyzers.cend())
{
std::cout << "MISRA mapping is specified, but MISRA rules group is not enabled. Check the '-" << CmdAnalyzerFlagName_Short <<"' flag." << std::endl;
}
for (int i = 0; i < argc; ++i)
{
for (const char *arg = argv[i]; *arg != '\0'; ++arg)
{
char c = *arg;
if (isalpha(c) || isdigit(c) || strchr(".,_-/", c) != nullptr)
{
m_options.cmdLine += c;
}
else
{
m_options.cmdLine += '\\';
m_options.cmdLine += c;
}
}
m_options.cmdLine += ' ';
}
for (auto &worker : m_workers)
{
worker->Run(m_options);
}
}
catch (const ConfigException& e)
{
const char* err = e.what();
if (err != nullptr && err[0] != '\0')
std::cerr << e.what() << std::endl;
return 1;
}
catch (const std::exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
if (m_options.indicateWarnings)
{
for (auto &worker : m_workers)
{
auto logWorker = dynamic_cast<const PlogConverter::LogParserWorker*>(worker.get());
if (logWorker != nullptr && logWorker->GetPrintedWarnings() > 0)
{
return 2;
}
}
}
return 0;
}
const std::string Application::AppName_Default = "Analyzer log conversion tool.";
const std::string Application::AppName_Win = "Analyzer log conversion tool.";
const std::string Application::AboutPVSStudio = R"(
PVS-Studio is a static code analyzer and SAST (static application security
testing) tool that is available for C and C++ desktop and embedded development,
C# and Java under Windows, Linux and macOS.
)";
const char Application::CmdAnalyzerFlagName_Short = 'a';
const std::string Application::CmdAnalyzerFlagName_Full = "analyzer";
void Application::SetCmdOptions(int argc, const char** argv)
{
using namespace args;
ArgumentParser parser("");
#ifdef _WIN32
parser.Prog(AppName_Win);
#else
parser.Prog(AppName_Default);
#endif
parser.helpParams.usageString = "Usage:";
parser.helpParams.progindent = 0;
parser.helpParams.width = 70;
parser.helpParams.progtailindent = unsigned(parser.helpParams.usageString.length() + parser.Prog().length() + 2);
parser.helpParams.helpindent = 30;
parser.helpParams.flagindent = 4;
parser.helpParams.optionsString = "Options:";
parser.helpParams.proglineShowFlags = true;
parser.helpParams.proglinePreferShortFlags = true;
parser.helpParams.showTerminator = false;
parser.helpParams.showValueName = false;
parser.helpParams.addDefault = true;
parser.helpParams.addChoices = true;
//todo case-insensitive flags/args
HelpFlag helpFlag(parser, "HELP", "Show this help page", { 'h', "help" }, Options::Hidden);
args::MapFlagList<std::string, OutputFactory::AllocFunction> renderTypes(parser, "TYPES", "Render types for output.",
{ 't', "renderTypes" }, outputFactory.getMap());
ValueFlag<std::string> outputFile(parser, "FILE", "Output file.", { 'o', "output" }, Options::Single);
outputFile.HelpDefault("<stdout>");
ValueFlag<std::string> sourceRoot(parser, "PATH", "A path to the project directory.", { 'r', "srcRoot" }, Options::Single);
ValueFlag<std::string> analyzer(parser, "TYPES", "Specifies analyzer(s) and level(s) to be used for filtering, i.e. 'GA:1,2;64:1;OP:1,2,3;CS:1;MISRA:1,2'",
{ CmdAnalyzerFlagName_Short, CmdAnalyzerFlagName_Full }, "GA:1,2", Options::Single);
ValueFlag<std::string> excludedCodes(parser, "CODES", "Error codes to disable, i.e. V112,V122.", { 'd', "excludedCodes" }, Options::Single);
ValueFlag<std::string> settings(parser, "FILE", "Path to PVS-Studio settings file. Can be used to specify additional disabled error codes.",
{ 's', "settings" }, Options::Single);
ValueFlag<std::string> name(parser, "FILENAME", "Template name for resulting output files.", { 'n', "name" }, Options::Single);
MapFlagList<std::string, SecurityCodeMapping> mappingTypes(parser, "NAME", "Enable mapping of PVS-Studio error codes to other rule sets.",
{ 'm',"errorCodeMapping" }, { { "cwe", SecurityCodeMapping::CWE }, { "misra", SecurityCodeMapping::MISRA } });
ValueFlag<std::string> projectName(parser, "PROJNAME", "Name of the project for fullhtml render type.", { 'p', "projectName" }, "", Options::Single);
ValueFlag<std::string> projectVersion(parser, "PROJVERSION", "Version of the project for fullhtml render type.", { 'v', "projectVersion" }, "", Options::Single);
Flag useCerr(parser, "CERR", "Use stderr instead of stdout.", { 'e', "cerr" }, Options::Single);
Flag indicateWarnings(parser, "INDICATEWARNINGS", "Set this option to detect the presense of analyzer warnings after filtering analysis log by setting the converter exit code to '2'.", { 'w', "indicate-warnings" }, Options::Single);
PositionalList<std::string> logs(parser, "logs", "Input files.", Options::Required | Options::HiddenFromDescription);
CompletionFlag comp(parser, {"complete"});
try
{
parser.ParseCLI(argc, argv);
m_options.output = Expand(get(outputFile));
std::transform(get(logs).begin(), get(logs).end(), std::back_inserter(m_options.inputFiles), &Expand);
m_options.projectRoot = Expand(get(sourceRoot));
m_options.formats = get(renderTypes);
m_options.projectName = get(projectName);
m_options.projectVersion = get(projectVersion);
m_options.codeMappings = get(mappingTypes);
m_options.configFile = Expand(get(settings));
m_options.outputName = Expand(get(name));
m_options.useStderr = useCerr;
m_options.indicateWarnings = indicateWarnings;
Split(get(excludedCodes), ",", std::inserter(m_options.disabledWarnings, m_options.disabledWarnings.begin()));
ParseEnabledAnalyzers(get(analyzer), m_options.enabledAnalyzers);
}
catch (Completion &e)
{
std::cout << e.what();
exit(0);
}
catch (Help&)
{
std::cout << parser;
throw ConfigException("");
}
catch (Error &e)
{
std::cerr << e.what() << std::endl;
std::cout << parser;
throw ConfigException("");
}
}
void Application::SetConfigOptions(const std::string& path)
{
if (path.empty())
{
return;
}
const static std::vector<std::string> multiArguments = { "exclude-path" };
std::string enabledAnalyzers;
ConfigParser configParser(path, multiArguments);
configParser.get("errors-off", m_options.disabledWarnings, " ");
configParser.getMulti("exclude-path", m_options.disabledPaths);
configParser.get("disabled-keywords", m_options.disabledKeywords);
configParser.get("enabled-analyzers", enabledAnalyzers);
if (m_options.projectRoot.empty())
{
configParser.get("sourcetree-root", m_options.projectRoot);
if (!m_options.projectRoot.empty())
{
m_options.projectRoot = Expand(m_options.projectRoot);
}
}
if (!enabledAnalyzers.empty())
{
ParseEnabledAnalyzers(enabledAnalyzers, m_options.enabledAnalyzers);
}
}
}