[lld] r319922 - Add a checkLazy error checking variant.
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 6 10:52:14 PST 2017
Author: rafael
Date: Wed Dec 6 10:52:13 2017
New Revision: 319922
URL: http://llvm.org/viewvc/llvm-project?rev=319922&view=rev
Log:
Add a checkLazy error checking variant.
This avoids allocating the error message when there is no error that
check requires.
It avoids the code duplication of inlining check.
Modified:
lld/trunk/ELF/InputFiles.cpp
lld/trunk/include/lld/Common/ErrorHandler.h
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=319922&r1=319921&r2=319922&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Wed Dec 6 10:52:13 2017
@@ -209,10 +209,8 @@ typename ELFT::SymRange ELFFileBase<ELFT
template <class ELFT>
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
- auto RetOrErr = getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX);
- if (RetOrErr)
- return *RetOrErr;
- fatal(toString(this) + ": " + toString(RetOrErr.takeError()));
+ return checkLazy(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX),
+ [=]() { return toString(this); });
}
template <class ELFT>
@@ -642,10 +640,8 @@ template <class ELFT> Symbol *ObjFile<EL
return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
}
- auto NameOrErr = Sym->getName(this->StringTable);
- if (!NameOrErr)
- fatal(toString(this) + ": " + toString(NameOrErr.takeError()));
- StringRef Name = *NameOrErr;
+ StringRef Name = checkLazy(Sym->getName(this->StringTable),
+ [=]() { return toString(this); });
switch (Sym->st_shndx) {
case SHN_UNDEF:
Modified: lld/trunk/include/lld/Common/ErrorHandler.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Common/ErrorHandler.h?rev=319922&r1=319921&r2=319922&view=diff
==============================================================================
--- lld/trunk/include/lld/Common/ErrorHandler.h (original)
+++ lld/trunk/include/lld/Common/ErrorHandler.h Wed Dec 6 10:52:13 2017
@@ -30,6 +30,7 @@
#include "lld/Common/LLVM.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileOutputBuffer.h"
@@ -99,6 +100,13 @@ template <class T> T check(Expected<T> E
return std::move(*E);
}
+// A lazy variant that only allocates error messages when there is an error.
+template <class T>
+T checkLazy(Expected<T> E, llvm::function_ref<std::string()> getPrefix) {
+ if (!E)
+ fatal(getPrefix() + ": " + toString(E.takeError()));
+ return std::move(*E);
+}
} // namespace lld
#endif
More information about the llvm-commits
mailing list