Define make_dynamic_error_code(const char *).

The function took either StringRef or Twine. Since string literals are
ambiguous when resolving the overloading, many code calls used this
function with explicit type conversion. That led awkward code like
make_dynamic_error_code(Twine("Error occurred")).

This patch adds a function definition for string literals, so that
you can directly call the function with literals.

llvm-svn: 234841
This commit is contained in:
Rui Ueyama 2015-04-14 02:34:09 +00:00
parent 7348ddaa74
commit e75e50c045
12 changed files with 43 additions and 41 deletions

View File

@ -50,6 +50,7 @@ inline std::error_code make_error_code(LinkerScriptReaderError e) {
/// supplied error string.
/// Note: Once ErrorOr<> is updated to work with errors other than error_code,
/// this can be updated to return some other kind of error.
std::error_code make_dynamic_error_code(const char *msg);
std::error_code make_dynamic_error_code(StringRef msg);
std::error_code make_dynamic_error_code(const Twine &msg);

View File

@ -107,6 +107,10 @@ private:
static dynamic_error_category categorySingleton;
std::error_code make_dynamic_error_code(const char *msg) {
return make_dynamic_error_code(StringRef(msg));
}
std::error_code make_dynamic_error_code(StringRef msg) {
return std::error_code(categorySingleton.add(msg), categorySingleton);
}

View File

@ -124,7 +124,7 @@ getFileMagic(StringRef path, llvm::sys::fs::file_magic &magic) {
case llvm::sys::fs::file_magic::unknown:
return std::error_code();
default:
return make_dynamic_error_code(StringRef("unknown type of object file"));
return make_dynamic_error_code("unknown type of object file");
}
}

View File

@ -69,14 +69,14 @@ std::error_code MipsELFFlagsMerger::mergeHeaderFlags(uint8_t newClass,
// Check bitness.
if (_is64Bit != (newClass == ELFCLASS64))
return make_dynamic_error_code(
Twine("Bitness is incompatible with that of the selected target"));
"Bitness is incompatible with that of the selected target");
// We support two ABI: O32 and N64. The last one does not have
// the corresponding ELF flag.
uint32_t inAbi = newFlags & EF_MIPS_ABI;
uint32_t supportedAbi = _is64Bit ? 0 : uint32_t(EF_MIPS_ABI_O32);
if (inAbi != supportedAbi)
return make_dynamic_error_code(Twine("Unsupported ABI"));
return make_dynamic_error_code("Unsupported ABI");
// ... and reduced set of architectures ...
uint32_t newArch = newFlags & EF_MIPS_ARCH;
@ -94,12 +94,12 @@ std::error_code MipsELFFlagsMerger::mergeHeaderFlags(uint8_t newClass,
case EF_MIPS_ARCH_64R6:
break;
default:
return make_dynamic_error_code(Twine("Unsupported instruction set"));
return make_dynamic_error_code("Unsupported instruction set");
}
// ... and still do not support MIPS-16 extension.
if (newFlags & EF_MIPS_ARCH_ASE_M16)
return make_dynamic_error_code(Twine("Unsupported extension: MIPS16"));
return make_dynamic_error_code("Unsupported extension: MIPS16");
// PIC code is inherently CPIC and may not set CPIC flag explicitly.
// Ensure that this flag will exist in the linked file.
@ -129,14 +129,13 @@ std::error_code MipsELFFlagsMerger::mergeHeaderFlags(uint8_t newClass,
// Check mixing -mnan=2008 / -mnan=legacy modules.
if ((newFlags & EF_MIPS_NAN2008) != (_flags & EF_MIPS_NAN2008))
return make_dynamic_error_code(
Twine("Linking -mnan=2008 and -mnan=legacy modules"));
"Linking -mnan=2008 and -mnan=legacy modules");
// Check ISA compatibility and update the extension flag.
uint32_t oldArch = _flags & EF_MIPS_ARCH;
if (!matchMipsISA(newArch, oldArch)) {
if (!matchMipsISA(oldArch, newArch))
return make_dynamic_error_code(
Twine("Linking modules with incompatible ISA"));
return make_dynamic_error_code("Linking modules with incompatible ISA");
_flags &= ~EF_MIPS_ARCH;
_flags |= newArch;
}

View File

@ -350,8 +350,7 @@ static std::error_code adjustJumpOpCode(uint64_t &ins, uint64_t tgt,
uint32_t opCross = toMicro ? 0x1d : 0x3c;
if ((tgt & 1) != toMicro)
return make_dynamic_error_code(
Twine("Incorrect bit 0 for the jalx target"));
return make_dynamic_error_code("Incorrect bit 0 for the jalx target");
if (tgt & 2)
return make_dynamic_error_code(Twine("The jalx target 0x") +

View File

@ -18,15 +18,15 @@ namespace lld {
namespace elf {
inline std::error_code make_unhandled_reloc_error() {
return make_dynamic_error_code(Twine("Unhandled reference type"));
return make_dynamic_error_code("Unhandled reference type");
}
inline std::error_code make_out_of_range_reloc_error() {
return make_dynamic_error_code(Twine("Relocation out of range"));
return make_dynamic_error_code("Relocation out of range");
}
inline std::error_code make_unaligned_range_reloc_error() {
return make_dynamic_error_code(Twine("Relocation not aligned"));
return make_dynamic_error_code("Relocation not aligned");
}
} // end namespace elf

View File

@ -619,7 +619,7 @@ std::error_code ArchHandler_arm::getReferenceInfo(
*addend += (clearThumbBit(instruction, *target) - reloc.value);
return std::error_code();
default:
return make_dynamic_error_code(Twine("unsupported arm relocation type"));
return make_dynamic_error_code("unsupported arm relocation type");
}
return std::error_code();
}
@ -777,7 +777,7 @@ ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1,
pointerDiff = true;
break;
default:
return make_dynamic_error_code(Twine("unsupported arm relocation pair"));
return make_dynamic_error_code("unsupported arm relocation pair");
}
const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom];
std::error_code ec;
@ -800,8 +800,8 @@ ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1,
if (ec)
return ec;
if (scatterable && (fromTarget != inAtom))
return make_dynamic_error_code(Twine("SECTDIFF relocation where "
"subtrahend label is not in atom"));
return make_dynamic_error_code(
"SECTDIFF relocation where subtrahend label is not in atom");
*kind = delta32;
value = clearThumbBit(instruction, *target);
*addend = (int32_t)(value - (toAddress - fixupAddress));
@ -815,29 +815,28 @@ ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1,
if (ec)
return ec;
if (fromTarget != inAtom)
return make_dynamic_error_code(
Twine("ARM_RELOC_HALF_SECTDIFF relocation "
"where subtrahend label is not in atom"));
return make_dynamic_error_code("ARM_RELOC_HALF_SECTDIFF relocation "
"where subtrahend label is not in atom");
other16 = (reloc2.offset & 0xFFFF);
if (thumbReloc) {
if (top) {
if (!isThumbMovt(instruction))
return make_dynamic_error_code(Twine("expected movt instruction"));
return make_dynamic_error_code("expected movt instruction");
}
else {
if (!isThumbMovw(instruction))
return make_dynamic_error_code(Twine("expected movw instruction"));
return make_dynamic_error_code("expected movw instruction");
}
instruction16 = getWordFromThumbMov(instruction);
}
else {
if (top) {
if (!isArmMovt(instruction))
return make_dynamic_error_code(Twine("expected movt instruction"));
return make_dynamic_error_code("expected movt instruction");
}
else {
if (!isArmMovw(instruction))
return make_dynamic_error_code(Twine("expected movw instruction"));
return make_dynamic_error_code("expected movw instruction");
}
instruction16 = getWordFromArmMov(instruction);
}
@ -854,22 +853,22 @@ ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1,
if (thumbReloc) {
if (top) {
if (!isThumbMovt(instruction))
return make_dynamic_error_code(Twine("expected movt instruction"));
return make_dynamic_error_code("expected movt instruction");
}
else {
if (!isThumbMovw(instruction))
return make_dynamic_error_code(Twine("expected movw instruction"));
return make_dynamic_error_code("expected movw instruction");
}
instruction16 = getWordFromThumbMov(instruction);
}
else {
if (top) {
if (!isArmMovt(instruction))
return make_dynamic_error_code(Twine("expected movt instruction"));
return make_dynamic_error_code("expected movt instruction");
}
else {
if (!isArmMovw(instruction))
return make_dynamic_error_code(Twine("expected movw instruction"));
return make_dynamic_error_code("expected movw instruction");
}
instruction16 = getWordFromArmMov(instruction);
}

View File

@ -437,7 +437,7 @@ std::error_code ArchHandler_arm64::getReferenceInfo(
*addend = 0;
return std::error_code();
default:
return make_dynamic_error_code(Twine("unsupported arm64 relocation type"));
return make_dynamic_error_code("unsupported arm64 relocation type");
}
}
@ -491,7 +491,7 @@ std::error_code ArchHandler_arm64::getPairReferenceInfo(
*addend = (int32_t)*(const little32_t *)fixupContent + offsetInAtom;
return std::error_code();
default:
return make_dynamic_error_code(Twine("unsupported arm64 relocation pair"));
return make_dynamic_error_code("unsupported arm64 relocation pair");
}
}

View File

@ -334,7 +334,7 @@ ArchHandler_x86::getReferenceInfo(const Relocation &reloc,
*addend = *(const ulittle32_t *)fixupContent - reloc.value;
break;
default:
return make_dynamic_error_code(Twine("unsupported i386 relocation type"));
return make_dynamic_error_code("unsupported i386 relocation type");
}
return std::error_code();
}
@ -376,8 +376,8 @@ ArchHandler_x86::getPairReferenceInfo(const normalized::Relocation &reloc1,
return ec;
if (fromTarget != inAtom) {
if (*target != inAtom)
return make_dynamic_error_code(Twine("SECTDIFF relocation where "
"neither target is in atom"));
return make_dynamic_error_code(
"SECTDIFF relocation where neither target is in atom");
*kind = negDelta32;
*addend = toAddress - value - fromAddress;
*target = fromTarget;
@ -400,7 +400,7 @@ ArchHandler_x86::getPairReferenceInfo(const normalized::Relocation &reloc1,
return std::error_code();
break;
default:
return make_dynamic_error_code(Twine("unsupported i386 relocation type"));
return make_dynamic_error_code("unsupported i386 relocation type");
}
}

View File

@ -331,7 +331,7 @@ ArchHandler_x86_64::getReferenceInfo(const Relocation &reloc,
typedef std::error_code E;
*kind = kindFromReloc(reloc);
if (*kind == invalid)
return make_dynamic_error_code(Twine("unknown type"));
return make_dynamic_error_code("unknown type");
const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom];
uint64_t targetAddress;
switch (*kind) {
@ -413,7 +413,7 @@ ArchHandler_x86_64::getPairReferenceInfo(const normalized::Relocation &reloc1,
Reference::Addend *addend) {
*kind = kindFromRelocPair(reloc1, reloc2);
if (*kind == invalid)
return make_dynamic_error_code(Twine("unknown pair"));
return make_dynamic_error_code("unknown pair");
const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom];
typedef std::error_code E;
uint64_t targetAddress;
@ -421,7 +421,7 @@ ArchHandler_x86_64::getPairReferenceInfo(const normalized::Relocation &reloc1,
if (E ec = atomFromSymbolIndex(reloc1.symbol, &fromTarget))
return ec;
if (fromTarget != inAtom)
return make_dynamic_error_code(Twine("pointer diff not in base atom"));
return make_dynamic_error_code("pointer diff not in base atom");
switch (*kind) {
case delta64:
if (E ec = atomFromSymbolIndex(reloc2.symbol, target))

View File

@ -558,17 +558,17 @@ std::error_code convertRelocs(const Section &section,
*result = target;
return std::error_code();
}
return make_dynamic_error_code(Twine("no atom found for defined symbol"));
return make_dynamic_error_code("no atom found for defined symbol");
} else if ((sym->type & N_TYPE) == N_UNDF) {
const lld::Atom *target = file.findUndefAtom(sym->name);
if (target) {
*result = target;
return std::error_code();
}
return make_dynamic_error_code(Twine("no undefined atom found for sym"));
return make_dynamic_error_code("no undefined atom found for sym");
} else {
// Search undefs
return make_dynamic_error_code(Twine("no atom found for symbol"));
return make_dynamic_error_code("no atom found for symbol");
}
};

View File

@ -257,7 +257,7 @@ public:
// Check if the total size is valid.
if (std::size_t(end - buf) != sizeof(COFF::ImportHeader) + dataSize)
return make_dynamic_error_code(StringRef("Broken import library"));
return make_dynamic_error_code("Broken import library");
uint16_t hint = read16le(buf + offsetof(COFF::ImportHeader, OrdinalHint));
StringRef symbolName(buf + sizeof(COFF::ImportHeader));