[libc-commits] [libc] 9cdd4ea - [libc][NFC] Add explicit casts to ctype functions
Siva Chandra Reddy via libc-commits
libc-commits at lists.llvm.org
Mon Aug 23 11:17:29 PDT 2021
Author: Alfonso Gregory
Date: 2021-08-23T18:17:20Z
New Revision: 9cdd4ea06f094efaedb46e848e004baaad7cb183
URL: https://github.com/llvm/llvm-project/commit/9cdd4ea06f094efaedb46e848e004baaad7cb183
DIFF: https://github.com/llvm/llvm-project/commit/9cdd4ea06f094efaedb46e848e004baaad7cb183.diff
LOG: [libc][NFC] Add explicit casts to ctype functions
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D106902
Added:
Modified:
libc/src/__support/ctype_utils.h
libc/src/ctype/isalnum.cpp
libc/src/ctype/isalpha.cpp
libc/src/ctype/isascii.cpp
libc/src/ctype/isblank.cpp
libc/src/ctype/iscntrl.cpp
libc/src/ctype/isdigit.cpp
libc/src/ctype/isgraph.cpp
libc/src/ctype/islower.cpp
libc/src/ctype/isprint.cpp
libc/src/ctype/ispunct.cpp
libc/src/ctype/isspace.cpp
libc/src/ctype/isupper.cpp
libc/src/ctype/isxdigit.cpp
libc/src/ctype/tolower.cpp
libc/src/ctype/toupper.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/ctype_utils.h b/libc/src/__support/ctype_utils.h
index 65b6ca00f51f4..f3f4b366d91d9 100644
--- a/libc/src/__support/ctype_utils.h
+++ b/libc/src/__support/ctype_utils.h
@@ -18,19 +18,21 @@ namespace internal {
// of a function call by inlining them.
// ------------------------------------------------------
-static constexpr int isalpha(unsigned ch) { return (ch | 32) - 'a' < 26; }
+static constexpr bool isalpha(unsigned ch) { return (ch | 32) - 'a' < 26; }
-static constexpr int isdigit(unsigned ch) { return (ch - '0') < 10; }
+static constexpr bool isdigit(unsigned ch) { return (ch - '0') < 10; }
-static constexpr int isalnum(unsigned ch) { return isalpha(ch) || isdigit(ch); }
+static constexpr bool isalnum(unsigned ch) {
+ return isalpha(ch) || isdigit(ch);
+}
-static constexpr int isgraph(unsigned ch) { return 0x20 < ch && ch < 0x7f; }
+static constexpr bool isgraph(unsigned ch) { return 0x20 < ch && ch < 0x7f; }
-static constexpr int islower(unsigned ch) { return (ch - 'a') < 26; }
+static constexpr bool islower(unsigned ch) { return (ch - 'a') < 26; }
-static constexpr int isupper(unsigned ch) { return (ch - 'A') < 26; }
+static constexpr bool isupper(unsigned ch) { return (ch - 'A') < 26; }
-static constexpr int isspace(unsigned ch) {
+static constexpr bool isspace(unsigned ch) {
return ch == ' ' || (ch - '\t') < 5;
}
diff --git a/libc/src/ctype/isalnum.cpp b/libc/src/ctype/isalnum.cpp
index 07ff54aa38e6c..ce3608fceae36 100644
--- a/libc/src/ctype/isalnum.cpp
+++ b/libc/src/ctype/isalnum.cpp
@@ -15,6 +15,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
-LLVM_LIBC_FUNCTION(int, isalnum, (int c)) { return internal::isalnum(c); }
+LLVM_LIBC_FUNCTION(int, isalnum, (int c)) {
+ return static_cast<int>(internal::isalnum(static_cast<unsigned>(c)));
+}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/isalpha.cpp b/libc/src/ctype/isalpha.cpp
index 9e2b7a7377daf..37f80f90cd2c8 100644
--- a/libc/src/ctype/isalpha.cpp
+++ b/libc/src/ctype/isalpha.cpp
@@ -15,6 +15,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
-LLVM_LIBC_FUNCTION(int, isalpha, (int c)) { return internal::isalpha(c); }
+LLVM_LIBC_FUNCTION(int, isalpha, (int c)) {
+ return static_cast<int>(internal::isalpha(static_cast<unsigned>(c)));
+}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/isascii.cpp b/libc/src/ctype/isascii.cpp
index c12915ecd6ee3..8357563c7f3cc 100644
--- a/libc/src/ctype/isascii.cpp
+++ b/libc/src/ctype/isascii.cpp
@@ -12,6 +12,8 @@
namespace __llvm_libc {
-LLVM_LIBC_FUNCTION(int, isascii, (int c)) { return (c & (~0x7f)) == 0; }
+LLVM_LIBC_FUNCTION(int, isascii, (int c)) {
+ return static_cast<int>((c & (~0x7f)) == 0);
+}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/isblank.cpp b/libc/src/ctype/isblank.cpp
index 1c3061379b293..eed838c8ac8db 100644
--- a/libc/src/ctype/isblank.cpp
+++ b/libc/src/ctype/isblank.cpp
@@ -15,8 +15,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
LLVM_LIBC_FUNCTION(int, isblank, (int c)) {
- const unsigned char ch = static_cast<char>(c);
- return ch == ' ' || ch == '\t';
+ const unsigned char ch = static_cast<unsigned char>(c);
+ return static_cast<int>(ch == ' ' || ch == '\t');
}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/iscntrl.cpp b/libc/src/ctype/iscntrl.cpp
index b061199c47ec5..6c471a58102cd 100644
--- a/libc/src/ctype/iscntrl.cpp
+++ b/libc/src/ctype/iscntrl.cpp
@@ -15,8 +15,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
LLVM_LIBC_FUNCTION(int, iscntrl, (int c)) {
- const unsigned char ch = static_cast<char>(c);
- return ch < 0x20 || ch == 0x7f;
+ const unsigned char ch = static_cast<unsigned char>(c);
+ return static_cast<int>(ch < 0x20 || ch == 0x7f);
}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/isdigit.cpp b/libc/src/ctype/isdigit.cpp
index 79a8b106f925b..b478384916d1b 100644
--- a/libc/src/ctype/isdigit.cpp
+++ b/libc/src/ctype/isdigit.cpp
@@ -14,6 +14,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
-LLVM_LIBC_FUNCTION(int, isdigit, (int c)) { return internal::isdigit(c); }
+LLVM_LIBC_FUNCTION(int, isdigit, (int c)) {
+ return static_cast<int>(internal::isdigit(static_cast<unsigned>(c)));
+}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/isgraph.cpp b/libc/src/ctype/isgraph.cpp
index 8d0bb38c2ead2..6a1f55e85eea9 100644
--- a/libc/src/ctype/isgraph.cpp
+++ b/libc/src/ctype/isgraph.cpp
@@ -15,6 +15,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
-LLVM_LIBC_FUNCTION(int, isgraph, (int c)) { return internal::isgraph(c); }
+LLVM_LIBC_FUNCTION(int, isgraph, (int c)) {
+ return static_cast<int>(internal::isgraph(static_cast<unsigned>(c)));
+}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/islower.cpp b/libc/src/ctype/islower.cpp
index 73ce42e912bf4..b21ccd491ebcf 100644
--- a/libc/src/ctype/islower.cpp
+++ b/libc/src/ctype/islower.cpp
@@ -15,6 +15,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
-LLVM_LIBC_FUNCTION(int, islower, (int c)) { return internal::islower(c); }
+LLVM_LIBC_FUNCTION(int, islower, (int c)) {
+ return static_cast<int>(internal::islower(static_cast<unsigned>(c)));
+}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/isprint.cpp b/libc/src/ctype/isprint.cpp
index a80500b133c4f..dd9a085e17cbc 100644
--- a/libc/src/ctype/isprint.cpp
+++ b/libc/src/ctype/isprint.cpp
@@ -15,8 +15,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
LLVM_LIBC_FUNCTION(int, isprint, (int c)) {
- const unsigned ch = c;
- return (ch - ' ') < 95;
+ const unsigned ch = static_cast<unsigned>(c);
+ return static_cast<int>((ch - ' ') < 95);
}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/ispunct.cpp b/libc/src/ctype/ispunct.cpp
index 46c2fec6d21b9..7ccad4b0ecdc0 100644
--- a/libc/src/ctype/ispunct.cpp
+++ b/libc/src/ctype/ispunct.cpp
@@ -16,7 +16,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
LLVM_LIBC_FUNCTION(int, ispunct, (int c)) {
- return !internal::isalnum(c) && internal::isgraph(c);
+ const unsigned ch = static_cast<unsigned>(c);
+ return static_cast<int>(!internal::isalnum(ch) && internal::isgraph(ch));
}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/isspace.cpp b/libc/src/ctype/isspace.cpp
index ae86491127736..8cd76c4ea6d2a 100644
--- a/libc/src/ctype/isspace.cpp
+++ b/libc/src/ctype/isspace.cpp
@@ -15,6 +15,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
-LLVM_LIBC_FUNCTION(int, isspace, (int c)) { return internal::isspace(c); }
+LLVM_LIBC_FUNCTION(int, isspace, (int c)) {
+ return static_cast<int>(internal::isspace(static_cast<unsigned>(c)));
+}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/isupper.cpp b/libc/src/ctype/isupper.cpp
index bf3c0cf1f9297..dcb4cf1b4db53 100644
--- a/libc/src/ctype/isupper.cpp
+++ b/libc/src/ctype/isupper.cpp
@@ -15,6 +15,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
-LLVM_LIBC_FUNCTION(int, isupper, (int c)) { return internal::isupper(c); }
+LLVM_LIBC_FUNCTION(int, isupper, (int c)) {
+ return static_cast<int>(internal::isupper(static_cast<unsigned>(c)));
+}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/isxdigit.cpp b/libc/src/ctype/isxdigit.cpp
index e2a618de4e1ac..96efc8f1d9300 100644
--- a/libc/src/ctype/isxdigit.cpp
+++ b/libc/src/ctype/isxdigit.cpp
@@ -16,8 +16,8 @@ namespace __llvm_libc {
// TODO: Currently restricted to default locale.
// These should be extended using locale information.
LLVM_LIBC_FUNCTION(int, isxdigit, (int c)) {
- const unsigned ch = c;
- return internal::isdigit(ch) || (ch | 32) - 'a' < 6;
+ const unsigned ch = static_cast<unsigned>(c);
+ return static_cast<int>(internal::isdigit(ch) || (ch | 32) - 'a' < 6);
}
} // namespace __llvm_libc
diff --git a/libc/src/ctype/tolower.cpp b/libc/src/ctype/tolower.cpp
index 140c548b996c5..8fbb5aaded48a 100644
--- a/libc/src/ctype/tolower.cpp
+++ b/libc/src/ctype/tolower.cpp
@@ -17,7 +17,7 @@ namespace __llvm_libc {
// These should be extended using locale information.
LLVM_LIBC_FUNCTION(int, tolower, (int c)) {
if (internal::isupper(c))
- return c + 'a' - 'A';
+ return c + ('a' - 'A');
return c;
}
diff --git a/libc/src/ctype/toupper.cpp b/libc/src/ctype/toupper.cpp
index d2491ce224397..b216cc277b8c2 100644
--- a/libc/src/ctype/toupper.cpp
+++ b/libc/src/ctype/toupper.cpp
@@ -17,7 +17,7 @@ namespace __llvm_libc {
// These should be extended using locale information.
LLVM_LIBC_FUNCTION(int, toupper, (int c)) {
if (internal::islower(c))
- return c + 'A' - 'a';
+ return c - ('a' - 'A');
return c;
}
More information about the libc-commits
mailing list