[libc-commits] [libc] 1f0c237 - [libc][NFC] Remove the now unused WrapperGen tool and tests.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Tue Jan 10 14:20:47 PST 2023


Author: Siva Chandra Reddy
Date: 2023-01-10T22:20:06Z
New Revision: 1f0c237a249a3304f4b80e6e611611d32aef7369

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

LOG: [libc][NFC] Remove the now unused WrapperGen tool and tests.

Added: 
    

Modified: 
    libc/test/utils/CMakeLists.txt
    libc/utils/CMakeLists.txt

Removed: 
    libc/test/utils/tools/CMakeLists.txt
    libc/test/utils/tools/WrapperGen/CMakeLists.txt
    libc/test/utils/tools/WrapperGen/testapi.td
    libc/test/utils/tools/WrapperGen/wrappergen_test.cpp
    libc/utils/tools/CMakeLists.txt
    libc/utils/tools/WrapperGen/CMakeLists.txt
    libc/utils/tools/WrapperGen/Main.cpp


################################################################################
diff  --git a/libc/test/utils/CMakeLists.txt b/libc/test/utils/CMakeLists.txt
index d98c401f18a83..235eb606fbc18 100644
--- a/libc/test/utils/CMakeLists.txt
+++ b/libc/test/utils/CMakeLists.txt
@@ -1,8 +1,2 @@
 add_subdirectory(FPUtil)
 add_subdirectory(UnitTest)
-
-if(NOT LLVM_LIBC_FULL_BUILD)
-  return()
-endif()
-
-add_subdirectory(tools)

diff  --git a/libc/test/utils/tools/CMakeLists.txt b/libc/test/utils/tools/CMakeLists.txt
deleted file mode 100644
index 5819f3933912d..0000000000000
--- a/libc/test/utils/tools/CMakeLists.txt
+++ /dev/null
@@ -1,42 +0,0 @@
-add_libc_testsuite(libc-tool-unittests)
-
-function(add_libc_tool_unittest target_name)
-
-  cmake_parse_arguments(
-    "LIBC_TOOL_UNITTEST"
-    "" # No optional arguments
-    "" # Single value arguments
-    "SRCS;DEPENDS;ARGS" # Multi-value arguments
-    ${ARGN}
-  )
-
-  add_executable(${target_name}
-    EXCLUDE_FROM_ALL
-    ${LIBC_TOOL_UNITTEST_SRCS}
-  )
-  target_link_libraries(${target_name}
-    PRIVATE
-    llvm_gtest_main
-    llvm_gtest
-    ${LIBC_TOOL_UNITTEST_DEPENDS}
-  )
-
-  add_custom_command(
-    TARGET ${target_name}
-    POST_BUILD
-    COMMAND $<TARGET_FILE:${target_name}>
-            ${LIBC_TOOL_UNITTEST_ARGS}
-  )
-  add_dependencies(libc-tool-unittests ${target_name})
-
-  target_compile_options(${target_name} PUBLIC -fno-rtti)
-  target_link_libraries(${target_name} PRIVATE LLVMSupport)
-endfunction()
-
-# Build the gtest library needed for unittests if we do not have it already.
-if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest AND NOT TARGET llvm_gtest)
-  include_directories(${LLVM_LIBC_INCLUDE_DIRS})
-  add_subdirectory(${LLVM_THIRD_PARTY_DIR}/unittest third-party/unittest)
-endif()
-
-add_subdirectory(WrapperGen)

diff  --git a/libc/test/utils/tools/WrapperGen/CMakeLists.txt b/libc/test/utils/tools/WrapperGen/CMakeLists.txt
deleted file mode 100644
index 80a833999a5ad..0000000000000
--- a/libc/test/utils/tools/WrapperGen/CMakeLists.txt
+++ /dev/null
@@ -1,15 +0,0 @@
-if(LIBC_TARGET_OS_IS_BAREMETAL)
-  return()
-endif()
-
-add_libc_tool_unittest(
-  wrappergen_test
-  SRCS 
-    wrappergen_test.cpp   
-  ARGS
-    --path=${LIBC_SOURCE_DIR}
-    --tool=${LLVM_BINARY_DIR}/bin/libc-wrappergen
-    --api=${LIBC_SOURCE_DIR}/test/utils/tools/WrapperGen/testapi.td
-)
-
-add_dependencies(wrappergen_test libc-wrappergen)

diff  --git a/libc/test/utils/tools/WrapperGen/testapi.td b/libc/test/utils/tools/WrapperGen/testapi.td
deleted file mode 100644
index 6349ec08bfb4f..0000000000000
--- a/libc/test/utils/tools/WrapperGen/testapi.td
+++ /dev/null
@@ -1,2 +0,0 @@
-include "spec/spec.td"
-include "spec/stdc.td"

diff  --git a/libc/test/utils/tools/WrapperGen/wrappergen_test.cpp b/libc/test/utils/tools/WrapperGen/wrappergen_test.cpp
deleted file mode 100644
index 1408fc6ab0a7e..0000000000000
--- a/libc/test/utils/tools/WrapperGen/wrappergen_test.cpp
+++ /dev/null
@@ -1,350 +0,0 @@
-//===-- Unittests for WrapperGen ------------------------------------------===//
-//
-// 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 "llvm/ADT/SmallString.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/Twine.h"
-#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/Error.h"
-#include "llvm/Support/FileSystem.h"
-#include "llvm/Support/FileUtilities.h"
-#include "llvm/Support/MemoryBuffer.h"
-#include "llvm/Support/Program.h"
-#include "llvm/Support/raw_ostream.h"
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include <unistd.h>
-#include <optional>
-
-llvm::cl::opt<std::string>
-    LibcPath("path", llvm::cl::desc("Path to the top level libc directory."),
-             llvm::cl::value_desc("<path to libc>"), llvm::cl::Required);
-llvm::cl::opt<std::string>
-    ToolPath("tool", llvm::cl::desc("Path to the tool executable."),
-             llvm::cl::value_desc("<path to tool>"), llvm::cl::Required);
-llvm::cl::opt<std::string>
-    APIPath("api",
-            llvm::cl::desc("Path to the api tablegen file used by the tests."),
-            llvm::cl::value_desc("<path to testapi.td>"), llvm::cl::Required);
-
-class WrapperGenTest : public ::testing::Test {
-public:
-  std::string IncludeArg;
-  std::string APIArg;
-  llvm::StringRef ProgPath;
-  llvm::Expected<llvm::sys::fs::TempFile> STDOutFile =
-      llvm::sys::fs::TempFile::create("wrappergen-stdout-%%-%%-%%-%%.txt");
-  llvm::Expected<llvm::sys::fs::TempFile> STDErrFile =
-      llvm::sys::fs::TempFile::create("wrappergen-stderr-%%-%%-%%-%%.txt");
-
-protected:
-  void SetUp() override {
-    IncludeArg = "-I=";
-    IncludeArg.append(LibcPath);
-    APIArg = APIPath;
-    ProgPath = llvm::StringRef(ToolPath);
-
-    if (!STDOutFile) {
-      llvm::errs() << "Error: " << llvm::toString(STDOutFile.takeError())
-                   << "\n";
-      llvm::report_fatal_error(
-          "Temporary file failed to initialize for libc-wrappergen tests.");
-    }
-    if (!STDErrFile) {
-      llvm::errs() << "Error: " << llvm::toString(STDErrFile.takeError())
-                   << "\n";
-      llvm::report_fatal_error(
-          "Temporary file failed to initialize for libc-wrappergen tests.");
-    }
-  }
-  void TearDown() override {
-    llvm::consumeError(STDOutFile.get().discard());
-    llvm::consumeError(STDErrFile.get().discard());
-  }
-};
-
-using LlvmLibcWrapperGenTest = WrapperGenTest;
-
-TEST_F(LlvmLibcWrapperGenTest, RunWrapperGenAndGetNoErrors) {
-  std::optional<llvm::StringRef> Redirects[] = {
-      std::nullopt, llvm::StringRef(STDOutFile.get().TmpName),
-      llvm::StringRef(STDErrFile.get().TmpName)};
-
-  llvm::StringRef ArgV[] = {ProgPath,
-                            llvm::StringRef(IncludeArg),
-                            llvm::StringRef(APIArg),
-                            "--gen-wrapper",
-                            "--name",
-                            "strlen"};
-
-  int ExitCode =
-      llvm::sys::ExecuteAndWait(ProgPath, ArgV, std::nullopt, Redirects);
-
-  EXPECT_EQ(ExitCode, 0);
-
-  auto STDErrOrError = llvm::MemoryBuffer::getFile(STDErrFile.get().TmpName);
-  std::string STDErrOutput = STDErrOrError.get()->getBuffer().str();
-  ASSERT_EQ(STDErrOutput, "");
-}
-
-TEST_F(LlvmLibcWrapperGenTest, RunWrapperGenOnStrlen) {
-  std::optional<llvm::StringRef> Redirects[] = {
-      std::nullopt, llvm::StringRef(STDOutFile.get().TmpName),
-      llvm::StringRef(STDErrFile.get().TmpName)};
-
-  llvm::StringRef ArgV[] = {ProgPath,
-                            llvm::StringRef(IncludeArg),
-                            llvm::StringRef(APIArg),
-                            "--gen-wrapper",
-                            "--name",
-                            "strlen"};
-
-  int ExitCode =
-      llvm::sys::ExecuteAndWait(ProgPath, ArgV, std::nullopt, Redirects);
-
-  EXPECT_EQ(ExitCode, 0);
-
-  auto STDErrOrError = llvm::MemoryBuffer::getFile(STDErrFile.get().TmpName);
-  std::string STDErrOutput = STDErrOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDErrOutput, "");
-
-  auto STDOutOrError = llvm::MemoryBuffer::getFile(STDOutFile.get().TmpName);
-  std::string STDOutOutput = STDOutOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDOutOutput, "#include \"src/string/strlen.h\"\n"
-                          "extern \"C\" size_t strlen(const char * __arg0) {\n"
-                          "  return __llvm_libc::strlen(__arg0);\n"
-                          "}\n");
-  // TODO:(michaelrj) Figure out how to make this output comparison
-  // less brittle. Currently it's just comparing the output of the program
-  // to an exact string, this means that even a small formatting change
-  // would break this test.
-}
-
-TEST_F(LlvmLibcWrapperGenTest, GenAliasForStrlen) {
-  std::optional<llvm::StringRef> Redirects[] = {
-      std::nullopt, llvm::StringRef(STDOutFile.get().TmpName),
-      llvm::StringRef(STDErrFile.get().TmpName)};
-
-  llvm::StringRef ArgV[] = {ProgPath,
-                            llvm::StringRef(IncludeArg),
-                            llvm::StringRef(APIArg),
-                            "--gen-alias",
-                            "--mangled-name",
-                            "__llvm_libc_strlen_mangled_name",
-                            "--name",
-                            "strlen"};
-
-  int ExitCode =
-      llvm::sys::ExecuteAndWait(ProgPath, ArgV, std::nullopt, Redirects);
-
-  EXPECT_EQ(ExitCode, 0);
-
-  auto STDErrOrError = llvm::MemoryBuffer::getFile(STDErrFile.get().TmpName);
-  std::string STDErrOutput = STDErrOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDErrOutput, "");
-
-  auto STDOutOrError = llvm::MemoryBuffer::getFile(STDOutFile.get().TmpName);
-  std::string STDOutOutput = STDOutOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDOutOutput,
-            "extern \"C\" size_t strlen(const char * __arg0) "
-            "__attribute__((alias(\"__llvm_libc_strlen_mangled_name\")));\n");
-  // TODO:(michaelrj) Figure out how to make this output comparison
-  // less brittle. Currently it's just comparing the output of the program
-  // to an exact string, this means that even a small formatting change
-  // would break this test.
-}
-
-TEST_F(LlvmLibcWrapperGenTest, DeclStrlenAliasUsingMangledNameFile) {
-  std::optional<llvm::StringRef> Redirects[] = {
-      std::nullopt, llvm::StringRef(STDOutFile.get().TmpName),
-      llvm::StringRef(STDErrFile.get().TmpName)};
-
-  const char *MangledNameFileContent =
-      "abc\nxyz__llvm_libc_strlen_mangled_name\nijk\n";
-  llvm::SmallVector<char> MangledNameFilePath;
-  auto MangledNameFileCreateError = llvm::sys::fs::createUniqueFile(
-      "libc-wrappergen-test-aliasee-file-%%-%%-%%-%%.txt", MangledNameFilePath);
-  ASSERT_FALSE(MangledNameFileCreateError);
-  auto MangledNameFileWriteError = llvm::writeFileAtomically(
-      "libc-wrappergen-temp-test-aliasee-file-%%-%%-%%-%%.txt",
-      llvm::StringRef(MangledNameFilePath.data()),
-      llvm::StringRef(MangledNameFileContent));
-  ASSERT_FALSE(MangledNameFileWriteError);
-
-  llvm::StringRef ArgV[] = {ProgPath,
-                            llvm::StringRef(IncludeArg),
-                            llvm::StringRef(APIArg),
-                            "--gen-alias",
-                            "--mangled-name-file",
-                            llvm::StringRef(MangledNameFilePath.data()),
-                            "--name",
-                            "strlen"};
-
-  int ExitCode =
-      llvm::sys::ExecuteAndWait(ProgPath, ArgV, std::nullopt, Redirects);
-
-  EXPECT_EQ(ExitCode, 0);
-
-  auto STDErrOrError = llvm::MemoryBuffer::getFile(STDErrFile.get().TmpName);
-  std::string STDErrOutput = STDErrOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDErrOutput, "");
-
-  auto STDOutOrError = llvm::MemoryBuffer::getFile(STDOutFile.get().TmpName);
-  std::string STDOutOutput = STDOutOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(
-      STDOutOutput,
-      "extern \"C\" size_t strlen(const char * __arg0) "
-      "__attribute__((alias(\"xyz__llvm_libc_strlen_mangled_name\")));\n");
-}
-
-/////////////////////////////////////////////////////////////////////
-/////////////////////////////////////////////////////////////////////
-// BAD INPUT TESTS
-// all of the tests after this point are testing inputs that should
-// return errors
-/////////////////////////////////////////////////////////////////////
-
-TEST_F(LlvmLibcWrapperGenTest,
-       RunWrapperGenOnStrlenWithMangledNameAndMangledNameFile) {
-  std::optional<llvm::StringRef> Redirects[] = {
-      std::nullopt, llvm::StringRef(STDOutFile.get().TmpName),
-      llvm::StringRef(STDErrFile.get().TmpName)};
-
-  llvm::StringRef ArgV[] = {ProgPath,
-                            llvm::StringRef(IncludeArg),
-                            llvm::StringRef(APIArg),
-                            "--gen-alias",
-                            "--mangled-name",
-                            "__llvm_libc_strlen_mangled_name",
-                            "--mangled-name-file",
-                            "non-existant-mangled-name-file.txt",
-                            "--name",
-                            "strlen"};
-
-  int ExitCode =
-      llvm::sys::ExecuteAndWait(ProgPath, ArgV, std::nullopt, Redirects);
-
-  EXPECT_EQ(ExitCode, 1);
-
-  auto STDErrOrError = llvm::MemoryBuffer::getFile(STDErrFile.get().TmpName);
-  std::string STDErrOutput = STDErrOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDErrOutput,
-            "error: The options 'mangled-name' and 'mangled-name-file' "
-            "cannot be specified simultaneously.\n");
-
-  auto STDOutOrError = llvm::MemoryBuffer::getFile(STDOutFile.get().TmpName);
-  std::string STDOutOutput = STDOutOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDOutOutput, "");
-}
-
-TEST_F(LlvmLibcWrapperGenTest, RunWrapperGenOnBadFuncName) {
-  std::optional<llvm::StringRef> Redirects[] = {
-      std::nullopt, llvm::StringRef(STDOutFile.get().TmpName),
-      llvm::StringRef(STDErrFile.get().TmpName)};
-
-  llvm::StringRef BadFuncName = "FAKE_TEST_FUNC";
-
-  llvm::StringRef ArgV[] = {ProgPath,
-                            llvm::StringRef(IncludeArg),
-                            llvm::StringRef(APIArg),
-                            "--gen-wrapper",
-                            "--name",
-                            BadFuncName};
-
-  int ExitCode =
-      llvm::sys::ExecuteAndWait(ProgPath, ArgV, std::nullopt, Redirects);
-
-  EXPECT_EQ(ExitCode, 1);
-
-  auto STDErrOrError = llvm::MemoryBuffer::getFile(STDErrFile.get().TmpName);
-  std::string STDErrOutput = STDErrOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDErrOutput, ("error: Function '" + BadFuncName +
-                           "' not found in any standard spec.\n")
-                              .str());
-
-  auto STDOutOrError = llvm::MemoryBuffer::getFile(STDOutFile.get().TmpName);
-  std::string STDOutOutput = STDOutOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDOutOutput, "");
-}
-
-TEST_F(LlvmLibcWrapperGenTest, RunWrapperGenOnStrlenWithBadMangledNameFile) {
-  std::optional<llvm::StringRef> Redirects[] = {
-      std::nullopt, llvm::StringRef(STDOutFile.get().TmpName),
-      llvm::StringRef(STDErrFile.get().TmpName)};
-
-  llvm::StringRef BadMangledNameFileName = "FILE_THAT_DOESNT_EXIST.txt";
-
-  llvm::StringRef ArgV[] = {ProgPath,
-                            llvm::StringRef(IncludeArg),
-                            llvm::StringRef(APIArg),
-                            "--gen-alias",
-                            "--mangled-name-file",
-                            BadMangledNameFileName,
-                            "--name",
-                            "strlen"};
-
-  int ExitCode =
-      llvm::sys::ExecuteAndWait(ProgPath, ArgV, std::nullopt, Redirects);
-
-  EXPECT_EQ(ExitCode, 1);
-
-  auto STDErrOrError = llvm::MemoryBuffer::getFile(STDErrFile.get().TmpName);
-  std::string STDErrOutput = STDErrOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDErrOutput, ("error: Unable to read the mangled name file " +
-                           BadMangledNameFileName + "\n")
-                              .str());
-
-  auto STDOutOrError = llvm::MemoryBuffer::getFile(STDOutFile.get().TmpName);
-  std::string STDOutOutput = STDOutOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDOutOutput, "");
-}
-
-TEST_F(LlvmLibcWrapperGenTest, RunWithMangledNameFileMissingLLVMLibcName) {
-  std::optional<llvm::StringRef> Redirects[] = {
-      std::nullopt, llvm::StringRef(STDOutFile.get().TmpName),
-      llvm::StringRef(STDErrFile.get().TmpName)};
-
-  llvm::SmallVector<char> MangledNameFilePath;
-  auto MangledNameFileCreateError = llvm::sys::fs::createUniqueFile(
-      "libc-wrappergen-test-mangled-name-file-%%-%%-%%-%%.txt",
-      MangledNameFilePath);
-  ASSERT_FALSE(MangledNameFileCreateError);
-
-  llvm::StringRef ArgV[] = {ProgPath,
-                            llvm::StringRef(IncludeArg),
-                            llvm::StringRef(APIArg),
-                            "--gen-alias",
-                            "--mangled-name-file",
-                            llvm::StringRef(MangledNameFilePath.data()),
-                            "--name",
-                            "strlen"};
-
-  int ExitCode =
-      llvm::sys::ExecuteAndWait(ProgPath, ArgV, std::nullopt, Redirects);
-
-  EXPECT_NE(ExitCode, 0);
-
-  auto STDErrOrError = llvm::MemoryBuffer::getFile(STDErrFile.get().TmpName);
-  std::string STDErrOutput = STDErrOrError.get()->getBuffer().str();
-
-  ASSERT_EQ(STDErrOutput, ("error: Did not find an LLVM libc mangled name in " +
-                           MangledNameFilePath + "\n")
-                              .str());
-}

diff  --git a/libc/utils/CMakeLists.txt b/libc/utils/CMakeLists.txt
index 1c312c3a1b805..da8abd77783cd 100644
--- a/libc/utils/CMakeLists.txt
+++ b/libc/utils/CMakeLists.txt
@@ -5,5 +5,4 @@ add_subdirectory(UnitTest)
 if(LLVM_LIBC_FULL_BUILD AND NOT
   (LIBC_TARGET_ARCHITECTURE_IS_GPU OR LIBC_TARGET_OS_IS_BAREMETAL))
   add_subdirectory(IntegrationTest)
-  add_subdirectory(tools)
 endif()

diff  --git a/libc/utils/tools/CMakeLists.txt b/libc/utils/tools/CMakeLists.txt
deleted file mode 100644
index e975b7f293af9..0000000000000
--- a/libc/utils/tools/CMakeLists.txt
+++ /dev/null
@@ -1 +0,0 @@
-add_subdirectory(WrapperGen)

diff  --git a/libc/utils/tools/WrapperGen/CMakeLists.txt b/libc/utils/tools/WrapperGen/CMakeLists.txt
deleted file mode 100644
index 5fd78591e9b3c..0000000000000
--- a/libc/utils/tools/WrapperGen/CMakeLists.txt
+++ /dev/null
@@ -1,8 +0,0 @@
-set(LLVM_LINK_COMPONENTS Support)
-
-add_tablegen(libc-wrappergen LLVM_LIBC
-  Main.cpp
-)
-
-target_include_directories(libc-wrappergen PRIVATE ${LIBC_SOURCE_DIR})
-target_link_libraries(libc-wrappergen PRIVATE LibcTableGenUtil)

diff  --git a/libc/utils/tools/WrapperGen/Main.cpp b/libc/utils/tools/WrapperGen/Main.cpp
deleted file mode 100644
index 5711827b457c8..0000000000000
--- a/libc/utils/tools/WrapperGen/Main.cpp
+++ /dev/null
@@ -1,198 +0,0 @@
-//===-- "main" function of libc-wrappergen --------------------------------===//
-//
-// 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 "utils/LibcTableGenUtil/APIIndexer.h"
-
-#include "llvm/ADT/StringRef.h"
-#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/Error.h"
-#include "llvm/Support/JSON.h"
-#include "llvm/Support/MemoryBuffer.h"
-#include "llvm/TableGen/Error.h"
-#include "llvm/TableGen/Main.h"
-
-#include <fstream>
-#include <map>
-#include <sstream>
-#include <string>
-
-llvm::cl::opt<bool>
-    GenWrapper("gen-wrapper",
-               llvm::cl::desc("Generate a C wrapper for <name>."));
-llvm::cl::opt<bool> GenAlias("gen-alias",
-                             llvm::cl::desc("Generate a C alias for <name>."));
-
-llvm::cl::opt<std::string>
-    FunctionName("name", llvm::cl::desc("Name of the function to be wrapped."),
-                 llvm::cl::value_desc("<function name>"), llvm::cl::Required);
-llvm::cl::opt<std::string> MangledNameString(
-    "mangled-name", llvm::cl::desc("Declare as an alias to this mangled name."),
-    llvm::cl::value_desc("<aliasee string>"));
-llvm::cl::opt<std::string> MangledNameFile(
-    "mangled-name-file",
-    llvm::cl::desc("Declare as an alias to the C name read from "
-                   "this file."),
-    llvm::cl::value_desc("<path to a file containing alias name>"));
-llvm::cl::opt<std::string>
-    AppendToFile("append-to-file",
-                 llvm::cl::desc("Append the generated content at the end of "
-                                "the contents of this file."),
-                 llvm::cl::value_desc("<path to a file>"));
-
-void validateOpts() {
-  int ActionCount = 0;
-  if (GenWrapper)
-    ++ActionCount;
-  if (GenAlias)
-    ++ActionCount;
-  if (ActionCount != 1) {
-    llvm::PrintFatalError("Exactly one of {--gen-wrapper, --gen-alias} "
-                          "should be specified");
-  }
-  if (!MangledNameString.empty() && !MangledNameFile.empty()) {
-    llvm::PrintFatalError("The options 'mangled-name' and 'mangled-name-file' "
-                          "cannot be specified simultaneously.");
-  }
-}
-
-static std::string getMangledName() {
-  if (!MangledNameString.empty())
-    return MangledNameString;
-
-  if (MangledNameFile.empty())
-    llvm::PrintFatalError("At least one of --mangled-name or "
-                          "--mangled-name-file should be specified.");
-
-  auto ErrorOrBuf = llvm::MemoryBuffer::getFile(MangledNameFile);
-  if (!ErrorOrBuf)
-    llvm::PrintFatalError("Unable to read the mangled name file " +
-                          MangledNameFile);
-  llvm::StringRef FileContent = ErrorOrBuf.get()->getBuffer().trim();
-  llvm::SmallVector<llvm::StringRef> Lines;
-  FileContent.split(Lines, '\n');
-  for (llvm::StringRef L : Lines) {
-    if (L.contains("__llvm_libc"))
-      return std::string(L);
-  }
-  llvm::PrintFatalError("Did not find an LLVM libc mangled name in " +
-                        MangledNameFile);
-  return std::string();
-}
-
-void writeAppendToFile(llvm::raw_ostream &OS) {
-  auto ErrorOrBuf = llvm::MemoryBuffer::getFile(AppendToFile);
-  if (!ErrorOrBuf) {
-    llvm::PrintFatalError("Unable to read the file '" + AppendToFile +
-                          "' to append to.");
-  }
-  OS << ErrorOrBuf.get()->getBuffer().trim() << '\n';
-}
-
-llvm::Record *getFunctionSpec(const llvm_libc::APIIndexer &Indexer) {
-  auto Iter = Indexer.FunctionSpecMap.find(FunctionName);
-  if (Iter == Indexer.FunctionSpecMap.end()) {
-    llvm::PrintFatalError("Function '" + FunctionName +
-                          "' not found in any standard spec.");
-  }
-  auto &NameSpecPair = *Iter;
-  return NameSpecPair.second;
-}
-
-std::pair<std::string, bool> writeFunctionHeader(llvm_libc::APIIndexer &Indexer,
-                                                 llvm::Record *FunctionSpec,
-                                                 llvm::raw_ostream &OS) {
-  llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
-  llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");
-  std::string ReturnTypeString = Indexer.getTypeAsString(ReturnType);
-  bool ShouldReturn = true;
-  // We are generating C wrappers in C++ code. So, we should convert the C
-  // _Noreturn to the C++ [[noreturn]].
-  llvm::StringRef NR("_Noreturn "); // Note the space after _Noreturn
-  llvm::StringRef RT(ReturnTypeString);
-  if (RT.startswith(NR)) {
-    RT = RT.drop_front(NR.size() - 1); // - 1 because of the space.
-    ReturnTypeString = std::string("[[noreturn]]") + std::string(RT);
-    ShouldReturn = false;
-  }
-  OS << "extern \"C\" " << ReturnTypeString << " " << FunctionName << "(";
-
-  auto ArgsList = FunctionSpec->getValueAsListOfDefs("Args");
-  std::stringstream CallArgs;
-  std::string ArgPrefix("__arg");
-  for (size_t i = 0; i < ArgsList.size(); ++i) {
-    llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType");
-    auto TypeName = Indexer.getTypeAsString(ArgType);
-
-    if (TypeName.compare("void") == 0) {
-      if (ArgsList.size() == 1) {
-        break;
-      } else {
-        // the reason this is a fatal error is that a void argument means this
-        // function has no arguments; multiple copies of no arguments is an
-        // error.
-        llvm::PrintFatalError(
-            "The specification for function " + FunctionName +
-            " lists other arguments along with a void argument.");
-      }
-    }
-
-    OS << TypeName << " " << ArgPrefix << i;
-    CallArgs << ArgPrefix << i;
-    if (i < ArgsList.size() - 1) {
-      OS << ", ";
-      CallArgs << ", ";
-    }
-  }
-  return make_pair(CallArgs.str(), ShouldReturn);
-}
-
-static bool generateWrapper(llvm::raw_ostream &OS,
-                            llvm::RecordKeeper &Records) {
-  llvm_libc::APIIndexer Indexer(Records);
-  llvm::Record *FunctionSpec = getFunctionSpec(Indexer);
-  if (AppendToFile.empty()) {
-    std::string Header = Indexer.FunctionToHeaderMap[FunctionName];
-    auto RelPath =
-        llvm::StringRef(Header).drop_back(2); // Drop the ".h" suffix.
-    OS << "#include \"src/" << RelPath << "/" << FunctionName << ".h\"\n";
-  } else {
-    writeAppendToFile(OS);
-  }
-  auto Pair = writeFunctionHeader(Indexer, FunctionSpec, OS);
-  OS << ") {\n"
-     << "  " << (Pair.second ? "return " : "")
-     << "__llvm_libc::" << FunctionName << "(" << Pair.first << ");\n"
-     << "}\n";
-  return false;
-}
-
-static bool generateAlias(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
-  if (!AppendToFile.empty())
-    writeAppendToFile(OS);
-  llvm_libc::APIIndexer Indexer(Records);
-  llvm::Record *FunctionSpec = getFunctionSpec(Indexer);
-  auto Pair = writeFunctionHeader(Indexer, FunctionSpec, OS);
-  OS << ") __attribute__((alias(\"" << getMangledName() << "\")));\n";
-  return false;
-}
-
-static bool wrapperGenMain(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
-  validateOpts();
-
-  if (GenWrapper)
-    return generateWrapper(OS, Records);
-  if (GenAlias)
-    return generateAlias(OS, Records);
-
-  __builtin_unreachable();
-}
-
-int main(int argc, char *argv[]) {
-  llvm::cl::ParseCommandLineOptions(argc, argv);
-  return TableGenMain(argv[0], wrapperGenMain);
-}


        


More information about the libc-commits mailing list