Skip to content

Commit 61c6359

Browse files
committed
fixed #14384 - added Platform::windows to specify if a platform is a Windows one
1 parent 34b9c45 commit 61c6359

5 files changed

Lines changed: 40 additions & 13 deletions

File tree

lib/platform.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ bool Platform::set(Type t)
6262
case Type::Win32W:
6363
case Type::Win32A:
6464
type = t;
65+
windows = true;
6566
sizeof_bool = 1; // 4 in Visual C++ 4.2
6667
sizeof_short = 2;
6768
sizeof_int = 4;
@@ -79,6 +80,7 @@ bool Platform::set(Type t)
7980
return true;
8081
case Type::Win64:
8182
type = t;
83+
windows = true;
8284
sizeof_bool = 1;
8385
sizeof_short = 2;
8486
sizeof_int = 4;
@@ -231,6 +233,14 @@ bool Platform::loadFromFile(const std::vector<std::string>& paths, const std::st
231233
return loadFromXmlDocument(&doc);
232234
}
233235

236+
static const char* xmlText(const tinyxml2::XMLElement* node, bool& error)
237+
{
238+
const char* const str = node->GetText();
239+
if (!str)
240+
error = true;
241+
return str;
242+
}
243+
234244
static unsigned int xmlTextAsUInt(const tinyxml2::XMLElement* node, bool& error)
235245
{
236246
unsigned int retval = 0;
@@ -239,6 +249,14 @@ static unsigned int xmlTextAsUInt(const tinyxml2::XMLElement* node, bool& error)
239249
return retval;
240250
}
241251

252+
static unsigned int xmlTextAsBool(const tinyxml2::XMLElement* node, bool& error)
253+
{
254+
bool retval = false;
255+
if (node->QueryBoolText(&retval) != tinyxml2::XML_SUCCESS)
256+
error = true;
257+
return retval;
258+
}
259+
242260
bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
243261
{
244262
const tinyxml2::XMLElement * const rootnode = doc->FirstChildElement();
@@ -250,11 +268,9 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
250268
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
251269
const char* name = node->Name();
252270
if (std::strcmp(name, "default-sign") == 0) {
253-
const char* str = node->GetText();
254-
if (str)
271+
const char * const str = xmlText(node, error);
272+
if (!error)
255273
defaultSign = *str;
256-
else
257-
error = true;
258274
} else if (std::strcmp(name, "char_bit") == 0)
259275
char_bit = xmlTextAsUInt(node, error);
260276
else if (std::strcmp(name, "sizeof") == 0) {
@@ -284,6 +300,9 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
284300
sizeof_wchar_t = xmlTextAsUInt(sz, error);
285301
}
286302
}
303+
else if (std::strcmp(node->Name(), "windows") == 0) {
304+
windows = xmlTextAsBool(node, error);
305+
}
287306
}
288307
calculateBitMembers();
289308
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
@@ -10371,7 +10371,7 @@ void Tokenizer::simplifyMicrosoftStringFunctions()
1037110371
if (!mSettings.platform.isWindows())
1037210372
return;
1037310373

10374-
const bool ansi = mSettings.platform.type == Platform::Type::Win32A;
10374+
const bool ansi = (mSettings.platform.type == Platform::Type::Win32A); // TODO: check for UNICODE define instead
1037510375
for (Token *tok = list.front(); tok; tok = tok->next()) {
1037610376
if (tok->strAt(1) != "(")
1037710377
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)