Cppcheck
settings.cpp
Go to the documentation of this file.
00001 /*
00002  * Cppcheck - A tool for static C/C++ code analysis
00003  * Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "settings.h"
00020 #include "path.h"
00021 #include "preprocessor.h"       // Preprocessor
00022 
00023 #include <fstream>
00024 #include <set>
00025 
00026 Settings::Settings()
00027     : _terminate(false),
00028       debug(false), debugwarnings(false), debugFalsePositive(false),
00029       inconclusive(false), experimental(false),
00030       _errorsOnly(false),
00031       _inlineSuppressions(false),
00032       _verbose(false),
00033       _force(false),
00034       _relativePaths(false),
00035       _xml(false), _xml_version(1),
00036       _jobs(1),
00037       _exitCode(0),
00038       _showtime(0),
00039       _maxConfigs(12),
00040       enforcedLang(None),
00041       reportProgress(false),
00042       checkConfiguration(false)
00043 {
00044     // This assumes the code you are checking is for the same architecture this is compiled on.
00045 #if defined(_WIN64)
00046     platform(Win64);
00047 #elif defined(_WIN32)
00048     platform(Win32A);
00049 #else
00050     platform(Unspecified);
00051 #endif
00052 }
00053 
00054 std::string Settings::addEnabled(const std::string &str)
00055 {
00056     // Enable parameters may be comma separated...
00057     if (str.find(",") != std::string::npos) {
00058         std::string::size_type prevPos = 0;
00059         std::string::size_type pos = 0;
00060         while ((pos = str.find(",", pos)) != std::string::npos) {
00061             if (pos == prevPos)
00062                 return std::string("cppcheck: --enable parameter is empty");
00063             const std::string errmsg(addEnabled(str.substr(prevPos, pos - prevPos)));
00064             if (!errmsg.empty())
00065                 return errmsg;
00066             ++pos;
00067             prevPos = pos;
00068         }
00069         if (prevPos >= str.length())
00070             return std::string("cppcheck: --enable parameter is empty");
00071         return addEnabled(str.substr(prevPos));
00072     }
00073 
00074     bool handled = false;
00075 
00076     static std::set<std::string> id;
00077     if (id.empty()) {
00078         id.insert("warning");
00079         id.insert("style");
00080         id.insert("performance");
00081         id.insert("portability");
00082         id.insert("information");
00083         id.insert("missingInclude");
00084         id.insert("unusedFunction");
00085 #ifndef NDEBUG
00086         id.insert("internal");
00087 #endif
00088     }
00089 
00090     if (str == "all") {
00091         std::set<std::string>::const_iterator it;
00092         for (it = id.begin(); it != id.end(); ++it) {
00093             if (*it == "internal")
00094                 continue;
00095 
00096             _enabled.insert(*it);
00097         }
00098     } else if (id.find(str) != id.end()) {
00099         _enabled.insert(str);
00100         if (str == "information") {
00101             _enabled.insert("missingInclude");
00102         }
00103     } else if (!handled) {
00104         if (str.empty())
00105             return std::string("cppcheck: --enable parameter is empty");
00106         else
00107             return std::string("cppcheck: there is no --enable parameter with the name '" + str + "'");
00108     }
00109 
00110     return std::string("");
00111 }
00112 
00113 bool Settings::isEnabled(const std::string &str) const
00114 {
00115     return bool(_enabled.find(str) != _enabled.end());
00116 }
00117 
00118 
00119 bool Settings::append(const std::string &filename)
00120 {
00121     std::ifstream fin(filename.c_str());
00122     if (!fin.is_open()) {
00123         return false;
00124     }
00125     std::string line;
00126     while (std::getline(fin, line)) {
00127         _append += line + "\n";
00128     }
00129     Preprocessor::preprocessWhitespaces(_append);
00130     return true;
00131 }
00132 
00133 const std::string &Settings::append() const
00134 {
00135     return _append;
00136 }
00137 
00138 bool Settings::platform(PlatformType type)
00139 {
00140     switch (type) {
00141     case Unspecified: // same as system this code was compile on
00142         platformType = type;
00143         sizeof_bool = sizeof(bool);
00144         sizeof_short = sizeof(short);
00145         sizeof_int = sizeof(int);
00146         sizeof_long = sizeof(long);
00147         sizeof_long_long = sizeof(long long);
00148         sizeof_float = sizeof(float);
00149         sizeof_double = sizeof(double);
00150         sizeof_long_double = sizeof(long double);
00151         sizeof_wchar_t = sizeof(wchar_t);
00152         sizeof_size_t = sizeof(std::size_t);
00153         sizeof_pointer = sizeof(void *);
00154         return true;
00155     case Win32W:
00156     case Win32A:
00157         platformType = type;
00158         sizeof_bool = 1; // 4 in Visual C++ 4.2
00159         sizeof_short = 2;
00160         sizeof_int = 4;
00161         sizeof_long = 4;
00162         sizeof_long_long = 8;
00163         sizeof_float = 4;
00164         sizeof_double = 8;
00165         sizeof_long_double = 8;
00166         sizeof_wchar_t = 2;
00167         sizeof_size_t = 4;
00168         sizeof_pointer = 4;
00169         return true;
00170     case Win64:
00171         platformType = type;
00172         sizeof_bool = 1;
00173         sizeof_short = 2;
00174         sizeof_int = 4;
00175         sizeof_long = 4;
00176         sizeof_long_long = 8;
00177         sizeof_float = 4;
00178         sizeof_double = 8;
00179         sizeof_long_double = 8;
00180         sizeof_wchar_t = 2;
00181         sizeof_size_t = 8;
00182         sizeof_pointer = 8;
00183         return true;
00184     case Unix32:
00185         platformType = type;
00186         sizeof_bool = 1;
00187         sizeof_short = 2;
00188         sizeof_int = 4;
00189         sizeof_long = 4;
00190         sizeof_long_long = 8;
00191         sizeof_float = 4;
00192         sizeof_double = 8;
00193         sizeof_long_double = 12;
00194         sizeof_wchar_t = 4;
00195         sizeof_size_t = 4;
00196         sizeof_pointer = 4;
00197         return true;
00198     case Unix64:
00199         platformType = type;
00200         sizeof_bool = 1;
00201         sizeof_short = 2;
00202         sizeof_int = 4;
00203         sizeof_long = 8;
00204         sizeof_long_long = 8;
00205         sizeof_float = 4;
00206         sizeof_double = 8;
00207         sizeof_long_double = 16;
00208         sizeof_wchar_t = 4;
00209         sizeof_size_t = 8;
00210         sizeof_pointer = 8;
00211         return true;
00212     }
00213 
00214     // unsupported platform
00215     return false;
00216 }
00217 
00218 bool Settings::platformFile(const std::string &filename)
00219 {
00220     (void)filename;
00221     /** @todo TBD */
00222 
00223     return false;
00224 }