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