180814287SRaphael Isemann //===-- TestUtilities.cpp -------------------------------------------------===//
2a6db4167STim Hammerquist //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a6db4167STim Hammerquist //
7a6db4167STim Hammerquist //===----------------------------------------------------------------------===//
8a6db4167STim Hammerquist 
9a6db4167STim Hammerquist #include "TestUtilities.h"
10a6db4167STim Hammerquist #include "llvm/ADT/SmallString.h"
11a2e270faSPavel Labath #include "llvm/ObjectYAML/yaml2obj.h"
12a6db4167STim Hammerquist #include "llvm/Support/FileSystem.h"
13a6db4167STim Hammerquist #include "llvm/Support/Path.h"
148a777920SGreg Clayton #include "llvm/Support/Program.h"
15a2e270faSPavel Labath #include "llvm/Support/YAMLTraits.h"
16a2e270faSPavel Labath #include "gtest/gtest.h"
17a2e270faSPavel Labath 
18a2e270faSPavel Labath using namespace lldb_private;
19a6db4167STim Hammerquist 
20a6db4167STim Hammerquist extern const char *TestMainArgv0;
21a6db4167STim Hammerquist 
GetInputFilePath(const llvm::Twine & name)22a6db4167STim Hammerquist std::string lldb_private::GetInputFilePath(const llvm::Twine &name) {
23a6db4167STim Hammerquist   llvm::SmallString<128> result = llvm::sys::path::parent_path(TestMainArgv0);
24a6db4167STim Hammerquist   llvm::sys::fs::make_absolute(result);
25a6db4167STim Hammerquist   llvm::sys::path::append(result, "Inputs", name);
26adcd0268SBenjamin Kramer   return std::string(result.str());
27a6db4167STim Hammerquist }
288a777920SGreg Clayton 
fromYaml(llvm::StringRef Yaml)29a2e270faSPavel Labath llvm::Expected<TestFile> TestFile::fromYaml(llvm::StringRef Yaml) {
30a4a00cedSFred Riss   std::string Buffer;
31a4a00cedSFred Riss   llvm::raw_string_ostream OS(Buffer);
32a2e270faSPavel Labath   llvm::yaml::Input YIn(Yaml);
3369ba3defSGeorge Rimar   if (!llvm::yaml::convertYAML(YIn, OS, [](const llvm::Twine &Msg) {}))
3469ba3defSGeorge Rimar     return llvm::createStringError(llvm::inconvertibleErrorCode(),
3569ba3defSGeorge Rimar                                    "convertYAML() failed");
36a4a00cedSFred Riss   return TestFile(std::move(Buffer));
37a2e270faSPavel Labath }
38a2e270faSPavel Labath 
fromYamlFile(const llvm::Twine & Name)39a2e270faSPavel Labath llvm::Expected<TestFile> TestFile::fromYamlFile(const llvm::Twine &Name) {
40a2e270faSPavel Labath   auto BufferOrError =
41*c83cd8feSAbhina Sreeskantharajan       llvm::MemoryBuffer::getFile(GetInputFilePath(Name), /*IsText=*/false,
42*c83cd8feSAbhina Sreeskantharajan                                   /*RequiresNullTerminator=*/false);
43a2e270faSPavel Labath   if (!BufferOrError)
44a2e270faSPavel Labath     return llvm::errorCodeToError(BufferOrError.getError());
45a2e270faSPavel Labath   return fromYaml(BufferOrError.get()->getBuffer());
46a2e270faSPavel Labath }
47