Skip to content

Commit 537b099

Browse files
authored
fixed #14384 - added Platform::windows to specify if a platform is a Windows one (#4960)
1 parent e43606c commit 537b099

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

lib/platform.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ bool Platform::set(Type t)
4040
case Type::Unspecified: // unknown type sizes (sizes etc are set but are not known)
4141
case Type::Native: // same as system this code was compile on
4242
type = t;
43+
windows = false;
4344
sizeof_bool = sizeof(bool);
4445
sizeof_short = sizeof(short);
4546
sizeof_int = sizeof(int);
@@ -62,6 +63,7 @@ bool Platform::set(Type t)
6263
case Type::Win32W:
6364
case Type::Win32A:
6465
type = t;
66+
windows = true;
6567
sizeof_bool = 1; // 4 in Visual C++ 4.2
6668
sizeof_short = 2;
6769
sizeof_int = 4;
@@ -79,6 +81,7 @@ bool Platform::set(Type t)
7981
return true;
8082
case Type::Win64:
8183
type = t;
84+
windows = true;
8285
sizeof_bool = 1;
8386
sizeof_short = 2;
8487
sizeof_int = 4;
@@ -96,6 +99,7 @@ bool Platform::set(Type t)
9699
return true;
97100
case Type::Unix32:
98101
type = t;
102+
windows = false;
99103
sizeof_bool = 1;
100104
sizeof_short = 2;
101105
sizeof_int = 4;
@@ -113,6 +117,7 @@ bool Platform::set(Type t)
113117
return true;
114118
case Type::Unix64:
115119
type = t;
120+
windows = false;
116121
sizeof_bool = 1;
117122
sizeof_short = 2;
118123
sizeof_int = 4;
@@ -138,6 +143,7 @@ bool Platform::set(Type t)
138143

139144
bool Platform::set(const std::string& platformstr, std::string& errstr, const std::vector<std::string>& paths, bool debug)
140145
{
146+
// TODO: needs to be invalidated in case it was already set
141147
if (platformstr == "win32A")
142148
set(Type::Win32A);
143149
else if (platformstr == "win32W")
@@ -231,6 +237,14 @@ bool Platform::loadFromFile(const std::vector<std::string>& paths, const std::st
231237
return loadFromXmlDocument(&doc);
232238
}
233239

240+
static const char* xmlText(const tinyxml2::XMLElement* node, bool& error)
241+
{
242+
const char* const str = node->GetText();
243+
if (!str)
244+
error = true;
245+
return str;
246+
}
247+
234248
static unsigned int xmlTextAsUInt(const tinyxml2::XMLElement* node, bool& error)
235249
{
236250
unsigned int retval = 0;
@@ -239,6 +253,14 @@ static unsigned int xmlTextAsUInt(const tinyxml2::XMLElement* node, bool& error)
239253
return retval;
240254
}
241255

256+
static unsigned int xmlTextAsBool(const tinyxml2::XMLElement* node, bool& error)
257+
{
258+
bool retval = false;
259+
if (node->QueryBoolText(&retval) != tinyxml2::XML_SUCCESS)
260+
error = true;
261+
return retval;
262+
}
263+
242264
bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
243265
{
244266
const tinyxml2::XMLElement * const rootnode = doc->FirstChildElement();
@@ -250,11 +272,9 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
250272
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
251273
const char* name = node->Name();
252274
if (std::strcmp(name, "default-sign") == 0) {
253-
const char* str = node->GetText();
254-
if (str)
275+
const char * const str = xmlText(node, error);
276+
if (!error)
255277
defaultSign = *str;
256-
else
257-
error = true;
258278
} else if (std::strcmp(name, "char_bit") == 0)
259279
char_bit = xmlTextAsUInt(node, error);
260280
else if (std::strcmp(name, "sizeof") == 0) {
@@ -284,6 +304,9 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
284304
sizeof_wchar_t = xmlTextAsUInt(sz, error);
285305
}
286306
}
307+
else if (std::strcmp(node->Name(), "windows") == 0) {
308+
windows = xmlTextAsBool(node, error);
309+
}
287310
}
288311
calculateBitMembers();
289312
type = Type::File;

lib/platform.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class CPPCHECKLIB Platform {
132132

133133
char defaultSign; // unsigned:'u', signed:'s', unknown:'\0'
134134

135+
bool windows{false}; // indicates if the platform is Windows
136+
135137
enum Type : std::uint8_t {
136138
Unspecified, // No platform specified
137139
Native, // whatever system this code was compiled on
@@ -167,9 +169,7 @@ class CPPCHECKLIB Platform {
167169
* @return true if Windows platform type.
168170
*/
169171
bool isWindows() const {
170-
return type == Type::Win32A ||
171-
type == Type::Win32W ||
172-
type == Type::Win64;
172+
return windows;
173173
}
174174

175175
const char *toString() const {

lib/symboldatabase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7851,6 +7851,7 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
78517851

78527852
else if (Token::simpleMatch(tok->previous(), "sizeof (")) {
78537853
ValueType valuetype(ValueType::Sign::UNSIGNED, ValueType::Type::LONG, 0U);
7854+
// TODO: handle via sizeof_size_t instead
78547855
if (mSettings.platform.type == Platform::Type::Win64)
78557856
valuetype.type = ValueType::Type::LONGLONG;
78567857

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10443,7 +10443,7 @@ void Tokenizer::simplifyMicrosoftStringFunctions()
1044310443
if (!mSettings.platform.isWindows())
1044410444
return;
1044510445

10446-
const bool ansi = mSettings.platform.type == Platform::Type::Win32A;
10446+
const bool ansi = (mSettings.platform.type == Platform::Type::Win32A); // TODO: check for UNICODE define instead
1044710447
for (Token *tok = list.front(); tok; tok = tok->next()) {
1044810448
if (tok->strAt(1) != "(")
1044910449
continue;

test/testplatform.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ class TestPlatform : public TestFixture {
3737
TEST_CASE(valid_config_win32w);
3838
TEST_CASE(valid_config_unix32);
3939
TEST_CASE(valid_config_win64);
40+
// TODO: test native and unspecified
4041
TEST_CASE(valid_config_file_1);
4142
TEST_CASE(valid_config_file_2);
42-
TEST_CASE(valid_config_file_3);
4343
TEST_CASE(valid_config_file_4);
4444
TEST_CASE(invalid_config_file_1);
45+
TEST_CASE(invalid_config_file_2);
4546
TEST_CASE(empty_elements);
4647
TEST_CASE(default_platform);
4748
TEST_CASE(limitsDefines);
@@ -210,6 +211,7 @@ class TestPlatform : public TestFixture {
210211
// Similar to the avr8 platform file.
211212
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
212213
"<platform>\n"
214+
" <windows>false</windows>\n"
213215
" <char_bit>8</char_bit>\n"
214216
" <default-sign>unsigned</default-sign>\n"
215217
" <sizeof>\n"
@@ -254,6 +256,7 @@ class TestPlatform : public TestFixture {
254256
// char_bit > 8.
255257
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
256258
"<platform>\n"
259+
" <windows>true</windows>\n"
257260
" <char_bit>20</char_bit>\n"
258261
" <default-sign>signed</default-sign>\n"
259262
" <sizeof>\n"
@@ -273,7 +276,7 @@ class TestPlatform : public TestFixture {
273276
PlatformTest platform;
274277
ASSERT(readPlatform(platform, xmldata));
275278
ASSERT_EQUALS(Platform::Type::File, platform.type);
276-
ASSERT(!platform.isWindows());
279+
ASSERT(platform.isWindows());
277280
ASSERT_EQUALS(20, platform.char_bit);
278281
ASSERT_EQUALS('s', platform.defaultSign);
279282
ASSERT_EQUALS(1, platform.sizeof_bool);
@@ -293,11 +296,12 @@ class TestPlatform : public TestFixture {
293296
ASSERT_EQUALS(100, platform.long_long_bit);
294297
}
295298

296-
void valid_config_file_3() const {
297-
// Valid platform configuration without any usable information.
299+
void invalid_config_file_2() const {
300+
// Invalid platform configuration without any usable information.
298301
// Similar like an empty file.
299302
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
300303
"<platform>\n"
304+
" <windows1>true</windows1>\n"
301305
" <char_bit1>8</char_bit1>\n"
302306
" <default-sign1>unsigned</default-sign1>\n"
303307
" <sizeof1>\n"
@@ -324,6 +328,7 @@ class TestPlatform : public TestFixture {
324328
// set to 0.
325329
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
326330
"<platform>\n"
331+
" <windows>true</windows>\n"
327332
" <char_bit>0</char_bit>\n"
328333
" <default-sign>z</default-sign>\n"
329334
" <sizeof>\n"
@@ -343,7 +348,7 @@ class TestPlatform : public TestFixture {
343348
PlatformTest platform;
344349
ASSERT(readPlatform(platform, xmldata));
345350
ASSERT_EQUALS(Platform::Type::File, platform.type);
346-
ASSERT(!platform.isWindows());
351+
ASSERT(platform.isWindows());
347352
ASSERT_EQUALS(0, platform.char_bit);
348353
ASSERT_EQUALS('z', platform.defaultSign);
349354
ASSERT_EQUALS(0, platform.sizeof_bool);
@@ -367,6 +372,7 @@ class TestPlatform : public TestFixture {
367372
// Invalid XML file: mismatching elements "boolt" vs "bool".
368373
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
369374
"<platform>\n"
375+
" <windows>false</windows>\n"
370376
" <char_bit>8</char_bit>\n"
371377
" <default-sign>unsigned</default-sign>\n"
372378
" <sizeof>\n"
@@ -392,6 +398,7 @@ class TestPlatform : public TestFixture {
392398
// Similar like an empty file.
393399
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
394400
"<platform>\n"
401+
" <windows></windows>\n"
395402
" <char_bit></char_bit>\n"
396403
" <default-sign></default-sign>\n"
397404
" <sizeof>\n"

0 commit comments

Comments
 (0)