[libc-commits] [libc] e1c54d4 - [libc] move printf_main in to object library

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Jun 9 14:35:23 PDT 2022


Author: Michael Jones
Date: 2022-06-09T14:35:18-07:00
New Revision: e1c54d4ddc5e1633cdeebb36b24d9a81aec77c33

URL: https://github.com/llvm/llvm-project/commit/e1c54d4ddc5e1633cdeebb36b24d9a81aec77c33
DIFF: https://github.com/llvm/llvm-project/commit/e1c54d4ddc5e1633cdeebb36b24d9a81aec77c33.diff

LOG: [libc] move printf_main in to object library

Previously printf_main was a header library, but header library
dependencies don't work properly so it's been moved to an object
library. Additionally, the writers have been marked inline.

Reviewed By: sivachandra, lntue

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

Added: 
    libc/src/stdio/printf_core/file_writer.cpp
    libc/src/stdio/printf_core/printf_main.cpp
    libc/src/stdio/printf_core/string_writer.cpp

Modified: 
    libc/src/stdio/printf_core/CMakeLists.txt
    libc/src/stdio/printf_core/file_writer.h
    libc/src/stdio/printf_core/printf_main.h
    libc/src/stdio/printf_core/string_writer.h

Removed: 
    


################################################################################
diff  --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt
index 8e2f162982362..2b814f072f1d9 100644
--- a/libc/src/stdio/printf_core/CMakeLists.txt
+++ b/libc/src/stdio/printf_core/CMakeLists.txt
@@ -20,16 +20,20 @@ add_object_library(
     libc.src.string.memory_utils.memset_implementation
 )
 
-add_header_library(
+add_object_library(
   string_writer
+  SRCS
+    string_writer.cpp
   HDRS
     string_writer.h
   DEPENDS
     libc.src.string.memory_utils.memcpy_implementation
 )
 
-add_header_library(
+add_object_library(
   file_writer
+  SRCS
+    file_writer.cpp
   HDRS
     file_writer.h
   DEPENDS
@@ -62,8 +66,10 @@ add_object_library(
 )
 
 
-add_header_library(
+add_object_library(
   printf_main
+  SRCS
+    printf_main.cpp
   HDRS
     printf_main.h
   DEPENDS

diff  --git a/libc/src/stdio/printf_core/file_writer.cpp b/libc/src/stdio/printf_core/file_writer.cpp
new file mode 100644
index 0000000000000..465e4cba57fbb
--- /dev/null
+++ b/libc/src/stdio/printf_core/file_writer.cpp
@@ -0,0 +1,23 @@
+//===-- FILE Writer implementation for printf -------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/printf_core/file_writer.h"
+#include "src/__support/File/file.h"
+#include <stddef.h>
+
+namespace __llvm_libc {
+namespace printf_core {
+
+void write_to_file(void *raw_pointer, const char *__restrict to_write,
+                   size_t len) {
+  __llvm_libc::File *file = reinterpret_cast<__llvm_libc::File *>(raw_pointer);
+  file->write(to_write, len);
+}
+
+} // namespace printf_core
+} // namespace __llvm_libc

diff  --git a/libc/src/stdio/printf_core/file_writer.h b/libc/src/stdio/printf_core/file_writer.h
index 10fea82d83098..755ed32a1b422 100644
--- a/libc/src/stdio/printf_core/file_writer.h
+++ b/libc/src/stdio/printf_core/file_writer.h
@@ -1,4 +1,4 @@
-//===-- FILE Writer class for printf ----------------------------*- C++ -*-===//
+//===-- FILE Writer definition for printf -----------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -18,10 +18,7 @@ namespace printf_core {
 // write_to_file treats raw_pointer as a File and calls its write
 // function.
 void write_to_file(void *raw_pointer, const char *__restrict to_write,
-                   size_t len) {
-  __llvm_libc::File *file = reinterpret_cast<__llvm_libc::File *>(raw_pointer);
-  file->write(to_write, len);
-}
+                   size_t len);
 
 } // namespace printf_core
 } // namespace __llvm_libc

diff  --git a/libc/src/stdio/printf_core/printf_main.cpp b/libc/src/stdio/printf_core/printf_main.cpp
new file mode 100644
index 0000000000000..7ec046f458be4
--- /dev/null
+++ b/libc/src/stdio/printf_core/printf_main.cpp
@@ -0,0 +1,38 @@
+//===-- Starting point for printf -------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/printf_core/printf_main.h"
+
+#include "src/__support/arg_list.h"
+#include "src/stdio/printf_core/converter.h"
+#include "src/stdio/printf_core/core_structs.h"
+#include "src/stdio/printf_core/parser.h"
+#include "src/stdio/printf_core/writer.h"
+
+#include <stddef.h>
+
+namespace __llvm_libc {
+namespace printf_core {
+
+int printf_main(Writer *writer, const char *__restrict str,
+                internal::ArgList &args) {
+  Parser parser(str, args);
+
+  for (FormatSection cur_section = parser.get_next_section();
+       cur_section.raw_len > 0; cur_section = parser.get_next_section()) {
+    if (cur_section.has_conv)
+      convert(writer, cur_section);
+    else
+      writer->write(cur_section.raw_string, cur_section.raw_len);
+  }
+
+  return writer->get_chars_written();
+}
+
+} // namespace printf_core
+} // namespace __llvm_libc

diff  --git a/libc/src/stdio/printf_core/printf_main.h b/libc/src/stdio/printf_core/printf_main.h
index 84ca69a5ce034..341bd4cef8543 100644
--- a/libc/src/stdio/printf_core/printf_main.h
+++ b/libc/src/stdio/printf_core/printf_main.h
@@ -10,9 +10,6 @@
 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H
 
 #include "src/__support/arg_list.h"
-#include "src/stdio/printf_core/converter.h"
-#include "src/stdio/printf_core/core_structs.h"
-#include "src/stdio/printf_core/parser.h"
 #include "src/stdio/printf_core/writer.h"
 
 #include <stddef.h>
@@ -21,19 +18,7 @@ namespace __llvm_libc {
 namespace printf_core {
 
 int printf_main(Writer *writer, const char *__restrict str,
-                internal::ArgList &args) {
-  Parser parser(str, args);
-
-  for (FormatSection cur_section = parser.get_next_section();
-       cur_section.raw_len > 0; cur_section = parser.get_next_section()) {
-    if (cur_section.has_conv)
-      convert(writer, cur_section);
-    else
-      writer->write(cur_section.raw_string, cur_section.raw_len);
-  }
-
-  return writer->get_chars_written();
-}
+                internal::ArgList &args);
 
 } // namespace printf_core
 } // namespace __llvm_libc

diff  --git a/libc/src/stdio/printf_core/string_writer.cpp b/libc/src/stdio/printf_core/string_writer.cpp
new file mode 100644
index 0000000000000..b53c2cc4a93eb
--- /dev/null
+++ b/libc/src/stdio/printf_core/string_writer.cpp
@@ -0,0 +1,34 @@
+//===-- String Writer implementation for printf -----------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/printf_core/string_writer.h"
+#include "src/string/memory_utils/memcpy_implementations.h"
+#include <stddef.h>
+
+namespace __llvm_libc {
+namespace printf_core {
+
+void StringWriter::write(const char *__restrict to_write, size_t len) {
+  if (len > available_capacity)
+    len = available_capacity;
+
+  if (len > 0) {
+    inline_memcpy(cur_buffer, to_write, len);
+    cur_buffer += len;
+    available_capacity -= len;
+  }
+}
+
+void write_to_string(void *raw_pointer, const char *__restrict to_write,
+                     size_t len) {
+  StringWriter *string_writer = reinterpret_cast<StringWriter *>(raw_pointer);
+  string_writer->write(to_write, len);
+}
+
+} // namespace printf_core
+} // namespace __llvm_libc

diff  --git a/libc/src/stdio/printf_core/string_writer.h b/libc/src/stdio/printf_core/string_writer.h
index d8d6dbf670b78..93b31371f3bae 100644
--- a/libc/src/stdio/printf_core/string_writer.h
+++ b/libc/src/stdio/printf_core/string_writer.h
@@ -1,4 +1,4 @@
-//===-- String Writer class for printf -----------------------*- C++ -*-===//
+//===-- String Writer definition for printf ---------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -26,16 +26,8 @@ class StringWriter {
   StringWriter(char *__restrict buffer, size_t max_len = ~size_t(0))
       : cur_buffer(buffer), available_capacity(max_len) {}
 
-  void write(const char *__restrict to_write, size_t len) {
-    if (len > available_capacity)
-      len = available_capacity;
+  void write(const char *__restrict to_write, size_t len);
 
-    if (len > 0) {
-      inline_memcpy(cur_buffer, to_write, len);
-      cur_buffer += len;
-      available_capacity -= len;
-    }
-  }
   // Terminate should only be called if the original max length passed to
   // snprintf was greater than 0. It writes a null byte to the end of the
   // cur_buffer, regardless of available_capacity.
@@ -45,10 +37,7 @@ class StringWriter {
 // write_to_string treats raw_pointer as a StringWriter and calls its write
 // function.
 void write_to_string(void *raw_pointer, const char *__restrict to_write,
-                     size_t len) {
-  StringWriter *string_writer = reinterpret_cast<StringWriter *>(raw_pointer);
-  string_writer->write(to_write, len);
-}
+                     size_t len);
 
 } // namespace printf_core
 } // namespace __llvm_libc


        


More information about the libc-commits mailing list