Cppcheck
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-2015 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 
27  : _terminate(false),
28  debug(false),
29  debugwarnings(false),
30  debugFalsePositive(false),
31  dump(false),
32  exceptionHandling(false),
33  inconclusive(false),
34  jointSuppressionReport(false),
35  experimental(false),
36  _errorsOnly(false),
37  _inlineSuppressions(false),
38  _verbose(false),
39  _force(false),
40  _relativePaths(false),
41  _xml(false), _xml_version(1),
42  _jobs(1),
43  _loadAverage(0),
44  _exitCode(0),
45  _showtime(SHOWTIME_NONE),
46  _maxConfigs(12),
47  enforcedLang(None),
48  reportProgress(false),
49  checkConfiguration(false),
50  checkLibrary(false)
51 {
52  // This assumes the code you are checking is for the same architecture this is compiled on.
53 #if defined(_WIN64)
54  platform(Win64);
55 #elif defined(_WIN32)
57 #else
59 #endif
60 }
61 
62 std::string Settings::addEnabled(const std::string &str)
63 {
64  // Enable parameters may be comma separated...
65  if (str.find(",") != std::string::npos) {
66  std::string::size_type prevPos = 0;
67  std::string::size_type pos = 0;
68  while ((pos = str.find(",", pos)) != std::string::npos) {
69  if (pos == prevPos)
70  return std::string("cppcheck: --enable parameter is empty");
71  const std::string errmsg(addEnabled(str.substr(prevPos, pos - prevPos)));
72  if (!errmsg.empty())
73  return errmsg;
74  ++pos;
75  prevPos = pos;
76  }
77  if (prevPos >= str.length())
78  return std::string("cppcheck: --enable parameter is empty");
79  return addEnabled(str.substr(prevPos));
80  }
81 
82  bool handled = false;
83 
84  static std::set<std::string> id;
85  if (id.empty()) {
86  id.insert("warning");
87  id.insert("style");
88  id.insert("performance");
89  id.insert("portability");
90  id.insert("information");
91  id.insert("missingInclude");
92  id.insert("unusedFunction");
93 #ifdef CHECK_INTERNAL
94  id.insert("internal");
95 #endif
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 if (!handled) {
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: // same as system this code was compile on
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  return true;
158  case Win32W:
159  case Win32A:
160  platformType = type;
161  sizeof_bool = 1; // 4 in Visual C++ 4.2
162  sizeof_short = 2;
163  sizeof_int = 4;
164  sizeof_long = 4;
165  sizeof_long_long = 8;
166  sizeof_float = 4;
167  sizeof_double = 8;
168  sizeof_long_double = 8;
169  sizeof_wchar_t = 2;
170  sizeof_size_t = 4;
171  sizeof_pointer = 4;
172  return true;
173  case Win64:
174  platformType = type;
175  sizeof_bool = 1;
176  sizeof_short = 2;
177  sizeof_int = 4;
178  sizeof_long = 4;
179  sizeof_long_long = 8;
180  sizeof_float = 4;
181  sizeof_double = 8;
182  sizeof_long_double = 8;
183  sizeof_wchar_t = 2;
184  sizeof_size_t = 8;
185  sizeof_pointer = 8;
186  return true;
187  case Unix32:
188  platformType = type;
189  sizeof_bool = 1;
190  sizeof_short = 2;
191  sizeof_int = 4;
192  sizeof_long = 4;
193  sizeof_long_long = 8;
194  sizeof_float = 4;
195  sizeof_double = 8;
196  sizeof_long_double = 12;
197  sizeof_wchar_t = 4;
198  sizeof_size_t = 4;
199  sizeof_pointer = 4;
200  return true;
201  case Unix64:
202  platformType = type;
203  sizeof_bool = 1;
204  sizeof_short = 2;
205  sizeof_int = 4;
206  sizeof_long = 8;
207  sizeof_long_long = 8;
208  sizeof_float = 4;
209  sizeof_double = 8;
210  sizeof_long_double = 16;
211  sizeof_wchar_t = 4;
212  sizeof_size_t = 8;
213  sizeof_pointer = 8;
214  return true;
215  }
216 
217  // unsupported platform
218  return false;
219 }
220 
221 bool Settings::platformFile(const std::string &filename)
222 {
223  (void)filename;
224  /** @todo TBD */
225 
226  return false;
227 }