[gn][fidl] Fix sign and shortening warnings in visitor_unittests
The upcoming zircon gn build is less lenient and reports:
../system/utest/fidl-compiler/visitor_unittests.cpp:70:32: warning: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Wsign-compare]
int expected_end = (i + 10 < strlen(expected)) ? i + 10 : strlen(expected) - 1;
~~~~~~ ^ ~~~~~~~~~~~~~~~~
../system/utest/fidl-compiler/visitor_unittests.cpp:70:80: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
int expected_end = (i + 10 < strlen(expected)) ? i + 10 : strlen(expected) - 1;
~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~^~~
../system/utest/fidl-compiler/visitor_unittests.cpp:71:30: warning: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Wsign-compare]
int actual_end = (i + 10 < strlen(actual)) ? i + 10 : strlen(actual) - 1;
~~~~~~ ^ ~~~~~~~~~~~~~~
../system/utest/fidl-compiler/visitor_unittests.cpp:71:74: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
int actual_end = (i + 10 < strlen(actual)) ? i + 10 : strlen(actual) - 1;
~~~~~~~~~~ ~~~~~~~~~~~~~~~^~~
../system/utest/fidl-compiler/visitor_unittests.cpp:95:72: warning: implicit conversion loses integer precision: 'std::__2::basic_string<char, std::__2::char_traits<char>, std::__2::allocator<char> >::size_type' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32]
std::string d = targeted_diff(expected.c_str(), actual, output.size());
~~~~~~~~~~~~~ ~~~~~~~^~~~~~
As far as I could tell, I don't think any of these intend to be
negative, and now it's (somewhat) more clear that negative values will
not be passed to the string() constructor.
ZX-3415 #comment [gn][fidl] Fix sign and shortening warnings in visitor_unittests
Test: CQ
Change-Id: I83d71e95394ba1b6874a43c62c00e57f0d33e8de
diff --git a/zircon/system/utest/fidl-compiler/visitor_unittests.cpp b/zircon/system/utest/fidl-compiler/visitor_unittests.cpp
index 65c96e2..71f0280 100644
--- a/zircon/system/utest/fidl-compiler/visitor_unittests.cpp
+++ b/zircon/system/utest/fidl-compiler/visitor_unittests.cpp
@@ -53,11 +53,11 @@
// Provides more useful context for string diff than EXPECT_STR_EQ, which shows
// a limited prefix. When the string is long, and the difference is buried
// past the limited prefix, the limited prefix doesn't give useful information.
-std::string targeted_diff(const char* expected, const char* actual, int size) {
+std::string targeted_diff(const char* expected, const char* actual, size_t size) {
// We want two lines of useful context:
- int i = 0;
- int last_nl = 0;
- int last_last_nl = 0;
+ size_t i = 0;
+ size_t last_nl = 0;
+ size_t last_last_nl = 0;
while (i <= size && expected[i] == actual[i]) {
if (expected[i] == '\n') {
last_last_nl = last_nl;
@@ -66,9 +66,9 @@
i++;
}
- int start = last_last_nl;
- int expected_end = (i + 10 < strlen(expected)) ? i + 10 : strlen(expected) - 1;
- int actual_end = (i + 10 < strlen(actual)) ? i + 10 : strlen(actual) - 1;
+ size_t start = last_last_nl;
+ size_t expected_end = (i + 10 < strlen(expected)) ? i + 10 : strlen(expected) - 1;
+ size_t actual_end = (i + 10 < strlen(actual)) ? i + 10 : strlen(actual) - 1;
std::string s("Expected contains \"");
s.append(std::string(expected + start, expected_end - start));
s.append("\" and actual contains \"");