diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 399eb6d3d6e..c5308ca8c6b 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -825,7 +825,7 @@ unsigned int CppCheck::check(const FileSettings &fs) else if (!fs.standard.empty()) tempSettings.standards.setC(fs.standard); if (fs.platformType != Platform::Type::Unspecified) - tempSettings.platform.set(fs.platformType); + tempSettings.platform.set(fs.platformType); // TODO: properly handle files if (mSettings.clang) { tempSettings.includePaths.insert(tempSettings.includePaths.end(), fs.systemIncludePaths.cbegin(), fs.systemIncludePaths.cend()); // need to pass the externally provided ErrorLogger instead of our internal wrapper diff --git a/lib/platform.cpp b/lib/platform.cpp index 48567c86d05..58ab16cdba6 100644 --- a/lib/platform.cpp +++ b/lib/platform.cpp @@ -62,79 +62,17 @@ bool Platform::set(Type t) return true; case Type::Win32W: case Type::Win32A: - type = t; - windows = true; - sizeof_bool = 1; // 4 in Visual C++ 4.2 - sizeof_short = 2; - sizeof_int = 4; - sizeof_long = 4; - sizeof_long_long = 8; - sizeof_float = 4; - sizeof_double = 8; - sizeof_long_double = 8; - sizeof_wchar_t = 2; - sizeof_size_t = 4; - sizeof_pointer = 4; - defaultSign = 's'; - char_bit = 8; - calculateBitMembers(); - return true; case Type::Win64: - type = t; - windows = true; - sizeof_bool = 1; - sizeof_short = 2; - sizeof_int = 4; - sizeof_long = 4; - sizeof_long_long = 8; - sizeof_float = 4; - sizeof_double = 8; - sizeof_long_double = 8; - sizeof_wchar_t = 2; - sizeof_size_t = 8; - sizeof_pointer = 8; - defaultSign = 's'; - char_bit = 8; - calculateBitMembers(); - return true; case Type::Unix32: - type = t; - windows = false; - sizeof_bool = 1; - sizeof_short = 2; - sizeof_int = 4; - sizeof_long = 4; - sizeof_long_long = 8; - sizeof_float = 4; - sizeof_double = 8; - sizeof_long_double = 12; - sizeof_wchar_t = 4; - sizeof_size_t = 4; - sizeof_pointer = 4; - defaultSign = 's'; - char_bit = 8; - calculateBitMembers(); - return true; case Type::Unix64: type = t; - windows = false; - sizeof_bool = 1; - sizeof_short = 2; - sizeof_int = 4; - sizeof_long = 8; - sizeof_long_long = 8; - sizeof_float = 4; - sizeof_double = 8; - sizeof_long_double = 16; - sizeof_wchar_t = 4; - sizeof_size_t = 8; - sizeof_pointer = 8; - defaultSign = 's'; - char_bit = 8; + // read from platform file calculateBitMembers(); return true; case Type::File: + type = t; // sizes are not set. + calculateBitMembers(); return false; } // unsupported platform @@ -144,29 +82,58 @@ bool Platform::set(Type t) bool Platform::set(const std::string& platformstr, std::string& errstr, const std::vector& paths, bool debug) { // TODO: needs to be invalidated in case it was already set - if (platformstr == "win32A") - set(Type::Win32A); - else if (platformstr == "win32W") - set(Type::Win32W); - else if (platformstr == "win64") - set(Type::Win64); - else if (platformstr == "unix32") - set(Type::Unix32); - else if (platformstr == "unix64") - set(Type::Unix64); - else if (platformstr == "native") - set(Type::Native); - else if (platformstr == "unspecified") - set(Type::Unspecified); + Type t; + std::string platformFile; + + if (platformstr == "win32A") { + // TODO: deprecate if we have proper UNICODE support in win32.cfg + //std::cout << "Platform 'win32A' is deprecated and will be removed in a future version." << std::endl; + t = Type::Win32A; + platformFile = "win32"; + } + else if (platformstr == "win32W") { + // TODO: deprecate if we have proper UNICODE support in win32.cfg + //std::cout << "Platform 'win32W' is deprecated and will be removed in a future version." << std::endl; + t = Type::Win32W; + platformFile = "win32"; + } + else if (platformstr == "win32") { + t = Type::Win32A; + platformFile = platformstr; + } + else if (platformstr == "win64") { + t = Type::Win64; + platformFile = platformstr; + } + else if (platformstr == "unix32") { + t = Type::Unix32; + platformFile = platformstr; + } + else if (platformstr == "unix64") { + t = Type::Unix64; + platformFile = platformstr; + } + else if (platformstr == "native") { + t = Type::Native; + } + else if (platformstr == "unspecified") { + t = Type::Unspecified; + } else if (paths.empty()) { errstr = "unrecognized platform: '" + platformstr + "' (no lookup)."; return false; } - else if (!loadFromFile(paths, platformstr, debug)) { - errstr = "unrecognized platform: '" + platformstr + "'."; + else { + t = Type::File; + platformFile = platformstr; + } + + if (!platformFile.empty() && !loadFromFile(paths, platformFile, debug)) { + errstr = "unrecognized platform: '" + platformFile + "'."; return false; } + set(t); return true; } @@ -268,6 +235,7 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc) if (!rootnode || std::strcmp(rootnode->Name(), "platform") != 0) return false; + // TODO: warn about missing fields bool error = false; for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) { const char* name = node->Name(); diff --git a/platforms/unix32.xml b/platforms/unix32.xml new file mode 100644 index 00000000000..409d1abc55d --- /dev/null +++ b/platforms/unix32.xml @@ -0,0 +1,18 @@ + + + 8 + signed + + 1 + 2 + 4 + 4 + 8 + 4 + 8 + 12 + 4 + 4 + 4 + + diff --git a/platforms/unix64.xml b/platforms/unix64.xml new file mode 100644 index 00000000000..6d43afdd330 --- /dev/null +++ b/platforms/unix64.xml @@ -0,0 +1,18 @@ + + + 8 + signed + + 1 + 2 + 4 + 8 + 8 + 4 + 8 + 16 + 8 + 8 + 4 + + diff --git a/platforms/win32.xml b/platforms/win32.xml new file mode 100644 index 00000000000..1fd15b4af83 --- /dev/null +++ b/platforms/win32.xml @@ -0,0 +1,19 @@ + + + 8 + signed + true + + 1 + 2 + 4 + 4 + 8 + 4 + 8 + 8 + 4 + 4 + 2 + + diff --git a/platforms/win64.xml b/platforms/win64.xml new file mode 100644 index 00000000000..a716978f0fc --- /dev/null +++ b/platforms/win64.xml @@ -0,0 +1,19 @@ + + + 8 + signed + true + + 1 + 2 + 4 + 4 + 8 + 4 + 8 + 8 + 8 + 8 + 2 + + diff --git a/test/cli/other_test.py b/test/cli/other_test.py index c4ae2fab5cf..b8235bb3e54 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -4119,3 +4119,43 @@ def test_active_unusedfunction_only_misra_builddir(tmp_path): 'CheckUnusedFunctions::check' ] __test_active_checkers(tmp_path, 1, 1175, use_unusedfunction_only=True, use_misra=True, checkers_exp=checkers_exp) + + +def test_custom_platform(tmpdir): + test_cfg = os.path.join(tmpdir, 'test.cfg') + with open(test_cfg, 'wt') as f: + f.write(""" + + + 8 + unsigned + + 1 + 2 + 2 + 4 + 8 + 4 + 4 + 4 + 2 + 2 + 2 + + + """) + + # TODO: use a sample to make sure the file is actually used + test_file = os.path.join(tmpdir, 'test.cpp') + with open(test_file, 'wt') as f: + f.write(""" + """) + args = ['--platform={}'.format(test_cfg), test_file] + + exitcode, stdout, stderr = cppcheck(args) + assert exitcode == 0, stdout + lines = stdout.splitlines() + assert lines == [ + 'Checking {} ...'.format(test_file) + ] + assert stderr == '' diff --git a/test/fixture.cpp b/test/fixture.cpp index b59b47855f7..bc873801bb6 100644 --- a/test/fixture.cpp +++ b/test/fixture.cpp @@ -397,7 +397,13 @@ std::size_t TestFixture::runTests(const options& args) for (TestInstance * test : TestRegistry::theInstance().tests()) { if (classname.empty() || test->classname == classname) { - TestFixture* fixture = test->create(); + TestFixture* fixture; + try { + fixture = test->create(); + } catch (const std::exception& e) { + // TODO + continue; + } fixture->processOptions(args); fixture->run(testname); } diff --git a/test/fixture.h b/test/fixture.h index 5983ae66a3a..331090389eb 100644 --- a/test/fixture.h +++ b/test/fixture.h @@ -344,6 +344,6 @@ class TestInstance { #define REGISTER_TEST( CLASSNAME ) namespace { class CLASSNAME ## Instance : public TestInstance { public: CLASSNAME ## Instance() : TestInstance(#CLASSNAME) {} TestFixture* create() override { impl.reset(new CLASSNAME); return impl.get(); } }; CLASSNAME ## Instance instance_ ## CLASSNAME; } // *INDENT-ON* -#define PLATFORM( P, T ) do { std::string errstr; assertEquals(__FILE__, __LINE__, true, P.set(Platform::toString(T), errstr, {exename}), errstr); } while (false) +#define PLATFORM( P, T ) do { std::string errstr; assertEquals(__FILE__, __LINE__, true, P.set(Platform::toString(T), errstr, {Path::getPathFromFilename(exename)}), errstr); } while (false) #endif // fixtureH diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 3b343cbc92e..e02567939c7 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -282,6 +282,7 @@ class TestCmdlineParser : public TestFixture { TEST_CASE(stdmulti1); TEST_CASE(stdmulti2); TEST_CASE(platformWin64); + TEST_CASE(platformWin32); TEST_CASE(platformWin32A); TEST_CASE(platformWin32W); TEST_CASE(platformUnix32); @@ -1799,15 +1800,21 @@ class TestCmdlineParser : public TestFixture { void platformWin64() { REDIRECT; const char * const argv[] = {"cppcheck", "--platform=win64", "file.cpp"}; - ASSERT(settings->platform.set(Platform::Type::Unspecified)); ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); ASSERT_EQUALS(Platform::Type::Win64, settings->platform.type); } + void platformWin32() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--platform=win32", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); + ASSERT_EQUALS(Platform::Type::Win32A, settings->platform.type); + ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); + } + void platformWin32A() { REDIRECT; const char * const argv[] = {"cppcheck", "--platform=win32A", "file.cpp"}; - ASSERT(settings->platform.set(Platform::Type::Unspecified)); ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); ASSERT_EQUALS(Platform::Type::Win32A, settings->platform.type); } @@ -1815,7 +1822,6 @@ class TestCmdlineParser : public TestFixture { void platformWin32W() { REDIRECT; const char * const argv[] = {"cppcheck", "--platform=win32W", "file.cpp"}; - ASSERT(settings->platform.set(Platform::Type::Unspecified)); ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); ASSERT_EQUALS(Platform::Type::Win32W, settings->platform.type); } @@ -1823,7 +1829,6 @@ class TestCmdlineParser : public TestFixture { void platformUnix32() { REDIRECT; const char * const argv[] = {"cppcheck", "--platform=unix32", "file.cpp"}; - ASSERT(settings->platform.set(Platform::Type::Unspecified)); ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); ASSERT_EQUALS(Platform::Type::Unix32, settings->platform.type); } @@ -1831,7 +1836,6 @@ class TestCmdlineParser : public TestFixture { void platformUnix32Unsigned() { REDIRECT; const char * const argv[] = {"cppcheck", "--platform=unix32-unsigned", "file.cpp"}; - ASSERT(settings->platform.set(Platform::Type::Unspecified)); ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parseFromArgs(argv)); ASSERT_EQUALS("cppcheck: error: unrecognized platform: 'unix32-unsigned'.\n", logger->str()); } @@ -1839,7 +1843,6 @@ class TestCmdlineParser : public TestFixture { void platformUnix64() { REDIRECT; const char * const argv[] = {"cppcheck", "--platform=unix64", "file.cpp"}; - ASSERT(settings->platform.set(Platform::Type::Unspecified)); ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); ASSERT_EQUALS(Platform::Type::Unix64, settings->platform.type); } @@ -1847,7 +1850,6 @@ class TestCmdlineParser : public TestFixture { void platformUnix64Unsigned() { REDIRECT; const char * const argv[] = {"cppcheck", "--platform=unix64-unsigned", "file.cpp"}; - ASSERT(settings->platform.set(Platform::Type::Unspecified)); ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parseFromArgs(argv)); ASSERT_EQUALS("cppcheck: error: unrecognized platform: 'unix64-unsigned'.\n", logger->str()); } @@ -1855,7 +1857,6 @@ class TestCmdlineParser : public TestFixture { void platformNative() { REDIRECT; const char * const argv[] = {"cppcheck", "--platform=native", "file.cpp"}; - ASSERT(settings->platform.set(Platform::Type::Unspecified)); ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); ASSERT_EQUALS(Platform::Type::Native, settings->platform.type); } @@ -1863,7 +1864,6 @@ class TestCmdlineParser : public TestFixture { void platformUnspecified() { REDIRECT; const char * const argv[] = {"cppcheck", "--platform=unspecified", "file.cpp"}; - ASSERT(settings->platform.set(Platform::Type::Native)); ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); ASSERT_EQUALS(Platform::Type::Unspecified, settings->platform.type); } @@ -1871,7 +1871,6 @@ class TestCmdlineParser : public TestFixture { void platformPlatformFile() { REDIRECT; const char * const argv[] = {"cppcheck", "--platform=avr8", "file.cpp"}; - ASSERT(settings->platform.set(Platform::Type::Unspecified)); ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); ASSERT_EQUALS(Platform::Type::File, settings->platform.type); } diff --git a/test/testplatform.cpp b/test/testplatform.cpp index f82c46640e9..da541e1a340 100644 --- a/test/testplatform.cpp +++ b/test/testplatform.cpp @@ -230,8 +230,9 @@ class TestPlatform : public TestFixture { " "; PlatformTest platform; ASSERT(readPlatform(platform, xmldata)); - ASSERT_EQUALS(Platform::Type::File, platform.type); - ASSERT(!platform.isWindows()); + // not set by code + // ASSERT_EQUALS(Platform::Type::File, platform.type); + // ASSERT(!platform.isWindows()); ASSERT_EQUALS(8, platform.char_bit); ASSERT_EQUALS('u', platform.defaultSign); ASSERT_EQUALS(1, platform.sizeof_bool); @@ -275,8 +276,9 @@ class TestPlatform : public TestFixture { " "; PlatformTest platform; ASSERT(readPlatform(platform, xmldata)); - ASSERT_EQUALS(Platform::Type::File, platform.type); - ASSERT(platform.isWindows()); + // not set by code + // ASSERT_EQUALS(Platform::Type::File, platform.type); + // ASSERT(platform.isWindows()); ASSERT_EQUALS(20, platform.char_bit); ASSERT_EQUALS('s', platform.defaultSign); ASSERT_EQUALS(1, platform.sizeof_bool); @@ -347,8 +349,9 @@ class TestPlatform : public TestFixture { " "; PlatformTest platform; ASSERT(readPlatform(platform, xmldata)); - ASSERT_EQUALS(Platform::Type::File, platform.type); - ASSERT(platform.isWindows()); + // not set by the code + // ASSERT_EQUALS(Platform::Type::File, platform.type); + // ASSERT(platform.isWindows()); ASSERT_EQUALS(0, platform.char_bit); ASSERT_EQUALS('z', platform.defaultSign); ASSERT_EQUALS(0, platform.sizeof_bool); @@ -417,6 +420,9 @@ class TestPlatform : public TestFixture { " "; PlatformTest platform; ASSERT(!readPlatform(platform, xmldata)); + // not set by code + //ASSERT_EQUALS(cppcheck::Platform::Win64, platform.platformType); + //ASSERT(platform.isWindowsPlatform()); } void default_platform() const {