[libc] Make more of the libc unit testing llvm independent

(WIP, hopefully I'll add more to this patch before submitting)

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D91665
This commit is contained in:
Michael Jones 2020-11-17 23:47:19 +00:00
parent 3e18fb3390
commit 8a4ee3550b
5 changed files with 30 additions and 20 deletions

View File

@ -2,7 +2,6 @@ add_llvm_library(
LibcUnitTest
Test.cpp
Test.h
LINK_COMPONENTS Support
)
target_include_directories(LibcUnitTest PUBLIC ${LIBC_SOURCE_DIR})
add_dependencies(LibcUnitTest libc.utils.CPP.standalone_cpp)

View File

@ -11,6 +11,4 @@ add_llvm_library(
ExecuteFunction.h
${FDReaderFile}
FDReader.h
LINK_COMPONENTS
Support
)

View File

@ -7,9 +7,10 @@
//===----------------------------------------------------------------------===//
#include "ExecuteFunction.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <poll.h>
#include <signal.h>
@ -41,8 +42,8 @@ ProcessStatus invokeInSubprocess(FunctionCaller *Func, unsigned timeoutMS) {
return ProcessStatus::Error("pipe(2) failed");
// Don't copy the buffers into the child process and print twice.
llvm::outs().flush();
llvm::errs().flush();
std::cout.flush();
std::cerr.flush();
pid_t Pid = ::fork();
if (Pid == -1)
return ProcessStatus::Error("fork(2) failed");

View File

@ -7,17 +7,19 @@
//===----------------------------------------------------------------------===//
#include "FDReader.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cassert>
#include <cstring>
#include <iostream>
#include <unistd.h>
namespace __llvm_libc {
namespace testutils {
FDReader::FDReader() {
if (::pipe(pipefd))
llvm::report_fatal_error("pipe(2) failed");
if (::pipe(pipefd)) {
std::cerr << "pipe(2) failed";
abort();
}
}
FDReader::~FDReader() {
@ -26,15 +28,25 @@ FDReader::~FDReader() {
}
bool FDReader::matchWritten(const char *str) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> bufOrErr =
llvm::MemoryBuffer::getOpenFile(pipefd[0], "<pipe>",
/* FileSize (irrelevant) */ 0);
if (!bufOrErr) {
assert(0 && "Error reading from pipe");
return false;
::close(pipefd[1]);
constexpr ssize_t ChunkSize = 4096 * 4;
char Buffer[ChunkSize];
std::string PipeStr;
std::string InputStr(str);
for (int BytesRead; (BytesRead = ::read(pipefd[0], Buffer, ChunkSize));) {
if (BytesRead > 0) {
PipeStr.insert(PipeStr.size(), Buffer, BytesRead);
} else {
assert(0 && "Error reading from pipe");
return false;
}
}
const llvm::MemoryBuffer &buf = **bufOrErr;
return !std::strncmp(buf.getBufferStart(), str, buf.getBufferSize());
return PipeStr == InputStr;
}
} // namespace testutils

View File

@ -7,19 +7,19 @@
//===----------------------------------------------------------------------===//
#include "StreamWrapper.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <iostream>
#include <memory>
#include <string>
namespace __llvm_libc {
namespace testutils {
StreamWrapper outs() { return {std::addressof(llvm::outs())}; }
StreamWrapper outs() { return {std::addressof(std::cout)}; }
template <typename T> StreamWrapper &StreamWrapper::operator<<(T t) {
assert(OS);
llvm::raw_ostream &Stream = *reinterpret_cast<llvm::raw_ostream *>(OS);
std::ostream &Stream = *reinterpret_cast<std::ostream *>(OS);
Stream << t;
return *this;
}