Skip to content

Commit 5daf1b2

Browse files
committed
extracted hard-coded platform definitions for unix32, unix64, win32A, win32W and win64 to actual platform files
1 parent 1a52e3b commit 5daf1b2

11 files changed

Lines changed: 217 additions & 99 deletions

File tree

lib/platform.cpp

Lines changed: 53 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -63,86 +63,14 @@ bool Platform::set(Type t)
6363
return true;
6464
case Type::Win32W:
6565
case Type::Win32A:
66-
type = t;
67-
sizeof_bool = 1; // 4 in Visual C++ 4.2
68-
sizeof_short = 2;
69-
sizeof_int = 4;
70-
sizeof_long = 4;
71-
sizeof_long_long = 8;
72-
sizeof_float = 4;
73-
sizeof_double = 8;
74-
sizeof_long_double = 8;
75-
sizeof_wchar_t = 2;
76-
sizeof_size_t = 4;
77-
sizeof_pointer = 4;
78-
defaultSign = '\0';
79-
char_bit = 8;
80-
short_bit = char_bit * sizeof_short;
81-
int_bit = char_bit * sizeof_int;
82-
long_bit = char_bit * sizeof_long;
83-
long_long_bit = char_bit * sizeof_long_long;
84-
return true;
8566
case Type::Win64:
86-
type = t;
87-
sizeof_bool = 1;
88-
sizeof_short = 2;
89-
sizeof_int = 4;
90-
sizeof_long = 4;
91-
sizeof_long_long = 8;
92-
sizeof_float = 4;
93-
sizeof_double = 8;
94-
sizeof_long_double = 8;
95-
sizeof_wchar_t = 2;
96-
sizeof_size_t = 8;
97-
sizeof_pointer = 8;
98-
defaultSign = '\0';
99-
char_bit = 8;
100-
short_bit = char_bit * sizeof_short;
101-
int_bit = char_bit * sizeof_int;
102-
long_bit = char_bit * sizeof_long;
103-
long_long_bit = char_bit * sizeof_long_long;
104-
return true;
10567
case Type::Unix32:
106-
type = t;
107-
sizeof_bool = 1;
108-
sizeof_short = 2;
109-
sizeof_int = 4;
110-
sizeof_long = 4;
111-
sizeof_long_long = 8;
112-
sizeof_float = 4;
113-
sizeof_double = 8;
114-
sizeof_long_double = 12;
115-
sizeof_wchar_t = 4;
116-
sizeof_size_t = 4;
117-
sizeof_pointer = 4;
118-
defaultSign = '\0';
119-
char_bit = 8;
120-
short_bit = char_bit * sizeof_short;
121-
int_bit = char_bit * sizeof_int;
122-
long_bit = char_bit * sizeof_long;
123-
long_long_bit = char_bit * sizeof_long_long;
124-
return true;
12568
case Type::Unix64:
12669
type = t;
127-
sizeof_bool = 1;
128-
sizeof_short = 2;
129-
sizeof_int = 4;
130-
sizeof_long = 8;
131-
sizeof_long_long = 8;
132-
sizeof_float = 4;
133-
sizeof_double = 8;
134-
sizeof_long_double = 16;
135-
sizeof_wchar_t = 4;
136-
sizeof_size_t = 8;
137-
sizeof_pointer = 8;
138-
defaultSign = '\0';
139-
char_bit = 8;
140-
short_bit = char_bit * sizeof_short;
141-
int_bit = char_bit * sizeof_int;
142-
long_bit = char_bit * sizeof_long;
143-
long_long_bit = char_bit * sizeof_long_long;
70+
// read from platform file
14471
return true;
14572
case Type::File:
73+
type = t;
14674
// sizes are not set.
14775
return false;
14876
}
@@ -152,40 +80,73 @@ bool Platform::set(Type t)
15280

15381
bool Platform::set(const std::string& platformstr, std::string& errstr, const std::vector<std::string>& paths, bool debug)
15482
{
155-
if (platformstr == "win32A")
156-
set(Type::Win32A);
157-
else if (platformstr == "win32W")
158-
set(Type::Win32W);
159-
else if (platformstr == "win64")
160-
set(Type::Win64);
161-
else if (platformstr == "unix32")
162-
set(Type::Unix32);
163-
else if (platformstr == "unix64")
164-
set(Type::Unix64);
165-
else if (platformstr == "native")
166-
set(Type::Native);
167-
else if (platformstr == "unspecified")
168-
set(Type::Unspecified);
83+
Type t;
84+
std::string platformFile;
85+
86+
if (platformstr == "win32A") {
87+
// TODO: deprecate
88+
//std::cout << "Platform 'win32A' is deprecated and will be removed in a future version. Please use 'win32a' instead." << std::endl;
89+
t = Type::Win32A;
90+
platformFile = "win32a";
91+
}
92+
else if (platformstr == "win32a") {
93+
t = Type::Win32A;
94+
platformFile = platformstr;
95+
}
96+
else if (platformstr == "win32W") {
97+
// TODO: deprecate
98+
//std::cout << "Platform 'win32W' is deprecated and will be removed in a future version. Please use 'win32w' instead." << std::endl;
99+
t = Type::Win32W;
100+
platformFile = "win32w";
101+
}
102+
else if (platformstr == "win32w") {
103+
t = Type::Win32W;
104+
platformFile = platformstr;
105+
}
106+
else if (platformstr == "win64") {
107+
t = Type::Win64;
108+
platformFile = platformstr;
109+
}
110+
else if (platformstr == "unix32") {
111+
t = Type::Unix32;
112+
platformFile = platformstr;
113+
}
114+
else if (platformstr == "unix64") {
115+
t = Type::Unix64;
116+
platformFile = platformstr;
117+
}
118+
else if (platformstr == "native") {
119+
t = Type::Native;
120+
}
121+
else if (platformstr == "unspecified") {
122+
t = Type::Unspecified;
123+
}
169124
else if (paths.empty()) {
170125
errstr = "unrecognized platform: '" + platformstr + "' (no lookup).";
171126
return false;
172127
}
173128
else {
129+
t = Type::File;
130+
platformFile = platformstr;
131+
}
132+
133+
if (!platformFile.empty()) {
174134
bool found = false;
175135
for (const std::string& path : paths) {
176136
if (debug)
177-
std::cout << "looking for platform '" + platformstr + "' in '" + path + "'" << std::endl;
178-
if (loadFromFile(path.c_str(), platformstr, debug)) {
137+
std::cout << "looking for platform '" + platformFile + "' in '" + path + "'" << std::endl;
138+
if (loadFromFile(path.c_str(), platformFile, debug)) {
179139
found = true;
180140
break;
181141
}
182142
}
183143
if (!found) {
184-
errstr = "unrecognized platform: '" + platformstr + "'.";
144+
errstr = "unrecognized platform: '" + platformFile + "'.";
185145
return false;
186146
}
187147
}
188148

149+
set(t);
189150
return true;
190151
}
191152

@@ -248,6 +209,7 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
248209
if (!rootnode || std::strcmp(rootnode->Name(), "platform") != 0)
249210
return false;
250211

212+
// TODO: warn about missing fields
251213
bool error = false;
252214
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
253215
const char* name = node->Name();
@@ -293,7 +255,6 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
293255
long_bit = char_bit * sizeof_long;
294256
long_long_bit = char_bit * sizeof_long_long;
295257

296-
type = Type::File;
297258
return !error;
298259
}
299260

platforms/unix32.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<platform>
3+
<char_bit>8</char_bit>
4+
<default-sign>&#x000;</default-sign>
5+
<sizeof>
6+
<bool>1</bool>
7+
<short>2</short>
8+
<int>4</int>
9+
<long>4</long>
10+
<long-long>8</long-long>
11+
<float>4</float>
12+
<double>8</double>
13+
<long-double>12</long-double>
14+
<pointer>4</pointer>
15+
<size_t>4</size_t>
16+
<wchar_t>4</wchar_t>
17+
</sizeof>
18+
</platform>

platforms/unix64.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<platform>
3+
<char_bit>8</char_bit>
4+
<default-sign>&#x000;</default-sign>
5+
<sizeof>
6+
<bool>1</bool>
7+
<short>2</short>
8+
<int>4</int>
9+
<long>8</long>
10+
<long-long>8</long-long>
11+
<float>4</float>
12+
<double>8</double>
13+
<long-double>16</long-double>
14+
<pointer>8</pointer>
15+
<size_t>8</size_t>
16+
<wchar_t>4</wchar_t>
17+
</sizeof>
18+
</platform>

platforms/win32a.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<platform>
3+
<char_bit>8</char_bit>
4+
<default-sign>&#x000;</default-sign>
5+
<sizeof>
6+
<bool>1</bool><!--4 in Visual C++ 4.2-->
7+
<short>2</short>
8+
<int>4</int>
9+
<long>4</long>
10+
<long-long>8</long-long>
11+
<float>4</float>
12+
<double>8</double>
13+
<long-double>8</long-double>
14+
<pointer>4</pointer>
15+
<size_t>4</size_t>
16+
<wchar_t>2</wchar_t>
17+
</sizeof>
18+
</platform>

platforms/win32w.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<platform>
3+
<char_bit>8</char_bit>
4+
<default-sign>&#x000;</default-sign>
5+
<sizeof>
6+
<bool>1</bool><!--4 in Visual C++ 4.2-->
7+
<short>2</short>
8+
<int>4</int>
9+
<long>4</long>
10+
<long-long>8</long-long>
11+
<float>4</float>
12+
<double>8</double>
13+
<long-double>8</long-double>
14+
<pointer>4</pointer>
15+
<size_t>4</size_t>
16+
<wchar_t>2</wchar_t>
17+
</sizeof>
18+
</platform>

platforms/win64.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<platform>
3+
<char_bit>8</char_bit>
4+
<default-sign>&#x000;</default-sign>
5+
<sizeof>
6+
<bool>1</bool>
7+
<short>2</short>
8+
<int>4</int>
9+
<long>4</long>
10+
<long-long>8</long-long>
11+
<float>4</float>
12+
<double>8</double>
13+
<long-double>8</long-double>
14+
<pointer>8</pointer>
15+
<size_t>8</size_t>
16+
<wchar_t>2</wchar_t>
17+
</sizeof>
18+
</platform>

releasenotes.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ Deprecations:
1616
-
1717

1818
Other:
19-
- Removed deperecated support for builds via qmake.
19+
- Removed deprecated support for builds via qmake.
20+
- The `win32a` and `win32w` aliases have been added for the platforms `win32A` and `win32W` respectively.
2021
-

test/cli/other_test.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,3 +2276,43 @@ def test_dumpfile_platform(tmpdir):
22762276
break
22772277
assert ' wchar_t_bit="' in platform
22782278
assert ' size_t_bit="' in platform
2279+
2280+
2281+
def test_custom_platform(tmpdir):
2282+
test_cfg = os.path.join(tmpdir, 'test.cfg')
2283+
with open(test_cfg, 'wt') as f:
2284+
f.write("""
2285+
<?xml version="1.0"?>
2286+
<platform>
2287+
<char_bit>8</char_bit>
2288+
<default-sign>unsigned</default-sign>
2289+
<sizeof>
2290+
<bool>1</bool>
2291+
<short>2</short>
2292+
<int>2</int>
2293+
<long>4</long>
2294+
<long-long>8</long-long>
2295+
<float>4</float>
2296+
<double>4</double>
2297+
<long-double>4</long-double>
2298+
<pointer>2</pointer>
2299+
<size_t>2</size_t>
2300+
<wchar_t>2</wchar_t>
2301+
</sizeof>
2302+
</platform>
2303+
""")
2304+
2305+
# TODO: use a sample to make sure the file is actually used
2306+
test_file = os.path.join(tmpdir, 'test.cpp')
2307+
with open(test_file, 'wt') as f:
2308+
f.write("""
2309+
""")
2310+
args = ['--platform={}'.format(test_cfg), test_file]
2311+
2312+
exitcode, stdout, stderr = cppcheck(args)
2313+
assert exitcode == 0, stdout
2314+
lines = stdout.splitlines()
2315+
assert lines == [
2316+
'Checking {} ...'.format(test_file)
2317+
]
2318+
assert stderr == ''

test/cli/test-other.py

Whitespace-only changes.

test/testcmdlineparser.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ class TestCmdlineParser : public TestFixture {
243243
TEST_CASE(stdmulti2);
244244
TEST_CASE(platformWin64);
245245
TEST_CASE(platformWin32A);
246+
TEST_CASE(platformWin32a);
246247
TEST_CASE(platformWin32W);
248+
TEST_CASE(platformWin32w);
247249
TEST_CASE(platformUnix32);
248250
TEST_CASE(platformUnix32Unsigned);
249251
TEST_CASE(platformUnix64);
@@ -1534,6 +1536,15 @@ class TestCmdlineParser : public TestFixture {
15341536
ASSERT_EQUALS(Platform::Type::Win32A, settings->platform.type);
15351537
}
15361538

1539+
void platformWin32a() {
1540+
REDIRECT;
1541+
const char * const argv[] = {"cppcheck", "--platform=win32a", "file.cpp"};
1542+
ASSERT(settings->platform.set(cppcheck::Platform::Type::Unspecified));
1543+
ASSERT(parser->parseFromArgs(3, argv));
1544+
ASSERT_EQUALS(cppcheck::Platform::Type::Win32A, settings->platform.type);
1545+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1546+
}
1547+
15371548
void platformWin32W() {
15381549
REDIRECT;
15391550
const char * const argv[] = {"cppcheck", "--platform=win32W", "file.cpp"};
@@ -1542,6 +1553,15 @@ class TestCmdlineParser : public TestFixture {
15421553
ASSERT_EQUALS(Platform::Type::Win32W, settings->platform.type);
15431554
}
15441555

1556+
void platformWin32w() {
1557+
REDIRECT;
1558+
const char * const argv[] = {"cppcheck", "--platform=win32w", "file.cpp"};
1559+
ASSERT(settings->platform.set(cppcheck::Platform::Type::Unspecified));
1560+
ASSERT(parser->parseFromArgs(3, argv));
1561+
ASSERT_EQUALS(cppcheck::Platform::Type::Win32W, settings->platform.type);
1562+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1563+
}
1564+
15451565
void platformUnix32() {
15461566
REDIRECT;
15471567
const char * const argv[] = {"cppcheck", "--platform=unix32", "file.cpp"};

0 commit comments

Comments
 (0)