Branch data Line data Source code
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 "settings.h"
20 : : #include "path.h"
21 : : #include "preprocessor.h" // Preprocessor
22 : :
23 : : #include <fstream>
24 : : #include <set>
25 : :
26 : 230359 : Settings::Settings()
27 : : : _terminate(false),
28 : : debug(false), debugwarnings(false), debugFalsePositive(false),
29 : : inconclusive(false), experimental(false),
30 : : _errorsOnly(false),
31 : : _inlineSuppressions(false),
32 : : _verbose(false),
33 : : _force(false),
34 : : _relativePaths(false),
35 : : _xml(false), _xml_version(1),
36 : : _jobs(1),
37 : : _exitCode(0),
38 : : _showtime(0),
39 : : _maxConfigs(12),
40 : : enforcedLang(None),
41 : : reportProgress(false),
42 [ + - ][ + - ]: 230359 : checkConfiguration(false)
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
43 : : {
44 : : // This assumes the code you are checking is for the same architecture this is compiled on.
45 : : #if defined(_WIN64)
46 : : platform(Win64);
47 : : #elif defined(_WIN32)
48 : : platform(Win32A);
49 : : #else
50 : 230359 : platform(Unspecified);
51 : : #endif
52 : 230359 : }
53 : :
54 : 306156 : std::string Settings::addEnabled(const std::string &str)
55 : : {
56 : : // Enable parameters may be comma separated...
57 [ + + ]: 306156 : if (str.find(",") != std::string::npos) {
58 : 45 : std::string::size_type prevPos = 0;
59 : 45 : std::string::size_type pos = 0;
60 [ + + ]: 135 : while ((pos = str.find(",", pos)) != std::string::npos) {
61 [ - + ]: 90 : if (pos == prevPos)
62 [ # # ]: 0 : return std::string("cppcheck: --enable parameter is empty");
63 [ + - ][ + - ]: 90 : const std::string errmsg(addEnabled(str.substr(prevPos, pos - prevPos)));
64 [ + - ][ - + ]: 90 : if (!errmsg.empty())
65 [ # # ]: 0 : return errmsg;
66 : 90 : ++pos;
67 [ + - ]: 180 : prevPos = pos;
68 : 90 : }
69 [ - + ]: 45 : if (prevPos >= str.length())
70 [ # # ]: 0 : return std::string("cppcheck: --enable parameter is empty");
71 [ + - ]: 45 : return addEnabled(str.substr(prevPos));
72 : : }
73 : :
74 : 306111 : bool handled = false;
75 : :
76 [ + + ][ + - ]: 306111 : static std::set<std::string> id;
[ + - ][ # # ]
77 [ + + ]: 306111 : if (id.empty()) {
78 [ + - ][ + - ]: 45 : id.insert("warning");
[ + - ]
79 [ + - ][ + - ]: 45 : id.insert("style");
[ + - ]
80 [ + - ][ + - ]: 45 : id.insert("performance");
[ + - ]
81 [ + - ][ + - ]: 45 : id.insert("portability");
[ + - ]
82 [ + - ][ + - ]: 45 : id.insert("information");
[ + - ]
83 [ + - ][ + - ]: 45 : id.insert("missingInclude");
[ + - ]
84 [ + - ][ + - ]: 45 : id.insert("unusedFunction");
[ + - ]
85 : : #ifndef NDEBUG
86 [ + - ][ + - ]: 45 : id.insert("internal");
[ + - ]
87 : : #endif
88 : : }
89 : :
90 [ + + ]: 306111 : if (str == "all") {
91 : 1170 : std::set<std::string>::const_iterator it;
92 [ + + ]: 10530 : for (it = id.begin(); it != id.end(); ++it) {
93 [ + + ]: 9360 : if (*it == "internal")
94 : 1170 : continue;
95 : :
96 : 8190 : _enabled.insert(*it);
97 : : }
98 [ + - ]: 304941 : } else if (id.find(str) != id.end()) {
99 : 304941 : _enabled.insert(str);
100 [ + + ]: 304941 : if (str == "information") {
101 [ + - ][ + - ]: 1201 : _enabled.insert("missingInclude");
[ + - ]
102 : : }
103 [ # # ]: 0 : } else if (!handled) {
104 [ # # ]: 0 : if (str.empty())
105 [ # # ]: 0 : return std::string("cppcheck: --enable parameter is empty");
106 : : else
107 [ # # ]: 0 : return std::string("cppcheck: there is no --enable parameter with the name '" + str + "'");
108 : : }
109 : :
110 [ + - ]: 306156 : return std::string("");
111 : : }
112 : :
113 : 1721681 : bool Settings::isEnabled(const std::string &str) const
114 : : {
115 : 1721681 : return bool(_enabled.find(str) != _enabled.end());
116 : : }
117 : :
118 : :
119 : 0 : bool Settings::append(const std::string &filename)
120 : : {
121 : 0 : std::ifstream fin(filename.c_str());
122 [ # # ][ # # ]: 0 : if (!fin.is_open()) {
123 : 0 : return false;
124 : : }
125 [ # # ]: 0 : std::string line;
126 [ # # ][ # # ]: 0 : while (std::getline(fin, line)) {
[ # # ]
127 [ # # ][ # # ]: 0 : _append += line + "\n";
[ # # ]
128 : : }
129 [ # # ]: 0 : Preprocessor::preprocessWhitespaces(_append);
130 [ # # ]: 0 : return true;
131 : : }
132 : :
133 : 736 : const std::string &Settings::append() const
134 : : {
135 : 736 : return _append;
136 : : }
137 : :
138 : 266640 : bool Settings::platform(PlatformType type)
139 : : {
140 [ + + + + : 266640 : switch (type) {
+ - ]
141 : : case Unspecified: // same as system this code was compile on
142 : 266430 : platformType = type;
143 : 266430 : sizeof_bool = sizeof(bool);
144 : 266430 : sizeof_short = sizeof(short);
145 : 266430 : sizeof_int = sizeof(int);
146 : 266430 : sizeof_long = sizeof(long);
147 : 266430 : sizeof_long_long = sizeof(long long);
148 : 266430 : sizeof_float = sizeof(float);
149 : 266430 : sizeof_double = sizeof(double);
150 : 266430 : sizeof_long_double = sizeof(long double);
151 : 266430 : sizeof_wchar_t = sizeof(wchar_t);
152 : 266430 : sizeof_size_t = sizeof(std::size_t);
153 : 266430 : sizeof_pointer = sizeof(void *);
154 : 266430 : return true;
155 : : case Win32W:
156 : : case Win32A:
157 : 206 : platformType = type;
158 : 206 : sizeof_bool = 1; // 4 in Visual C++ 4.2
159 : 206 : sizeof_short = 2;
160 : 206 : sizeof_int = 4;
161 : 206 : sizeof_long = 4;
162 : 206 : sizeof_long_long = 8;
163 : 206 : sizeof_float = 4;
164 : 206 : sizeof_double = 8;
165 : 206 : sizeof_long_double = 8;
166 : 206 : sizeof_wchar_t = 2;
167 : 206 : sizeof_size_t = 4;
168 : 206 : sizeof_pointer = 4;
169 : 206 : return true;
170 : : case Win64:
171 : 2 : platformType = type;
172 : 2 : sizeof_bool = 1;
173 : 2 : sizeof_short = 2;
174 : 2 : sizeof_int = 4;
175 : 2 : sizeof_long = 4;
176 : 2 : sizeof_long_long = 8;
177 : 2 : sizeof_float = 4;
178 : 2 : sizeof_double = 8;
179 : 2 : sizeof_long_double = 8;
180 : 2 : sizeof_wchar_t = 2;
181 : 2 : sizeof_size_t = 8;
182 : 2 : sizeof_pointer = 8;
183 : 2 : return true;
184 : : case Unix32:
185 : 1 : platformType = type;
186 : 1 : sizeof_bool = 1;
187 : 1 : sizeof_short = 2;
188 : 1 : sizeof_int = 4;
189 : 1 : sizeof_long = 4;
190 : 1 : sizeof_long_long = 8;
191 : 1 : sizeof_float = 4;
192 : 1 : sizeof_double = 8;
193 : 1 : sizeof_long_double = 12;
194 : 1 : sizeof_wchar_t = 4;
195 : 1 : sizeof_size_t = 4;
196 : 1 : sizeof_pointer = 4;
197 : 1 : return true;
198 : : case Unix64:
199 : 1 : platformType = type;
200 : 1 : sizeof_bool = 1;
201 : 1 : sizeof_short = 2;
202 : 1 : sizeof_int = 4;
203 : 1 : sizeof_long = 8;
204 : 1 : sizeof_long_long = 8;
205 : 1 : sizeof_float = 4;
206 : 1 : sizeof_double = 8;
207 : 1 : sizeof_long_double = 16;
208 : 1 : sizeof_wchar_t = 4;
209 : 1 : sizeof_size_t = 8;
210 : 1 : sizeof_pointer = 8;
211 : 1 : return true;
212 : : }
213 : :
214 : : // unsupported platform
215 : 266640 : return false;
216 : : }
217 : :
218 : 0 : bool Settings::platformFile(const std::string &filename)
219 : : {
220 : : (void)filename;
221 : : /** @todo TBD */
222 : :
223 : 0 : return false;
224 : : }
|