[libc] Add strlen implementation.

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
This commit is contained in:
Paula Toth 2020-04-03 14:47:15 -07:00
parent 1893065d7b
commit 1fcfd30fae
8 changed files with 99 additions and 5 deletions

View File

@ -4,4 +4,5 @@ add_libc_fuzzer(
strcpy_fuzz.cpp
DEPENDS
strcpy
strlen
)

View 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
)
# ------------------------------------------------------------------------------

View 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;
}

View 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

View File

@ -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

20
libc/src/string/strlen.h Normal file
View File

@ -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

View 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.

View File

@ -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);
}