[libc] Add strlen implementation.
authorPaula Toth <paulatoth@google.com>
Fri, 3 Apr 2020 21:47:15 +0000 (14:47 -0700)
committerPaula Toth <paulatoth@google.com>
Fri, 3 Apr 2020 22:11:35 +0000 (15:11 -0700)
Summary: This should fix the call to a non internal libc function.

Reviewers: sivachandra, abrachet

Reviewed By: sivachandra

Subscribers: xbolva00, mgorny, MaskRay, tschuett, libc-commits

Tags: #libc-project

Differential Revision: https://reviews.llvm.org/D77279

libc/fuzzing/string/CMakeLists.txt
libc/src/string/CMakeLists.txt
libc/src/string/strcat.cpp
libc/src/string/strcpy.cpp
libc/src/string/strlen.cpp [new file with mode: 0644]
libc/src/string/strlen.h [new file with mode: 0644]
libc/test/src/string/CMakeLists.txt
libc/test/src/string/strlen_test.cpp [new file with mode: 0644]

index 0482b96..c28c0e4 100644 (file)
@@ -4,4 +4,5 @@ add_libc_fuzzer(
     strcpy_fuzz.cpp
   DEPENDS
     strcpy
+    strlen
 )
index 729ccaa..abe9787 100644 (file)
@@ -9,6 +9,7 @@ add_entrypoint_object(
   DEPENDS
     strcpy
     string_h
+    strlen
 )
 
 add_entrypoint_object(
@@ -19,6 +20,17 @@ add_entrypoint_object(
     strcpy.h
   DEPENDS
     string_h
+    strlen
+)
+
+add_entrypoint_object(
+  strlen
+  SRCS
+    strlen.cpp
+  HDRS
+    strlen.h
+  DEPENDS
+    string_h
 )
 
 # ------------------------------------------------------------------------------
index 366b185..4fc10da 100644 (file)
@@ -7,16 +7,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/string/strcat.h"
+#include "src/string/strcpy.h"
+#include "src/string/strlen.h"
 
 #include "src/__support/common.h"
-#include "src/string/strcpy.h"
 
 namespace __llvm_libc {
 
 char *LLVM_LIBC_ENTRYPOINT(strcat)(char *dest, const char *src) {
-  // We do not yet have an implementaion of strlen in so we will use strlen
-  // from another libc.
-  __llvm_libc::strcpy(dest + ::strlen(dest), src);
+  __llvm_libc::strcpy(dest + __llvm_libc::strlen(dest), src);
   return dest;
 }
 
index 22fe4cc..46cdb1c 100644 (file)
@@ -7,13 +7,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/string/strcpy.h"
+#include "src/string/strlen.h"
 
 #include "src/__support/common.h"
 
 namespace __llvm_libc {
 
 char *LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {
-  return reinterpret_cast<char *>(::memcpy(dest, src, ::strlen(src) + 1));
+  return reinterpret_cast<char *>(
+      ::memcpy(dest, src, __llvm_libc::strlen(src) + 1));
 }
 
 } // namespace __llvm_libc
diff --git a/libc/src/string/strlen.cpp b/libc/src/string/strlen.cpp
new file mode 100644 (file)
index 0000000..3aeafc3
--- /dev/null
@@ -0,0 +1,24 @@
+//===-------------------- Implementation of strlen ------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/string/strlen.h"
+
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+// TODO: investigate the performance of this function.
+// There might be potential for compiler optmization.
+size_t LLVM_LIBC_ENTRYPOINT(strlen)(const char *src) {
+  const char *end = src;
+  while (*end != '\0')
+    ++end;
+  return end - src;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/string/strlen.h b/libc/src/string/strlen.h
new file mode 100644 (file)
index 0000000..32f1d9a
--- /dev/null
@@ -0,0 +1,20 @@
+//===----------------- Implementation header for strlen -------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRING_STRLEN_H
+#define LLVM_LIBC_SRC_STRING_STRLEN_H
+
+#include "include/string.h"
+
+namespace __llvm_libc {
+
+size_t strlen(const char *src);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STRING_STRLEN_H
index 43536e9..cb13451 100644 (file)
@@ -11,6 +11,7 @@ add_libc_unittest(
   DEPENDS
     strcat
     strcpy
+    strlen
 )
 
 add_libc_unittest(
@@ -21,6 +22,17 @@ add_libc_unittest(
     strcpy_test.cpp
   DEPENDS
     strcpy
+    strlen
+)
+
+add_libc_unittest(
+  strlen_test
+  SUITE
+    libc_string_unittests
+  SRCS
+    strlen_test.cpp
+  DEPENDS
+    strlen
 )
 
 # Tests all implementations of memcpy that can run on the host.
diff --git a/libc/test/src/string/strlen_test.cpp b/libc/test/src/string/strlen_test.cpp
new file mode 100644 (file)
index 0000000..f2df81d
--- /dev/null
@@ -0,0 +1,24 @@
+//===----------------------- Unittests for strlen -------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/string/strlen.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(StrLenTest, EmptyString) {
+  const char *empty = "";
+
+  size_t result = __llvm_libc::strlen(empty);
+  ASSERT_EQ((size_t)0, result);
+}
+
+TEST(StrLenTest, AnyString) {
+  const char *any = "Hello World!";
+
+  size_t result = __llvm_libc::strlen(any);
+  ASSERT_EQ((size_t)12, result);
+}