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