Changeset View
Changeset View
Standalone View
Standalone View
llvm/unittests/Support/FormatChkTest.cpp
- This file was added.
//===- FormatChkTest.cpp - Unit tests for checked string formatting -------===// | |||||
// | |||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||||
// See https://llvm.org/LICENSE.txt for license information. | |||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||||
// | |||||
//===----------------------------------------------------------------------===// | |||||
#include "llvm/Support/Format.h" | |||||
#include "gtest/gtest.h" | |||||
#include <vector> | |||||
using namespace llvm; | |||||
namespace { | |||||
constexpr auto ST_Unknown = llvm::PrintfStyleFormatReader::ST_Unknown; | |||||
constexpr auto ST_WideChar = llvm::PrintfStyleFormatReader::ST_WideChar; | |||||
constexpr auto ST_Int = llvm::PrintfStyleFormatReader::ST_Int; | |||||
constexpr auto ST_UInt = llvm::PrintfStyleFormatReader::ST_UInt; | |||||
constexpr auto ST_Long = llvm::PrintfStyleFormatReader::ST_Long; | |||||
constexpr auto ST_ULong = llvm::PrintfStyleFormatReader::ST_ULong; | |||||
constexpr auto ST_LongLong = llvm::PrintfStyleFormatReader::ST_LongLong; | |||||
constexpr auto ST_ULongLong = llvm::PrintfStyleFormatReader::ST_ULongLong; | |||||
constexpr auto ST_IntMax = llvm::PrintfStyleFormatReader::ST_IntMax; | |||||
constexpr auto ST_UIntMax = llvm::PrintfStyleFormatReader::ST_UIntMax; | |||||
constexpr auto ST_Size = llvm::PrintfStyleFormatReader::ST_Size; | |||||
constexpr auto ST_Ptrdiff = llvm::PrintfStyleFormatReader::ST_Ptrdiff; | |||||
constexpr auto ST_Double = llvm::PrintfStyleFormatReader::ST_Double; | |||||
constexpr auto ST_LongDouble = llvm::PrintfStyleFormatReader::ST_LongDouble; | |||||
constexpr auto ST_CString = llvm::PrintfStyleFormatReader::ST_CString; | |||||
constexpr auto ST_WideCString = llvm::PrintfStyleFormatReader::ST_WideCString; | |||||
constexpr auto ST_VoidPointer = llvm::PrintfStyleFormatReader::ST_VoidPointer; | |||||
constexpr auto ST_Count_Char = llvm::PrintfStyleFormatReader::ST_Count_Char; | |||||
constexpr auto ST_Count_Short = llvm::PrintfStyleFormatReader::ST_Count_Short; | |||||
constexpr auto ST_Count_Int = llvm::PrintfStyleFormatReader::ST_Count_Int; | |||||
constexpr auto ST_Count_Long = llvm::PrintfStyleFormatReader::ST_Count_Long; | |||||
constexpr auto ST_Count_LongLong = | |||||
llvm::PrintfStyleFormatReader::ST_Count_LongLong; | |||||
constexpr auto ST_Count_IntMax = llvm::PrintfStyleFormatReader::ST_Count_IntMax; | |||||
constexpr auto ST_Count_Size = llvm::PrintfStyleFormatReader::ST_Count_Size; | |||||
constexpr auto ST_Count_Ptrdiff = | |||||
llvm::PrintfStyleFormatReader::ST_Count_Ptrdiff; | |||||
using STVec = std::vector<PrintfStyleFormatReader::SpecifierType>; | |||||
STVec ParseFormatString(const char *Fmt, bool SignSensitive = true) { | |||||
STVec Result; | |||||
PrintfStyleFormatReader Reader(Fmt, SignSensitive); | |||||
while (auto Spec = Reader.NextSpecifier()) { | |||||
Result.push_back(Spec); | |||||
} | |||||
return Result; | |||||
} | |||||
void ExpectEq(const char *Fmt, const STVec &RHS) { | |||||
EXPECT_EQ(ParseFormatString(Fmt), RHS); | |||||
} | |||||
void ExpectEq(const char *Fmt, bool SignSensitive, const STVec &RHS) { | |||||
EXPECT_EQ(ParseFormatString(Fmt, SignSensitive), RHS); | |||||
} | |||||
} // namespace | |||||
TEST(FormatReader, EmptyFormatString) { | |||||
EXPECT_EQ(ParseFormatString(""), | |||||
std::vector<PrintfStyleFormatReader::SpecifierType>()); | |||||
} | |||||
TEST(FormatReader, PercentEscape) { | |||||
EXPECT_EQ(ParseFormatString("%%"), | |||||
std::vector<PrintfStyleFormatReader::SpecifierType>()); | |||||
} | |||||
TEST(FormatReader, PercentAtEnd) { ExpectEq("%", {ST_Unknown}); } | |||||
TEST(FormatReader, PercentWithWidth) { ExpectEq("%ll%", {ST_Unknown}); } | |||||
TEST(FormatReader, OneFormat) { | |||||
ExpectEq("%i xx", {ST_Int}); | |||||
ExpectEq("yy %i", {ST_Int}); | |||||
ExpectEq("yy %i xx", {ST_Int}); | |||||
} | |||||
TEST(FormatReader, TwoFormats) { | |||||
ExpectEq("%i yy %f xx", {ST_Int, ST_Double}); | |||||
ExpectEq("zz %i yy %f", {ST_Int, ST_Double}); | |||||
ExpectEq("zz %i yy %f xx", {ST_Int, ST_Double}); | |||||
} | |||||
TEST(FormatReader, IntFlags) { | |||||
ExpectEq("% i", {ST_Int}); | |||||
ExpectEq("%+i", {ST_Int}); | |||||
ExpectEq("%-i", {ST_Int}); | |||||
ExpectEq("%#i", {ST_Int}); | |||||
ExpectEq("%0i", {ST_Int}); | |||||
} | |||||
TEST(FormatReader, LongWidth) { | |||||
ExpectEq("%1li", {ST_Long}); | |||||
ExpectEq("%11li", {ST_Long}); | |||||
ExpectEq("%1111li", {ST_Long}); | |||||
ExpectEq("%10li", {ST_Long}); | |||||
ExpectEq("%*li", {ST_Int, ST_Long}); | |||||
ExpectEq("%*l!", {ST_Unknown}); | |||||
} | |||||
TEST(FormatReader, LongPrecision) { | |||||
ExpectEq("%.1li", {ST_Long}); | |||||
ExpectEq("%.11li", {ST_Long}); | |||||
ExpectEq("%.1111li", {ST_Long}); | |||||
ExpectEq("%.10li", {ST_Long}); | |||||
ExpectEq("%.*li", {ST_Int, ST_Long}); | |||||
ExpectEq("%.*l!", {ST_Unknown}); | |||||
ExpectEq("%1.1li", {ST_Long}); | |||||
ExpectEq("%11.11li", {ST_Long}); | |||||
ExpectEq("%111.1111li", {ST_Long}); | |||||
ExpectEq("%110.10li", {ST_Long}); | |||||
ExpectEq("%1.*li", {ST_Int, ST_Long}); | |||||
ExpectEq("%1.*l!", {ST_Unknown}); | |||||
ExpectEq("%*.*li", {ST_Int, ST_Int, ST_Long}); | |||||
ExpectEq("%*.*l!", {ST_Unknown}); | |||||
} | |||||
TEST(FormatReader, IntSpecifiers) { | |||||
ExpectEq("%hhi", false, {ST_Int}); | |||||
ExpectEq("%hhi", true, {ST_Int}); | |||||
ExpectEq("%hhd", false, {ST_Int}); | |||||
ExpectEq("%hhd", true, {ST_Int}); | |||||
ExpectEq("%hi", false, {ST_Int}); | |||||
ExpectEq("%hi", true, {ST_Int}); | |||||
ExpectEq("%hd", false, {ST_Int}); | |||||
ExpectEq("%hd", true, {ST_Int}); | |||||
ExpectEq("%i", false, {ST_Int}); | |||||
ExpectEq("%i", true, {ST_Int}); | |||||
ExpectEq("%d", false, {ST_Int}); | |||||
ExpectEq("%d", true, {ST_Int}); | |||||
ExpectEq("%li", false, {ST_Long}); | |||||
ExpectEq("%li", true, {ST_Long}); | |||||
ExpectEq("%ld", false, {ST_Long}); | |||||
ExpectEq("%ld", true, {ST_Long}); | |||||
ExpectEq("%lli", false, {ST_LongLong}); | |||||
ExpectEq("%lli", true, {ST_LongLong}); | |||||
ExpectEq("%lld", false, {ST_LongLong}); | |||||
ExpectEq("%lld", true, {ST_LongLong}); | |||||
ExpectEq("%ji", false, {ST_IntMax}); | |||||
ExpectEq("%ji", true, {ST_IntMax}); | |||||
ExpectEq("%jd", false, {ST_IntMax}); | |||||
ExpectEq("%jd", true, {ST_IntMax}); | |||||
ExpectEq("%zi", false, {ST_Size}); | |||||
ExpectEq("%zi", true, {ST_Size}); | |||||
ExpectEq("%zd", false, {ST_Size}); | |||||
ExpectEq("%zd", true, {ST_Size}); | |||||
ExpectEq("%ti", false, {ST_Ptrdiff}); | |||||
ExpectEq("%ti", true, {ST_Ptrdiff}); | |||||
ExpectEq("%td", false, {ST_Ptrdiff}); | |||||
ExpectEq("%td", true, {ST_Ptrdiff}); | |||||
ExpectEq("%Li", false, {ST_Unknown}); | |||||
ExpectEq("%Li", true, {ST_Unknown}); | |||||
ExpectEq("%Ld", false, {ST_Unknown}); | |||||
ExpectEq("%Ld", true, {ST_Unknown}); | |||||
} | |||||
TEST(FormatReader, UIntSpecifiers) { | |||||
ExpectEq("%hhu", false, {ST_Int}); | |||||
ExpectEq("%hhu", true, {ST_UInt}); | |||||
ExpectEq("%hho", false, {ST_Int}); | |||||
ExpectEq("%hho", true, {ST_UInt}); | |||||
ExpectEq("%hhx", false, {ST_Int}); | |||||
ExpectEq("%hhx", true, {ST_UInt}); | |||||
ExpectEq("%hhX", false, {ST_Int}); | |||||
ExpectEq("%hhX", true, {ST_UInt}); | |||||
ExpectEq("%hu", false, {ST_Int}); | |||||
ExpectEq("%hu", true, {ST_UInt}); | |||||
ExpectEq("%ho", false, {ST_Int}); | |||||
ExpectEq("%ho", true, {ST_UInt}); | |||||
ExpectEq("%hx", false, {ST_Int}); | |||||
ExpectEq("%hx", true, {ST_UInt}); | |||||
ExpectEq("%hX", false, {ST_Int}); | |||||
ExpectEq("%hX", true, {ST_UInt}); | |||||
ExpectEq("%u", false, {ST_Int}); | |||||
ExpectEq("%u", true, {ST_UInt}); | |||||
ExpectEq("%o", false, {ST_Int}); | |||||
ExpectEq("%o", true, {ST_UInt}); | |||||
ExpectEq("%x", false, {ST_Int}); | |||||
ExpectEq("%x", true, {ST_UInt}); | |||||
ExpectEq("%X", false, {ST_Int}); | |||||
ExpectEq("%X", true, {ST_UInt}); | |||||
ExpectEq("%lu", false, {ST_Long}); | |||||
ExpectEq("%lu", true, {ST_ULong}); | |||||
ExpectEq("%lo", false, {ST_Long}); | |||||
ExpectEq("%lo", true, {ST_ULong}); | |||||
ExpectEq("%lx", false, {ST_Long}); | |||||
ExpectEq("%lx", true, {ST_ULong}); | |||||
ExpectEq("%lX", false, {ST_Long}); | |||||
ExpectEq("%lX", true, {ST_ULong}); | |||||
ExpectEq("%llu", false, {ST_LongLong}); | |||||
ExpectEq("%llu", true, {ST_ULongLong}); | |||||
ExpectEq("%llo", false, {ST_LongLong}); | |||||
ExpectEq("%llo", true, {ST_ULongLong}); | |||||
ExpectEq("%llx", false, {ST_LongLong}); | |||||
ExpectEq("%llx", true, {ST_ULongLong}); | |||||
ExpectEq("%llX", false, {ST_LongLong}); | |||||
ExpectEq("%llX", true, {ST_ULongLong}); | |||||
ExpectEq("%ju", false, {ST_IntMax}); | |||||
ExpectEq("%ju", true, {ST_UIntMax}); | |||||
ExpectEq("%jo", false, {ST_IntMax}); | |||||
ExpectEq("%jo", true, {ST_UIntMax}); | |||||
ExpectEq("%jx", false, {ST_IntMax}); | |||||
ExpectEq("%jx", true, {ST_UIntMax}); | |||||
ExpectEq("%jX", false, {ST_IntMax}); | |||||
ExpectEq("%jX", true, {ST_UIntMax}); | |||||
ExpectEq("%zu", false, {ST_Size}); | |||||
ExpectEq("%zu", true, {ST_Size}); | |||||
ExpectEq("%zo", false, {ST_Size}); | |||||
ExpectEq("%zo", true, {ST_Size}); | |||||
ExpectEq("%zx", false, {ST_Size}); | |||||
ExpectEq("%zx", true, {ST_Size}); | |||||
ExpectEq("%zX", false, {ST_Size}); | |||||
ExpectEq("%zX", true, {ST_Size}); | |||||
ExpectEq("%tu", false, {ST_Ptrdiff}); | |||||
ExpectEq("%tu", true, {ST_Ptrdiff}); | |||||
ExpectEq("%to", false, {ST_Ptrdiff}); | |||||
ExpectEq("%to", true, {ST_Ptrdiff}); | |||||
ExpectEq("%tx", false, {ST_Ptrdiff}); | |||||
ExpectEq("%tx", true, {ST_Ptrdiff}); | |||||
ExpectEq("%tX", false, {ST_Ptrdiff}); | |||||
ExpectEq("%tX", true, {ST_Ptrdiff}); | |||||
ExpectEq("%Lu", {ST_Unknown}); | |||||
ExpectEq("%Lo", {ST_Unknown}); | |||||
ExpectEq("%Lx", {ST_Unknown}); | |||||
ExpectEq("%LX", {ST_Unknown}); | |||||
} | |||||
TEST(FormatReader, FloatSpecifiers) { | |||||
ExpectEq("%a", {ST_Double}); | |||||
ExpectEq("%e", {ST_Double}); | |||||
ExpectEq("%f", {ST_Double}); | |||||
ExpectEq("%g", {ST_Double}); | |||||
ExpectEq("%La", {ST_LongDouble}); | |||||
ExpectEq("%Le", {ST_LongDouble}); | |||||
ExpectEq("%Lf", {ST_LongDouble}); | |||||
ExpectEq("%Lg", {ST_LongDouble}); | |||||
ExpectEq("%ha", {ST_Unknown}); | |||||
ExpectEq("%he", {ST_Unknown}); | |||||
ExpectEq("%hf", {ST_Unknown}); | |||||
ExpectEq("%hg", {ST_Unknown}); | |||||
ExpectEq("%hha", {ST_Unknown}); | |||||
ExpectEq("%hhe", {ST_Unknown}); | |||||
ExpectEq("%hhf", {ST_Unknown}); | |||||
ExpectEq("%hhg", {ST_Unknown}); | |||||
ExpectEq("%la", {ST_Unknown}); | |||||
ExpectEq("%le", {ST_Unknown}); | |||||
ExpectEq("%lf", {ST_Unknown}); | |||||
ExpectEq("%lg", {ST_Unknown}); | |||||
ExpectEq("%lla", {ST_Unknown}); | |||||
ExpectEq("%lle", {ST_Unknown}); | |||||
ExpectEq("%llf", {ST_Unknown}); | |||||
ExpectEq("%llg", {ST_Unknown}); | |||||
} | |||||
TEST(FormatReader, CharSpecifiers) { | |||||
ExpectEq("%hhc", {ST_Unknown}); | |||||
ExpectEq("%hc", {ST_Unknown}); | |||||
ExpectEq("%c", {ST_Int}); | |||||
ExpectEq("%lc", {ST_WideChar}); | |||||
ExpectEq("%llc", {ST_Unknown}); | |||||
ExpectEq("%jc", {ST_Unknown}); | |||||
ExpectEq("%zc", {ST_Unknown}); | |||||
ExpectEq("%tc", {ST_Unknown}); | |||||
ExpectEq("%Lc", {ST_Unknown}); | |||||
} | |||||
TEST(FormatReader, StringSpecifiers) { | |||||
ExpectEq("%hhs", {ST_Unknown}); | |||||
ExpectEq("%hs", {ST_Unknown}); | |||||
ExpectEq("%s", {ST_CString}); | |||||
ExpectEq("%ls", {ST_WideCString}); | |||||
ExpectEq("%lls", {ST_Unknown}); | |||||
ExpectEq("%js", {ST_Unknown}); | |||||
ExpectEq("%zs", {ST_Unknown}); | |||||
ExpectEq("%ts", {ST_Unknown}); | |||||
ExpectEq("%Ls", {ST_Unknown}); | |||||
} | |||||
TEST(FormatReader, VoidPointerSpecifiers) { | |||||
ExpectEq("%hhp", {ST_Unknown}); | |||||
ExpectEq("%hp", {ST_Unknown}); | |||||
ExpectEq("%p", {ST_VoidPointer}); | |||||
ExpectEq("%lp", {ST_Unknown}); | |||||
ExpectEq("%llp", {ST_Unknown}); | |||||
ExpectEq("%jp", {ST_Unknown}); | |||||
ExpectEq("%zp", {ST_Unknown}); | |||||
ExpectEq("%tp", {ST_Unknown}); | |||||
ExpectEq("%Lp", {ST_Unknown}); | |||||
} | |||||
TEST(FormatReader, CountSpecifiers) { | |||||
ExpectEq("%hhn", {ST_Count_Char}); | |||||
ExpectEq("%hn", {ST_Count_Short}); | |||||
ExpectEq("%n", {ST_Count_Int}); | |||||
ExpectEq("%ln", {ST_Count_Long}); | |||||
ExpectEq("%lln", {ST_Count_LongLong}); | |||||
ExpectEq("%jn", {ST_Count_IntMax}); | |||||
ExpectEq("%zn", {ST_Count_Size}); | |||||
ExpectEq("%tn", {ST_Count_Ptrdiff}); | |||||
ExpectEq("%Ln", {ST_Unknown}); | |||||
} |