forked from csutils/csdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser-common.cc
105 lines (90 loc) · 2.96 KB
/
parser-common.cc
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
/*
* Copyright (C) 2020 Red Hat, Inc.
*
* This file is part of csdiff.
*
* csdiff is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* csdiff is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
*/
#include "parser-common.hh"
#include "regex.hh"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/lexical_cast.hpp>
int parseInt(const std::string &str, const int fallback)
{
try {
return boost::lexical_cast<int>(str);
}
catch (boost::bad_lexical_cast &) {
return fallback;
}
}
struct ImpliedAttrDigger::Private {
typedef std::map<std::string, std::string> TMap;
TMap langByChecker;
const RE reToolWarning = RE("^([A-Z_]+)_WARNING$");
};
ImpliedAttrDigger::ImpliedAttrDigger():
d(new Private)
{
d->langByChecker["CLANG_WARNING"] = "c/c++";
d->langByChecker["COMPILER_WARNING"] = "c/c++";
d->langByChecker["CPPCHECK_WARNING"] = "c/c++";
d->langByChecker["GCC_ANALYZER_WARNING"] = "c/c++";
d->langByChecker["PROSPECTOR_WARNING"] = "python";
d->langByChecker["SHELLCHECK_WARNING"] = "shell";
d->langByChecker["SMATCH_WARNING"] = "c/c++";
}
ImpliedAttrDigger::~ImpliedAttrDigger()
{
delete d;
}
void ImpliedAttrDigger::inferLangFromChecker(
Defect *pDef,
const bool onlyIfMissing)
const
{
if (onlyIfMissing && !pDef->language.empty())
// language already assigned
return;
Private::TMap::const_iterator it = d->langByChecker.find(pDef->checker);
if (d->langByChecker.end() == it)
// not found
return;
// found --> assign from map
pDef->language = it->second;
}
void ImpliedAttrDigger::inferToolFromChecker(
Defect *pDef,
const bool onlyIfMissing)
const
{
if (onlyIfMissing && !pDef->tool.empty())
// tool already assigned
return;
boost::smatch sm;
if (boost::regex_match(pDef->checker, sm, d->reToolWarning)) {
// extract tool="gcc-analyzer" out of checker="GCC_ANALYZER_WARNING"
std::string tool = sm[/* tool */ 1];
boost::algorithm::to_lower(tool);
boost::algorithm::replace_all(tool, "_", "-");
if (tool == "compiler")
// we use COMPILER_WARNING for "gcc" due to historical reasons
tool = "gcc";
pDef->tool = std::move(tool);
}
else
// no tool matched --> assume coverity
pDef->tool = "coverity";
}