1 //===- TestUtilities.cpp ----------------------------------------*- C++ -*-===// 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 "TestUtilities.h" 10 #include "llvm/ADT/SmallString.h" 11 #include "llvm/Support/FileSystem.h" 12 #include "llvm/Support/Path.h" 13 #include "llvm/Support/Program.h" 14 15 extern const char *TestMainArgv0; 16 17 std::string lldb_private::GetInputFilePath(const llvm::Twine &name) { 18 llvm::SmallString<128> result = llvm::sys::path::parent_path(TestMainArgv0); 19 llvm::sys::fs::make_absolute(result); 20 llvm::sys::path::append(result, "Inputs", name); 21 return result.str(); 22 } 23 24 llvm::Error 25 lldb_private::ReadYAMLObjectFile(const llvm::Twine &yaml_name, 26 llvm::SmallString<128> &object_file) { 27 std::string yaml = GetInputFilePath(yaml_name); 28 llvm::StringRef args[] = {YAML2OBJ, yaml}; 29 llvm::StringRef obj_ref = object_file; 30 const llvm::Optional<llvm::StringRef> redirects[] = {llvm::None, obj_ref, 31 llvm::None}; 32 if (llvm::sys::ExecuteAndWait(YAML2OBJ, args, llvm::None, redirects) != 0) 33 return llvm::createStringError(llvm::inconvertibleErrorCode(), 34 "Error running yaml2obj %s.", yaml.c_str()); 35 uint64_t size; 36 if (auto ec = llvm::sys::fs::file_size(object_file, size)) 37 return llvm::errorCodeToError(ec); 38 if (size == 0) 39 return llvm::createStringError( 40 llvm::inconvertibleErrorCode(), 41 "Empty object file created from yaml2obj %s.", yaml.c_str()); 42 return llvm::Error::success(); 43 } 44