[libc] add putc, fputc, and putchar
authorMichael Jones <michaelrj@google.com>
Fri, 14 Oct 2022 21:03:46 +0000 (14:03 -0700)
committerMichael Jones <michaelrj@google.com>
Mon, 17 Oct 2022 23:29:04 +0000 (16:29 -0700)
These three functions are simple, but needed for libc build testing.

Reviewed By: lntue

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

libc/config/linux/x86_64/entrypoints.txt
libc/spec/stdc.td
libc/src/stdio/CMakeLists.txt
libc/src/stdio/fputc.cpp [new file with mode: 0644]
libc/src/stdio/fputc.h [new file with mode: 0644]
libc/src/stdio/putc.cpp [new file with mode: 0644]
libc/src/stdio/putc.h [new file with mode: 0644]
libc/src/stdio/putchar.cpp [new file with mode: 0644]
libc/src/stdio/putchar.h [new file with mode: 0644]
libc/test/src/stdio/CMakeLists.txt
libc/test/src/stdio/putc_test.cpp [new file with mode: 0644]

index 6198b35..e41a897 100644 (file)
@@ -359,6 +359,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.ferror_unlocked
     libc.src.stdio.fflush
     libc.src.stdio.fopen
+    libc.src.stdio.fputc
     libc.src.stdio.fputs
     libc.src.stdio.fopencookie
     libc.src.stdio.fread
@@ -369,6 +370,8 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.fwrite_unlocked
     libc.src.stdio.fprintf
     libc.src.stdio.printf
+    libc.src.stdio.putc
+    libc.src.stdio.putchar
     libc.src.stdio.puts
     libc.src.stdio.stderr
     libc.src.stdio.stdout
index 8672811..4d6c0b1 100644 (file)
@@ -536,6 +536,23 @@ def StdC : StandardSpec<"stdc"> {
                ArgSpec<ConstCharPtr>]
           >,
           FunctionSpec<
+              "fputc",
+              RetValSpec<IntType>,
+              [ArgSpec<IntType>,
+               ArgSpec<FILEPtr>]
+          >,
+          FunctionSpec<
+              "putc",
+              RetValSpec<IntType>,
+              [ArgSpec<IntType>,
+               ArgSpec<FILEPtr>]
+          >,
+          FunctionSpec<
+              "putchar",
+              RetValSpec<IntType>,
+              [ArgSpec<IntType>]
+          >,
+          FunctionSpec<
               "fputs",
               RetValSpec<IntType>,
               [ArgSpec<ConstCharRestrictedPtr>,
index e928847..68fdb9a 100644 (file)
@@ -185,6 +185,42 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  fputc
+  SRCS
+    fputc.cpp
+  HDRS
+    fputc.h
+  DEPENDS
+    libc.include.stdio
+    libc.src.__support.File.file
+    libc.src.__support.File.platform_file
+)
+
+add_entrypoint_object(
+  putc
+  SRCS
+    putc.cpp
+  HDRS
+    putc.h
+  DEPENDS
+    libc.include.stdio
+    libc.src.__support.File.file
+    libc.src.__support.File.platform_file
+)
+
+add_entrypoint_object(
+  putchar
+  SRCS
+    putchar.cpp
+  HDRS
+    putchar.h
+  DEPENDS
+    libc.include.stdio
+    libc.src.__support.File.file
+    libc.src.__support.File.platform_file
+)
+
+add_entrypoint_object(
   fputs
   SRCS
     fputs.cpp
diff --git a/libc/src/stdio/fputc.cpp b/libc/src/stdio/fputc.cpp
new file mode 100644 (file)
index 0000000..1120ab2
--- /dev/null
@@ -0,0 +1,26 @@
+//===-- Implementation of fputc -------------------------------------------===//
+//
+// 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/stdio/fputc.h"
+#include "src/__support/File/file.h"
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, fputc, (int c, ::FILE *stream)) {
+  unsigned char uc = static_cast<unsigned char>(c);
+  size_t written = reinterpret_cast<__llvm_libc::File *>(stream)->write(&uc, 1);
+  if (1 != written) {
+    // The stream should be in an error state in this case.
+    return EOF;
+  }
+  return 0;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/stdio/fputc.h b/libc/src/stdio/fputc.h
new file mode 100644 (file)
index 0000000..24732ec
--- /dev/null
@@ -0,0 +1,20 @@
+//===-- Implementation header of fputc --------------------------*- C++ -*-===//
+//
+// 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_STDIO_FPUTC_H
+#define LLVM_LIBC_SRC_STDIO_FPUTC_H
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+int fputc(int c, ::FILE *stream);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDIO_FPUTC_H
diff --git a/libc/src/stdio/putc.cpp b/libc/src/stdio/putc.cpp
new file mode 100644 (file)
index 0000000..f3f9221
--- /dev/null
@@ -0,0 +1,26 @@
+//===-- Implementation of putc --------------------------------------------===//
+//
+// 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/stdio/putc.h"
+#include "src/__support/File/file.h"
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, putc, (int c, ::FILE *stream)) {
+  unsigned char uc = static_cast<unsigned char>(c);
+  size_t written = reinterpret_cast<__llvm_libc::File *>(stream)->write(&uc, 1);
+  if (1 != written) {
+    // The stream should be in an error state in this case.
+    return EOF;
+  }
+  return 0;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/stdio/putc.h b/libc/src/stdio/putc.h
new file mode 100644 (file)
index 0000000..f21cad0
--- /dev/null
@@ -0,0 +1,20 @@
+//===-- Implementation header of putc ---------------------------*- C++ -*-===//
+//
+// 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_STDIO_PUTC_H
+#define LLVM_LIBC_SRC_STDIO_PUTC_H
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+int putc(int c, ::FILE *stream);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDIO_PUTC_H
diff --git a/libc/src/stdio/putchar.cpp b/libc/src/stdio/putchar.cpp
new file mode 100644 (file)
index 0000000..919a42b
--- /dev/null
@@ -0,0 +1,26 @@
+//===-- Implementation of putchar -----------------------------------------===//
+//
+// 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/stdio/putchar.h"
+#include "src/__support/File/file.h"
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, putchar, (int c)) {
+  unsigned char uc = static_cast<unsigned char>(c);
+  size_t written = __llvm_libc::stdout->write(&uc, 1);
+  if (1 != written) {
+    // The stream should be in an error state in this case.
+    return EOF;
+  }
+  return 0;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/stdio/putchar.h b/libc/src/stdio/putchar.h
new file mode 100644 (file)
index 0000000..e6df828
--- /dev/null
@@ -0,0 +1,20 @@
+//===-- Implementation header of putchar ------------------------*- C++ -*-===//
+//
+// 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_STDIO_PUTCHAR_H
+#define LLVM_LIBC_SRC_STDIO_PUTCHAR_H
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+int putchar(int c);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDIO_PUTCHAR_H
index 0922b52..1a2a2f2 100644 (file)
@@ -121,6 +121,20 @@ add_libc_unittest(
     libc.src.stdio.puts
 )
 
+add_libc_unittest(
+  putc_test
+  SUITE
+    libc_stdio_unittests
+  SRCS
+    putc_test.cpp
+  DEPENDS
+    libc.src.stdio.putc
+    libc.src.stdio.fclose
+    libc.src.stdio.ferror
+    libc.src.stdio.fopen
+    libc.src.stdio.fread
+)
+
 if(${LIBC_TARGET_OS} STREQUAL "linux")
   add_libc_unittest(
     remove_test
diff --git a/libc/test/src/stdio/putc_test.cpp b/libc/test/src/stdio/putc_test.cpp
new file mode 100644 (file)
index 0000000..7859588
--- /dev/null
@@ -0,0 +1,47 @@
+//===-- Unittests for putc ---------------------------------------------===//
+//
+// 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/stdio/fclose.h"
+#include "src/stdio/ferror.h"
+#include "src/stdio/fopen.h"
+#include "src/stdio/fread.h"
+
+#include "src/stdio/putc.h"
+
+#include "utils/UnitTest/Test.h"
+
+#include <errno.h>
+#include <stdio.h>
+
+TEST(LlvmLibcPutcTest, WriteToFile) {
+  constexpr char FILENAME[] = "testdata/putc_output.test";
+  ::FILE *file = __llvm_libc::fopen(FILENAME, "w");
+  ASSERT_FALSE(file == nullptr);
+
+  constexpr char simple[] = "simple letters";
+  for (size_t i = 0; i < sizeof(simple); ++i) {
+    ASSERT_EQ(__llvm_libc::putc(simple[i], file), 0);
+  }
+
+  ASSERT_EQ(0, __llvm_libc::fclose(file));
+
+  file = __llvm_libc::fopen(FILENAME, "r");
+  ASSERT_FALSE(file == nullptr);
+  char data[50];
+
+  ASSERT_EQ(__llvm_libc::fread(data, 1, sizeof(simple) - 1, file),
+            sizeof(simple) - 1);
+  data[sizeof(simple) - 1] = '\0';
+
+  ASSERT_STREQ(data, simple);
+
+  ASSERT_EQ(__llvm_libc::ferror(file), 0);
+  EXPECT_LT(__llvm_libc::putc('L', file), 0);
+
+  ASSERT_EQ(__llvm_libc::fclose(file), 0);
+}