[llvm] Fix ODRViolations for VersionTuple YAML specializations NFC

It appears for Swift there was confusing errors when trying to parse APINotes, when libAPINotes and libInterfaceStub are linked, they both export symbol
`__ZN4llvm4yaml7yamlizeINS_12VersionTupleEEENSt3__19enable_ifIXsr16has_ScalarTraitsIT_EE5valueEvE4typeERNS0_2IOERS5_bRNS0_12EmptyContextE`, and discovered
same symbol defined within llvm-ifs.

This consolidates the boilerplate into YAMLTraits and defers the specific validation in reading the whole input.
fixes: rdar://problem/70450563

Reviewed By: phosek, dblaikie

Differential Revision: https://reviews.llvm.org/D89764
This commit is contained in:
Cyndy Ishida 2020-10-20 17:50:29 -07:00
parent 4aa97e3dac
commit acb33cba6d
5 changed files with 41 additions and 43 deletions

View File

@ -20,6 +20,7 @@
#include "llvm/Support/Endian.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/VersionTuple.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
@ -1711,6 +1712,12 @@ struct ScalarTraits<Hex64> {
static QuotingType mustQuote(StringRef) { return QuotingType::None; }
};
template <> struct ScalarTraits<VersionTuple> {
static void output(const VersionTuple &Value, void *, llvm::raw_ostream &Out);
static StringRef input(StringRef, void *, VersionTuple &);
static QuotingType mustQuote(StringRef) { return QuotingType::None; }
};
// Define non-member operator>> so that Input can stream in a document list.
template <typename T>
inline std::enable_if_t<has_DocumentListTraits<T>::value, Input &>

View File

@ -69,28 +69,6 @@ template <> struct ScalarTraits<ELFArchMapper> {
static QuotingType mustQuote(StringRef) { return QuotingType::None; }
};
/// YAML traits for TbeVersion.
template <> struct ScalarTraits<VersionTuple> {
static void output(const VersionTuple &Value, void *,
llvm::raw_ostream &Out) {
Out << Value.getAsString();
}
static StringRef input(StringRef Scalar, void *, VersionTuple &Value) {
if (Value.tryParse(Scalar))
return StringRef("Can't parse version: invalid version format.");
if (Value > TBEVersionCurrent)
return StringRef("Unsupported TBE version.");
// Returning empty StringRef indicates successful parse.
return StringRef();
}
// Don't place quotation marks around version value.
static QuotingType mustQuote(StringRef) { return QuotingType::None; }
};
/// YAML traits for ELFSymbol.
template <> struct MappingTraits<ELFSymbol> {
static void mapping(IO &IO, ELFSymbol &Symbol) {
@ -149,6 +127,11 @@ Expected<std::unique_ptr<ELFStub>> elfabi::readTBEFromBuffer(StringRef Buf) {
if (std::error_code Err = YamlIn.error())
return createStringError(Err, "YAML failed reading as TBE");
if (Stub->TbeVersion > elfabi::TBEVersionCurrent)
return make_error<StringError>(
"TBE version " + Stub->TbeVersion.getAsString() + " is unsupported.",
std::make_error_code(std::errc::invalid_argument));
return std::move(Stub);
}

View File

@ -1102,3 +1102,15 @@ StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
Val = Num;
return StringRef();
}
void ScalarTraits<VersionTuple>::output(const VersionTuple &Val, void *,
llvm::raw_ostream &Out) {
Out << Val.getAsString();
}
StringRef ScalarTraits<VersionTuple>::input(StringRef Scalar, void *,
VersionTuple &Val) {
if (Val.tryParse(Scalar))
return "invalid version format";
return StringRef();
}

View File

@ -104,27 +104,6 @@ template <> struct ScalarEnumerationTraits<IFSSymbolType> {
}
};
template <> struct ScalarTraits<VersionTuple> {
static void output(const VersionTuple &Value, void *,
llvm::raw_ostream &Out) {
Out << Value.getAsString();
}
static StringRef input(StringRef Scalar, void *, VersionTuple &Value) {
if (Value.tryParse(Scalar))
return StringRef("Can't parse version: invalid version format.");
if (Value > IFSVersionCurrent)
return StringRef("Unsupported IFS version.");
// Returning empty StringRef indicates successful parse.
return StringRef();
}
// Don't place quotation marks around version value.
static QuotingType mustQuote(StringRef) { return QuotingType::None; }
};
/// YAML traits for IFSSymbol.
template <> struct MappingTraits<IFSSymbol> {
static void mapping(IO &IO, IFSSymbol &Symbol) {
@ -210,6 +189,11 @@ static Expected<std::unique_ptr<IFSStub>> readInputFile(StringRef FilePath) {
if (std::error_code Err = YamlIn.error())
return createStringError(Err, "Failed reading Interface Stub File.");
if (Stub->IfsVersion > IFSVersionCurrent)
return make_error<StringError>(
"IFS version " + Stub->IfsVersion.getAsString() + " is unsupported.",
std::make_error_code(std::errc::invalid_argument));
return std::move(Stub);
}

View File

@ -144,6 +144,18 @@ TEST(ElfYamlTextAPI, YAMLUnreadableTBE) {
ASSERT_THAT_ERROR(StubOrErr.takeError(), Failed());
}
TEST(ElfYamlTextAPI, YAMLUnsupportedVersion) {
const char Data[] = "--- !tapi-tbe\n"
"TbeVersion: 9.9.9\n"
"SoName: test.so\n"
"Arch: x86_64\n"
"Symbols: {}\n"
"...\n";
Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
std::string ErrorMessage = toString(StubOrErr.takeError());
EXPECT_EQ("TBE version 9.9.9 is unsupported.", ErrorMessage);
}
TEST(ElfYamlTextAPI, YAMLWritesTBESymbols) {
const char Expected[] =
"--- !tapi-tbe\n"