Cppcheck
platform.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 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 //---------------------------------------------------------------------------
20 #ifndef platformH
21 #define platformH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 #include "standards.h"
26 
27 #include <climits>
28 #include <cstddef>
29 #include <stdexcept>
30 #include <string>
31 #include <vector>
32 
33 /// @addtogroup Core
34 /// @{
35 
36 namespace tinyxml2 {
37  class XMLDocument;
38 }
39 
40 /**
41  * @brief Platform settings
42  */
44 private:
45  static long long min_value(int bit) {
46  if (bit >= 64)
47  return LLONG_MIN;
48  return -(1LL << (bit-1));
49  }
50 
51  static long long max_value(int bit) {
52  if (bit >= 64)
53  return (~0ULL) >> 1;
54  return (1LL << (bit-1)) - 1LL;
55  }
56 
57  /** provides list of defines specified by the limit.h/climits includes */
58  std::string getLimitsDefines(bool c99) const;
59 public:
60  Platform();
61 
62  bool isIntValue(long long value) const {
63  return value >= min_value(int_bit) && value <= max_value(int_bit);
64  }
65 
66  bool isIntValue(unsigned long long value) const {
67  const unsigned long long intMax = max_value(int_bit);
68  return value <= intMax;
69  }
70 
71  bool isLongValue(long long value) const {
72  return value >= min_value(long_bit) && value <= max_value(long_bit);
73  }
74 
75  bool isLongValue(unsigned long long value) const {
76  const unsigned long long longMax = max_value(long_bit);
77  return value <= longMax;
78  }
79 
80  bool isLongLongValue(unsigned long long value) const {
81  const unsigned long long longLongMax = max_value(long_long_bit);
82  return value <= longLongMax;
83  }
84 
85  nonneg int char_bit; /// bits in char
86  nonneg int short_bit; /// bits in short
87  nonneg int int_bit; /// bits in int
88  nonneg int long_bit; /// bits in long
89  nonneg int long_long_bit; /// bits in long long
90 
91  /** size of standard types */
92  std::size_t sizeof_bool;
93  std::size_t sizeof_short;
94  std::size_t sizeof_int;
95  std::size_t sizeof_long;
96  std::size_t sizeof_long_long;
97  std::size_t sizeof_float;
98  std::size_t sizeof_double;
99  std::size_t sizeof_long_double;
100  std::size_t sizeof_wchar_t;
101  std::size_t sizeof_size_t;
102  std::size_t sizeof_pointer;
103 
104  char defaultSign; // unsigned:'u', signed:'s', unknown:'\0'
105 
106  enum Type {
107  Unspecified, // No platform specified
108  Native, // whatever system this code was compiled on
114  File
115  };
116 
117  /** platform type */
119 
120  /** set the platform type for predefined platforms - deprecated use set(const std::string&, std::string&) instead */
121  bool set(Type t);
122 
123  /** set the platform type */
124  bool set(const std::string& platformstr, std::string& errstr, const std::vector<std::string>& paths = {}, bool verbose = false);
125 
126  /**
127  * load platform file
128  * @param exename application path
129  * @param filename platform filename
130  * @param verbose log verbose information about the lookup
131  * @return returns true if file was loaded successfully
132  */
133  bool loadFromFile(const char exename[], const std::string &filename, bool verbose = false);
134 
135  /** load platform from xml document, primarily for testing */
136  bool loadFromXmlDocument(const tinyxml2::XMLDocument *doc);
137 
138  /**
139  * @brief Returns true if platform type is Windows
140  * @return true if Windows platform type.
141  */
142  bool isWindows() const {
143  return type == Type::Win32A ||
144  type == Type::Win32W ||
145  type == Type::Win64;
146  }
147 
148  const char *toString() const {
149  return toString(type);
150  }
151 
152  static const char *toString(Type pt) {
153  switch (pt) {
154  case Type::Unspecified:
155  return "unspecified";
156  case Type::Native:
157  return "native";
158  case Type::Win32A:
159  return "win32A";
160  case Type::Win32W:
161  return "win32W";
162  case Type::Win64:
163  return "win64";
164  case Type::Unix32:
165  return "unix32";
166  case Type::Unix64:
167  return "unix64";
168  case Type::File:
169  return "platformFile";
170  default:
171  throw std::runtime_error("unknown platform");
172  }
173  }
174 
175  long long unsignedCharMax() const {
176  return max_value(char_bit + 1);
177  }
178 
179  long long signedCharMax() const {
180  return max_value(char_bit);
181  }
182 
183  long long signedCharMin() const {
184  return min_value(char_bit);
185  }
186 
187  /** provides list of defines specified by the limit.h/climits includes */
188  std::string getLimitsDefines(Standards::cstd_t cstd) const;
189  /** provides list of defines specified by the limit.h/climits includes */
190  std::string getLimitsDefines(Standards::cppstd_t cppstd) const;
191 };
192 
193 /// @}
194 //---------------------------------------------------------------------------
195 #endif // platformH
Platform settings.
Definition: platform.h:43
nonneg int short_bit
bits in char
Definition: platform.h:86
bool isLongValue(unsigned long long value) const
Definition: platform.h:75
nonneg int int_bit
bits in short
Definition: platform.h:87
std::size_t sizeof_bool
bits in long long
Definition: platform.h:92
std::size_t sizeof_long_long
Definition: platform.h:96
char defaultSign
Definition: platform.h:104
bool isLongLongValue(unsigned long long value) const
Definition: platform.h:80
std::size_t sizeof_long_double
Definition: platform.h:99
static const char * toString(Type pt)
Definition: platform.h:152
nonneg int long_bit
bits in int
Definition: platform.h:88
@ Unix64
Definition: platform.h:113
@ Native
Definition: platform.h:108
@ Unspecified
Definition: platform.h:107
@ Win32A
Definition: platform.h:109
@ Unix32
Definition: platform.h:112
@ Win32W
Definition: platform.h:110
bool isLongValue(long long value) const
Definition: platform.h:71
long long unsignedCharMax() const
Definition: platform.h:175
bool isIntValue(long long value) const
Definition: platform.h:62
bool isIntValue(unsigned long long value) const
Definition: platform.h:66
std::size_t sizeof_short
Definition: platform.h:93
std::size_t sizeof_int
Definition: platform.h:94
std::size_t sizeof_pointer
Definition: platform.h:102
static long long max_value(int bit)
Definition: platform.h:51
std::size_t sizeof_size_t
Definition: platform.h:101
nonneg int char_bit
Definition: platform.h:85
bool isWindows() const
Returns true if platform type is Windows.
Definition: platform.h:142
long long signedCharMin() const
Definition: platform.h:183
std::size_t sizeof_float
Definition: platform.h:97
long long signedCharMax() const
Definition: platform.h:179
const char * toString() const
Definition: platform.h:148
nonneg int long_long_bit
bits in long
Definition: platform.h:89
Type type
platform type
Definition: platform.h:118
std::size_t sizeof_double
Definition: platform.h:98
std::size_t sizeof_long
Definition: platform.h:95
std::size_t sizeof_wchar_t
Definition: platform.h:100
static long long min_value(int bit)
Definition: platform.h:45
Information about a class type.
std::string toString(Color c)
Definition: color.cpp:54
#define CPPCHECKLIB
Definition: config.h:35
#define nonneg
Definition: config.h:138
Definition: check.h:31
cstd_t
C code standard.
Definition: standards.h:40
cppstd_t
C++ code standard.
Definition: standards.h:43