fixed `platforms` lookup / set proper platform type for `unix32-unsigned` and `unix64-unsigned` / copy `platforms` in CMake (#4464)

This commit is contained in:
Oliver Stöneberg 2023-01-26 22:05:40 +01:00 committed by GitHub
parent b12aebc817
commit e5572835c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 80 additions and 37 deletions

View File

@ -79,6 +79,11 @@ add_custom_target(copy_addons ALL
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/addons"
COMMENT "Copying addons files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
add_custom_target(copy_platforms ALL
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/platforms"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/platforms"
COMMENT "Copying platforms files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
if(USE_BUNDLED_TINYXML2)
message(STATUS "Using bundled version of tinyxml2")
add_subdirectory(externals/tinyxml2)

View File

@ -47,6 +47,7 @@ target_link_libraries(cppcheck ${CMAKE_THREAD_LIBS_INIT})
add_dependencies(cppcheck copy_cfg)
add_dependencies(cppcheck copy_addons)
add_dependencies(cppcheck copy_platforms)
add_dependencies(cppcheck run-dmake)
install(TARGETS cppcheck

View File

@ -611,13 +611,21 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings->platform(Settings::Native);
else if (platform == "unspecified")
mSettings->platform(Settings::Unspecified);
else if (!mSettings->loadPlatformFile(argv[0], platform)) {
else if (!mSettings->loadPlatformFile(argv[0], platform, mSettings->verbose)) {
std::string message("unrecognized platform: \"");
message += platform;
message += "\".";
printError(message);
return false;
}
// TODO: remove
// these are loaded via external files and thus have Settings::PlatformFile set instead.
// override the type so they behave like the regular platforms.
if (platform == "unix32-unsigned")
mSettings->platformType = Settings::Unix32;
else if (platform == "unix64-unsigned")
mSettings->platformType = Settings::Unix64;
}
// Write results in results.plist
@ -678,7 +686,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings->platform(Settings::Native);
else if (platform == "unspecified" || platform == "Unspecified" || platform.empty())
;
else if (!mSettings->loadPlatformFile(projectFile.c_str(), platform) && !mSettings->loadPlatformFile(argv[0], platform)) {
else if (!mSettings->loadPlatformFile(projectFile.c_str(), platform, mSettings->verbose) && !mSettings->loadPlatformFile(argv[0], platform, mSettings->verbose)) {
std::string message("unrecognized platform: \"");
message += platform;
message += "\".";

View File

@ -21,6 +21,7 @@
#include "path.h"
#include <cstring>
#include <iostream>
#include <limits>
#include <vector>
@ -156,15 +157,16 @@ bool cppcheck::Platform::platform(cppcheck::Platform::PlatformType type)
return false;
}
bool cppcheck::Platform::loadPlatformFile(const char exename[], const std::string &filename)
bool cppcheck::Platform::loadPlatformFile(const char exename[], const std::string &filename, bool verbose)
{
// open file..
tinyxml2::XMLDocument doc;
if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) {
// TODO: only append .xml if missing
// TODO: use native separators
std::vector<std::string> filenames;
filenames.push_back(filename);
filenames.push_back(filename + ".xml");
filenames.push_back("platforms/" + filename);
filenames.push_back("platforms/" + filename + ".xml");
if (exename && (std::string::npos != Path::fromNativeSeparators(exename).find('/'))) {
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + filename);
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + filename);
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename);
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename + ".xml");
@ -176,16 +178,24 @@ bool cppcheck::Platform::loadPlatformFile(const char exename[], const std::strin
filenames.push_back(filesdir + ("platforms/" + filename));
filenames.push_back(filesdir + ("platforms/" + filename + ".xml"));
#endif
// open file..
tinyxml2::XMLDocument doc;
bool success = false;
for (const std::string & f : filenames) {
if (verbose)
std::cout << "try to load platform file '" << f << "' ... ";
if (doc.LoadFile(f.c_str()) == tinyxml2::XML_SUCCESS) {
if (verbose)
std::cout << "Success" << std::endl;
success = true;
break;
}
if (verbose)
std::cout << doc.ErrorStr() << std::endl;
}
if (!success)
return false;
}
return loadFromXmlDocument(&doc);
}

View File

@ -120,9 +120,10 @@ namespace cppcheck {
* load platform file
* @param exename application path
* @param filename platform filename
* @param verbose log verbose information about the lookup
* @return returns true if file was loaded successfully
*/
bool loadPlatformFile(const char exename[], const std::string &filename);
bool loadPlatformFile(const char exename[], const std::string &filename, bool verbose = false);
/** load platform from xml document, primarily for testing */
bool loadFromXmlDocument(const tinyxml2::XMLDocument *doc);

View File

@ -35,6 +35,7 @@ if (BUILD_TESTS)
add_dependencies(testrunner copy_cfg)
add_dependencies(testrunner copy_addons)
add_dependencies(testrunner copy_platforms)
add_dependencies(testrunner run-dmake)
if (LIBXML2_XMLLINT_EXECUTABLE)

View File

@ -124,10 +124,12 @@ private:
TEST_CASE(platformWin32A);
TEST_CASE(platformWin32W);
TEST_CASE(platformUnix32);
TEST_CASE(platformUnix32Unsigned);
TEST_CASE(platformUnix64);
TEST_CASE(platformUnix64Unsigned);
TEST_CASE(platformNative);
TEST_CASE(platformUnspecified);
//TEST_CASE(platformPlatformFile);
TEST_CASE(platformPlatformFile);
TEST_CASE(platformUnknown);
TEST_CASE(plistEmpty);
TEST_CASE(plistDoesNotExist);
@ -1002,6 +1004,15 @@ private:
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}
void platformUnix32Unsigned() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--platform=unix32-unsigned", "file.cpp"};
ASSERT(settings.platform(Settings::Unspecified));
ASSERT(defParser.parseFromArgs(3, argv));
ASSERT(settings.platformType == Settings::Unix32);
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}
void platformUnix64() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--platform=unix64", "file.cpp"};
@ -1011,6 +1022,15 @@ private:
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}
void platformUnix64Unsigned() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--platform=unix64-unsigned", "file.cpp"};
ASSERT(settings.platform(Settings::Unspecified));
ASSERT(defParser.parseFromArgs(3, argv));
ASSERT(settings.platformType == Settings::Unix64);
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}
void platformNative() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--platform=native", "file.cpp"};
@ -1029,17 +1049,14 @@ private:
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}
/*
// TODO: the file is not found because of a bug in the lookup code
void platformPlatformFile() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--platform=avr8", "file.cpp"};
ASSERT(settings.platform(Settings::Unspecified));
TODO_ASSERT_EQUALS(true, false, defParser.parseFromArgs(3, argv));
TODO_ASSERT_EQUALS(Settings::PlatformFile, Settings::Unspecified, settings.platformType);
TODO_ASSERT_EQUALS("cppcheck: error: unrecognized platform: \"avr8\".\n", "", GET_REDIRECT_OUTPUT);
ASSERT_EQUALS(true, defParser.parseFromArgs(3, argv));
ASSERT_EQUALS(Settings::PlatformFile, settings.platformType);
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}
*/
void platformUnknown() {
REDIRECT;