Cppcheck
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
settings.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2016 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 "preprocessor.h" // Preprocessor
21 #include "utils.h"
22 #include "tinyxml2.h"
23 
24 #include <fstream>
25 #include <set>
26 
28  : _terminated(false),
29  debug(false),
30  debugnormal(false),
31  debugwarnings(false),
32  dump(false),
33  exceptionHandling(false),
34  inconclusive(false),
35  jointSuppressionReport(false),
36  experimental(false),
37  quiet(false),
38  inlineSuppressions(false),
39  verbose(false),
40  force(false),
41  relativePaths(false),
42  xml(false), xml_version(1),
43  jobs(1),
44  loadAverage(0),
45  exitCode(0),
46  showtime(SHOWTIME_NONE),
47  preprocessOnly(false),
48  maxConfigs(12),
49  enforcedLang(None),
50  reportProgress(false),
51  checkConfiguration(false),
52  checkLibrary(false)
53 {
54  // This assumes the code you are checking is for the same architecture this is compiled on.
55 #if defined(_WIN64)
56  platform(Win64);
57 #elif defined(_WIN32)
59 #else
61 #endif
62 }
63 
64 namespace {
65  const std::set<std::string> id = make_container< std::set<std::string> > ()
66  << "warning"
67  << "style"
68  << "performance"
69  << "portability"
70  << "information"
71  << "missingInclude"
72  << "unusedFunction"
73 #ifdef CHECK_INTERNAL
74  << "internal"
75 #endif
76  ;
77 }
78 std::string Settings::addEnabled(const std::string &str)
79 {
80  // Enable parameters may be comma separated...
81  if (str.find(',') != std::string::npos) {
82  std::string::size_type prevPos = 0;
83  std::string::size_type pos = 0;
84  while ((pos = str.find(',', pos)) != std::string::npos) {
85  if (pos == prevPos)
86  return std::string("cppcheck: --enable parameter is empty");
87  const std::string errmsg(addEnabled(str.substr(prevPos, pos - prevPos)));
88  if (!errmsg.empty())
89  return errmsg;
90  ++pos;
91  prevPos = pos;
92  }
93  if (prevPos >= str.length())
94  return std::string("cppcheck: --enable parameter is empty");
95  return addEnabled(str.substr(prevPos));
96  }
97 
98  if (str == "all") {
99  std::set<std::string>::const_iterator it;
100  for (it = id.begin(); it != id.end(); ++it) {
101  if (*it == "internal")
102  continue;
103 
104  _enabled.insert(*it);
105  }
106  } else if (id.find(str) != id.end()) {
107  _enabled.insert(str);
108  if (str == "information") {
109  _enabled.insert("missingInclude");
110  }
111  } else {
112  if (str.empty())
113  return std::string("cppcheck: --enable parameter is empty");
114  else
115  return std::string("cppcheck: there is no --enable parameter with the name '" + str + "'");
116  }
117 
118  return std::string("");
119 }
120 
121 
122 bool Settings::append(const std::string &filename)
123 {
124  std::ifstream fin(filename.c_str());
125  if (!fin.is_open()) {
126  return false;
127  }
128  std::string line;
129  while (std::getline(fin, line)) {
130  _append += line + "\n";
131  }
133  return true;
134 }
135 
136 const std::string &Settings::append() const
137 {
138  return _append;
139 }
140 
142 {
143  switch (type) {
144  case Unspecified:
145  platformType = type;
146  sizeof_bool = sizeof(bool);
147  sizeof_short = sizeof(short);
148  sizeof_int = sizeof(int);
149  sizeof_long = sizeof(long);
150  sizeof_long_long = sizeof(long long);
151  sizeof_float = sizeof(float);
152  sizeof_double = sizeof(double);
153  sizeof_long_double = sizeof(long double);
154  sizeof_wchar_t = sizeof(wchar_t);
155  sizeof_size_t = sizeof(std::size_t);
156  sizeof_pointer = sizeof(void *);
157  defaultSign = '\0';
158  char_bit = 8;
163  return true;
164  case Native: // same as system this code was compile on
165  platformType = type;
166  sizeof_bool = sizeof(bool);
167  sizeof_short = sizeof(short);
168  sizeof_int = sizeof(int);
169  sizeof_long = sizeof(long);
170  sizeof_long_long = sizeof(long long);
171  sizeof_float = sizeof(float);
172  sizeof_double = sizeof(double);
173  sizeof_long_double = sizeof(long double);
174  sizeof_wchar_t = sizeof(wchar_t);
175  sizeof_size_t = sizeof(std::size_t);
176  sizeof_pointer = sizeof(void *);
177  {
178  char x = -1;
179  defaultSign = (x < 0) ? 's' : 'u';
180  }
181  char_bit = 8;
186  return true;
187  case Win32W:
188  case Win32A:
189  platformType = type;
190  sizeof_bool = 1; // 4 in Visual C++ 4.2
191  sizeof_short = 2;
192  sizeof_int = 4;
193  sizeof_long = 4;
194  sizeof_long_long = 8;
195  sizeof_float = 4;
196  sizeof_double = 8;
197  sizeof_long_double = 8;
198  sizeof_wchar_t = 2;
199  sizeof_size_t = 4;
200  sizeof_pointer = 4;
201  defaultSign = '\0';
202  char_bit = 8;
207  return true;
208  case Win64:
209  platformType = type;
210  sizeof_bool = 1;
211  sizeof_short = 2;
212  sizeof_int = 4;
213  sizeof_long = 4;
214  sizeof_long_long = 8;
215  sizeof_float = 4;
216  sizeof_double = 8;
217  sizeof_long_double = 8;
218  sizeof_wchar_t = 2;
219  sizeof_size_t = 8;
220  sizeof_pointer = 8;
221  defaultSign = '\0';
222  char_bit = 8;
227  return true;
228  case Unix32:
229  platformType = type;
230  sizeof_bool = 1;
231  sizeof_short = 2;
232  sizeof_int = 4;
233  sizeof_long = 4;
234  sizeof_long_long = 8;
235  sizeof_float = 4;
236  sizeof_double = 8;
237  sizeof_long_double = 12;
238  sizeof_wchar_t = 4;
239  sizeof_size_t = 4;
240  sizeof_pointer = 4;
241  defaultSign = '\0';
242  char_bit = 8;
247  return true;
248  case Unix64:
249  platformType = type;
250  sizeof_bool = 1;
251  sizeof_short = 2;
252  sizeof_int = 4;
253  sizeof_long = 8;
254  sizeof_long_long = 8;
255  sizeof_float = 4;
256  sizeof_double = 8;
257  sizeof_long_double = 16;
258  sizeof_wchar_t = 4;
259  sizeof_size_t = 8;
260  sizeof_pointer = 8;
261  defaultSign = '\0';
262  char_bit = 8;
267  return true;
268  }
269 
270  // unsupported platform
271  return false;
272 }
273 
274 bool Settings::platformFile(const std::string &filename)
275 {
276  // open file..
277  tinyxml2::XMLDocument doc;
278  if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS)
279  return false;
280 
281  const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
282 
283  if (!rootnode || std::strcmp(rootnode->Name(),"platform") != 0)
284  return false;
285 
286  for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
287  if (std::strcmp(node->Name(), "default-sign") == 0)
288  defaultSign = *node->GetText();
289  else if (std::strcmp(node->Name(), "char_bit") == 0)
290  char_bit = std::atoi(node->GetText());
291  else if (std::strcmp(node->Name(), "sizeof") == 0) {
292  for (const tinyxml2::XMLElement *sz = node->FirstChildElement(); sz; sz = sz->NextSiblingElement()) {
293  if (std::strcmp(node->Name(), "short") == 0)
294  sizeof_short = std::atoi(node->GetText());
295  else if (std::strcmp(node->Name(), "int") == 0)
296  sizeof_int = std::atoi(node->GetText());
297  else if (std::strcmp(node->Name(), "long") == 0)
298  sizeof_long = std::atoi(node->GetText());
299  else if (std::strcmp(node->Name(), "long-long") == 0)
300  sizeof_long_long = std::atoi(node->GetText());
301  else if (std::strcmp(node->Name(), "float") == 0)
302  sizeof_float = std::atoi(node->GetText());
303  else if (std::strcmp(node->Name(), "double") == 0)
304  sizeof_double = std::atoi(node->GetText());
305  else if (std::strcmp(node->Name(), "long-double") == 0)
306  sizeof_long_double = std::atoi(node->GetText());
307  else if (std::strcmp(node->Name(), "pointer") == 0)
308  sizeof_pointer = std::atoi(node->GetText());
309  else if (std::strcmp(node->Name(), "size_t") == 0)
310  sizeof_size_t = std::atoi(node->GetText());
311  else if (std::strcmp(node->Name(), "wchar_t") == 0)
312  sizeof_wchar_t = std::atoi(node->GetText());
313  }
314  }
315  }
316 
321 
322  return true;
323 }
unsigned int long_bit
bits in int
Definition: settings.h:249
unsigned int sizeof_short
Definition: settings.h:254
unsigned int sizeof_bool
bits in long long
Definition: settings.h:253
PlatformType
Definition: settings.h:267
PlatformType platformType
platform type
Definition: settings.h:278
const std::string & append() const
get append code (–append)
Definition: settings.cpp:136
unsigned int sizeof_int
Definition: settings.h:255
unsigned int sizeof_double
Definition: settings.h:259
unsigned int sizeof_wchar_t
Definition: settings.h:261
unsigned int char_bit
Definition: settings.h:246
bool platform(PlatformType type)
set the platform type for predefined platforms
Definition: settings.cpp:141
unsigned int sizeof_long_long
Definition: settings.h:257
static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &_settings)
std::string _append
Code to append in the checks.
Definition: settings.h:47
Settings()
Definition: settings.cpp:27
static void preprocessWhitespaces(std::string &processedFile)
preprocess all whitespaces
unsigned int short_bit
bits in char
Definition: settings.h:247
unsigned int sizeof_pointer
Definition: settings.h:263
std::set< std::string > _enabled
enable extra checks by id
Definition: settings.h:50
unsigned int sizeof_long
Definition: settings.h:256
std::string addEnabled(const std::string &str)
Enable extra checks by id.
Definition: settings.cpp:78
unsigned int sizeof_long_double
Definition: settings.h:260
unsigned int int_bit
bits in short
Definition: settings.h:248
unsigned int long_long_bit
bits in long
Definition: settings.h:250
char defaultSign
Definition: settings.h:265
unsigned int sizeof_float
Definition: settings.h:258
bool platformFile(const std::string &filename)
set the platform type for user specified platforms
Definition: settings.cpp:274
unsigned int sizeof_size_t
Definition: settings.h:262