Skip to content

Commit e557283

Browse files
authored
fixed platforms lookup / set proper platform type for unix32-unsigned and unix64-unsigned / copy platforms in CMake (#4464)
1 parent b12aebc commit e557283

7 files changed

Lines changed: 80 additions & 37 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ add_custom_target(copy_addons ALL
7979
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/addons"
8080
COMMENT "Copying addons files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
8181

82+
add_custom_target(copy_platforms ALL
83+
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/platforms"
84+
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/platforms"
85+
COMMENT "Copying platforms files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
86+
8287
if(USE_BUNDLED_TINYXML2)
8388
message(STATUS "Using bundled version of tinyxml2")
8489
add_subdirectory(externals/tinyxml2)

cli/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ target_link_libraries(cppcheck ${CMAKE_THREAD_LIBS_INIT})
4747

4848
add_dependencies(cppcheck copy_cfg)
4949
add_dependencies(cppcheck copy_addons)
50+
add_dependencies(cppcheck copy_platforms)
5051
add_dependencies(cppcheck run-dmake)
5152

5253
install(TARGETS cppcheck

cli/cmdlineparser.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,13 +611,21 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
611611
mSettings->platform(Settings::Native);
612612
else if (platform == "unspecified")
613613
mSettings->platform(Settings::Unspecified);
614-
else if (!mSettings->loadPlatformFile(argv[0], platform)) {
614+
else if (!mSettings->loadPlatformFile(argv[0], platform, mSettings->verbose)) {
615615
std::string message("unrecognized platform: \"");
616616
message += platform;
617617
message += "\".";
618618
printError(message);
619619
return false;
620620
}
621+
622+
// TODO: remove
623+
// these are loaded via external files and thus have Settings::PlatformFile set instead.
624+
// override the type so they behave like the regular platforms.
625+
if (platform == "unix32-unsigned")
626+
mSettings->platformType = Settings::Unix32;
627+
else if (platform == "unix64-unsigned")
628+
mSettings->platformType = Settings::Unix64;
621629
}
622630

623631
// Write results in results.plist
@@ -678,7 +686,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
678686
mSettings->platform(Settings::Native);
679687
else if (platform == "unspecified" || platform == "Unspecified" || platform.empty())
680688
;
681-
else if (!mSettings->loadPlatformFile(projectFile.c_str(), platform) && !mSettings->loadPlatformFile(argv[0], platform)) {
689+
else if (!mSettings->loadPlatformFile(projectFile.c_str(), platform, mSettings->verbose) && !mSettings->loadPlatformFile(argv[0], platform, mSettings->verbose)) {
682690
std::string message("unrecognized platform: \"");
683691
message += platform;
684692
message += "\".";

lib/platform.cpp

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "path.h"
2222

2323
#include <cstring>
24+
#include <iostream>
2425
#include <limits>
2526
#include <vector>
2627

@@ -156,36 +157,45 @@ bool cppcheck::Platform::platform(cppcheck::Platform::PlatformType type)
156157
return false;
157158
}
158159

159-
bool cppcheck::Platform::loadPlatformFile(const char exename[], const std::string &filename)
160+
bool cppcheck::Platform::loadPlatformFile(const char exename[], const std::string &filename, bool verbose)
160161
{
161-
// open file..
162-
tinyxml2::XMLDocument doc;
163-
if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) {
164-
std::vector<std::string> filenames;
165-
filenames.push_back(filename + ".xml");
166-
if (exename && (std::string::npos != Path::fromNativeSeparators(exename).find('/'))) {
167-
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + filename);
168-
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + filename);
169-
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename);
170-
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename + ".xml");
171-
}
162+
// TODO: only append .xml if missing
163+
// TODO: use native separators
164+
std::vector<std::string> filenames;
165+
filenames.push_back(filename);
166+
filenames.push_back(filename + ".xml");
167+
filenames.push_back("platforms/" + filename);
168+
filenames.push_back("platforms/" + filename + ".xml");
169+
if (exename && (std::string::npos != Path::fromNativeSeparators(exename).find('/'))) {
170+
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + filename);
171+
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename);
172+
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename + ".xml");
173+
}
172174
#ifdef FILESDIR
173-
std::string filesdir = FILESDIR;
174-
if (!filesdir.empty() && filesdir[filesdir.size()-1] != '/')
175-
filesdir += '/';
176-
filenames.push_back(filesdir + ("platforms/" + filename));
177-
filenames.push_back(filesdir + ("platforms/" + filename + ".xml"));
175+
std::string filesdir = FILESDIR;
176+
if (!filesdir.empty() && filesdir[filesdir.size()-1] != '/')
177+
filesdir += '/';
178+
filenames.push_back(filesdir + ("platforms/" + filename));
179+
filenames.push_back(filesdir + ("platforms/" + filename + ".xml"));
178180
#endif
179-
bool success = false;
180-
for (const std::string & f : filenames) {
181-
if (doc.LoadFile(f.c_str()) == tinyxml2::XML_SUCCESS) {
182-
success = true;
183-
break;
184-
}
181+
182+
// open file..
183+
tinyxml2::XMLDocument doc;
184+
bool success = false;
185+
for (const std::string & f : filenames) {
186+
if (verbose)
187+
std::cout << "try to load platform file '" << f << "' ... ";
188+
if (doc.LoadFile(f.c_str()) == tinyxml2::XML_SUCCESS) {
189+
if (verbose)
190+
std::cout << "Success" << std::endl;
191+
success = true;
192+
break;
185193
}
186-
if (!success)
187-
return false;
194+
if (verbose)
195+
std::cout << doc.ErrorStr() << std::endl;
188196
}
197+
if (!success)
198+
return false;
189199

190200
return loadFromXmlDocument(&doc);
191201
}

lib/platform.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ namespace cppcheck {
120120
* load platform file
121121
* @param exename application path
122122
* @param filename platform filename
123+
* @param verbose log verbose information about the lookup
123124
* @return returns true if file was loaded successfully
124125
*/
125-
bool loadPlatformFile(const char exename[], const std::string &filename);
126+
bool loadPlatformFile(const char exename[], const std::string &filename, bool verbose = false);
126127

127128
/** load platform from xml document, primarily for testing */
128129
bool loadFromXmlDocument(const tinyxml2::XMLDocument *doc);

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ if (BUILD_TESTS)
3535

3636
add_dependencies(testrunner copy_cfg)
3737
add_dependencies(testrunner copy_addons)
38+
add_dependencies(testrunner copy_platforms)
3839
add_dependencies(testrunner run-dmake)
3940

4041
if (LIBXML2_XMLLINT_EXECUTABLE)

test/testcmdlineparser.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ class TestCmdlineParser : public TestFixture {
124124
TEST_CASE(platformWin32A);
125125
TEST_CASE(platformWin32W);
126126
TEST_CASE(platformUnix32);
127+
TEST_CASE(platformUnix32Unsigned);
127128
TEST_CASE(platformUnix64);
129+
TEST_CASE(platformUnix64Unsigned);
128130
TEST_CASE(platformNative);
129131
TEST_CASE(platformUnspecified);
130-
//TEST_CASE(platformPlatformFile);
132+
TEST_CASE(platformPlatformFile);
131133
TEST_CASE(platformUnknown);
132134
TEST_CASE(plistEmpty);
133135
TEST_CASE(plistDoesNotExist);
@@ -1002,6 +1004,15 @@ class TestCmdlineParser : public TestFixture {
10021004
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
10031005
}
10041006

1007+
void platformUnix32Unsigned() {
1008+
REDIRECT;
1009+
const char * const argv[] = {"cppcheck", "--platform=unix32-unsigned", "file.cpp"};
1010+
ASSERT(settings.platform(Settings::Unspecified));
1011+
ASSERT(defParser.parseFromArgs(3, argv));
1012+
ASSERT(settings.platformType == Settings::Unix32);
1013+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1014+
}
1015+
10051016
void platformUnix64() {
10061017
REDIRECT;
10071018
const char * const argv[] = {"cppcheck", "--platform=unix64", "file.cpp"};
@@ -1011,6 +1022,15 @@ class TestCmdlineParser : public TestFixture {
10111022
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
10121023
}
10131024

1025+
void platformUnix64Unsigned() {
1026+
REDIRECT;
1027+
const char * const argv[] = {"cppcheck", "--platform=unix64-unsigned", "file.cpp"};
1028+
ASSERT(settings.platform(Settings::Unspecified));
1029+
ASSERT(defParser.parseFromArgs(3, argv));
1030+
ASSERT(settings.platformType == Settings::Unix64);
1031+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1032+
}
1033+
10141034
void platformNative() {
10151035
REDIRECT;
10161036
const char * const argv[] = {"cppcheck", "--platform=native", "file.cpp"};
@@ -1029,17 +1049,14 @@ class TestCmdlineParser : public TestFixture {
10291049
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
10301050
}
10311051

1032-
/*
1033-
// TODO: the file is not found because of a bug in the lookup code
1034-
void platformPlatformFile() {
1052+
void platformPlatformFile() {
10351053
REDIRECT;
10361054
const char * const argv[] = {"cppcheck", "--platform=avr8", "file.cpp"};
10371055
ASSERT(settings.platform(Settings::Unspecified));
1038-
TODO_ASSERT_EQUALS(true, false, defParser.parseFromArgs(3, argv));
1039-
TODO_ASSERT_EQUALS(Settings::PlatformFile, Settings::Unspecified, settings.platformType);
1040-
TODO_ASSERT_EQUALS("cppcheck: error: unrecognized platform: \"avr8\".\n", "", GET_REDIRECT_OUTPUT);
1041-
}
1042-
*/
1056+
ASSERT_EQUALS(true, defParser.parseFromArgs(3, argv));
1057+
ASSERT_EQUALS(Settings::PlatformFile, settings.platformType);
1058+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1059+
}
10431060

10441061
void platformUnknown() {
10451062
REDIRECT;

0 commit comments

Comments
 (0)