Files
revng-revng/include/revng/Support/Error.h
Giacomo Vercesi 72d9b46e4b Error.h: improve joinErrors
Change the `joinError` functions to have stronger guarantees when used:
* Change the template signature to `RangeOf` to ensure that the
  container has `llvm::Error`s.
* Check that the size of the passed container is actually positive
  before deferencing `.begin()`.
2025-03-13 15:07:25 +01:00

45 lines
1.0 KiB
C++

#pragma once
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "revng/ADT/Concepts.h"
#include "revng/Support/Assert.h"
namespace revng {
template<typename... Ts>
inline llvm::Error createError(char const *Fmt, const Ts &...Vals) {
return llvm::createStringError(llvm::inconvertibleErrorCode(), Fmt, Vals...);
}
inline llvm::Error createError(const llvm::Twine &S) {
return llvm::createStringError(llvm::inconvertibleErrorCode(), S);
}
inline void cantFail(std::error_code EC) {
revng_assert(not EC);
}
template<RangeOf<llvm::Error> T>
inline llvm::Error joinErrors(T &Container) {
auto Size = std::ranges::distance(Container);
revng_check(Size > 0);
auto Iter = Container.begin();
llvm::Error Result{ std::move(*Iter) };
if (Size == 1) {
return Result;
}
for (Iter++; Iter < Container.end(); Iter++) {
Result = llvm::joinErrors(std::move(Result), std::move(*Iter));
}
return Result;
}
} // namespace revng