[libc-commits] [libc] 9ab6c1a - [libc] Let wrappergen pick LLVM libc mangled name from aliasee file.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Fri Dec 11 14:33:24 PST 2020


Author: Siva Chandra Reddy
Date: 2020-12-11T14:33:03-08:00
New Revision: 9ab6c1a99f82fa13017ddccc606eecfe24e92043

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

LOG: [libc] Let wrappergen pick LLVM libc mangled name from aliasee file.

Along the way, made a change to run tool unittests when the target
"check-libc" is run by introducing a libc testsuite for tool unittests.

Reviewed By: michaelrj

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

Added: 
    

Modified: 
    libc/test/utils/tools/CMakeLists.txt
    libc/test/utils/tools/WrapperGen/CMakeLists.txt
    libc/test/utils/tools/WrapperGen/wrappergen_test.cpp
    libc/utils/tools/WrapperGen/Main.cpp

Removed: 
    


################################################################################
diff  --git a/libc/test/utils/tools/CMakeLists.txt b/libc/test/utils/tools/CMakeLists.txt
index 9a06be546115..0e343f154eac 100644
--- a/libc/test/utils/tools/CMakeLists.txt
+++ b/libc/test/utils/tools/CMakeLists.txt
@@ -1,4 +1,4 @@
-add_custom_target(libc-tool-util-tests)
+add_libc_testsuite(libc-tool-unittests)
 
 function(add_libc_tool_unittest target_name)
 
@@ -27,7 +27,7 @@ function(add_libc_tool_unittest target_name)
     COMMAND $<TARGET_FILE:${target_name}> 
             ${LIBC_TOOL_UNITTEST_ARGS}
   )
-  add_dependencies(libc-tool-util-tests ${target_name})
+  add_dependencies(libc-tool-unittests ${target_name})
 
   target_compile_options(${target_name} PUBLIC -fno-rtti)
   target_link_libraries(${target_name} PRIVATE LLVMSupport)

diff  --git a/libc/test/utils/tools/WrapperGen/CMakeLists.txt b/libc/test/utils/tools/WrapperGen/CMakeLists.txt
index cfb3dab15ebf..8f41badd48c4 100644
--- a/libc/test/utils/tools/WrapperGen/CMakeLists.txt
+++ b/libc/test/utils/tools/WrapperGen/CMakeLists.txt
@@ -1,4 +1,5 @@
-add_libc_tool_unittest( wrappergen_test
+add_libc_tool_unittest(
+  wrappergen_test
   SRCS 
     wrappergen_test.cpp   
   ARGS

diff  --git a/libc/test/utils/tools/WrapperGen/wrappergen_test.cpp b/libc/test/utils/tools/WrapperGen/wrappergen_test.cpp
index a6c29b7640e8..4cb7a31de942 100644
--- a/libc/test/utils/tools/WrapperGen/wrappergen_test.cpp
+++ b/libc/test/utils/tools/WrapperGen/wrappergen_test.cpp
@@ -12,6 +12,7 @@
 #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"
@@ -149,6 +150,49 @@ TEST_F(WrapperGenTest, RunWrapperGenOnStrlenWithAliasee) {
   // would break this test.
 }
 
+TEST_F(WrapperGenTest, DeclStrlenAliasUsingAliaseeFile) {
+  llvm::Optional<llvm::StringRef> Redirects[] = {
+      llvm::None, llvm::StringRef(STDOutFile.get().TmpName),
+      llvm::StringRef(STDErrFile.get().TmpName)};
+
+  const char *AliaseeFileContent = "abc\nxyz__llvm_libcSTRLEN_ALIAS\nijk\n";
+  llvm::SmallVector<char> AliaseeFilePath;
+  auto AliaseeFileCreateError = llvm::sys::fs::createUniqueFile(
+      "libc-wrappergen-test-aliasee-file-%%-%%-%%-%%.txt", AliaseeFilePath);
+  ASSERT_FALSE(AliaseeFileCreateError);
+  auto AliaseeFileWriteError = llvm::writeFileAtomically(
+      "libc-wrappergen-temp-test-aliasee-file-%%-%%-%%-%%.txt",
+      llvm::StringRef(AliaseeFilePath.data()),
+      llvm::StringRef(AliaseeFileContent));
+  ASSERT_FALSE(AliaseeFileWriteError);
+
+  llvm::StringRef ArgV[] = {ProgPath,
+                            llvm::StringRef(IncludeArg),
+                            llvm::StringRef(APIArg),
+                            "--aliasee-file",
+                            llvm::StringRef(AliaseeFilePath.data()),
+                            "--name",
+                            "strlen"};
+
+  int ExitCode =
+      llvm::sys::ExecuteAndWait(ProgPath, ArgV, llvm::None, 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_libcSTRLEN_ALIAS\")));\n");
+}
+
+/////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////
 // BAD INPUT TESTS
 // all of the tests after this point are testing inputs that should
@@ -245,3 +289,34 @@ TEST_F(WrapperGenTest, RunWrapperGenOnStrlenWithBadAliaseeFile) {
 
   ASSERT_EQ(STDOutOutput, "");
 }
+
+TEST_F(WrapperGenTest, RunWithAliaseeFileMissingLLVMLibcName) {
+  llvm::Optional<llvm::StringRef> Redirects[] = {
+      llvm::None, llvm::StringRef(STDOutFile.get().TmpName),
+      llvm::StringRef(STDErrFile.get().TmpName)};
+
+  llvm::SmallVector<char> AliaseeFilePath;
+  auto AliaseeFileCreateError = llvm::sys::fs::createUniqueFile(
+      "libc-wrappergen-test-aliasee-file-%%-%%-%%-%%.txt", AliaseeFilePath);
+  ASSERT_FALSE(AliaseeFileCreateError);
+
+  llvm::StringRef ArgV[] = {ProgPath,
+                            llvm::StringRef(IncludeArg),
+                            llvm::StringRef(APIArg),
+                            "--aliasee-file",
+                            llvm::StringRef(AliaseeFilePath.data()),
+                            "--name",
+                            "strlen"};
+
+  int ExitCode =
+      llvm::sys::ExecuteAndWait(ProgPath, ArgV, llvm::None, 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 " +
+                           AliaseeFilePath + "\n")
+                              .str());
+}

diff  --git a/libc/utils/tools/WrapperGen/Main.cpp b/libc/utils/tools/WrapperGen/Main.cpp
index 3a1a37a3377c..7c43f611daac 100644
--- a/libc/utils/tools/WrapperGen/Main.cpp
+++ b/libc/utils/tools/WrapperGen/Main.cpp
@@ -42,7 +42,16 @@ static std::string GetAliaseeName() {
   auto ErrorOrBuf = llvm::MemoryBuffer::getFile(AliaseeFile);
   if (!ErrorOrBuf)
     llvm::PrintFatalError("Unable to read the aliasee file " + AliaseeFile);
-  return std::string(ErrorOrBuf.get()->getBuffer().trim());
+  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 " +
+                        AliaseeFile);
+  return std::string();
 }
 
 static bool WrapperGenMain(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {


        


More information about the libc-commits mailing list