1 //===-- FDReader.cpp ------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "FDReader.h" 10 #include "llvm/Support/MemoryBuffer.h" 11 #include <cassert> 12 #include <cstring> 13 #include <unistd.h> 14 15 namespace __llvm_libc { 16 namespace testutils { 17 18 FDReader::FDReader() { 19 if (::pipe(pipefd)) 20 llvm::report_fatal_error("pipe(2) failed"); 21 } 22 23 FDReader::~FDReader() { 24 ::close(pipefd[0]); 25 ::close(pipefd[1]); 26 } 27 28 bool FDReader::matchWritten(const char *str) { 29 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> bufOrErr = 30 llvm::MemoryBuffer::getOpenFile(pipefd[0], "<pipe>", 31 /* FileSize (irrelevant) */ 0); 32 if (!bufOrErr) { 33 assert(0 && "Error reading from pipe"); 34 return false; 35 } 36 const llvm::MemoryBuffer &buf = **bufOrErr; 37 return !std::strncmp(buf.getBufferStart(), str, buf.getBufferSize()); 38 } 39 40 } // namespace testutils 41 } // namespace __llvm_libc 42