Skip to content

Commit 481f8f4

Browse files
authored
Add missing headers and std qualification, use std::numeric_limits in… (#1546)
* Add missing headers and std qualification, use std::numeric_limits instead of macros, and use std::memcpy/std::swprintf instead of the non-standard memcpy_s/swprintf_s * Remove a accidentally added redundant comment * Add some headers as suggested by Copilot * Use std::copy_n instead of std::memcpy, and protect std::numeric_limits<T>::max from the max macro
1 parent 8864ac6 commit 481f8f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+814
-805
lines changed

cppwinrt/cmd_reader.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <limits>
66
#include <climits>
77
#include <cstdint>
8+
#include <cctype>
89
#include <string>
910
#include <string_view>
1011
#include <map>
@@ -203,7 +204,7 @@ namespace cppwinrt
203204

204205
while (true)
205206
{
206-
DWORD actual_size = GetModuleFileNameA(nullptr, path.data(), 1 + static_cast<uint32_t>(path.size()));
207+
DWORD actual_size = GetModuleFileNameA(nullptr, path.data(), 1 + static_cast<std::uint32_t>(path.size()));
207208

208209
if (actual_size < 1 + path.size())
209210
{
@@ -237,12 +238,12 @@ namespace cppwinrt
237238
}
238239

239240
auto key = open_sdk();
240-
uint32_t index{};
241+
std::uint32_t index{};
241242
std::array<char, 100> subkey;
242243
std::array<unsigned long, 4> version_parts{};
243244
std::string result;
244245

245-
while (0 == RegEnumKeyA(key.handle, index++, subkey.data(), static_cast<uint32_t>(subkey.size())))
246+
while (0 == RegEnumKeyA(key.handle, index++, subkey.data(), static_cast<std::uint32_t>(subkey.size())))
246247
{
247248
if (!std::regex_match(subkey.data(), match, rx))
248249
{
@@ -258,7 +259,7 @@ namespace cppwinrt
258259
char* next_part = subkey.data();
259260
bool force_newer = false;
260261

261-
for (size_t i = 0; ; ++i)
262+
for (std::size_t i = 0; ; ++i)
262263
{
263264
auto version_part = strtoul(next_part, &next_part, 10);
264265

@@ -312,19 +313,19 @@ namespace cppwinrt
312313

313314
struct option
314315
{
315-
static constexpr uint32_t no_min = 0;
316-
static constexpr uint32_t no_max = UINT_MAX;
316+
static constexpr std::uint32_t no_min = 0;
317+
static constexpr std::uint32_t no_max = (std::numeric_limits<std::uint32_t>::max)();
317318

318319
std::string_view name;
319-
uint32_t min{ no_min };
320-
uint32_t max{ no_max };
320+
std::uint32_t min{ no_min };
321+
std::uint32_t max{ no_max };
321322
std::string_view arg{};
322323
std::string_view desc{};
323324
};
324325

325326
struct reader
326327
{
327-
template <typename C, typename V, size_t numOptions>
328+
template <typename C, typename V, std::size_t numOptions>
328329
reader(C const argc, V const argv, const option(& options)[numOptions])
329330
{
330331
#ifdef _DEBUG
@@ -449,9 +450,9 @@ namespace cppwinrt
449450
#if defined(_WIN32) || defined(_WIN64)
450451
std::array<char, 260> local{};
451452
#ifdef _WIN64
452-
ExpandEnvironmentStringsA("%windir%\\System32\\WinMetadata", local.data(), static_cast<uint32_t>(local.size()));
453+
ExpandEnvironmentStringsA("%windir%\\System32\\WinMetadata", local.data(), static_cast<std::uint32_t>(local.size()));
453454
#else
454-
ExpandEnvironmentStringsA("%windir%\\SysNative\\WinMetadata", local.data(), static_cast<uint32_t>(local.size()));
455+
ExpandEnvironmentStringsA("%windir%\\SysNative\\WinMetadata", local.data(), static_cast<std::uint32_t>(local.size()));
455456
#endif
456457
add_directory(local.data());
457458
#else /* defined(_WIN32) || defined(_WIN64) */
@@ -586,7 +587,7 @@ namespace cppwinrt
586587
std::filesystem::path response_path{ std::string{ arg } };
587588
std::string extension = response_path.extension().generic_string();
588589
std::transform(extension.begin(), extension.end(), extension.begin(),
589-
[](auto c) { return static_cast<unsigned char>(::tolower(c)); });
590+
[](auto c) { return static_cast<unsigned char>(std::tolower(c)); });
590591

591592
// Check if misuse of @ prefix, so if directory or metadata file instead of response file.
592593
if (is_directory(response_path) || extension == ".winmd")
@@ -597,20 +598,20 @@ namespace cppwinrt
597598
std::ifstream response_file(absolute(response_path));
598599
while (getline(response_file, line_buf))
599600
{
600-
size_t argc = 0;
601+
std::size_t argc = 0;
601602
std::vector<std::string> argv;
602603

603604
parse_command_line(line_buf.data(), argv, &argc);
604605

605-
for (size_t i = 0; i < argc; i++)
606+
for (std::size_t i = 0; i < argc; i++)
606607
{
607608
extract_option(argv[i], options, last);
608609
}
609610
}
610611
}
611612

612613
template <typename Character>
613-
static void parse_command_line(Character* cmdstart, std::vector<std::string>& argv, size_t* argument_count)
614+
static void parse_command_line(Character* cmdstart, std::vector<std::string>& argv, std::size_t* argument_count)
614615
{
615616

616617
std::string arg;

cppwinrt/code_writers.h

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -359,35 +359,35 @@ namespace cppwinrt
359359
using std::get;
360360

361361
w.write_printf("0x%08X,0x%04X,0x%04X,{ 0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X }",
362-
get<uint32_t>(get<ElemSig>(args[0].value).value),
363-
get<uint16_t>(get<ElemSig>(args[1].value).value),
364-
get<uint16_t>(get<ElemSig>(args[2].value).value),
365-
get<uint8_t>(get<ElemSig>(args[3].value).value),
366-
get<uint8_t>(get<ElemSig>(args[4].value).value),
367-
get<uint8_t>(get<ElemSig>(args[5].value).value),
368-
get<uint8_t>(get<ElemSig>(args[6].value).value),
369-
get<uint8_t>(get<ElemSig>(args[7].value).value),
370-
get<uint8_t>(get<ElemSig>(args[8].value).value),
371-
get<uint8_t>(get<ElemSig>(args[9].value).value),
372-
get<uint8_t>(get<ElemSig>(args[10].value).value));
362+
get<std::uint32_t>(get<ElemSig>(args[0].value).value),
363+
get<std::uint16_t>(get<ElemSig>(args[1].value).value),
364+
get<std::uint16_t>(get<ElemSig>(args[2].value).value),
365+
get<std::uint8_t>(get<ElemSig>(args[3].value).value),
366+
get<std::uint8_t>(get<ElemSig>(args[4].value).value),
367+
get<std::uint8_t>(get<ElemSig>(args[5].value).value),
368+
get<std::uint8_t>(get<ElemSig>(args[6].value).value),
369+
get<std::uint8_t>(get<ElemSig>(args[7].value).value),
370+
get<std::uint8_t>(get<ElemSig>(args[8].value).value),
371+
get<std::uint8_t>(get<ElemSig>(args[9].value).value),
372+
get<std::uint8_t>(get<ElemSig>(args[10].value).value));
373373
}
374374

375375
static void write_guid_comment(writer& w, std::vector<FixedArgSig> const& args)
376376
{
377377
using std::get;
378378

379379
w.write_printf("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
380-
get<uint32_t>(get<ElemSig>(args[0].value).value),
381-
get<uint16_t>(get<ElemSig>(args[1].value).value),
382-
get<uint16_t>(get<ElemSig>(args[2].value).value),
383-
get<uint8_t>(get<ElemSig>(args[3].value).value),
384-
get<uint8_t>(get<ElemSig>(args[4].value).value),
385-
get<uint8_t>(get<ElemSig>(args[5].value).value),
386-
get<uint8_t>(get<ElemSig>(args[6].value).value),
387-
get<uint8_t>(get<ElemSig>(args[7].value).value),
388-
get<uint8_t>(get<ElemSig>(args[8].value).value),
389-
get<uint8_t>(get<ElemSig>(args[9].value).value),
390-
get<uint8_t>(get<ElemSig>(args[10].value).value));
380+
get<std::uint32_t>(get<ElemSig>(args[0].value).value),
381+
get<std::uint16_t>(get<ElemSig>(args[1].value).value),
382+
get<std::uint16_t>(get<ElemSig>(args[2].value).value),
383+
get<std::uint8_t>(get<ElemSig>(args[3].value).value),
384+
get<std::uint8_t>(get<ElemSig>(args[4].value).value),
385+
get<std::uint8_t>(get<ElemSig>(args[5].value).value),
386+
get<std::uint8_t>(get<ElemSig>(args[6].value).value),
387+
get<std::uint8_t>(get<ElemSig>(args[7].value).value),
388+
get<std::uint8_t>(get<ElemSig>(args[8].value).value),
389+
get<std::uint8_t>(get<ElemSig>(args[9].value).value),
390+
get<std::uint8_t>(get<ElemSig>(args[10].value).value));
391391
}
392392

393393
static void write_category(writer& w, TypeDef const& type, std::string_view const& category)
@@ -561,15 +561,15 @@ namespace cppwinrt
561561

562562
if (param.Flags().In())
563563
{
564-
format = "uint32_t%, %";
564+
format = "std::uint32_t%, %";
565565
}
566566
else if (param_signature->ByRef())
567567
{
568-
format = "uint32_t*%, %*";
568+
format = "std::uint32_t*%, %*";
569569
}
570570
else
571571
{
572-
format = "uint32_t%, %";
572+
format = "std::uint32_t%, %";
573573
}
574574

575575
w.write(format, bind<write_array_size_name>(param), bind<write_abi_arg_out>(param_signature->Type()));
@@ -605,7 +605,7 @@ namespace cppwinrt
605605

606606
if (type.is_szarray())
607607
{
608-
w.write("uint32_t* __%Size, %**", method_signature.return_param_name(), type);
608+
w.write("std::uint32_t* __%Size, %**", method_signature.return_param_name(), type);
609609
}
610610
else
611611
{
@@ -748,7 +748,7 @@ namespace cppwinrt
748748
break;
749749
}
750750

751-
auto format = R"( virtual int32_t __stdcall %(%) noexcept = 0;
751+
auto format = R"( virtual std::int32_t __stdcall %(%) noexcept = 0;
752752
)";
753753

754754
for (auto&& method : info.type.MethodList())
@@ -788,7 +788,7 @@ namespace cppwinrt
788788
}
789789

790790

791-
auto format = R"( virtual int32_t __stdcall %(%) noexcept = 0;
791+
auto format = R"( virtual std::int32_t __stdcall %(%) noexcept = 0;
792792
)";
793793

794794
auto abi_guard = w.push_abi_types(true);
@@ -821,7 +821,7 @@ namespace cppwinrt
821821
{
822822
struct WINRT_IMPL_ABI_DECL type : unknown_abi
823823
{
824-
virtual int32_t __stdcall Invoke(%) noexcept = 0;
824+
virtual std::int32_t __stdcall Invoke(%) noexcept = 0;
825825
};
826826
};
827827
)";
@@ -1054,7 +1054,7 @@ namespace cppwinrt
10541054
if (category == param_category::array_type)
10551055
{
10561056
auto format = R"(
1057-
uint32_t %_impl_size{};
1057+
std::uint32_t %_impl_size{};
10581058
%* %{};)";
10591059

10601060
auto abi_guard = w.push_abi_types(true);
@@ -1317,7 +1317,7 @@ namespace cppwinrt
13171317
w.write(R"(
13181318
auto data() const
13191319
{
1320-
uint8_t* data{};
1320+
std::uint8_t* data{};
13211321
static_cast<D const&>(*this).template as<IBufferByteAccess>()->Buffer(&data);
13221322
return data;
13231323
}
@@ -1328,8 +1328,8 @@ namespace cppwinrt
13281328
w.write(R"(
13291329
auto data() const
13301330
{
1331-
uint8_t* data{};
1332-
uint32_t capacity{};
1331+
std::uint8_t* data{};
1332+
std::uint32_t capacity{};
13331333
check_hresult(static_cast<D const&>(*this).template as<IMemoryBufferByteAccess>()->GetBuffer(&data, &capacity));
13341334
return data;
13351335
}
@@ -1480,7 +1480,7 @@ namespace cppwinrt
14801480
using iterator_concept = std::input_iterator_tag;
14811481
using iterator_category = std::input_iterator_tag;
14821482
using value_type = T;
1483-
using difference_type = ptrdiff_t;
1483+
using difference_type = std::ptrdiff_t;
14841484
using pointer = void;
14851485
using reference = T;
14861486
)");
@@ -1491,7 +1491,7 @@ namespace cppwinrt
14911491
using iterator_concept = std::input_iterator_tag;
14921492
using iterator_category = std::input_iterator_tag;
14931493
using value_type = Windows::Foundation::IInspectable;
1494-
using difference_type = ptrdiff_t;
1494+
using difference_type = std::ptrdiff_t;
14951495
using pointer = void;
14961496
using reference = Windows::Foundation::IInspectable;
14971497
)");
@@ -1874,7 +1874,7 @@ namespace cppwinrt
18741874

18751875
if (is_noexcept(method))
18761876
{
1877-
format = R"( int32_t __stdcall %(%) noexcept final
1877+
format = R"( std::int32_t __stdcall %(%) noexcept final
18781878
{
18791879
% typename D::abi_guard guard(this->shim());
18801880
%
@@ -1884,7 +1884,7 @@ namespace cppwinrt
18841884
}
18851885
else
18861886
{
1887-
format = R"( int32_t __stdcall %(%) noexcept final try
1887+
format = R"( std::int32_t __stdcall %(%) noexcept final try
18881888
{
18891889
% typename D::abi_guard guard(this->shim());
18901890
%
@@ -1906,7 +1906,7 @@ namespace cppwinrt
19061906
{
19071907
// Special-case IMap*::Lookup to look for a TryLookup here, to avoid extranous throw/originates
19081908
std::string tryLookupUpCall = "this->shim().TryLookup";
1909-
format = R"( int32_t __stdcall %(%) noexcept final try
1909+
format = R"( std::int32_t __stdcall %(%) noexcept final try
19101910
{
19111911
% typename D::abi_guard guard(this->shim());
19121912
if constexpr (has_TryLookup_v<D, K>)
@@ -2596,7 +2596,7 @@ struct WINRT_IMPL_EMPTY_BASES produce_dispatch_to_overridable<T, D, %>
25962596
{
25972597
delegate(H&& handler) : implements_delegate<%, H>(std::forward<H>(handler)) {}
25982598
2599-
int32_t __stdcall Invoke(%) noexcept final try
2599+
std::int32_t __stdcall Invoke(%) noexcept final try
26002600
{
26012601
% %
26022602
return 0;
@@ -2801,7 +2801,7 @@ struct WINRT_IMPL_EMPTY_BASES produce_dispatch_to_overridable<T, D, %>
28012801

28022802
static void write_struct_equality(writer& w, std::vector<std::pair<std::string_view, std::string>> const& fields)
28032803
{
2804-
for (size_t i = 0; i != fields.size(); ++i)
2804+
for (std::size_t i = 0; i != fields.size(); ++i)
28052805
{
28062806
w.write(" left.% == right.%", fields[i].first, fields[i].first);
28072807

@@ -2877,9 +2877,9 @@ struct WINRT_IMPL_EMPTY_BASES produce_dispatch_to_overridable<T, D, %>
28772877
return false;
28782878
};
28792879

2880-
for (size_t left = 0; left < structs.size(); ++left)
2880+
for (std::size_t left = 0; left < structs.size(); ++left)
28812881
{
2882-
for (size_t right = left + 1; right < structs.size(); ++right)
2882+
for (std::size_t right = left + 1; right < structs.size(); ++right)
28832883
{
28842884
if (depends(w, structs[left], structs[right]))
28852885
{

0 commit comments

Comments
 (0)