2016-08-13 10:50:03 +02:00
|
|
|
/*
|
2021-08-07 20:51:18 +02:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2021-08-07 20:51:18 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-08-13 10:50:03 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef platformH
|
|
|
|
#define platformH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "config.h"
|
2023-10-21 17:21:12 +02:00
|
|
|
#include "standards.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2018-10-20 15:28:34 +02:00
|
|
|
#include <climits>
|
2023-02-11 10:44:56 +01:00
|
|
|
#include <stdexcept>
|
2016-08-13 10:50:03 +02:00
|
|
|
#include <string>
|
2023-02-08 20:30:43 +01:00
|
|
|
#include <vector>
|
2016-08-13 10:50:03 +02:00
|
|
|
|
|
|
|
/// @addtogroup Core
|
|
|
|
/// @{
|
|
|
|
|
2018-02-15 21:01:34 +01:00
|
|
|
namespace tinyxml2 {
|
|
|
|
class XMLDocument;
|
|
|
|
}
|
|
|
|
|
2023-10-13 16:02:04 +02:00
|
|
|
/**
|
|
|
|
* @brief Platform settings
|
|
|
|
*/
|
|
|
|
class CPPCHECKLIB Platform {
|
|
|
|
private:
|
|
|
|
static long long min_value(int bit) {
|
|
|
|
if (bit >= 64)
|
|
|
|
return LLONG_MIN;
|
|
|
|
return -(1LL << (bit-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static long long max_value(int bit) {
|
|
|
|
if (bit >= 64)
|
|
|
|
return (~0ULL) >> 1;
|
|
|
|
return (1LL << (bit-1)) - 1LL;
|
|
|
|
}
|
2023-10-21 17:21:12 +02:00
|
|
|
|
|
|
|
/** provides list of defines specified by the limit.h/climits includes */
|
|
|
|
std::string getLimitsDefines(bool c99) const;
|
2023-10-13 16:02:04 +02:00
|
|
|
public:
|
|
|
|
Platform();
|
|
|
|
|
|
|
|
bool isIntValue(long long value) const {
|
|
|
|
return value >= min_value(int_bit) && value <= max_value(int_bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isIntValue(unsigned long long value) const {
|
|
|
|
const unsigned long long intMax = max_value(int_bit);
|
|
|
|
return value <= intMax;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isLongValue(long long value) const {
|
|
|
|
return value >= min_value(long_bit) && value <= max_value(long_bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isLongValue(unsigned long long value) const {
|
|
|
|
const unsigned long long longMax = max_value(long_bit);
|
|
|
|
return value <= longMax;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isLongLongValue(unsigned long long value) const {
|
|
|
|
const unsigned long long longLongMax = max_value(long_long_bit);
|
|
|
|
return value <= longLongMax;
|
|
|
|
}
|
|
|
|
|
|
|
|
nonneg int char_bit; /// bits in char
|
|
|
|
nonneg int short_bit; /// bits in short
|
|
|
|
nonneg int int_bit; /// bits in int
|
|
|
|
nonneg int long_bit; /// bits in long
|
|
|
|
nonneg int long_long_bit; /// bits in long long
|
|
|
|
|
|
|
|
/** size of standard types */
|
|
|
|
nonneg int sizeof_bool;
|
|
|
|
nonneg int sizeof_short;
|
|
|
|
nonneg int sizeof_int;
|
|
|
|
nonneg int sizeof_long;
|
|
|
|
nonneg int sizeof_long_long;
|
|
|
|
nonneg int sizeof_float;
|
|
|
|
nonneg int sizeof_double;
|
|
|
|
nonneg int sizeof_long_double;
|
|
|
|
nonneg int sizeof_wchar_t;
|
|
|
|
nonneg int sizeof_size_t;
|
|
|
|
nonneg int sizeof_pointer;
|
|
|
|
|
|
|
|
char defaultSign; // unsigned:'u', signed:'s', unknown:'\0'
|
|
|
|
|
|
|
|
enum Type {
|
|
|
|
Unspecified, // No platform specified
|
|
|
|
Native, // whatever system this code was compiled on
|
|
|
|
Win32A,
|
|
|
|
Win32W,
|
|
|
|
Win64,
|
|
|
|
Unix32,
|
|
|
|
Unix64,
|
|
|
|
File
|
|
|
|
};
|
2016-10-16 11:36:22 +02:00
|
|
|
|
2023-10-13 16:02:04 +02:00
|
|
|
/** platform type */
|
|
|
|
Type type;
|
2020-02-19 07:51:39 +01:00
|
|
|
|
2023-10-13 16:02:04 +02:00
|
|
|
/** set the platform type for predefined platforms - deprecated use set(const std::string&, std::string&) instead */
|
|
|
|
bool set(Type t);
|
2016-10-16 11:36:22 +02:00
|
|
|
|
2023-10-13 16:02:04 +02:00
|
|
|
/** set the platform type */
|
|
|
|
bool set(const std::string& platformstr, std::string& errstr, const std::vector<std::string>& paths = {}, bool verbose = false);
|
2020-02-19 07:51:39 +01:00
|
|
|
|
2023-10-13 16:02:04 +02:00
|
|
|
/**
|
|
|
|
* load platform file
|
|
|
|
* @param exename application path
|
|
|
|
* @param filename platform filename
|
|
|
|
* @param verbose log verbose information about the lookup
|
|
|
|
* @return returns true if file was loaded successfully
|
|
|
|
*/
|
|
|
|
bool loadFromFile(const char exename[], const std::string &filename, bool verbose = false);
|
2018-03-13 13:07:10 +01:00
|
|
|
|
2023-10-13 16:02:04 +02:00
|
|
|
/** load platform from xml document, primarily for testing */
|
|
|
|
bool loadFromXmlDocument(const tinyxml2::XMLDocument *doc);
|
2017-10-18 17:30:47 +02:00
|
|
|
|
2023-10-13 16:02:04 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns true if platform type is Windows
|
|
|
|
* @return true if Windows platform type.
|
|
|
|
*/
|
|
|
|
bool isWindows() const {
|
|
|
|
return type == Type::Win32A ||
|
|
|
|
type == Type::Win32W ||
|
|
|
|
type == Type::Win64;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *toString() const {
|
|
|
|
return toString(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *toString(Type pt) {
|
|
|
|
switch (pt) {
|
|
|
|
case Type::Unspecified:
|
|
|
|
return "unspecified";
|
|
|
|
case Type::Native:
|
|
|
|
return "native";
|
|
|
|
case Type::Win32A:
|
|
|
|
return "win32A";
|
|
|
|
case Type::Win32W:
|
|
|
|
return "win32W";
|
|
|
|
case Type::Win64:
|
|
|
|
return "win64";
|
|
|
|
case Type::Unix32:
|
|
|
|
return "unix32";
|
|
|
|
case Type::Unix64:
|
|
|
|
return "unix64";
|
|
|
|
case Type::File:
|
|
|
|
return "platformFile";
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("unknown platform");
|
2017-10-18 17:30:47 +02:00
|
|
|
}
|
2023-10-13 16:02:04 +02:00
|
|
|
}
|
2017-10-18 17:30:47 +02:00
|
|
|
|
2023-10-13 16:02:04 +02:00
|
|
|
long long unsignedCharMax() const {
|
|
|
|
return max_value(char_bit + 1);
|
|
|
|
}
|
2017-10-18 17:30:47 +02:00
|
|
|
|
2023-10-13 16:02:04 +02:00
|
|
|
long long signedCharMax() const {
|
|
|
|
return max_value(char_bit);
|
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
|
2023-10-13 16:02:04 +02:00
|
|
|
long long signedCharMin() const {
|
|
|
|
return min_value(char_bit);
|
|
|
|
}
|
2023-10-21 17:21:12 +02:00
|
|
|
|
|
|
|
/** provides list of defines specified by the limit.h/climits includes */
|
|
|
|
std::string getLimitsDefines(Standards::cstd_t cstd) const;
|
|
|
|
/** provides list of defines specified by the limit.h/climits includes */
|
|
|
|
std::string getLimitsDefines(Standards::cppstd_t cppstd) const;
|
2023-10-13 16:02:04 +02:00
|
|
|
};
|
2016-08-13 10:50:03 +02:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif // platformH
|