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 #include "utils.h"
23 
24 #include <fstream>
25 #include <set>
26 
28  : _terminate(false),
29  debug(false),
30  debugnormal(false),
31  debugwarnings(false),
32  debugFalsePositive(false),
33  dump(false),
34  exceptionHandling(false),
35  inconclusive(false),
36  jointSuppressionReport(false),
37  experimental(false),
38  quiet(false),
39  _inlineSuppressions(false),
40  _verbose(false),
41  _force(false),
42  _relativePaths(false),
43  _xml(false), _xml_version(1),
44  _jobs(1),
45  _loadAverage(0),
46  _exitCode(0),
47  _showtime(SHOWTIME_NONE),
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  static 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: // 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 }