LLVM 20.0.0git
SupportHelpers.h
Go to the documentation of this file.
1//===- Testing/Support/SupportHelpers.h -----------------------------------===//
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#ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
10#define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
11
13#include "llvm/Support/Error.h"
15#include "llvm/Support/Path.h"
17#include "gmock/gmock-matchers.h"
18#include "gtest/gtest-printers.h"
19
20#include <optional>
21#include <string>
22
23namespace llvm {
24namespace detail {
26 std::vector<std::shared_ptr<ErrorInfoBase>> Infos;
27
28 bool Success() const { return Infos.empty(); }
29};
30
31template <typename T> struct ExpectedHolder : public ErrorHolder {
33 : ErrorHolder(std::move(Err)), Exp(Exp) {}
34
36};
37
38inline void PrintTo(const ErrorHolder &Err, std::ostream *Out) {
39 raw_os_ostream OS(*Out);
40 OS << (Err.Success() ? "succeeded" : "failed");
41 if (!Err.Success()) {
42 const char *Delim = " (";
43 for (const auto &Info : Err.Infos) {
44 OS << Delim;
45 Delim = "; ";
46 Info->log(OS);
47 }
48 OS << ")";
49 }
50}
51
52template <typename T>
53void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) {
54 if (Item.Success()) {
55 *Out << "succeeded with value " << ::testing::PrintToString(*Item.Exp);
56 } else {
57 PrintTo(static_cast<const ErrorHolder &>(Item), Out);
58 }
59}
60
61template <class InnerMatcher> class ValueIsMatcher {
62public:
63 explicit ValueIsMatcher(InnerMatcher ValueMatcher)
64 : ValueMatcher(ValueMatcher) {}
65
66 template <class T>
67 operator ::testing::Matcher<const std::optional<T> &>() const {
68 return ::testing::MakeMatcher(
69 new Impl<T>(::testing::SafeMatcherCast<T>(ValueMatcher)));
70 }
71
72 template <class T, class O = std::optional<T>>
73 class Impl : public ::testing::MatcherInterface<const O &> {
74 public:
75 explicit Impl(const ::testing::Matcher<T> &ValueMatcher)
76 : ValueMatcher(ValueMatcher) {}
77
78 bool MatchAndExplain(const O &Input,
79 testing::MatchResultListener *L) const override {
80 return Input && ValueMatcher.MatchAndExplain(*Input, L);
81 }
82
83 void DescribeTo(std::ostream *OS) const override {
84 *OS << "has a value that ";
85 ValueMatcher.DescribeTo(OS);
86 }
87 void DescribeNegationTo(std::ostream *OS) const override {
88 *OS << "does not have a value that ";
89 ValueMatcher.DescribeTo(OS);
90 }
91
92 private:
93 testing::Matcher<T> ValueMatcher;
94 };
95
96private:
97 InnerMatcher ValueMatcher;
98};
99} // namespace detail
100
101/// Matches an std::optional<T> with a value that conforms to an inner matcher.
102/// To match std::nullopt you could use Eq(std::nullopt).
103template <class InnerMatcher>
104detail::ValueIsMatcher<InnerMatcher> ValueIs(const InnerMatcher &ValueMatcher) {
105 return detail::ValueIsMatcher<InnerMatcher>(ValueMatcher);
106}
107namespace unittest {
108
109SmallString<128> getInputFileDirectory(const char *Argv0);
110
111/// A RAII object that creates a temporary directory upon initialization and
112/// removes it upon destruction.
113class TempDir {
114 SmallString<128> Path;
115
116public:
117 /// Creates a managed temporary directory.
118 ///
119 /// @param Name The name of the directory to create.
120 /// @param Unique If true, the directory will be created using
121 /// llvm::sys::fs::createUniqueDirectory.
122 explicit TempDir(StringRef Name, bool Unique = false) {
123 std::error_code EC;
124 if (Unique) {
126 if (!EC) {
127 // Resolve any symlinks in the new directory.
128 std::string UnresolvedPath(Path.str());
129 EC = llvm::sys::fs::real_path(UnresolvedPath, Path);
130 }
131 } else {
132 Path = Name;
134 }
135 if (EC)
136 Path.clear();
137 EXPECT_FALSE(EC) << EC.message();
138 }
139
141 if (!Path.empty()) {
142 EXPECT_FALSE(llvm::sys::fs::remove_directories(Path.str()));
143 }
144 }
145
146 TempDir(const TempDir &) = delete;
147 TempDir &operator=(const TempDir &) = delete;
148
149 TempDir(TempDir &&) = default;
150 TempDir &operator=(TempDir &&) = default;
151
152 /// The path to the temporary directory.
153 StringRef path() const { return Path; }
154
155 /// The null-terminated C string pointing to the path.
156 const char *c_str() { return Path.c_str(); }
157
158 /// Creates a new path by appending the argument to the path of the managed
159 /// directory using the native path separator.
160 SmallString<128> path(StringRef component) const {
161 SmallString<128> Result(Path);
162 SmallString<128> ComponentToAppend(component);
163 llvm::sys::path::native(ComponentToAppend);
164 llvm::sys::path::append(Result, Twine(ComponentToAppend));
165 return Result;
166 }
167};
168
169/// A RAII object that creates a link upon initialization and
170/// removes it upon destruction.
171///
172/// The link may be a soft or a hard link, depending on the platform.
173class TempLink {
174 SmallString<128> Path;
175
176public:
177 /// Creates a managed link at path Link pointing to Target.
179 Path = Link;
180 std::error_code EC = sys::fs::create_link(Target, Link);
181 if (EC)
182 Path.clear();
183 EXPECT_FALSE(EC);
184 }
186 if (!Path.empty()) {
187 EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
188 }
189 }
190
191 TempLink(const TempLink &) = delete;
192 TempLink &operator=(const TempLink &) = delete;
193
194 TempLink(TempLink &&) = default;
196
197 /// The path to the link.
198 StringRef path() const { return Path; }
199};
200
201/// A RAII object that creates a file upon initialization and
202/// removes it upon destruction.
203class TempFile {
204 SmallString<128> Path;
205
206public:
207 /// Creates a managed file.
208 ///
209 /// @param Name The name of the file to create.
210 /// @param Contents The string to write to the file.
211 /// @param Unique If true, the file will be created using
212 /// llvm::sys::fs::createTemporaryFile.
213 TempFile(StringRef Name, StringRef Suffix = "", StringRef Contents = "",
214 bool Unique = false) {
215 std::error_code EC;
216 int fd;
217 if (Unique) {
218 EC = llvm::sys::fs::createTemporaryFile(Name, Suffix, fd, Path);
219 } else {
220 Path = Name;
221 if (!Suffix.empty()) {
222 Path.append(".");
223 Path.append(Suffix);
224 }
225 EC = llvm::sys::fs::openFileForWrite(Path, fd);
226 }
227 EXPECT_FALSE(EC);
228 raw_fd_ostream OS(fd, /*shouldClose*/ true);
229 OS << Contents;
230 OS.flush();
231 EXPECT_FALSE(OS.error());
232 if (EC || OS.error())
233 Path.clear();
234 }
236 if (!Path.empty()) {
237 EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
238 }
239 }
240
241 TempFile(const TempFile &) = delete;
242 TempFile &operator=(const TempFile &) = delete;
243
244 TempFile(TempFile &&) = default;
246
247 /// The path to the file.
248 StringRef path() const { return Path; }
249};
250
251} // namespace unittest
252} // namespace llvm
253
254#endif
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
std::string Name
raw_pwrite_stream & OS
This file defines the SmallString class.
Tagged union holding either a T or a Error.
Definition: Error.h:481
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
void DescribeNegationTo(std::ostream *OS) const override
void DescribeTo(std::ostream *OS) const override
Impl(const ::testing::Matcher< T > &ValueMatcher)
bool MatchAndExplain(const O &Input, testing::MatchResultListener *L) const override
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:460
raw_os_ostream - A raw_ostream that writes to an std::ostream.
A RAII object that creates a temporary directory upon initialization and removes it upon destruction.
const char * c_str()
The null-terminated C string pointing to the path.
StringRef path() const
The path to the temporary directory.
TempDir(StringRef Name, bool Unique=false)
Creates a managed temporary directory.
TempDir & operator=(TempDir &&)=default
TempDir & operator=(const TempDir &)=delete
SmallString< 128 > path(StringRef component) const
Creates a new path by appending the argument to the path of the managed directory using the native pa...
TempDir(TempDir &&)=default
TempDir(const TempDir &)=delete
A RAII object that creates a file upon initialization and removes it upon destruction.
StringRef path() const
The path to the file.
TempFile(StringRef Name, StringRef Suffix="", StringRef Contents="", bool Unique=false)
Creates a managed file.
TempFile(const TempFile &)=delete
TempFile(TempFile &&)=default
TempFile & operator=(TempFile &&)=default
TempFile & operator=(const TempFile &)=delete
void PrintTo(const ErrorHolder &Err, std::ostream *Out)
std::error_code remove_directories(const Twine &path, bool IgnoreErrors=true)
Recursively delete a directory.
std::error_code create_link(const Twine &to, const Twine &from)
Create a link from from to to.
std::error_code real_path(const Twine &path, SmallVectorImpl< char > &output, bool expand_tilde=false)
Collapse all .
std::error_code create_directory(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create the directory in path.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: FileSystem.h:1062
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath, OpenFlags Flags=OF_None)
Create a file in the system temporary directory.
Definition: Path.cpp:864
std::error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl< char > &ResultPath)
Definition: Path.cpp:886
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:457
SmallString< 128 > getInputFileDirectory(const char *Argv0)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1856
detail::ValueIsMatcher< InnerMatcher > ValueIs(const InnerMatcher &ValueMatcher)
Matches an std::optional<T> with a value that conforms to an inner matcher.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
std::vector< std::shared_ptr< ErrorInfoBase > > Infos
ExpectedHolder(ErrorHolder Err, Expected< T > &Exp)