Implemented error handling in cppcheck::Platform::loadFromXmlDocument(), enabled related unit test (#8409) (#4181)
This commit is contained in:
parent
86a8d88729
commit
44097b59ff
|
@ -191,6 +191,14 @@ bool cppcheck::Platform::loadPlatformFile(const char exename[], const std::strin
|
||||||
return loadFromXmlDocument(&doc);
|
return loadFromXmlDocument(&doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static unsigned int xmlTextAsUInt(const tinyxml2::XMLElement* node, bool& error)
|
||||||
|
{
|
||||||
|
unsigned int retval = 0;
|
||||||
|
if (node->QueryUnsignedText(&retval) != tinyxml2::XML_SUCCESS)
|
||||||
|
error = true;
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
bool cppcheck::Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
|
bool cppcheck::Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
|
||||||
{
|
{
|
||||||
const tinyxml2::XMLElement * const rootnode = doc->FirstChildElement();
|
const tinyxml2::XMLElement * const rootnode = doc->FirstChildElement();
|
||||||
|
@ -198,35 +206,40 @@ bool cppcheck::Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
|
||||||
if (!rootnode || std::strcmp(rootnode->Name(), "platform") != 0)
|
if (!rootnode || std::strcmp(rootnode->Name(), "platform") != 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
bool error = false;
|
||||||
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
|
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
|
||||||
if (std::strcmp(node->Name(), "default-sign") == 0)
|
if (std::strcmp(node->Name(), "default-sign") == 0) {
|
||||||
defaultSign = *node->GetText();
|
const char* str = node->GetText();
|
||||||
else if (std::strcmp(node->Name(), "char_bit") == 0)
|
if (str)
|
||||||
char_bit = std::atoi(node->GetText());
|
defaultSign = *str;
|
||||||
|
else
|
||||||
|
error = true;
|
||||||
|
} else if (std::strcmp(node->Name(), "char_bit") == 0)
|
||||||
|
char_bit = xmlTextAsUInt(node, error);
|
||||||
else if (std::strcmp(node->Name(), "sizeof") == 0) {
|
else if (std::strcmp(node->Name(), "sizeof") == 0) {
|
||||||
for (const tinyxml2::XMLElement *sz = node->FirstChildElement(); sz; sz = sz->NextSiblingElement()) {
|
for (const tinyxml2::XMLElement *sz = node->FirstChildElement(); sz; sz = sz->NextSiblingElement()) {
|
||||||
if (std::strcmp(sz->Name(), "short") == 0)
|
if (std::strcmp(sz->Name(), "short") == 0)
|
||||||
sizeof_short = std::atoi(sz->GetText());
|
sizeof_short = xmlTextAsUInt(sz, error);
|
||||||
else if (std::strcmp(sz->Name(), "bool") == 0)
|
else if (std::strcmp(sz->Name(), "bool") == 0)
|
||||||
sizeof_bool = std::atoi(sz->GetText());
|
sizeof_bool = xmlTextAsUInt(sz, error);
|
||||||
else if (std::strcmp(sz->Name(), "int") == 0)
|
else if (std::strcmp(sz->Name(), "int") == 0)
|
||||||
sizeof_int = std::atoi(sz->GetText());
|
sizeof_int = xmlTextAsUInt(sz, error);
|
||||||
else if (std::strcmp(sz->Name(), "long") == 0)
|
else if (std::strcmp(sz->Name(), "long") == 0)
|
||||||
sizeof_long = std::atoi(sz->GetText());
|
sizeof_long = xmlTextAsUInt(sz, error);
|
||||||
else if (std::strcmp(sz->Name(), "long-long") == 0)
|
else if (std::strcmp(sz->Name(), "long-long") == 0)
|
||||||
sizeof_long_long = std::atoi(sz->GetText());
|
sizeof_long_long = xmlTextAsUInt(sz, error);
|
||||||
else if (std::strcmp(sz->Name(), "float") == 0)
|
else if (std::strcmp(sz->Name(), "float") == 0)
|
||||||
sizeof_float = std::atoi(sz->GetText());
|
sizeof_float = xmlTextAsUInt(sz, error);
|
||||||
else if (std::strcmp(sz->Name(), "double") == 0)
|
else if (std::strcmp(sz->Name(), "double") == 0)
|
||||||
sizeof_double = std::atoi(sz->GetText());
|
sizeof_double = xmlTextAsUInt(sz, error);
|
||||||
else if (std::strcmp(sz->Name(), "long-double") == 0)
|
else if (std::strcmp(sz->Name(), "long-double") == 0)
|
||||||
sizeof_long_double = std::atoi(sz->GetText());
|
sizeof_long_double = xmlTextAsUInt(sz, error);
|
||||||
else if (std::strcmp(sz->Name(), "pointer") == 0)
|
else if (std::strcmp(sz->Name(), "pointer") == 0)
|
||||||
sizeof_pointer = std::atoi(sz->GetText());
|
sizeof_pointer = xmlTextAsUInt(sz, error);
|
||||||
else if (std::strcmp(sz->Name(), "size_t") == 0)
|
else if (std::strcmp(sz->Name(), "size_t") == 0)
|
||||||
sizeof_size_t = std::atoi(sz->GetText());
|
sizeof_size_t = xmlTextAsUInt(sz, error);
|
||||||
else if (std::strcmp(sz->Name(), "wchar_t") == 0)
|
else if (std::strcmp(sz->Name(), "wchar_t") == 0)
|
||||||
sizeof_wchar_t = std::atoi(sz->GetText());
|
sizeof_wchar_t = xmlTextAsUInt(sz, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,5 +250,5 @@ bool cppcheck::Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
|
||||||
long_long_bit = char_bit * sizeof_long_long;
|
long_long_bit = char_bit * sizeof_long_long;
|
||||||
|
|
||||||
platformType = PlatformFile;
|
platformType = PlatformFile;
|
||||||
return true;
|
return !error;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ private:
|
||||||
TEST_CASE(valid_config_file_3);
|
TEST_CASE(valid_config_file_3);
|
||||||
TEST_CASE(valid_config_file_4);
|
TEST_CASE(valid_config_file_4);
|
||||||
TEST_CASE(invalid_config_file_1);
|
TEST_CASE(invalid_config_file_1);
|
||||||
// TEST_CASE(empty_elements); // TODO: Trac issue #8409
|
TEST_CASE(empty_elements);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool readPlatform(cppcheck::Platform& platform, const char* xmldata) {
|
static bool readPlatform(cppcheck::Platform& platform, const char* xmldata) {
|
||||||
|
@ -286,7 +286,6 @@ private:
|
||||||
ASSERT(!readPlatform(platform, xmldata));
|
ASSERT(!readPlatform(platform, xmldata));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0 // @TODO: Enable when Trac issue #8409 has been fixed
|
|
||||||
void empty_elements() {
|
void empty_elements() {
|
||||||
// Valid platform configuration without any usable information.
|
// Valid platform configuration without any usable information.
|
||||||
// Similar like an empty file.
|
// Similar like an empty file.
|
||||||
|
@ -310,18 +309,10 @@ private:
|
||||||
" </platform>";
|
" </platform>";
|
||||||
cppcheck::Platform platform;
|
cppcheck::Platform platform;
|
||||||
ASSERT(platform.platform(cppcheck::Platform::Win64));
|
ASSERT(platform.platform(cppcheck::Platform::Win64));
|
||||||
ASSERT(readPlatform(platform, xmldata));
|
ASSERT(!readPlatform(platform, xmldata));
|
||||||
ASSERT_EQUALS(platform.PlatformFile, platform.platformType);
|
ASSERT_EQUALS(platform.PlatformFile, platform.platformType);
|
||||||
ASSERT(!platform.isWindowsPlatform());
|
ASSERT(!platform.isWindowsPlatform());
|
||||||
ASSERT_EQUALS(8, platform.char_bit);
|
|
||||||
ASSERT_EQUALS('\0', platform.defaultSign);
|
|
||||||
ASSERT_EQUALS(1, platform.sizeof_bool);
|
|
||||||
ASSERT_EQUALS(2, platform.sizeof_short);
|
|
||||||
ASSERT_EQUALS(4, platform.sizeof_int);
|
|
||||||
ASSERT_EQUALS(4, platform.sizeof_long);
|
|
||||||
ASSERT_EQUALS(8, platform.sizeof_long_long);
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
REGISTER_TEST(TestPlatform)
|
REGISTER_TEST(TestPlatform)
|
||||||
|
|
Loading…
Reference in New Issue