| 1 |
//===- llvm/unittest/FileCheck/FileCheckTest.cpp - FileCheck tests --------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "llvm/FileCheck/FileCheck.h" |
| 10 |
#include "../lib/FileCheck/FileCheckImpl.h" |
| 11 |
#include "llvm/Support/Regex.h" |
| 12 |
#include "llvm/Testing/Support/Error.h" |
| 13 |
#include "gtest/gtest.h" |
| 14 |
#include <tuple> |
| 15 |
#include <unordered_set> |
| 16 |
|
| 17 |
using namespace llvm; |
| 18 |
|
| 19 |
namespace { |
| 20 |
|
| 21 |
class FileCheckTest : public ::testing::Test {}; |
| 22 |
|
| 23 |
static StringRef bufferize(SourceMgr &SM, StringRef Str) { |
| 24 |
std::unique_ptr<MemoryBuffer> Buffer = |
| 25 |
MemoryBuffer::getMemBufferCopy(Str, "TestBuffer"); |
| 26 |
StringRef StrBufferRef = Buffer->getBuffer(); |
| 27 |
SM.AddNewSourceBuffer(std::move(Buffer), SMLoc()); |
| 28 |
return StrBufferRef; |
| 29 |
} |
| 30 |
|
| 31 |
static std::string toString(const std::unordered_set<std::string> &Set) { |
| 32 |
bool First = true; |
| 33 |
std::string Str; |
| 34 |
for (StringRef S : Set) { |
| 35 |
Str += Twine(First ? "{" + S : ", " + S).str(); |
| 36 |
First = false; |
| 37 |
} |
| 38 |
Str += '}'; |
| 39 |
return Str; |
| 40 |
} |
| 41 |
|
| 42 |
template <typename ErrorT> |
| 43 |
static void expectSameErrors(std::unordered_set<std::string> ExpectedMsgs, |
| 44 |
Error Err) { |
| 45 |
auto AnyErrorMsgMatch = [&ExpectedMsgs](std::string &&ErrorMsg) -> bool { |
| 46 |
for (auto ExpectedMsgItr = ExpectedMsgs.begin(), |
| 47 |
ExpectedMsgEnd = ExpectedMsgs.end(); |
| 48 |
ExpectedMsgItr != ExpectedMsgEnd; ++ExpectedMsgItr) { |
| 49 |
if (ErrorMsg.find(*ExpectedMsgItr) != std::string::npos) { |
| 50 |
ExpectedMsgs.erase(ExpectedMsgItr); |
| 51 |
return true; |
| 52 |
} |
| 53 |
} |
| 54 |
return false; |
| 55 |
}; |
| 56 |
|
| 57 |
Error RemainingErrors = std::move(Err); |
| 58 |
do { |
| 59 |
RemainingErrors = |
| 60 |
handleErrors(std::move(RemainingErrors), [&](const ErrorT &E) { |
| 61 |
EXPECT_TRUE(AnyErrorMsgMatch(E.message())) |
| 62 |
<< "Unexpected error message:" << std::endl |
| 63 |
<< E.message(); |
| 64 |
}); |
| 65 |
} while (RemainingErrors && !ExpectedMsgs.empty()); |
| 66 |
EXPECT_THAT_ERROR(std::move(RemainingErrors), Succeeded()); |
| 67 |
EXPECT_TRUE(ExpectedMsgs.empty()) |
| 68 |
<< "Error message(s) not found:" << std::endl |
| 69 |
<< toString(ExpectedMsgs); |
| 70 |
} |
| 71 |
|
| 72 |
template <typename ErrorT> |
| 73 |
static void expectError(StringRef ExpectedMsg, Error Err) { |
| 74 |
expectSameErrors<ErrorT>({ExpectedMsg.str()}, std::move(Err)); |
| 75 |
} |
| 76 |
|
| 77 |
static void expectDiagnosticError(StringRef ExpectedMsg, Error Err) { |
| 78 |
expectError<ErrorDiagnostic>(ExpectedMsg, std::move(Err)); |
| 79 |
} |
| 80 |
|
| 81 |
constexpr uint64_t MaxUint64 = std::numeric_limits<uint64_t>::max(); |
| 82 |
constexpr int64_t MaxInt64 = std::numeric_limits<int64_t>::max(); |
| 83 |
constexpr int64_t MinInt64 = std::numeric_limits<int64_t>::min(); |
| 84 |
constexpr uint64_t AbsoluteMinInt64 = |
| 85 |
static_cast<uint64_t>(-(MinInt64 + 1)) + 1; |
| 86 |
constexpr uint64_t AbsoluteMaxInt64 = static_cast<uint64_t>(MaxInt64); |
| 87 |
|
| 88 |
struct ExpressionFormatParameterisedFixture |
| 89 |
: public ::testing::TestWithParam< |
| 90 |
std::tuple<ExpressionFormat::Kind, unsigned, bool>> { |
| 91 |
bool AlternateForm; |
| 92 |
unsigned Precision; |
| 93 |
bool Signed; |
| 94 |
bool AllowHex; |
| 95 |
bool AllowUpperHex; |
| 96 |
ExpressionFormat Format; |
| 97 |
Regex WildcardRegex; |
| 98 |
|
| 99 |
StringRef TenStr; |
| 100 |
StringRef FifteenStr; |
| 101 |
std::string MaxUint64Str; |
| 102 |
std::string MaxInt64Str; |
| 103 |
std::string MinInt64Str; |
| 104 |
StringRef FirstInvalidCharDigits; |
| 105 |
StringRef AcceptedHexOnlyDigits; |
| 106 |
StringRef RefusedHexOnlyDigits; |
| 107 |
|
| 108 |
SourceMgr SM; |
| 109 |
|
| 110 |
void SetUp() override { |
| 111 |
ExpressionFormat::Kind Kind; |
| 112 |
std::tie(Kind, Precision, AlternateForm) = GetParam(); |
| 113 |
AllowHex = Kind == ExpressionFormat::Kind::HexLower || |
| 114 |
Kind == ExpressionFormat::Kind::HexUpper; |
| 115 |
AllowUpperHex = Kind == ExpressionFormat::Kind::HexUpper; |
| 116 |
Signed = Kind == ExpressionFormat::Kind::Signed; |
| 117 |
Format = ExpressionFormat(Kind, Precision, AlternateForm); |
| 118 |
|
| 119 |
if (!AllowHex) { |
| 120 |
MaxUint64Str = std::to_string(MaxUint64); |
| 121 |
MaxInt64Str = std::to_string(MaxInt64); |
| 122 |
MinInt64Str = std::to_string(MinInt64); |
| 123 |
TenStr = "10"; |
| 124 |
FifteenStr = "15"; |
| 125 |
FirstInvalidCharDigits = "aA"; |
| 126 |
AcceptedHexOnlyDigits = RefusedHexOnlyDigits = "N/A"; |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
MaxUint64Str = AllowUpperHex ? "FFFFFFFFFFFFFFFF" : "ffffffffffffffff"; |
| 131 |
MaxInt64Str = AllowUpperHex ? "7FFFFFFFFFFFFFFF" : "7fffffffffffffff"; |
| 132 |
TenStr = AllowUpperHex ? "A" : "a"; |
| 133 |
FifteenStr = AllowUpperHex ? "F" : "f"; |
| 134 |
AcceptedHexOnlyDigits = AllowUpperHex ? "ABCDEF" : "abcdef"; |
| 135 |
RefusedHexOnlyDigits = AllowUpperHex ? "abcdef" : "ABCDEF"; |
| 136 |
MinInt64Str = "N/A"; |
| 137 |
FirstInvalidCharDigits = "gG"; |
| 138 |
} |
| 139 |
|
| 140 |
void checkWildcardRegexMatch(StringRef Input, |
| 141 |
unsigned TrailExtendTo = 0) const { |
| 142 |
ASSERT_TRUE(TrailExtendTo == 0 || AllowHex); |
| 143 |
SmallVector<StringRef, 4> Matches; |
| 144 |
std::string ExtendedInput = Input.str(); |
| 145 |
size_t PrefixSize = AlternateForm ? 2 : 0; |
| 146 |
if (TrailExtendTo > Input.size() - PrefixSize) { |
| 147 |
size_t ExtensionSize = PrefixSize + TrailExtendTo - Input.size(); |
| 148 |
ExtendedInput.append(ExtensionSize, Input[PrefixSize]); |
| 149 |
} |
| 150 |
ASSERT_TRUE(WildcardRegex.match(ExtendedInput, &Matches)) |
| 151 |
<< "Wildcard regex does not match " << ExtendedInput; |
| 152 |
EXPECT_EQ(Matches[0], ExtendedInput); |
| 153 |
} |
| 154 |
|
| 155 |
void checkWildcardRegexMatchFailure(StringRef Input) const { |
| 156 |
EXPECT_FALSE(WildcardRegex.match(Input)); |
| 157 |
} |
| 158 |
|
| 159 |
std::string addBasePrefix(StringRef Num) const { |
| 160 |
StringRef Prefix = AlternateForm ? "0x" : ""; |
| 161 |
return (Twine(Prefix) + Twine(Num)).str(); |
| 162 |
} |
| 163 |
|
| 164 |
void checkPerCharWildcardRegexMatchFailure(StringRef Chars) const { |
| 165 |
for (auto C : Chars) { |
| 166 |
std::string Str = addBasePrefix(StringRef(&C, 1)); |
| 167 |
EXPECT_FALSE(WildcardRegex.match(Str)); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
std::string padWithLeadingZeros(StringRef NumStr) const { |
| 172 |
bool Negative = NumStr.startswith("-"); |
| 173 |
if (NumStr.size() - unsigned(Negative) >= Precision) |
| 174 |
return NumStr.str(); |
| 175 |
|
| 176 |
std::string PaddedStr; |
| 177 |
if (Negative) { |
| 178 |
PaddedStr = "-"; |
| 179 |
NumStr = NumStr.drop_front(); |
| 180 |
} |
| 181 |
PaddedStr.append(Precision - NumStr.size(), '0'); |
| 182 |
PaddedStr.append(NumStr.str()); |
| 183 |
return PaddedStr; |
| 184 |
} |
| 185 |
|
| 186 |
template <class T> void checkMatchingString(T Val, StringRef ExpectedStr) { |
| 187 |
Expected<std::string> MatchingString = |
| 188 |
Format.getMatchingString(ExpressionValue(Val)); |
| 189 |
ASSERT_THAT_EXPECTED(MatchingString, Succeeded()) |
| 190 |
<< "No matching string for " << Val; |
| 191 |
EXPECT_EQ(*MatchingString, ExpectedStr); |
| 192 |
} |
| 193 |
|
| 194 |
template <class T> void checkMatchingStringFailure(T Val) { |
| 195 |
Expected<std::string> MatchingString = |
| 196 |
Format.getMatchingString(ExpressionValue(Val)); |
| 197 |
// Error message tested in ExpressionValue unit tests. |
| 198 |
EXPECT_THAT_EXPECTED(MatchingString, Failed()); |
| 199 |
} |
| 200 |
|
| 201 |
Expected<ExpressionValue> getValueFromStringReprFailure(StringRef Str) { |
| 202 |
StringRef BufferizedStr = bufferize(SM, Str); |
| 203 |
return Format.valueFromStringRepr(BufferizedStr, SM); |
| 204 |
} |
| 205 |
|
| 206 |
template <class T> |
| 207 |
void checkValueFromStringRepr(StringRef Str, T ExpectedVal) { |
| 208 |
Expected<ExpressionValue> ResultValue = getValueFromStringReprFailure(Str); |
| 209 |
ASSERT_THAT_EXPECTED(ResultValue, Succeeded()) |
| 210 |
<< "Failed to get value from " << Str; |
| 211 |
ASSERT_EQ(ResultValue->isNegative(), ExpectedVal < 0) |
| 212 |
<< "Value for " << Str << " is not " << ExpectedVal; |
| 213 |
if (ResultValue->isNegative()) |
| 214 |
EXPECT_EQ(cantFail(ResultValue->getSignedValue()), |
| 215 |
static_cast<int64_t>(ExpectedVal)); |
| 216 |
else |
| 217 |
EXPECT_EQ(cantFail(ResultValue->getUnsignedValue()), |
| 218 |
static_cast<uint64_t>(ExpectedVal)); |
| 219 |
} |
| 220 |
|
| 221 |
void checkValueFromStringReprFailure( |
| 222 |
StringRef Str, StringRef ErrorStr = "unable to represent numeric value") { |
| 223 |
Expected<ExpressionValue> ResultValue = getValueFromStringReprFailure(Str); |
| 224 |
expectDiagnosticError(ErrorStr, ResultValue.takeError()); |
| 225 |
} |
| 226 |
}; |
| 227 |
|
| 228 |
TEST_P(ExpressionFormatParameterisedFixture, FormatGetWildcardRegex) { |
| 229 |
// Wildcard regex is valid. |
| 230 |
Expected<std::string> WildcardPattern = Format.getWildcardRegex(); |
| 231 |
ASSERT_THAT_EXPECTED(WildcardPattern, Succeeded()); |
| 232 |
WildcardRegex = Regex((Twine("^") + *WildcardPattern + "$").str()); |
| 233 |
ASSERT_TRUE(WildcardRegex.isValid()); |
| 234 |
|
| 235 |
// Does not match empty string. |
| 236 |
checkWildcardRegexMatchFailure(""); |
| 237 |
|
| 238 |
// Matches all decimal digits, matches several of them and match 0x prefix |
| 239 |
// if and only if AlternateForm is true. |
| 240 |
StringRef LongNumber = "12345678901234567890"; |
| 241 |
StringRef PrefixedLongNumber = "0x12345678901234567890"; |
| 242 |
if (AlternateForm) { |
| 243 |
checkWildcardRegexMatch(PrefixedLongNumber); |
| 244 |
checkWildcardRegexMatchFailure(LongNumber); |
| 245 |
} else { |
| 246 |
checkWildcardRegexMatch(LongNumber); |
| 247 |
checkWildcardRegexMatchFailure(PrefixedLongNumber); |
| 248 |
} |
| 249 |
|
| 250 |
// Matches negative digits. |
| 251 |
LongNumber = "-12345678901234567890"; |
| 252 |
if (Signed) |
| 253 |
checkWildcardRegexMatch(LongNumber); |
| 254 |
else |
| 255 |
checkWildcardRegexMatchFailure(LongNumber); |
| 256 |
|
| 257 |
// Check non digits or digits with wrong casing are not matched. |
| 258 |
std::string LongNumberStr; |
| 259 |
if (AllowHex) { |
| 260 |
LongNumberStr = addBasePrefix(AcceptedHexOnlyDigits); |
| 261 |
checkWildcardRegexMatch(LongNumberStr, 16); |
| 262 |
checkPerCharWildcardRegexMatchFailure(RefusedHexOnlyDigits); |
| 263 |
} |
| 264 |
checkPerCharWildcardRegexMatchFailure(FirstInvalidCharDigits); |
| 265 |
|
| 266 |
// Check leading zeros are only accepted if number of digits is less than the |
| 267 |
// precision. |
| 268 |
LongNumber = "01234567890123456789"; |
| 269 |
if (Precision) { |
| 270 |
LongNumberStr = addBasePrefix(LongNumber.take_front(Precision)); |
| 271 |
checkWildcardRegexMatch(LongNumberStr); |
| 272 |
LongNumberStr = addBasePrefix(LongNumber.take_front(Precision - 1)); |
| 273 |
checkWildcardRegexMatchFailure(LongNumberStr); |
| 274 |
if (Precision < LongNumber.size()) { |
| 275 |
LongNumberStr = addBasePrefix(LongNumber.take_front(Precision + 1)); |
| 276 |
checkWildcardRegexMatchFailure(LongNumberStr); |
| 277 |
} |
| 278 |
} else { |
| 279 |
LongNumberStr = addBasePrefix(LongNumber); |
| 280 |
checkWildcardRegexMatch(LongNumberStr); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
TEST_P(ExpressionFormatParameterisedFixture, FormatGetMatchingString) { |
| 285 |
checkMatchingString(0, addBasePrefix(padWithLeadingZeros("0"))); |
| 286 |
checkMatchingString(9, addBasePrefix(padWithLeadingZeros("9"))); |
| 287 |
|
| 288 |
if (Signed) { |
| 289 |
checkMatchingString(-5, padWithLeadingZeros("-5")); |
| 290 |
checkMatchingStringFailure(MaxUint64); |
| 291 |
checkMatchingString(MaxInt64, padWithLeadingZeros(MaxInt64Str)); |
| 292 |
checkMatchingString(MinInt64, padWithLeadingZeros(MinInt64Str)); |
| 293 |
} else { |
| 294 |
checkMatchingStringFailure(-5); |
| 295 |
checkMatchingString(MaxUint64, |
| 296 |
addBasePrefix(padWithLeadingZeros(MaxUint64Str))); |
| 297 |
checkMatchingString(MaxInt64, |
| 298 |
addBasePrefix(padWithLeadingZeros(MaxInt64Str))); |
| 299 |
checkMatchingStringFailure(MinInt64); |
| 300 |
} |
| 301 |
|
| 302 |
checkMatchingString(10, addBasePrefix(padWithLeadingZeros(TenStr))); |
| 303 |
checkMatchingString(15, addBasePrefix(padWithLeadingZeros(FifteenStr))); |
| 304 |
} |
| 305 |
|
| 306 |
TEST_P(ExpressionFormatParameterisedFixture, FormatValueFromStringRepr) { |
| 307 |
checkValueFromStringRepr(addBasePrefix("0"), 0); |
| 308 |
checkValueFromStringRepr(addBasePrefix("9"), 9); |
| 309 |
|
| 310 |
if (Signed) { |
| 311 |
checkValueFromStringRepr("-5", -5); |
| 312 |
checkValueFromStringReprFailure(MaxUint64Str); |
| 313 |
} else { |
| 314 |
checkValueFromStringReprFailure("-" + addBasePrefix("5")); |
| 315 |
checkValueFromStringRepr(addBasePrefix(MaxUint64Str), MaxUint64); |
| 316 |
} |
| 317 |
|
| 318 |
checkValueFromStringRepr(addBasePrefix(TenStr), 10); |
| 319 |
checkValueFromStringRepr(addBasePrefix(FifteenStr), 15); |
| 320 |
|
| 321 |
// Wrong casing is not tested because valueFromStringRepr() relies on |
| 322 |
// StringRef's getAsInteger() which does not allow to restrict casing. |
| 323 |
checkValueFromStringReprFailure(addBasePrefix("G")); |
| 324 |
|
| 325 |
if (AlternateForm) |
| 326 |
checkValueFromStringReprFailure("9", "missing alternate form prefix"); |
| 327 |
} |
| 328 |
|
| 329 |
TEST_P(ExpressionFormatParameterisedFixture, FormatBoolOperator) { |
| 330 |
EXPECT_TRUE(bool(Format)); |
| 331 |
} |
| 332 |
|
| 333 |
INSTANTIATE_TEST_SUITE_P( |
| 334 |
AllowedExplicitExpressionFormat, ExpressionFormatParameterisedFixture, |
| 335 |
::testing::Values( |
| 336 |
std::make_tuple(ExpressionFormat::Kind::Unsigned, 0, false), |
| 337 |
std::make_tuple(ExpressionFormat::Kind::Signed, 0, false), |
| 338 |
std::make_tuple(ExpressionFormat::Kind::HexLower, 0, false), |
| 339 |
std::make_tuple(ExpressionFormat::Kind::HexLower, 0, true), |
| 340 |
std::make_tuple(ExpressionFormat::Kind::HexUpper, 0, false), |
| 341 |
std::make_tuple(ExpressionFormat::Kind::HexUpper, 0, true), |
| 342 |
|
| 343 |
std::make_tuple(ExpressionFormat::Kind::Unsigned, 1, false), |
| 344 |
std::make_tuple(ExpressionFormat::Kind::Signed, 1, false), |
| 345 |
std::make_tuple(ExpressionFormat::Kind::HexLower, 1, false), |
| 346 |
std::make_tuple(ExpressionFormat::Kind::HexLower, 1, true), |
| 347 |
std::make_tuple(ExpressionFormat::Kind::HexUpper, 1, false), |
| 348 |
std::make_tuple(ExpressionFormat::Kind::HexUpper, 1, true), |
| 349 |
|
| 350 |
std::make_tuple(ExpressionFormat::Kind::Unsigned, 16, false), |
| 351 |
std::make_tuple(ExpressionFormat::Kind::Signed, 16, false), |
| 352 |
std::make_tuple(ExpressionFormat::Kind::HexLower, 16, false), |
| 353 |
std::make_tuple(ExpressionFormat::Kind::HexLower, 16, true), |
| 354 |
std::make_tuple(ExpressionFormat::Kind::HexUpper, 16, false), |
| 355 |
std::make_tuple(ExpressionFormat::Kind::HexUpper, 16, true), |
| 356 |
|
| 357 |
std::make_tuple(ExpressionFormat::Kind::Unsigned, 20, false), |
| 358 |
std::make_tuple(ExpressionFormat::Kind::Signed, 20, false))); |
| 359 |
|
| 360 |
TEST_F(FileCheckTest, NoFormatProperties) { |
| 361 |
ExpressionFormat NoFormat(ExpressionFormat::Kind::NoFormat); |
| 362 |
expectError<StringError>("trying to match value with invalid format", |
| 363 |
NoFormat.getWildcardRegex().takeError()); |
| 364 |
expectError<StringError>( |
| 365 |
"trying to match value with invalid format", |
| 366 |
NoFormat.getMatchingString(ExpressionValue(18u)).takeError()); |
| 367 |
EXPECT_FALSE(bool(NoFormat)); |
| 368 |
} |
| 369 |
|
| 370 |
TEST_F(FileCheckTest, FormatEqualityOperators) { |
| 371 |
ExpressionFormat UnsignedFormat(ExpressionFormat::Kind::Unsigned); |
| 372 |
ExpressionFormat UnsignedFormat2(ExpressionFormat::Kind::Unsigned); |
| 373 |
EXPECT_TRUE(UnsignedFormat == UnsignedFormat2); |
| 374 |
EXPECT_FALSE(UnsignedFormat != UnsignedFormat2); |
| 375 |
|
| 376 |
ExpressionFormat HexLowerFormat(ExpressionFormat::Kind::HexLower); |
| 377 |
EXPECT_FALSE(UnsignedFormat == HexLowerFormat); |
| 378 |
EXPECT_TRUE(UnsignedFormat != HexLowerFormat); |
| 379 |
|
| 380 |
ExpressionFormat NoFormat(ExpressionFormat::Kind::NoFormat); |
| 381 |
ExpressionFormat NoFormat2(ExpressionFormat::Kind::NoFormat); |
| 382 |
EXPECT_FALSE(NoFormat == NoFormat2); |
| 383 |
EXPECT_TRUE(NoFormat != NoFormat2); |
| 384 |
} |
| 385 |
|
| 386 |
TEST_F(FileCheckTest, FormatKindEqualityOperators) { |
| 387 |
ExpressionFormat UnsignedFormat(ExpressionFormat::Kind::Unsigned); |
| 388 |
EXPECT_TRUE(UnsignedFormat == ExpressionFormat::Kind::Unsigned); |
| 389 |
EXPECT_FALSE(UnsignedFormat != ExpressionFormat::Kind::Unsigned); |
| 390 |
EXPECT_FALSE(UnsignedFormat == ExpressionFormat::Kind::HexLower); |
| 391 |
EXPECT_TRUE(UnsignedFormat != ExpressionFormat::Kind::HexLower); |
| 392 |
ExpressionFormat NoFormat(ExpressionFormat::Kind::NoFormat); |
| 393 |
EXPECT_TRUE(NoFormat == ExpressionFormat::Kind::NoFormat); |
| 394 |
EXPECT_FALSE(NoFormat != ExpressionFormat::Kind::NoFormat); |
| 395 |
} |
| 396 |
|
| 397 |
template <class T1, class T2> |
| 398 |
static Expected<ExpressionValue> doValueOperation(binop_eval_t Operation, |
| 399 |
T1 LeftValue, T2 RightValue) { |
| 400 |
ExpressionValue LeftOperand(LeftValue); |
| 401 |
ExpressionValue RightOperand(RightValue); |
| 402 |
return Operation(LeftOperand, RightOperand); |
| 403 |
} |
| 404 |
|
| 405 |
template <class T> |
| 406 |
static void expectValueEqual(ExpressionValue ActualValue, T ExpectedValue) { |
| 407 |
EXPECT_EQ(ExpectedValue < 0, ActualValue.isNegative()); |
| 408 |
if (ExpectedValue < 0) { |
| 409 |
Expected<int64_t> SignedActualValue = ActualValue.getSignedValue(); |
| 410 |
ASSERT_THAT_EXPECTED(SignedActualValue, Succeeded()); |
| 411 |
EXPECT_EQ(*SignedActualValue, static_cast<int64_t>(ExpectedValue)); |
| 412 |
} else { |
| 413 |
Expected<uint64_t> UnsignedActualValue = ActualValue.getUnsignedValue(); |
| 414 |
ASSERT_THAT_EXPECTED(UnsignedActualValue, Succeeded()); |
| 415 |
EXPECT_EQ(*UnsignedActualValue, static_cast<uint64_t>(ExpectedValue)); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
template <class T1, class T2, class TR> |
| 420 |
static void expectOperationValueResult(binop_eval_t Operation, T1 LeftValue, |
| 421 |
T2 RightValue, TR ResultValue) { |
| 422 |
Expected<ExpressionValue> OperationResult = |
| 423 |
doValueOperation(Operation, LeftValue, RightValue); |
| 424 |
ASSERT_THAT_EXPECTED(OperationResult, Succeeded()); |
| 425 |
expectValueEqual(*OperationResult, ResultValue); |
| 426 |
} |
| 427 |
|
| 428 |
template <class T1, class T2> |
| 429 |
static void expectOperationValueResult(binop_eval_t Operation, T1 LeftValue, |
| 430 |
T2 RightValue) { |
| 431 |
expectError<OverflowError>( |
| 432 |
"overflow error", |
| 433 |
doValueOperation(Operation, LeftValue, RightValue).takeError()); |
| 434 |
} |
| 435 |
|
| 436 |
TEST_F(FileCheckTest, ExpressionValueGetUnsigned) { |
| 437 |
// Test positive value. |
| 438 |
Expected<uint64_t> UnsignedValue = ExpressionValue(10).getUnsignedValue(); |
| 439 |
ASSERT_THAT_EXPECTED(UnsignedValue, Succeeded()); |
| 440 |
EXPECT_EQ(*UnsignedValue, 10U); |
| 441 |
|
| 442 |
// Test 0. |
| 443 |
UnsignedValue = ExpressionValue(0).getUnsignedValue(); |
| 444 |
ASSERT_THAT_EXPECTED(UnsignedValue, Succeeded()); |
| 445 |
EXPECT_EQ(*UnsignedValue, 0U); |
| 446 |
|
| 447 |
// Test max positive value. |
| 448 |
UnsignedValue = ExpressionValue(MaxUint64).getUnsignedValue(); |
| 449 |
ASSERT_THAT_EXPECTED(UnsignedValue, Succeeded()); |
| 450 |
EXPECT_EQ(*UnsignedValue, MaxUint64); |
| 451 |
|
| 452 |
// Test failure with negative value. |
| 453 |
expectError<OverflowError>( |
| 454 |
"overflow error", ExpressionValue(-1).getUnsignedValue().takeError()); |
| 455 |
|
| 456 |
// Test failure with min negative value. |
| 457 |
expectError<OverflowError>( |
| 458 |
"overflow error", |
| 459 |
ExpressionValue(MinInt64).getUnsignedValue().takeError()); |
| 460 |
} |
| 461 |
|
| 462 |
TEST_F(FileCheckTest, ExpressionValueGetSigned) { |
| 463 |
// Test positive value. |
| 464 |
Expected<int64_t> SignedValue = ExpressionValue(10).getSignedValue(); |
| 465 |
ASSERT_THAT_EXPECTED(SignedValue, Succeeded()); |
| 466 |
EXPECT_EQ(*SignedValue, 10); |
| 467 |
|
| 468 |
// Test 0. |
| 469 |
SignedValue = ExpressionValue(0).getSignedValue(); |
| 470 |
ASSERT_THAT_EXPECTED(SignedValue, Succeeded()); |
| 471 |
EXPECT_EQ(*SignedValue, 0); |
| 472 |
|
| 473 |
// Test max int64_t. |
| 474 |
SignedValue = ExpressionValue(MaxInt64).getSignedValue(); |
| 475 |
ASSERT_THAT_EXPECTED(SignedValue, Succeeded()); |
| 476 |
EXPECT_EQ(*SignedValue, MaxInt64); |
| 477 |
|
| 478 |
// Test failure with too big positive value. |
| 479 |
expectError<OverflowError>( |
| 480 |
"overflow error", ExpressionValue(static_cast<uint64_t>(MaxInt64) + 1) |
| 481 |
.getSignedValue() |
| 482 |
.takeError()); |
| 483 |
|
| 484 |
// Test failure with max uint64_t. |
| 485 |
expectError<OverflowError>( |
| 486 |
"overflow error", |
| 487 |
ExpressionValue(MaxUint64).getSignedValue().takeError()); |
| 488 |
|
| 489 |
// Test negative value. |
| 490 |
SignedValue = ExpressionValue(-10).getSignedValue(); |
| 491 |
ASSERT_THAT_EXPECTED(SignedValue, Succeeded()); |
| 492 |
EXPECT_EQ(*SignedValue, -10); |
| 493 |
|
| 494 |
// Test min int64_t. |
| 495 |
SignedValue = ExpressionValue(MinInt64).getSignedValue(); |
| 496 |
ASSERT_THAT_EXPECTED(SignedValue, Succeeded()); |
| 497 |
EXPECT_EQ(*SignedValue, MinInt64); |
| 498 |
} |
| 499 |
|
| 500 |
TEST_F(FileCheckTest, ExpressionValueAbsolute) { |
| 501 |
// Test positive value. |
| 502 |
expectValueEqual(ExpressionValue(10).getAbsolute(), 10); |
| 503 |
|
| 504 |
// Test 0. |
| 505 |
expectValueEqual(ExpressionValue(0).getAbsolute(), 0); |
| 506 |
|
| 507 |
// Test max uint64_t. |
| 508 |
expectValueEqual(ExpressionValue(MaxUint64).getAbsolute(), MaxUint64); |
| 509 |
|
| 510 |
// Test negative value. |
| 511 |
expectValueEqual(ExpressionValue(-10).getAbsolute(), 10); |
| 512 |
|
| 513 |
// Test absence of overflow on min int64_t. |
| 514 |
expectValueEqual(ExpressionValue(MinInt64).getAbsolute(), |
| 515 |
static_cast<uint64_t>(-(MinInt64 + 10)) + 10); |
| 516 |
} |
| 517 |
|
| 518 |
TEST_F(FileCheckTest, ExpressionValueAddition) { |
| 519 |
// Test both negative values. |
| 520 |
expectOperationValueResult(operator+, -10, -10, -20); |
| 521 |
|
| 522 |
// Test both negative values with underflow. |
| 523 |
expectOperationValueResult(operator+, MinInt64, -1); |
| 524 |
expectOperationValueResult(operator+, MinInt64, MinInt64); |
| 525 |
|
| 526 |
// Test negative and positive value. |
| 527 |
expectOperationValueResult(operator+, -10, 10, 0); |
| 528 |
expectOperationValueResult(operator+, -10, 11, 1); |
| 529 |
expectOperationValueResult(operator+, -11, 10, -1); |
| 530 |
|
| 531 |
// Test positive and negative value. |
| 532 |
expectOperationValueResult(operator+, 10, -10, 0); |
| 533 |
expectOperationValueResult(operator+, 10, -11, -1); |
| 534 |
expectOperationValueResult(operator+, 11, -10, 1); |
| 535 |
|
| 536 |
// Test both positive values. |
| 537 |
expectOperationValueResult(operator+, 10, 10, 20); |
| 538 |
|
| 539 |
// Test both positive values with overflow. |
| 540 |
expectOperationValueResult(operator+, MaxUint64, 1); |
| 541 |
expectOperationValueResult(operator+, MaxUint64, MaxUint64); |
| 542 |
} |
| 543 |
|
| 544 |
TEST_F(FileCheckTest, ExpressionValueSubtraction) { |
| 545 |
// Test negative value and value bigger than int64_t max. |
| 546 |
expectOperationValueResult(operator-, -10, MaxUint64); |
| 547 |
|
| 548 |
// Test negative and positive value with underflow. |
| 549 |
expectOperationValueResult(operator-, MinInt64, 1); |
| 550 |
|
| 551 |
// Test negative and positive value. |
| 552 |
expectOperationValueResult(operator-, -10, 10, -20); |
| 553 |
|
| 554 |
// Test both negative values. |
| 555 |
expectOperationValueResult(operator-, -10, -10, 0); |
| 556 |
expectOperationValueResult(operator-, -11, -10, -1); |
| 557 |
expectOperationValueResult(operator-, -10, -11, 1); |
| 558 |
|
| 559 |
// Test positive and negative values. |
| 560 |
expectOperationValueResult(operator-, 10, -10, 20); |
| 561 |
|
| 562 |
// Test both positive values with result positive. |
| 563 |
expectOperationValueResult(operator-, 10, 5, 5); |
| 564 |
|
| 565 |
// Test both positive values with underflow. |
| 566 |
expectOperationValueResult(operator-, 0, MaxUint64); |
| 567 |
expectOperationValueResult(operator-, 0, |
| 568 |
static_cast<uint64_t>(-(MinInt64 + 10)) + 11); |
| 569 |
|
| 570 |
// Test both positive values with result < -(max int64_t) |
| 571 |
expectOperationValueResult(operator-, 10, |
| 572 |
static_cast<uint64_t>(MaxInt64) + 11, |
| 573 |
-MaxInt64 - 1); |
| 574 |
|
| 575 |
// Test both positive values with 0 > result > -(max int64_t) |
| 576 |
expectOperationValueResult(operator-, 10, 11, -1); |
| 577 |
} |
| 578 |
|
| 579 |
TEST_F(FileCheckTest, ExpressionValueMultiplication) { |
| 580 |
// Test mixed signed values. |
| 581 |
expectOperationValueResult(operator*, -3, 10, -30); |
| 582 |
expectOperationValueResult(operator*, 2, -17, -34); |
| 583 |
expectOperationValueResult(operator*, 0, MinInt64, 0); |
| 584 |
expectOperationValueResult(operator*, MinInt64, 1, MinInt64); |
| 585 |
expectOperationValueResult(operator*, 1, MinInt64, MinInt64); |
| 586 |
expectOperationValueResult(operator*, MaxInt64, -1, -MaxInt64); |
| 587 |
expectOperationValueResult(operator*, -1, MaxInt64, -MaxInt64); |
| 588 |
|
| 589 |
// Test both negative values. |
| 590 |
expectOperationValueResult(operator*, -3, -10, 30); |
| 591 |
expectOperationValueResult(operator*, -2, -17, 34); |
| 592 |
expectOperationValueResult(operator*, MinInt64, -1, AbsoluteMinInt64); |
| 593 |
|
| 594 |
// Test both positive values. |
| 595 |
expectOperationValueResult(operator*, 3, 10, 30); |
| 596 |
expectOperationValueResult(operator*, 2, 17, 34); |
| 597 |
expectOperationValueResult(operator*, 0, MaxUint64, 0); |
| 598 |
|
| 599 |
// Test negative results that underflow. |
| 600 |
expectOperationValueResult(operator*, -10, MaxInt64); |
| 601 |
expectOperationValueResult(operator*, MaxInt64, -10); |
| 602 |
expectOperationValueResult(operator*, 10, MinInt64); |
| 603 |
expectOperationValueResult(operator*, MinInt64, 10); |
| 604 |
expectOperationValueResult(operator*, -1, MaxUint64); |
| 605 |
expectOperationValueResult(operator*, MaxUint64, -1); |
| 606 |
expectOperationValueResult(operator*, -1, AbsoluteMaxInt64 + 2); |
| 607 |
expectOperationValueResult(operator*, AbsoluteMaxInt64 + 2, -1); |
| 608 |
|
| 609 |
// Test positive results that overflow. |
| 610 |
expectOperationValueResult(operator*, 10, MaxUint64); |
| 611 |
expectOperationValueResult(operator*, MaxUint64, 10); |
| 612 |
expectOperationValueResult(operator*, MinInt64, -10); |
| 613 |
expectOperationValueResult(operator*, -10, MinInt64); |
| 614 |
} |
| 615 |
|
| 616 |
TEST_F(FileCheckTest, ExpressionValueDivision) { |
| 617 |
// Test mixed signed values. |
| 618 |
expectOperationValueResult(operator/, -30, 10, -3); |
| 619 |
expectOperationValueResult(operator/, 34, -17, -2); |
| 620 |
expectOperationValueResult(operator/, 0, -10, 0); |
| 621 |
expectOperationValueResult(operator/, MinInt64, 1, MinInt64); |
| 622 |
expectOperationValueResult(operator/, MaxInt64, -1, -MaxInt64); |
| 623 |
expectOperationValueResult(operator/, -MaxInt64, 1, -MaxInt64); |
| 624 |
|
| 625 |
// Test both negative values. |
| 626 |
expectOperationValueResult(operator/, -30, -10, 3); |
| 627 |
expectOperationValueResult(operator/, -34, -17, 2); |
| 628 |
|
| 629 |
// Test both positive values. |
| 630 |
expectOperationValueResult(operator/, 30, 10, 3); |
| 631 |
expectOperationValueResult(operator/, 34, 17, 2); |
| 632 |
expectOperationValueResult(operator/, 0, 10, 0); |
| 633 |
|
| 634 |
// Test divide by zero. |
| 635 |
expectOperationValueResult(operator/, -10, 0); |
| 636 |
expectOperationValueResult(operator/, 10, 0); |
| 637 |
expectOperationValueResult(operator/, 0, 0); |
| 638 |
|
| 639 |
// Test negative result that underflows. |
| 640 |
expectOperationValueResult(operator/, MaxUint64, -1); |
| 641 |
expectOperationValueResult(operator/, AbsoluteMaxInt64 + 2, -1); |
| 642 |
} |
| 643 |
|
| 644 |
TEST_F(FileCheckTest, ExpressionValueEquality) { |
| 645 |
// Test negative and positive value. |
| 646 |
EXPECT_FALSE(ExpressionValue(5) == ExpressionValue(-3)); |
| 647 |
EXPECT_TRUE(ExpressionValue(5) != ExpressionValue(-3)); |
| 648 |
EXPECT_FALSE(ExpressionValue(-2) == ExpressionValue(6)); |
| 649 |
EXPECT_TRUE(ExpressionValue(-2) != ExpressionValue(6)); |
| 650 |
EXPECT_FALSE(ExpressionValue(-7) == ExpressionValue(7)); |
| 651 |
EXPECT_TRUE(ExpressionValue(-7) != ExpressionValue(7)); |
| 652 |
EXPECT_FALSE(ExpressionValue(4) == ExpressionValue(-4)); |
| 653 |
EXPECT_TRUE(ExpressionValue(4) != ExpressionValue(-4)); |
| 654 |
EXPECT_FALSE(ExpressionValue(MaxUint64) == ExpressionValue(-1)); |
| 655 |
EXPECT_TRUE(ExpressionValue(MaxUint64) != ExpressionValue(-1)); |
| 656 |
|
| 657 |
// Test both negative values. |
| 658 |
EXPECT_FALSE(ExpressionValue(-2) == ExpressionValue(-7)); |
| 659 |
EXPECT_TRUE(ExpressionValue(-2) != ExpressionValue(-7)); |
| 660 |
EXPECT_TRUE(ExpressionValue(-3) == ExpressionValue(-3)); |
| 661 |
EXPECT_FALSE(ExpressionValue(-3) != ExpressionValue(-3)); |
| 662 |
EXPECT_FALSE(ExpressionValue(MinInt64) == ExpressionValue(-1)); |
| 663 |
EXPECT_TRUE(ExpressionValue(MinInt64) != ExpressionValue(-1)); |
| 664 |
EXPECT_FALSE(ExpressionValue(MinInt64) == ExpressionValue(-0)); |
| 665 |
EXPECT_TRUE(ExpressionValue(MinInt64) != ExpressionValue(-0)); |
| 666 |
|
| 667 |
// Test both positive values. |
| 668 |
EXPECT_FALSE(ExpressionValue(8) == ExpressionValue(9)); |
| 669 |
EXPECT_TRUE(ExpressionValue(8) != ExpressionValue(9)); |
| 670 |
EXPECT_TRUE(ExpressionValue(1) == ExpressionValue(1)); |
| 671 |
EXPECT_FALSE(ExpressionValue(1) != ExpressionValue(1)); |
| 672 |
|
| 673 |
// Check the signedness of zero doesn't affect equality. |
| 674 |
EXPECT_TRUE(ExpressionValue(0) == ExpressionValue(0)); |
| 675 |
EXPECT_FALSE(ExpressionValue(0) != ExpressionValue(0)); |
| 676 |
EXPECT_TRUE(ExpressionValue(0) == ExpressionValue(-0)); |
| 677 |
EXPECT_FALSE(ExpressionValue(0) != ExpressionValue(-0)); |
| 678 |
EXPECT_TRUE(ExpressionValue(-0) == ExpressionValue(0)); |
| 679 |
EXPECT_FALSE(ExpressionValue(-0) != ExpressionValue(0)); |
| 680 |
EXPECT_TRUE(ExpressionValue(-0) == ExpressionValue(-0)); |
| 681 |
EXPECT_FALSE(ExpressionValue(-0) != ExpressionValue(-0)); |
| 682 |
} |
| 683 |
|
| 684 |
TEST_F(FileCheckTest, Literal) { |
| 685 |
SourceMgr SM; |
| 686 |
|
| 687 |
// Eval returns the literal's value. |
| 688 |
ExpressionLiteral Ten(bufferize(SM, "10"), 10u); |
| 689 |
Expected<ExpressionValue> Value = Ten.eval(); |
| 690 |
ASSERT_THAT_EXPECTED(Value, Succeeded()); |
| 691 |
EXPECT_EQ(10, cantFail(Value->getSignedValue())); |
| 692 |
Expected<ExpressionFormat> ImplicitFormat = Ten.getImplicitFormat(SM); |
| 693 |
ASSERT_THAT_EXPECTED(ImplicitFormat, Succeeded()); |
| 694 |
EXPECT_EQ(*ImplicitFormat, ExpressionFormat::Kind::NoFormat); |
| 695 |
|
| 696 |
// Min value can be correctly represented. |
| 697 |
ExpressionLiteral Min(bufferize(SM, std::to_string(MinInt64)), MinInt64); |
| 698 |
Value = Min.eval(); |
| 699 |
ASSERT_TRUE(bool(Value)); |
| 700 |
EXPECT_EQ(MinInt64, cantFail(Value->getSignedValue())); |
| 701 |
|
| 702 |
// Max value can be correctly represented. |
| 703 |
ExpressionLiteral Max(bufferize(SM, std::to_string(MaxUint64)), MaxUint64); |
| 704 |
Value = Max.eval(); |
| 705 |
ASSERT_THAT_EXPECTED(Value, Succeeded()); |
| 706 |
EXPECT_EQ(MaxUint64, cantFail(Value->getUnsignedValue())); |
| 707 |
} |
| 708 |
|
| 709 |
TEST_F(FileCheckTest, Expression) { |
| 710 |
SourceMgr SM; |
| 711 |
|
| 712 |
std::unique_ptr<ExpressionLiteral> Ten = |
| 713 |
std::make_unique<ExpressionLiteral>(bufferize(SM, "10"), 10u); |
| 714 |
ExpressionLiteral *TenPtr = Ten.get(); |
| 715 |
Expression Expr(std::move(Ten), |
| 716 |
ExpressionFormat(ExpressionFormat::Kind::HexLower)); |
| 717 |
EXPECT_EQ(Expr.getAST(), TenPtr); |
| 718 |
EXPECT_EQ(Expr.getFormat(), ExpressionFormat::Kind::HexLower); |
| 719 |
} |
| 720 |
|
| 721 |
static void |
| 722 |
expectUndefErrors(std::unordered_set<std::string> ExpectedUndefVarNames, |
| 723 |
Error Err) { |
| 724 |
EXPECT_THAT_ERROR(handleErrors(std::move(Err), |
| 725 |
[&](const UndefVarError &E) { |
| 726 |
EXPECT_EQ(ExpectedUndefVarNames.erase( |
| 727 |
std::string(E.getVarName())), |
| 728 |
1U); |
| 729 |
}), |
| 730 |
Succeeded()); |
| 731 |
EXPECT_TRUE(ExpectedUndefVarNames.empty()) << toString(ExpectedUndefVarNames); |
| 732 |
} |
| 733 |
|
| 734 |
TEST_F(FileCheckTest, NumericVariable) { |
| 735 |
SourceMgr SM; |
| 736 |
|
| 737 |
// Undefined variable: getValue and eval fail, error returned by eval holds |
| 738 |
// the name of the undefined variable. |
| 739 |
NumericVariable FooVar("FOO", |
| 740 |
ExpressionFormat(ExpressionFormat::Kind::Unsigned), 1); |
| 741 |
EXPECT_EQ("FOO", FooVar.getName()); |
| 742 |
EXPECT_EQ(FooVar.getImplicitFormat(), ExpressionFormat::Kind::Unsigned); |
| 743 |
NumericVariableUse FooVarUse("FOO", &FooVar); |
| 744 |
Expected<ExpressionFormat> ImplicitFormat = FooVarUse.getImplicitFormat(SM); |
| 745 |
ASSERT_THAT_EXPECTED(ImplicitFormat, Succeeded()); |
| 746 |
EXPECT_EQ(*ImplicitFormat, ExpressionFormat::Kind::Unsigned); |
| 747 |
EXPECT_FALSE(FooVar.getValue()); |
| 748 |
Expected<ExpressionValue> EvalResult = FooVarUse.eval(); |
| 749 |
expectUndefErrors({"FOO"}, EvalResult.takeError()); |
| 750 |
|
| 751 |
// Defined variable without string: only getValue and eval return value set. |
| 752 |
FooVar.setValue(ExpressionValue(42u)); |
| 753 |
std::optional<ExpressionValue> Value = FooVar.getValue(); |
| 754 |
ASSERT_TRUE(Value); |
| 755 |
EXPECT_EQ(42, cantFail(Value->getSignedValue())); |
| 756 |
EXPECT_FALSE(FooVar.getStringValue()); |
| 757 |
EvalResult = FooVarUse.eval(); |
| 758 |
ASSERT_THAT_EXPECTED(EvalResult, Succeeded()); |
| 759 |
EXPECT_EQ(42, cantFail(EvalResult->getSignedValue())); |
| 760 |
|
| 761 |
// Defined variable with string: getValue, eval, and getStringValue return |
| 762 |
// value set. |
| 763 |
StringRef StringValue = "925"; |
| 764 |
FooVar.setValue(ExpressionValue(925u), StringValue); |
| 765 |
Value = FooVar.getValue(); |
| 766 |
ASSERT_TRUE(Value); |
| 767 |
EXPECT_EQ(925, cantFail(Value->getSignedValue())); |
| 768 |
// getStringValue should return the same memory not just the same characters. |
| 769 |
EXPECT_EQ(StringValue.begin(), FooVar.getStringValue()->begin()); |
| 770 |
EXPECT_EQ(StringValue.end(), FooVar.getStringValue()->end()); |
| 771 |
EvalResult = FooVarUse.eval(); |
| 772 |
ASSERT_THAT_EXPECTED(EvalResult, Succeeded()); |
| 773 |
EXPECT_EQ(925, cantFail(EvalResult->getSignedValue())); |
| 774 |
EXPECT_EQ(925, cantFail(EvalResult->getSignedValue())); |
| 775 |
|
| 776 |
// Clearing variable: getValue and eval fail. Error returned by eval holds |
| 777 |
// the name of the cleared variable. |
| 778 |
FooVar.clearValue(); |
| 779 |
EXPECT_FALSE(FooVar.getValue()); |
| 780 |
EXPECT_FALSE(FooVar.getStringValue()); |
| 781 |
EvalResult = FooVarUse.eval(); |
| 782 |
expectUndefErrors({"FOO"}, EvalResult.takeError()); |
| 783 |
} |
| 784 |
|
| 785 |
TEST_F(FileCheckTest, Binop) { |
| 786 |
SourceMgr SM; |
| 787 |
|
| 788 |
StringRef ExprStr = bufferize(SM, "FOO+BAR"); |
| 789 |
StringRef FooStr = ExprStr.take_front(3); |
| 790 |
NumericVariable FooVar(FooStr, |
| 791 |
ExpressionFormat(ExpressionFormat::Kind::Unsigned), 1); |
| 792 |
FooVar.setValue(ExpressionValue(42u)); |
| 793 |
std::unique_ptr<NumericVariableUse> FooVarUse = |
| 794 |
std::make_unique<NumericVariableUse>(FooStr, &FooVar); |
| 795 |
StringRef BarStr = ExprStr.take_back(3); |
| 796 |
NumericVariable BarVar(BarStr, |
| 797 |
ExpressionFormat(ExpressionFormat::Kind::Unsigned), 2); |
| 798 |
BarVar.setValue(ExpressionValue(18u)); |
| 799 |
std::unique_ptr<NumericVariableUse> BarVarUse = |
| 800 |
std::make_unique<NumericVariableUse>(BarStr, &BarVar); |
| 801 |
binop_eval_t doAdd = operator+; |
| 802 |
BinaryOperation Binop(ExprStr, doAdd, std::move(FooVarUse), |
| 803 |
std::move(BarVarUse)); |
| 804 |
|
| 805 |
// Defined variables: eval returns right value; implicit format is as |
| 806 |
// expected. |
| 807 |
Expected<ExpressionValue> Value = Binop.eval(); |
| 808 |
ASSERT_THAT_EXPECTED(Value, Succeeded()); |
| 809 |
EXPECT_EQ(60, cantFail(Value->getSignedValue())); |
| 810 |
Expected<ExpressionFormat> ImplicitFormat = Binop.getImplicitFormat(SM); |
| 811 |
ASSERT_THAT_EXPECTED(ImplicitFormat, Succeeded()); |
| 812 |
EXPECT_EQ(*ImplicitFormat, ExpressionFormat::Kind::Unsigned); |
| 813 |
|
| 814 |
// 1 undefined variable: eval fails, error contains name of undefined |
| 815 |
// variable. |
| 816 |
FooVar.clearValue(); |
| 817 |
Value = Binop.eval(); |
| 818 |
expectUndefErrors({"FOO"}, Value.takeError()); |
| 819 |
|
| 820 |
// 2 undefined variables: eval fails, error contains names of all undefined |
| 821 |
// variables. |
| 822 |
BarVar.clearValue(); |
| 823 |
Value = Binop.eval(); |
| 824 |
expectUndefErrors({"FOO", "BAR"}, Value.takeError()); |
| 825 |
|
| 826 |
// Literal + Variable has format of variable. |
| 827 |
ExprStr = bufferize(SM, "FOO+18"); |
| 828 |
FooStr = ExprStr.take_front(3); |
| 829 |
StringRef EighteenStr = ExprStr.take_back(2); |
| 830 |
FooVarUse = std::make_unique<NumericVariableUse>(FooStr, &FooVar); |
| 831 |
std::unique_ptr<ExpressionLiteral> Eighteen = |
| 832 |
std::make_unique<ExpressionLiteral>(EighteenStr, 18u); |
| 833 |
Binop = BinaryOperation(ExprStr, doAdd, std::move(FooVarUse), |
| 834 |
std::move(Eighteen)); |
| 835 |
ImplicitFormat = Binop.getImplicitFormat(SM); |
| 836 |
ASSERT_THAT_EXPECTED(ImplicitFormat, Succeeded()); |
| 837 |
EXPECT_EQ(*ImplicitFormat, ExpressionFormat::Kind::Unsigned); |
| 838 |
ExprStr = bufferize(SM, "18+FOO"); |
| 839 |
FooStr = ExprStr.take_back(3); |
| 840 |
EighteenStr = ExprStr.take_front(2); |
| 841 |
FooVarUse = std::make_unique<NumericVariableUse>(FooStr, &FooVar); |
| 842 |
Eighteen = std::make_unique<ExpressionLiteral>(EighteenStr, 18u); |
| 843 |
Binop = BinaryOperation(ExprStr, doAdd, std::move(Eighteen), |
| 844 |
std::move(FooVarUse)); |
| 845 |
ImplicitFormat = Binop.getImplicitFormat(SM); |
| 846 |
ASSERT_THAT_EXPECTED(ImplicitFormat, Succeeded()); |
| 847 |
EXPECT_EQ(*ImplicitFormat, ExpressionFormat::Kind::Unsigned); |
| 848 |
|
| 849 |
// Variables with different implicit format conflict. |
| 850 |
ExprStr = bufferize(SM, "FOO+BAZ"); |
| 851 |
FooStr = ExprStr.take_front(3); |
| 852 |
StringRef BazStr = ExprStr.take_back(3); |
| 853 |
NumericVariable BazVar(BazStr, |
| 854 |
ExpressionFormat(ExpressionFormat::Kind::HexLower), 3); |
| 855 |
FooVarUse = std::make_unique<NumericVariableUse>(FooStr, &FooVar); |
| 856 |
std::unique_ptr<NumericVariableUse> BazVarUse = |
| 857 |
std::make_unique<NumericVariableUse>(BazStr, &BazVar); |
| 858 |
Binop = BinaryOperation(ExprStr, doAdd, std::move(FooVarUse), |
| 859 |
std::move(BazVarUse)); |
| 860 |
ImplicitFormat = Binop.getImplicitFormat(SM); |
| 861 |
expectDiagnosticError( |
| 862 |
"implicit format conflict between 'FOO' (%u) and 'BAZ' (%x), " |
| 863 |
"need an explicit format specifier", |
| 864 |
ImplicitFormat.takeError()); |
| 865 |
|
| 866 |
// All variable conflicts are reported. |
| 867 |
ExprStr = bufferize(SM, "(FOO+BAZ)+(FOO+QUUX)"); |
| 868 |
StringRef Paren1ExprStr = ExprStr.substr(1, 7); |
| 869 |
FooStr = Paren1ExprStr.take_front(3); |
| 870 |
BazStr = Paren1ExprStr.take_back(3); |
| 871 |
StringRef Paren2ExprStr = ExprStr.substr(ExprStr.rfind('(') + 1, 8); |
| 872 |
StringRef FooStr2 = Paren2ExprStr.take_front(3); |
| 873 |
StringRef QuuxStr = Paren2ExprStr.take_back(4); |
| 874 |
FooVarUse = std::make_unique<NumericVariableUse>(FooStr, &FooVar); |
| 875 |
BazVarUse = std::make_unique<NumericVariableUse>(BazStr, &BazVar); |
| 876 |
std::unique_ptr<NumericVariableUse> FooVarUse2 = |
| 877 |
std::make_unique<NumericVariableUse>(FooStr2, &FooVar); |
| 878 |
NumericVariable QuuxVar( |
| 879 |
QuuxStr, ExpressionFormat(ExpressionFormat::Kind::HexLower), 4); |
| 880 |
std::unique_ptr<NumericVariableUse> QuuxVarUse = |
| 881 |
std::make_unique<NumericVariableUse>(QuuxStr, &QuuxVar); |
| 882 |
std::unique_ptr<BinaryOperation> Binop1 = std::make_unique<BinaryOperation>( |
| 883 |
ExprStr.take_front(9), doAdd, std::move(FooVarUse), std::move(BazVarUse)); |
| 884 |
std::unique_ptr<BinaryOperation> Binop2 = std::make_unique<BinaryOperation>( |
| 885 |
ExprStr.take_back(10), doAdd, std::move(FooVarUse2), |
| 886 |
std::move(QuuxVarUse)); |
| 887 |
std::unique_ptr<BinaryOperation> OuterBinop = |
| 888 |
std::make_unique<BinaryOperation>(ExprStr, doAdd, std::move(Binop1), |
| 889 |
std::move(Binop2)); |
| 890 |
ImplicitFormat = OuterBinop->getImplicitFormat(SM); |
| 891 |
expectSameErrors<ErrorDiagnostic>( |
| 892 |
{("implicit format conflict between 'FOO' (%u) and 'BAZ' (%x), need an " |
| 893 |
"explicit format specifier"), |
| 894 |
("implicit format conflict between 'FOO' (%u) and 'QUUX' (%x), need an " |
| 895 |
"explicit format specifier")}, |
| 896 |
ImplicitFormat.takeError()); |
| 897 |
} |
| 898 |
|
| 899 |
TEST_F(FileCheckTest, ValidVarNameStart) { |
| 900 |
EXPECT_TRUE(Pattern::isValidVarNameStart('a')); |
| 901 |
EXPECT_TRUE(Pattern::isValidVarNameStart('G')); |
| 902 |
EXPECT_TRUE(Pattern::isValidVarNameStart('_')); |
| 903 |
EXPECT_FALSE(Pattern::isValidVarNameStart('2')); |
| 904 |
EXPECT_FALSE(Pattern::isValidVarNameStart('$')); |
| 905 |
EXPECT_FALSE(Pattern::isValidVarNameStart('@')); |
| 906 |
EXPECT_FALSE(Pattern::isValidVarNameStart('+')); |
| 907 |
EXPECT_FALSE(Pattern::isValidVarNameStart('-')); |
| 908 |
EXPECT_FALSE(Pattern::isValidVarNameStart(':')); |
| 909 |
} |
| 910 |
|
| 911 |
TEST_F(FileCheckTest, ParseVar) { |
| 912 |
SourceMgr SM; |
| 913 |
StringRef OrigVarName = bufferize(SM, "GoodVar42"); |
| 914 |
StringRef VarName = OrigVarName; |
| 915 |
Expected<Pattern::VariableProperties> ParsedVarResult = |
| 916 |
Pattern::parseVariable(VarName, SM); |
| 917 |
ASSERT_THAT_EXPECTED(ParsedVarResult, Succeeded()); |
| 918 |
EXPECT_EQ(ParsedVarResult->Name, OrigVarName); |
| 919 |
EXPECT_TRUE(VarName.empty()); |
| 920 |
EXPECT_FALSE(ParsedVarResult->IsPseudo); |
| 921 |
|
| 922 |
VarName = OrigVarName = bufferize(SM, "$GoodGlobalVar"); |
| 923 |
ParsedVarResult = Pattern::parseVariable(VarName, SM); |
| 924 |
ASSERT_THAT_EXPECTED(ParsedVarResult, Succeeded()); |
| 925 |
EXPECT_EQ(ParsedVarResult->Name, OrigVarName); |
| 926 |
EXPECT_TRUE(VarName.empty()); |
| 927 |
EXPECT_FALSE(ParsedVarResult->IsPseudo); |
| 928 |
|
| 929 |
VarName = OrigVarName = bufferize(SM, "@GoodPseudoVar"); |
| 930 |
ParsedVarResult = Pattern::parseVariable(VarName, SM); |
| 931 |
ASSERT_THAT_EXPECTED(ParsedVarResult, Succeeded()); |
| 932 |
EXPECT_EQ(ParsedVarResult->Name, OrigVarName); |
| 933 |
EXPECT_TRUE(VarName.empty()); |
| 934 |
EXPECT_TRUE(ParsedVarResult->IsPseudo); |
| 935 |
|
| 936 |
VarName = bufferize(SM, "42BadVar"); |
| 937 |
ParsedVarResult = Pattern::parseVariable(VarName, SM); |
| 938 |
expectDiagnosticError("invalid variable name", ParsedVarResult.takeError()); |
| 939 |
|
| 940 |
VarName = bufferize(SM, "$@"); |
| 941 |
ParsedVarResult = Pattern::parseVariable(VarName, SM); |
| 942 |
expectDiagnosticError("invalid variable name", ParsedVarResult.takeError()); |
| 943 |
|
| 944 |
VarName = OrigVarName = bufferize(SM, "B@dVar"); |
| 945 |
ParsedVarResult = Pattern::parseVariable(VarName, SM); |
| 946 |
ASSERT_THAT_EXPECTED(ParsedVarResult, Succeeded()); |
| 947 |
EXPECT_EQ(VarName, OrigVarName.substr(1)); |
| 948 |
EXPECT_EQ(ParsedVarResult->Name, "B"); |
| 949 |
EXPECT_FALSE(ParsedVarResult->IsPseudo); |
| 950 |
|
| 951 |
VarName = OrigVarName = bufferize(SM, "B$dVar"); |
| 952 |
ParsedVarResult = Pattern::parseVariable(VarName, SM); |
| 953 |
ASSERT_THAT_EXPECTED(ParsedVarResult, Succeeded()); |
| 954 |
EXPECT_EQ(VarName, OrigVarName.substr(1)); |
| 955 |
EXPECT_EQ(ParsedVarResult->Name, "B"); |
| 956 |
EXPECT_FALSE(ParsedVarResult->IsPseudo); |
| 957 |
|
| 958 |
VarName = bufferize(SM, "BadVar+"); |
| 959 |
ParsedVarResult = Pattern::parseVariable(VarName, SM); |
| 960 |
ASSERT_THAT_EXPECTED(ParsedVarResult, Succeeded()); |
| 961 |
EXPECT_EQ(VarName, "+"); |
| 962 |
EXPECT_EQ(ParsedVarResult->Name, "BadVar"); |
| 963 |
EXPECT_FALSE(ParsedVarResult->IsPseudo); |
| 964 |
|
| 965 |
VarName = bufferize(SM, "BadVar-"); |
| 966 |
ParsedVarResult = Pattern::parseVariable(VarName, SM); |
| 967 |
ASSERT_THAT_EXPECTED(ParsedVarResult, Succeeded()); |
| 968 |
EXPECT_EQ(VarName, "-"); |
| 969 |
EXPECT_EQ(ParsedVarResult->Name, "BadVar"); |
| 970 |
EXPECT_FALSE(ParsedVarResult->IsPseudo); |
| 971 |
|
| 972 |
VarName = bufferize(SM, "BadVar:"); |
| 973 |
ParsedVarResult = Pattern::parseVariable(VarName, SM); |
| 974 |
ASSERT_THAT_EXPECTED(ParsedVarResult, Succeeded()); |
| 975 |
EXPECT_EQ(VarName, ":"); |
| 976 |
EXPECT_EQ(ParsedVarResult->Name, "BadVar"); |
| 977 |
EXPECT_FALSE(ParsedVarResult->IsPseudo); |
| 978 |
} |
| 979 |
|
| 980 |
static void expectNotFoundError(Error Err) { |
| 981 |
expectError<NotFoundError>("String not found in input", std::move(Err)); |
| 982 |
} |
| 983 |
|
| 984 |
class PatternTester { |
| 985 |
private: |
| 986 |
size_t LineNumber = 1; |
| 987 |
SourceMgr SM; |
| 988 |
FileCheckRequest Req; |
| 989 |
FileCheckPatternContext Context; |
| 990 |
Pattern P{Check::CheckPlain, &Context, LineNumber}; |
| 991 |
|
| 992 |
public: |
| 993 |
PatternTester() { |
| 994 |
std::vector<StringRef> GlobalDefines = {"#FOO=42", "BAR=BAZ", "#add=7"}; |
| 995 |
// An ASSERT_FALSE would make more sense but cannot be used in a |
| 996 |
// constructor. |
| 997 |
EXPECT_THAT_ERROR(Context.defineCmdlineVariables(GlobalDefines, SM), |
| 998 |
Succeeded()); |
| 999 |
Context.createLineVariable(); |
| 1000 |
// Call parsePattern to have @LINE defined. |
| 1001 |
P.parsePattern("N/A", "CHECK", SM, Req); |
| 1002 |
// parsePattern does not expect to be called twice for the same line and |
| 1003 |
// will set FixedStr and RegExStr incorrectly if it is. Therefore prepare |
| 1004 |
// a pattern for a different line. |
| 1005 |
initNextPattern(); |
| 1006 |
} |
| 1007 |
|
| 1008 |
void initNextPattern() { |
| 1009 |
P = Pattern(Check::CheckPlain, &Context, ++LineNumber); |
| 1010 |
} |
| 1011 |
|
| 1012 |
size_t getLineNumber() const { return LineNumber; } |
| 1013 |
|
| 1014 |
Expected<std::unique_ptr<Expression>> |
| 1015 |
parseSubst(StringRef Expr, bool IsLegacyLineExpr = false) { |
| 1016 |
StringRef ExprBufferRef = bufferize(SM, Expr); |
| 1017 |
std::optional<NumericVariable *> DefinedNumericVariable; |
| 1018 |
return P.parseNumericSubstitutionBlock( |
| 1019 |
ExprBufferRef, DefinedNumericVariable, IsLegacyLineExpr, LineNumber, |
| 1020 |
&Context, SM); |
| 1021 |
} |
| 1022 |
|
| 1023 |
bool parsePattern(StringRef Pattern) { |
| 1024 |
StringRef PatBufferRef = bufferize(SM, Pattern); |
| 1025 |
return P.parsePattern(PatBufferRef, "CHECK", SM, Req); |
| 1026 |
} |
| 1027 |
|
| 1028 |
Expected<size_t> match(StringRef Buffer) { |
| 1029 |
StringRef BufferRef = bufferize(SM, Buffer); |
| 1030 |
Pattern::MatchResult Res = P.match(BufferRef, SM); |
| 1031 |
if (Res.TheError) |
| 1032 |
return std::move(Res.TheError); |
| 1033 |
return Res.TheMatch->Pos; |
| 1034 |
} |
| 1035 |
|
| 1036 |
void printVariableDefs(FileCheckDiag::MatchType MatchTy, |
| 1037 |
std::vector<FileCheckDiag> &Diags) { |
| 1038 |
P.printVariableDefs(SM, MatchTy, &Diags); |
| 1039 |
} |
| 1040 |
}; |
| 1041 |
|
| 1042 |
TEST_F(FileCheckTest, ParseNumericSubstitutionBlock) { |
| 1043 |
PatternTester Tester; |
| 1044 |
|
| 1045 |
// Variable definition. |
| 1046 |
|
| 1047 |
expectDiagnosticError("invalid variable name", |
| 1048 |
Tester.parseSubst("%VAR:").takeError()); |
| 1049 |
|
| 1050 |
expectDiagnosticError("definition of pseudo numeric variable unsupported", |
| 1051 |
Tester.parseSubst("@LINE:").takeError()); |
| 1052 |
|
| 1053 |
expectDiagnosticError("string variable with name 'BAR' already exists", |
| 1054 |
Tester.parseSubst("BAR:").takeError()); |
| 1055 |
|
| 1056 |
expectDiagnosticError("unexpected characters after numeric variable name", |
| 1057 |
Tester.parseSubst("VAR GARBAGE:").takeError()); |
| 1058 |
|
| 1059 |
// Change of format. |
| 1060 |
expectDiagnosticError("format different from previous variable definition", |
| 1061 |
Tester.parseSubst("%X,FOO:").takeError()); |
| 1062 |
|
| 1063 |
// Invalid format. |
| 1064 |
expectDiagnosticError("invalid matching format specification in expression", |
| 1065 |
Tester.parseSubst("X,VAR1:").takeError()); |
| 1066 |
expectDiagnosticError("invalid format specifier in expression", |
| 1067 |
Tester.parseSubst("%F,VAR1:").takeError()); |
| 1068 |
expectDiagnosticError("invalid matching format specification in expression", |
| 1069 |
Tester.parseSubst("%X a,VAR1:").takeError()); |
| 1070 |
|
| 1071 |
// Acceptable variable definition. |
| 1072 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("VAR1:"), Succeeded()); |
| 1073 |
EXPECT_THAT_EXPECTED(Tester.parseSubst(" VAR2:"), Succeeded()); |
| 1074 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("VAR3 :"), Succeeded()); |
| 1075 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("VAR3: "), Succeeded()); |
| 1076 |
|
| 1077 |
// Acceptable variable definition with format specifier. Use parsePattern for |
| 1078 |
// variables whose definition needs to be visible for later checks. |
| 1079 |
EXPECT_FALSE(Tester.parsePattern("[[#%u, VAR_UNSIGNED:]]")); |
| 1080 |
EXPECT_FALSE(Tester.parsePattern("[[#%x, VAR_LOWER_HEX:]]")); |
| 1081 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("%X, VAR_UPPER_HEX:"), Succeeded()); |
| 1082 |
|
| 1083 |
// Acceptable variable definition with precision specifier. |
| 1084 |
EXPECT_FALSE(Tester.parsePattern("[[#%.8X, PADDED_ADDR:]]")); |
| 1085 |
EXPECT_FALSE(Tester.parsePattern("[[#%.8, PADDED_NUM:]]")); |
| 1086 |
|
| 1087 |
// Acceptable variable definition in alternate form. |
| 1088 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("%#x, PREFIXED_ADDR:"), Succeeded()); |
| 1089 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("%#X, PREFIXED_ADDR:"), Succeeded()); |
| 1090 |
|
| 1091 |
// Acceptable variable definition in alternate form. |
| 1092 |
expectDiagnosticError("alternate form only supported for hex values", |
| 1093 |
Tester.parseSubst("%#u, PREFIXED_UNSI:").takeError()); |
| 1094 |
expectDiagnosticError("alternate form only supported for hex values", |
| 1095 |
Tester.parseSubst("%#d, PREFIXED_UNSI:").takeError()); |
| 1096 |
|
| 1097 |
// Acceptable variable definition from a numeric expression. |
| 1098 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("FOOBAR: FOO+1"), Succeeded()); |
| 1099 |
|
| 1100 |
// Numeric expression. Switch to next line to make above valid definition |
| 1101 |
// available in expressions. |
| 1102 |
Tester.initNextPattern(); |
| 1103 |
|
| 1104 |
// Invalid variable name. |
| 1105 |
expectDiagnosticError("invalid matching constraint or operand format", |
| 1106 |
Tester.parseSubst("%VAR").takeError()); |
| 1107 |
|
| 1108 |
expectDiagnosticError("invalid pseudo numeric variable '@FOO'", |
| 1109 |
Tester.parseSubst("@FOO").takeError()); |
| 1110 |
|
| 1111 |
// parsePattern() is used here instead of parseSubst() for the variable to be |
| 1112 |
// recorded in GlobalNumericVariableTable and thus appear defined to |
| 1113 |
// parseNumericVariableUse(). Note that the same pattern object is used for |
| 1114 |
// the parsePattern() and parseSubst() since no initNextPattern() is called, |
| 1115 |
// thus appearing as being on the same line from the pattern's point of view. |
| 1116 |
ASSERT_FALSE(Tester.parsePattern("[[#SAME_LINE_VAR:]]")); |
| 1117 |
expectDiagnosticError("numeric variable 'SAME_LINE_VAR' defined earlier in " |
| 1118 |
"the same CHECK directive", |
| 1119 |
Tester.parseSubst("SAME_LINE_VAR").takeError()); |
| 1120 |
|
| 1121 |
// Invalid use of variable defined on the same line from an expression not |
| 1122 |
// using any variable defined on the same line. |
| 1123 |
ASSERT_FALSE(Tester.parsePattern("[[#SAME_LINE_EXPR_VAR:@LINE+1]]")); |
| 1124 |
expectDiagnosticError("numeric variable 'SAME_LINE_EXPR_VAR' defined earlier " |
| 1125 |
"in the same CHECK directive", |
| 1126 |
Tester.parseSubst("SAME_LINE_EXPR_VAR").takeError()); |
| 1127 |
|
| 1128 |
// Valid use of undefined variable which creates the variable and record it |
| 1129 |
// in GlobalNumericVariableTable. |
| 1130 |
ASSERT_THAT_EXPECTED(Tester.parseSubst("UNDEF"), Succeeded()); |
| 1131 |
EXPECT_TRUE(Tester.parsePattern("[[UNDEF:.*]]")); |
| 1132 |
|
| 1133 |
// Invalid literal. |
| 1134 |
expectDiagnosticError("unsupported operation 'U'", |
| 1135 |
Tester.parseSubst("42U").takeError()); |
| 1136 |
|
| 1137 |
// Valid empty expression. |
| 1138 |
EXPECT_THAT_EXPECTED(Tester.parseSubst(""), Succeeded()); |
| 1139 |
|
| 1140 |
// Invalid equality matching constraint with empty expression. |
| 1141 |
expectDiagnosticError("empty numeric expression should not have a constraint", |
| 1142 |
Tester.parseSubst("==").takeError()); |
| 1143 |
|
| 1144 |
// Valid single operand expression. |
| 1145 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("FOO"), Succeeded()); |
| 1146 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("18"), Succeeded()); |
| 1147 |
EXPECT_THAT_EXPECTED(Tester.parseSubst(std::to_string(MaxUint64)), |
| 1148 |
Succeeded()); |
| 1149 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("0x12"), Succeeded()); |
| 1150 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("-30"), Succeeded()); |
| 1151 |
EXPECT_THAT_EXPECTED(Tester.parseSubst(std::to_string(MinInt64)), |
| 1152 |
Succeeded()); |
| 1153 |
|
| 1154 |
// Valid optional matching constraint. |
| 1155 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("==FOO"), Succeeded()); |
| 1156 |
|
| 1157 |
// Invalid matching constraint. |
| 1158 |
expectDiagnosticError("invalid matching constraint or operand format", |
| 1159 |
Tester.parseSubst("+=FOO").takeError()); |
| 1160 |
|
| 1161 |
// Invalid format. |
| 1162 |
expectDiagnosticError("invalid matching format specification in expression", |
| 1163 |
Tester.parseSubst("X,FOO:").takeError()); |
| 1164 |
expectDiagnosticError("invalid format specifier in expression", |
| 1165 |
Tester.parseSubst("%F,FOO").takeError()); |
| 1166 |
expectDiagnosticError("invalid matching format specification in expression", |
| 1167 |
Tester.parseSubst("%X a,FOO").takeError()); |
| 1168 |
|
| 1169 |
// Valid expression with 2 or more operands. |
| 1170 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("FOO+3"), Succeeded()); |
| 1171 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("FOO+0xC"), Succeeded()); |
| 1172 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("FOO-3+FOO"), Succeeded()); |
| 1173 |
|
| 1174 |
expectDiagnosticError("unsupported operation '/'", |
| 1175 |
Tester.parseSubst("@LINE/2").takeError()); |
| 1176 |
|
| 1177 |
expectDiagnosticError("missing operand in expression", |
| 1178 |
Tester.parseSubst("@LINE+").takeError()); |
| 1179 |
|
| 1180 |
// Errors in RHS operand are bubbled up by parseBinop() to |
| 1181 |
// parseNumericSubstitutionBlock(). |
| 1182 |
expectDiagnosticError("invalid operand format", |
| 1183 |
Tester.parseSubst("@LINE+%VAR").takeError()); |
| 1184 |
|
| 1185 |
// Invalid legacy @LINE expression with non literal rhs. |
| 1186 |
expectDiagnosticError( |
| 1187 |
"invalid operand format", |
| 1188 |
Tester.parseSubst("@LINE+@LINE", /*IsLegacyNumExpr=*/true).takeError()); |
| 1189 |
|
| 1190 |
// Invalid legacy @LINE expression made of a single literal. |
| 1191 |
expectDiagnosticError( |
| 1192 |
"invalid variable name", |
| 1193 |
Tester.parseSubst("2", /*IsLegacyNumExpr=*/true).takeError()); |
| 1194 |
|
| 1195 |
// Invalid hex literal in legacy @LINE expression. |
| 1196 |
expectDiagnosticError( |
| 1197 |
"unexpected characters at end of expression 'xC'", |
| 1198 |
Tester.parseSubst("@LINE+0xC", /*LegacyLineExpr=*/true).takeError()); |
| 1199 |
|
| 1200 |
// Valid expression with format specifier. |
| 1201 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("%u, FOO"), Succeeded()); |
| 1202 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("%d, FOO"), Succeeded()); |
| 1203 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("%x, FOO"), Succeeded()); |
| 1204 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("%X, FOO"), Succeeded()); |
| 1205 |
|
| 1206 |
// Valid expression with precision specifier. |
| 1207 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("%.8u, FOO"), Succeeded()); |
| 1208 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("%.8, FOO"), Succeeded()); |
| 1209 |
|
| 1210 |
// Valid legacy @LINE expression. |
| 1211 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("@LINE+2", /*IsLegacyNumExpr=*/true), |
| 1212 |
Succeeded()); |
| 1213 |
|
| 1214 |
// Invalid legacy @LINE expression with more than 2 operands. |
| 1215 |
expectDiagnosticError( |
| 1216 |
"unexpected characters at end of expression '+@LINE'", |
| 1217 |
Tester.parseSubst("@LINE+2+@LINE", /*IsLegacyNumExpr=*/true).takeError()); |
| 1218 |
expectDiagnosticError( |
| 1219 |
"unexpected characters at end of expression '+2'", |
| 1220 |
Tester.parseSubst("@LINE+2+2", /*IsLegacyNumExpr=*/true).takeError()); |
| 1221 |
|
| 1222 |
// Valid expression with several variables when their implicit formats do not |
| 1223 |
// conflict. |
| 1224 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("FOO+VAR_UNSIGNED"), Succeeded()); |
| 1225 |
|
| 1226 |
// Valid implicit format conflict in presence of explicit formats. |
| 1227 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("%X,FOO+VAR_LOWER_HEX"), Succeeded()); |
| 1228 |
|
| 1229 |
// Implicit format conflict. |
| 1230 |
expectDiagnosticError( |
| 1231 |
"implicit format conflict between 'FOO' (%u) and " |
| 1232 |
"'VAR_LOWER_HEX' (%x), need an explicit format specifier", |
| 1233 |
Tester.parseSubst("FOO+VAR_LOWER_HEX").takeError()); |
| 1234 |
|
| 1235 |
// Simple parenthesized expressions: |
| 1236 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("(1)"), Succeeded()); |
| 1237 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("(1+1)"), Succeeded()); |
| 1238 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("(1)+1"), Succeeded()); |
| 1239 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("((1)+1)"), Succeeded()); |
| 1240 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("((1)+X)"), Succeeded()); |
| 1241 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("((X)+Y)"), Succeeded()); |
| 1242 |
|
| 1243 |
expectDiagnosticError("missing operand in expression", |
| 1244 |
Tester.parseSubst("(").takeError()); |
| 1245 |
expectDiagnosticError("missing ')' at end of nested expression", |
| 1246 |
Tester.parseSubst("(1").takeError()); |
| 1247 |
expectDiagnosticError("missing operand in expression", |
| 1248 |
Tester.parseSubst("(1+").takeError()); |
| 1249 |
expectDiagnosticError("missing ')' at end of nested expression", |
| 1250 |
Tester.parseSubst("(1+1").takeError()); |
| 1251 |
expectDiagnosticError("missing ')' at end of nested expression", |
| 1252 |
Tester.parseSubst("((1+2+3").takeError()); |
| 1253 |
expectDiagnosticError("missing ')' at end of nested expression", |
| 1254 |
Tester.parseSubst("((1+2)+3").takeError()); |
| 1255 |
|
| 1256 |
// Test missing operation between operands: |
| 1257 |
expectDiagnosticError("unsupported operation '('", |
| 1258 |
Tester.parseSubst("(1)(2)").takeError()); |
| 1259 |
expectDiagnosticError("unsupported operation '('", |
| 1260 |
Tester.parseSubst("2(X)").takeError()); |
| 1261 |
|
| 1262 |
// Test more closing than opening parentheses. The diagnostic messages are |
| 1263 |
// not ideal, but for now simply check that we reject invalid input. |
| 1264 |
expectDiagnosticError("invalid matching constraint or operand format", |
| 1265 |
Tester.parseSubst(")").takeError()); |
| 1266 |
expectDiagnosticError("unsupported operation ')'", |
| 1267 |
Tester.parseSubst("1)").takeError()); |
| 1268 |
expectDiagnosticError("unsupported operation ')'", |
| 1269 |
Tester.parseSubst("(1+2))").takeError()); |
| 1270 |
expectDiagnosticError("unsupported operation ')'", |
| 1271 |
Tester.parseSubst("(2))").takeError()); |
| 1272 |
expectDiagnosticError("unsupported operation ')'", |
| 1273 |
Tester.parseSubst("(1))(").takeError()); |
| 1274 |
|
| 1275 |
// Valid expression with function call. |
| 1276 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("add(FOO,3)"), Succeeded()); |
| 1277 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("add (FOO,3)"), Succeeded()); |
| 1278 |
// Valid expression with nested function call. |
| 1279 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("add(FOO, min(BAR,10))"), Succeeded()); |
| 1280 |
// Valid expression with function call taking expression as argument. |
| 1281 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("add(FOO, (BAR+10) + 3)"), |
| 1282 |
Succeeded()); |
| 1283 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("add(FOO, min (BAR,10) + 3)"), |
| 1284 |
Succeeded()); |
| 1285 |
// Valid expression with variable named the same as a function. |
| 1286 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("add"), Succeeded()); |
| 1287 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("add+FOO"), Succeeded()); |
| 1288 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("FOO+add"), Succeeded()); |
| 1289 |
EXPECT_THAT_EXPECTED(Tester.parseSubst("add(add,add)+add"), Succeeded()); |
| 1290 |
|
| 1291 |
// Malformed call syntax. |
| 1292 |
expectDiagnosticError("missing ')' at end of call expression", |
| 1293 |
Tester.parseSubst("add(FOO,(BAR+7)").takeError()); |
| 1294 |
expectDiagnosticError("missing ')' at end of call expression", |
| 1295 |
Tester.parseSubst("add(FOO,min(BAR,7)").takeError()); |
| 1296 |
expectDiagnosticError("missing argument", |
| 1297 |
Tester.parseSubst("add(FOO,)").takeError()); |
| 1298 |
expectDiagnosticError("missing argument", |
| 1299 |
Tester.parseSubst("add(,FOO)").takeError()); |
| 1300 |
expectDiagnosticError("missing argument", |
| 1301 |
Tester.parseSubst("add(FOO,,3)").takeError()); |
| 1302 |
|
| 1303 |
// Valid call, but to an unknown function. |
| 1304 |
expectDiagnosticError("call to undefined function 'bogus_function'", |
| 1305 |
Tester.parseSubst("bogus_function(FOO,3)").takeError()); |
| 1306 |
expectDiagnosticError("call to undefined function '@add'", |
| 1307 |
Tester.parseSubst("@add(2,3)").takeError()); |
| 1308 |
expectDiagnosticError("call to undefined function '$add'", |
| 1309 |
Tester.parseSubst("$add(2,3)").takeError()); |
| 1310 |
expectDiagnosticError("call to undefined function 'FOO'", |
| 1311 |
Tester.parseSubst("FOO(2,3)").takeError()); |
| 1312 |
expectDiagnosticError("call to undefined function 'FOO'", |
| 1313 |
Tester.parseSubst("FOO (2,3)").takeError()); |
| 1314 |
|
| 1315 |
// Valid call, but with incorrect argument count. |
| 1316 |
expectDiagnosticError("function 'add' takes 2 arguments but 1 given", |
| 1317 |
Tester.parseSubst("add(FOO)").takeError()); |
| 1318 |
expectDiagnosticError("function 'add' takes 2 arguments but 3 given", |
| 1319 |
Tester.parseSubst("add(FOO,3,4)").takeError()); |
| 1320 |
|
| 1321 |
// Valid call, but not part of a valid expression. |
| 1322 |
expectDiagnosticError("unsupported operation 'a'", |
| 1323 |
Tester.parseSubst("2add(FOO,2)").takeError()); |
| 1324 |
expectDiagnosticError("unsupported operation 'a'", |
| 1325 |
Tester.parseSubst("FOO add(FOO,2)").takeError()); |
| 1326 |
expectDiagnosticError("unsupported operation 'a'", |
| 1327 |
Tester.parseSubst("add(FOO,2)add(FOO,2)").takeError()); |
| 1328 |
} |
| 1329 |
|
| 1330 |
TEST_F(FileCheckTest, ParsePattern) { |
| 1331 |
PatternTester Tester; |
| 1332 |
|
| 1333 |
// Invalid space in string substitution. |
| 1334 |
EXPECT_TRUE(Tester.parsePattern("[[ BAR]]")); |
| 1335 |
|
| 1336 |
// Invalid variable name in string substitution. |
| 1337 |
EXPECT_TRUE(Tester.parsePattern("[[42INVALID]]")); |
| 1338 |
|
| 1339 |
// Invalid string variable definition. |
| 1340 |
EXPECT_TRUE(Tester.parsePattern("[[@PAT:]]")); |
| 1341 |
EXPECT_TRUE(Tester.parsePattern("[[PAT+2:]]")); |
| 1342 |
|
| 1343 |
// Collision with numeric variable. |
| 1344 |
EXPECT_TRUE(Tester.parsePattern("[[FOO:]]")); |
| 1345 |
|
| 1346 |
// Invalid use of string variable. |
| 1347 |
EXPECT_TRUE(Tester.parsePattern("[[FOO-BAR]]")); |
| 1348 |
|
| 1349 |
// Valid use of string variable. |
| 1350 |
EXPECT_FALSE(Tester.parsePattern("[[BAR]]")); |
| 1351 |
|
| 1352 |
// Valid string variable definition. |
| 1353 |
EXPECT_FALSE(Tester.parsePattern("[[PAT:[0-9]+]]")); |
| 1354 |
|
| 1355 |
// Invalid numeric substitution. |
| 1356 |
EXPECT_TRUE(Tester.parsePattern("[[#42INVALID]]")); |
| 1357 |
|
| 1358 |
// Valid numeric substitution. |
| 1359 |
EXPECT_FALSE(Tester.parsePattern("[[#FOO]]")); |
| 1360 |
|
| 1361 |
// Valid legacy @LINE expression. |
| 1362 |
EXPECT_FALSE(Tester.parsePattern("[[@LINE+2]]")); |
| 1363 |
|
| 1364 |
// Invalid legacy @LINE expression with non decimal literal. |
| 1365 |
EXPECT_TRUE(Tester.parsePattern("[[@LINE+0x3]]")); |
| 1366 |
} |
| 1367 |
|
| 1368 |
TEST_F(FileCheckTest, Match) { |
| 1369 |
PatternTester Tester; |
| 1370 |
|
| 1371 |
// Check a substitution error is diagnosed. |
| 1372 |
ASSERT_FALSE(Tester.parsePattern("[[#%u, -1]]")); |
| 1373 |
expectDiagnosticError( |
| 1374 |
"unable to substitute variable or numeric expression: overflow error", |
| 1375 |
Tester.match("").takeError()); |
| 1376 |
|
| 1377 |
// Check matching an empty expression only matches a number. |
| 1378 |
Tester.initNextPattern(); |
| 1379 |
ASSERT_FALSE(Tester.parsePattern("[[#]]")); |
| 1380 |
expectNotFoundError(Tester.match("FAIL").takeError()); |
| 1381 |
EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); |
| 1382 |
|
| 1383 |
// Check matching a definition only matches a number with the right format. |
| 1384 |
Tester.initNextPattern(); |
| 1385 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR:]]")); |
| 1386 |
expectNotFoundError(Tester.match("FAIL").takeError()); |
| 1387 |
expectNotFoundError(Tester.match("").takeError()); |
| 1388 |
EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); |
| 1389 |
Tester.initNextPattern(); |
| 1390 |
Tester.parsePattern("[[#%u,NUMVAR_UNSIGNED:]]"); |
| 1391 |
expectNotFoundError(Tester.match("C").takeError()); |
| 1392 |
EXPECT_THAT_EXPECTED(Tester.match("20"), Succeeded()); |
| 1393 |
Tester.initNextPattern(); |
| 1394 |
Tester.parsePattern("[[#%x,NUMVAR_LOWER_HEX:]]"); |
| 1395 |
expectNotFoundError(Tester.match("g").takeError()); |
| 1396 |
expectNotFoundError(Tester.match("C").takeError()); |
| 1397 |
EXPECT_THAT_EXPECTED(Tester.match("c"), Succeeded()); |
| 1398 |
Tester.initNextPattern(); |
| 1399 |
Tester.parsePattern("[[#%X,NUMVAR_UPPER_HEX:]]"); |
| 1400 |
expectNotFoundError(Tester.match("H").takeError()); |
| 1401 |
expectNotFoundError(Tester.match("b").takeError()); |
| 1402 |
EXPECT_THAT_EXPECTED(Tester.match("B"), Succeeded()); |
| 1403 |
|
| 1404 |
// Check matching expressions with no explicit format matches the values in |
| 1405 |
// the right format. |
| 1406 |
Tester.initNextPattern(); |
| 1407 |
Tester.parsePattern("[[#NUMVAR_UNSIGNED-5]]"); |
| 1408 |
expectNotFoundError(Tester.match("f").takeError()); |
| 1409 |
expectNotFoundError(Tester.match("F").takeError()); |
| 1410 |
EXPECT_THAT_EXPECTED(Tester.match("15"), Succeeded()); |
| 1411 |
Tester.initNextPattern(); |
| 1412 |
Tester.parsePattern("[[#NUMVAR_LOWER_HEX+1]]"); |
| 1413 |
expectNotFoundError(Tester.match("13").takeError()); |
| 1414 |
expectNotFoundError(Tester.match("D").takeError()); |
| 1415 |
EXPECT_THAT_EXPECTED(Tester.match("d"), Succeeded()); |
| 1416 |
Tester.initNextPattern(); |
| 1417 |
Tester.parsePattern("[[#NUMVAR_UPPER_HEX+1]]"); |
| 1418 |
expectNotFoundError(Tester.match("12").takeError()); |
| 1419 |
expectNotFoundError(Tester.match("c").takeError()); |
| 1420 |
EXPECT_THAT_EXPECTED(Tester.match("C"), Succeeded()); |
| 1421 |
|
| 1422 |
// Check matching an undefined variable returns a NotFound error. |
| 1423 |
Tester.initNextPattern(); |
| 1424 |
ASSERT_FALSE(Tester.parsePattern("100")); |
| 1425 |
expectNotFoundError(Tester.match("101").takeError()); |
| 1426 |
|
| 1427 |
// Check matching the defined variable matches the correct number only. |
| 1428 |
Tester.initNextPattern(); |
| 1429 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR]]")); |
| 1430 |
EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); |
| 1431 |
|
| 1432 |
// Check matching several substitutions does not match them independently. |
| 1433 |
Tester.initNextPattern(); |
| 1434 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR]] [[#NUMVAR+2]]")); |
| 1435 |
expectNotFoundError(Tester.match("19 21").takeError()); |
| 1436 |
expectNotFoundError(Tester.match("18 21").takeError()); |
| 1437 |
EXPECT_THAT_EXPECTED(Tester.match("18 20"), Succeeded()); |
| 1438 |
|
| 1439 |
// Check matching a numeric expression using @LINE after a match failure uses |
| 1440 |
// the correct value for @LINE. |
| 1441 |
Tester.initNextPattern(); |
| 1442 |
ASSERT_FALSE(Tester.parsePattern("[[#@LINE]]")); |
| 1443 |
// Ok, @LINE matches the current line number. |
| 1444 |
EXPECT_THAT_EXPECTED(Tester.match(std::to_string(Tester.getLineNumber())), |
| 1445 |
Succeeded()); |
| 1446 |
Tester.initNextPattern(); |
| 1447 |
// Match with substitution failure. |
| 1448 |
ASSERT_FALSE(Tester.parsePattern("[[#UNKNOWN1+UNKNOWN2]]")); |
| 1449 |
expectSameErrors<ErrorDiagnostic>( |
| 1450 |
{"undefined variable: UNKNOWN1", "undefined variable: UNKNOWN2"}, |
| 1451 |
Tester.match("FOO").takeError()); |
| 1452 |
Tester.initNextPattern(); |
| 1453 |
// Check that @LINE matches the later (given the calls to initNextPattern()) |
| 1454 |
// line number. |
| 1455 |
EXPECT_FALSE(Tester.parsePattern("[[#@LINE]]")); |
| 1456 |
EXPECT_THAT_EXPECTED(Tester.match(std::to_string(Tester.getLineNumber())), |
| 1457 |
Succeeded()); |
| 1458 |
} |
| 1459 |
|
| 1460 |
TEST_F(FileCheckTest, MatchParen) { |
| 1461 |
PatternTester Tester; |
| 1462 |
// Check simple parenthesized expressions |
| 1463 |
Tester.initNextPattern(); |
| 1464 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR:]]")); |
| 1465 |
expectNotFoundError(Tester.match("FAIL").takeError()); |
| 1466 |
expectNotFoundError(Tester.match("").takeError()); |
| 1467 |
EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); |
| 1468 |
|
| 1469 |
Tester.initNextPattern(); |
| 1470 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR + (2 + 2)]]")); |
| 1471 |
expectNotFoundError(Tester.match("21").takeError()); |
| 1472 |
EXPECT_THAT_EXPECTED(Tester.match("22"), Succeeded()); |
| 1473 |
Tester.initNextPattern(); |
| 1474 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR + (2)]]")); |
| 1475 |
EXPECT_THAT_EXPECTED(Tester.match("20"), Succeeded()); |
| 1476 |
Tester.initNextPattern(); |
| 1477 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR+(2)]]")); |
| 1478 |
EXPECT_THAT_EXPECTED(Tester.match("20"), Succeeded()); |
| 1479 |
Tester.initNextPattern(); |
| 1480 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR+(NUMVAR)]]")); |
| 1481 |
EXPECT_THAT_EXPECTED(Tester.match("36"), Succeeded()); |
| 1482 |
|
| 1483 |
// Check nested parenthesized expressions: |
| 1484 |
Tester.initNextPattern(); |
| 1485 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR+(2+(2))]]")); |
| 1486 |
EXPECT_THAT_EXPECTED(Tester.match("22"), Succeeded()); |
| 1487 |
Tester.initNextPattern(); |
| 1488 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR+(2+(NUMVAR))]]")); |
| 1489 |
EXPECT_THAT_EXPECTED(Tester.match("38"), Succeeded()); |
| 1490 |
Tester.initNextPattern(); |
| 1491 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR+((((NUMVAR))))]]")); |
| 1492 |
EXPECT_THAT_EXPECTED(Tester.match("36"), Succeeded()); |
| 1493 |
Tester.initNextPattern(); |
| 1494 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR+((((NUMVAR)))-1)-1]]")); |
| 1495 |
EXPECT_THAT_EXPECTED(Tester.match("34"), Succeeded()); |
| 1496 |
|
| 1497 |
// Parentheses can also be the first character after the '#': |
| 1498 |
Tester.initNextPattern(); |
| 1499 |
ASSERT_FALSE(Tester.parsePattern("[[#(NUMVAR)]]")); |
| 1500 |
EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); |
| 1501 |
Tester.initNextPattern(); |
| 1502 |
ASSERT_FALSE(Tester.parsePattern("[[#(NUMVAR+2)]]")); |
| 1503 |
EXPECT_THAT_EXPECTED(Tester.match("20"), Succeeded()); |
| 1504 |
} |
| 1505 |
|
| 1506 |
TEST_F(FileCheckTest, MatchBuiltinFunctions) { |
| 1507 |
PatternTester Tester; |
| 1508 |
// Esnure #NUMVAR has the expected value. |
| 1509 |
Tester.initNextPattern(); |
| 1510 |
ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR:]]")); |
| 1511 |
expectNotFoundError(Tester.match("FAIL").takeError()); |
| 1512 |
expectNotFoundError(Tester.match("").takeError()); |
| 1513 |
EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); |
| 1514 |
|
| 1515 |
// Check each builtin function generates the expected result. |
| 1516 |
Tester.initNextPattern(); |
| 1517 |
ASSERT_FALSE(Tester.parsePattern("[[#add(NUMVAR,13)]]")); |
| 1518 |
EXPECT_THAT_EXPECTED(Tester.match("31"), Succeeded()); |
| 1519 |
Tester.initNextPattern(); |
| 1520 |
ASSERT_FALSE(Tester.parsePattern("[[#div(NUMVAR,3)]]")); |
| 1521 |
EXPECT_THAT_EXPECTED(Tester.match("6"), Succeeded()); |
| 1522 |
Tester.initNextPattern(); |
| 1523 |
ASSERT_FALSE(Tester.parsePattern("[[#max(NUMVAR,5)]]")); |
| 1524 |
EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); |
| 1525 |
Tester.initNextPattern(); |
| 1526 |
ASSERT_FALSE(Tester.parsePattern("[[#max(NUMVAR,99)]]")); |
| 1527 |
EXPECT_THAT_EXPECTED(Tester.match("99"), Succeeded()); |
| 1528 |
Tester.initNextPattern(); |
| 1529 |
ASSERT_FALSE(Tester.parsePattern("[[#min(NUMVAR,5)]]")); |
| 1530 |
EXPECT_THAT_EXPECTED(Tester.match("5"), Succeeded()); |
| 1531 |
Tester.initNextPattern(); |
| 1532 |
ASSERT_FALSE(Tester.parsePattern("[[#min(NUMVAR,99)]]")); |
| 1533 |
EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); |
| 1534 |
Tester.initNextPattern(); |
| 1535 |
ASSERT_FALSE(Tester.parsePattern("[[#mul(NUMVAR,3)]]")); |
| 1536 |
EXPECT_THAT_EXPECTED(Tester.match("54"), Succeeded()); |
| 1537 |
Tester.initNextPattern(); |
| 1538 |
ASSERT_FALSE(Tester.parsePattern("[[#sub(NUMVAR,7)]]")); |
| 1539 |
EXPECT_THAT_EXPECTED(Tester.match("11"), Succeeded()); |
| 1540 |
|
| 1541 |
// Check nested function calls. |
| 1542 |
Tester.initNextPattern(); |
| 1543 |
ASSERT_FALSE(Tester.parsePattern("[[#add(min(7,2),max(4,10))]]")); |
| 1544 |
EXPECT_THAT_EXPECTED(Tester.match("12"), Succeeded()); |
| 1545 |
|
| 1546 |
// Check function call that uses a variable of the same name. |
| 1547 |
Tester.initNextPattern(); |
| 1548 |
ASSERT_FALSE(Tester.parsePattern("[[#add(add,add)+min (add,3)+add]]")); |
| 1549 |
EXPECT_THAT_EXPECTED(Tester.match("24"), Succeeded()); |
| 1550 |
} |
| 1551 |
|
| 1552 |
TEST_F(FileCheckTest, Substitution) { |
| 1553 |
SourceMgr SM; |
| 1554 |
FileCheckPatternContext Context; |
| 1555 |
EXPECT_THAT_ERROR(Context.defineCmdlineVariables({"FOO=BAR"}, SM), |
| 1556 |
Succeeded()); |
| 1557 |
|
| 1558 |
// Substitution of an undefined string variable fails and error holds that |
| 1559 |
// variable's name. |
| 1560 |
StringSubstitution StringSubstitution(&Context, "VAR404", 42); |
| 1561 |
Expected<std::string> SubstValue = StringSubstitution.getResult(); |
| 1562 |
expectUndefErrors({"VAR404"}, SubstValue.takeError()); |
| 1563 |
|
| 1564 |
// Numeric substitution blocks constituted of defined numeric variables are |
| 1565 |
// substituted for the variable's value. |
| 1566 |
NumericVariable NVar("N", ExpressionFormat(ExpressionFormat::Kind::Unsigned), |
| 1567 |
1); |
| 1568 |
NVar.setValue(ExpressionValue(10u)); |
| 1569 |
auto NVarUse = std::make_unique<NumericVariableUse>("N", &NVar); |
| 1570 |
auto ExpressionN = std::make_unique<Expression>( |
| 1571 |
std::move(NVarUse), ExpressionFormat(ExpressionFormat::Kind::HexUpper)); |
| 1572 |
NumericSubstitution SubstitutionN(&Context, "N", std::move(ExpressionN), |
| 1573 |
/*InsertIdx=*/30); |
| 1574 |
SubstValue = SubstitutionN.getResult(); |
| 1575 |
ASSERT_THAT_EXPECTED(SubstValue, Succeeded()); |
| 1576 |
EXPECT_EQ("A", *SubstValue); |
| 1577 |
|
| 1578 |
// Substitution of an undefined numeric variable fails, error holds name of |
| 1579 |
// undefined variable. |
| 1580 |
NVar.clearValue(); |
| 1581 |
SubstValue = SubstitutionN.getResult(); |
| 1582 |
expectUndefErrors({"N"}, SubstValue.takeError()); |
| 1583 |
|
| 1584 |
// Substitution of a defined string variable returns the right value. |
| 1585 |
Pattern P(Check::CheckPlain, &Context, 1); |
| 1586 |
StringSubstitution = llvm::StringSubstitution(&Context, "FOO", 42); |
| 1587 |
SubstValue = StringSubstitution.getResult(); |
| 1588 |
ASSERT_THAT_EXPECTED(SubstValue, Succeeded()); |
| 1589 |
EXPECT_EQ("BAR", *SubstValue); |
| 1590 |
} |
| 1591 |
|
| 1592 |
TEST_F(FileCheckTest, FileCheckContext) { |
| 1593 |
FileCheckPatternContext Cxt; |
| 1594 |
SourceMgr SM; |
| 1595 |
|
| 1596 |
// No definition. |
| 1597 |
EXPECT_THAT_ERROR(Cxt.defineCmdlineVariables({}, SM), Succeeded()); |
| 1598 |
|
| 1599 |
// Missing equal sign. |
| 1600 |
expectDiagnosticError("missing equal sign in global definition", |
| 1601 |
Cxt.defineCmdlineVariables({"LocalVar"}, SM)); |
| 1602 |
expectDiagnosticError("missing equal sign in global definition", |
| 1603 |
Cxt.defineCmdlineVariables({"#LocalNumVar"}, SM)); |
| 1604 |
|
| 1605 |
// Empty variable name. |
| 1606 |
expectDiagnosticError("empty variable name", |
| 1607 |
Cxt.defineCmdlineVariables({"=18"}, SM)); |
| 1608 |
expectDiagnosticError("empty variable name", |
| 1609 |
Cxt.defineCmdlineVariables({"#=18"}, SM)); |
| 1610 |
|
| 1611 |
// Invalid variable name. |
| 1612 |
expectDiagnosticError("invalid variable name", |
| 1613 |
Cxt.defineCmdlineVariables({"18LocalVar=18"}, SM)); |
| 1614 |
expectDiagnosticError("invalid variable name", |
| 1615 |
Cxt.defineCmdlineVariables({"#18LocalNumVar=18"}, SM)); |
| 1616 |
|
| 1617 |
// Name conflict between pattern and numeric variable. |
| 1618 |
expectDiagnosticError( |
| 1619 |
"string variable with name 'LocalVar' already exists", |
| 1620 |
Cxt.defineCmdlineVariables({"LocalVar=18", "#LocalVar=36"}, SM)); |
| 1621 |
Cxt = FileCheckPatternContext(); |
| 1622 |
expectDiagnosticError( |
| 1623 |
"numeric variable with name 'LocalNumVar' already exists", |
| 1624 |
Cxt.defineCmdlineVariables({"#LocalNumVar=18", "LocalNumVar=36"}, SM)); |
| 1625 |
Cxt = FileCheckPatternContext(); |
| 1626 |
|
| 1627 |
// Invalid numeric value for numeric variable. |
| 1628 |
expectUndefErrors({"x"}, Cxt.defineCmdlineVariables({"#LocalNumVar=x"}, SM)); |
| 1629 |
|
| 1630 |
// Define local variables from command-line. |
| 1631 |
std::vector<StringRef> GlobalDefines; |
| 1632 |
// Clear local variables to remove dummy numeric variable x that |
| 1633 |
// parseNumericSubstitutionBlock would have created and stored in |
| 1634 |
// GlobalNumericVariableTable. |
| 1635 |
Cxt.clearLocalVars(); |
| 1636 |
GlobalDefines.emplace_back("LocalVar=FOO"); |
| 1637 |
GlobalDefines.emplace_back("EmptyVar="); |
| 1638 |
GlobalDefines.emplace_back("#LocalNumVar1=18"); |
| 1639 |
GlobalDefines.emplace_back("#%x,LocalNumVar2=LocalNumVar1+2"); |
| 1640 |
GlobalDefines.emplace_back("#LocalNumVar3=0xc"); |
| 1641 |
ASSERT_THAT_ERROR(Cxt.defineCmdlineVariables(GlobalDefines, SM), Succeeded()); |
| 1642 |
|
| 1643 |
// Create @LINE pseudo numeric variable and check it is present by matching |
| 1644 |
// it. |
| 1645 |
size_t LineNumber = 1; |
| 1646 |
Pattern P(Check::CheckPlain, &Cxt, LineNumber); |
| 1647 |
FileCheckRequest Req; |
| 1648 |
Cxt.createLineVariable(); |
| 1649 |
ASSERT_FALSE(P.parsePattern("[[@LINE]]", "CHECK", SM, Req)); |
| 1650 |
Pattern::MatchResult Res = P.match("1", SM); |
| 1651 |
ASSERT_THAT_ERROR(std::move(Res.TheError), Succeeded()); |
| 1652 |
|
| 1653 |
#ifndef NDEBUG |
| 1654 |
// Recreating @LINE pseudo numeric variable fails. |
| 1655 |
EXPECT_DEATH(Cxt.createLineVariable(), |
| 1656 |
"@LINE pseudo numeric variable already created"); |
| 1657 |
#endif |
| 1658 |
|
| 1659 |
// Check defined variables are present and undefined ones are absent. |
| 1660 |
StringRef LocalVarStr = "LocalVar"; |
| 1661 |
StringRef LocalNumVar1Ref = bufferize(SM, "LocalNumVar1"); |
| 1662 |
StringRef LocalNumVar2Ref = bufferize(SM, "LocalNumVar2"); |
| 1663 |
StringRef LocalNumVar3Ref = bufferize(SM, "LocalNumVar3"); |
| 1664 |
StringRef EmptyVarStr = "EmptyVar"; |
| 1665 |
StringRef UnknownVarStr = "UnknownVar"; |
| 1666 |
Expected<StringRef> LocalVar = Cxt.getPatternVarValue(LocalVarStr); |
| 1667 |
P = Pattern(Check::CheckPlain, &Cxt, ++LineNumber); |
| 1668 |
std::optional<NumericVariable *> DefinedNumericVariable; |
| 1669 |
Expected<std::unique_ptr<Expression>> ExpressionPointer = |
| 1670 |
P.parseNumericSubstitutionBlock(LocalNumVar1Ref, DefinedNumericVariable, |
| 1671 |
/*IsLegacyLineExpr=*/false, LineNumber, |
| 1672 |
&Cxt, SM); |
| 1673 |
ASSERT_THAT_EXPECTED(LocalVar, Succeeded()); |
| 1674 |
EXPECT_EQ(*LocalVar, "FOO"); |
| 1675 |
Expected<StringRef> EmptyVar = Cxt.getPatternVarValue(EmptyVarStr); |
| 1676 |
Expected<StringRef> UnknownVar = Cxt.getPatternVarValue(UnknownVarStr); |
| 1677 |
ASSERT_THAT_EXPECTED(ExpressionPointer, Succeeded()); |
| 1678 |
Expected<ExpressionValue> ExpressionVal = |
| 1679 |
(*ExpressionPointer)->getAST()->eval(); |
| 1680 |
ASSERT_THAT_EXPECTED(ExpressionVal, Succeeded()); |
| 1681 |
EXPECT_EQ(cantFail(ExpressionVal->getSignedValue()), 18); |
| 1682 |
ExpressionPointer = P.parseNumericSubstitutionBlock( |
| 1683 |
LocalNumVar2Ref, DefinedNumericVariable, |
| 1684 |
/*IsLegacyLineExpr=*/false, LineNumber, &Cxt, SM); |
| 1685 |
ASSERT_THAT_EXPECTED(ExpressionPointer, Succeeded()); |
| 1686 |
ExpressionVal = (*ExpressionPointer)->getAST()->eval(); |
| 1687 |
ASSERT_THAT_EXPECTED(ExpressionVal, Succeeded()); |
| 1688 |
EXPECT_EQ(cantFail(ExpressionVal->getSignedValue()), 20); |
| 1689 |
ExpressionPointer = P.parseNumericSubstitutionBlock( |
| 1690 |
LocalNumVar3Ref, DefinedNumericVariable, |
| 1691 |
/*IsLegacyLineExpr=*/false, LineNumber, &Cxt, SM); |
| 1692 |
ASSERT_THAT_EXPECTED(ExpressionPointer, Succeeded()); |
| 1693 |
ExpressionVal = (*ExpressionPointer)->getAST()->eval(); |
| 1694 |
ASSERT_THAT_EXPECTED(ExpressionVal, Succeeded()); |
| 1695 |
EXPECT_EQ(cantFail(ExpressionVal->getSignedValue()), 12); |
| 1696 |
ASSERT_THAT_EXPECTED(EmptyVar, Succeeded()); |
| 1697 |
EXPECT_EQ(*EmptyVar, ""); |
| 1698 |
expectUndefErrors({std::string(UnknownVarStr)}, UnknownVar.takeError()); |
| 1699 |
|
| 1700 |
// Clear local variables and check they become absent. |
| 1701 |
Cxt.clearLocalVars(); |
| 1702 |
LocalVar = Cxt.getPatternVarValue(LocalVarStr); |
| 1703 |
expectUndefErrors({std::string(LocalVarStr)}, LocalVar.takeError()); |
| 1704 |
// Check a numeric expression's evaluation fails if called after clearing of |
| 1705 |
// local variables, if it was created before. This is important because local |
| 1706 |
// variable clearing due to --enable-var-scope happens after numeric |
| 1707 |
// expressions are linked to the numeric variables they use. |
| 1708 |
expectUndefErrors({"LocalNumVar3"}, |
| 1709 |
(*ExpressionPointer)->getAST()->eval().takeError()); |
| 1710 |
P = Pattern(Check::CheckPlain, &Cxt, ++LineNumber); |
| 1711 |
ExpressionPointer = P.parseNumericSubstitutionBlock( |
| 1712 |
LocalNumVar1Ref, DefinedNumericVariable, /*IsLegacyLineExpr=*/false, |
| 1713 |
LineNumber, &Cxt, SM); |
| 1714 |
ASSERT_THAT_EXPECTED(ExpressionPointer, Succeeded()); |
| 1715 |
ExpressionVal = (*ExpressionPointer)->getAST()->eval(); |
| 1716 |
expectUndefErrors({"LocalNumVar1"}, ExpressionVal.takeError()); |
| 1717 |
ExpressionPointer = P.parseNumericSubstitutionBlock( |
| 1718 |
LocalNumVar2Ref, DefinedNumericVariable, /*IsLegacyLineExpr=*/false, |
| 1719 |
LineNumber, &Cxt, SM); |
| 1720 |
ASSERT_THAT_EXPECTED(ExpressionPointer, Succeeded()); |
| 1721 |
ExpressionVal = (*ExpressionPointer)->getAST()->eval(); |
| 1722 |
expectUndefErrors({"LocalNumVar2"}, ExpressionVal.takeError()); |
| 1723 |
EmptyVar = Cxt.getPatternVarValue(EmptyVarStr); |
| 1724 |
expectUndefErrors({"EmptyVar"}, EmptyVar.takeError()); |
| 1725 |
// Clear again because parseNumericSubstitutionBlock would have created a |
| 1726 |
// dummy variable and stored it in GlobalNumericVariableTable. |
| 1727 |
Cxt.clearLocalVars(); |
| 1728 |
|
| 1729 |
// Redefine global variables and check variables are defined again. |
| 1730 |
GlobalDefines.emplace_back("$GlobalVar=BAR"); |
| 1731 |
GlobalDefines.emplace_back("#$GlobalNumVar=36"); |
| 1732 |
ASSERT_THAT_ERROR(Cxt.defineCmdlineVariables(GlobalDefines, SM), Succeeded()); |
| 1733 |
StringRef GlobalVarStr = "$GlobalVar"; |
| 1734 |
StringRef GlobalNumVarRef = bufferize(SM, "$GlobalNumVar"); |
| 1735 |
Expected<StringRef> GlobalVar = Cxt.getPatternVarValue(GlobalVarStr); |
| 1736 |
ASSERT_THAT_EXPECTED(GlobalVar, Succeeded()); |
| 1737 |
EXPECT_EQ(*GlobalVar, "BAR"); |
| 1738 |
P = Pattern(Check::CheckPlain, &Cxt, ++LineNumber); |
| 1739 |
ExpressionPointer = P.parseNumericSubstitutionBlock( |
| 1740 |
GlobalNumVarRef, DefinedNumericVariable, /*IsLegacyLineExpr=*/false, |
| 1741 |
LineNumber, &Cxt, SM); |
| 1742 |
ASSERT_THAT_EXPECTED(ExpressionPointer, Succeeded()); |
| 1743 |
ExpressionVal = (*ExpressionPointer)->getAST()->eval(); |
| 1744 |
ASSERT_THAT_EXPECTED(ExpressionVal, Succeeded()); |
| 1745 |
EXPECT_EQ(cantFail(ExpressionVal->getSignedValue()), 36); |
| 1746 |
|
| 1747 |
// Clear local variables and check global variables remain defined. |
| 1748 |
Cxt.clearLocalVars(); |
| 1749 |
EXPECT_THAT_EXPECTED(Cxt.getPatternVarValue(GlobalVarStr), Succeeded()); |
| 1750 |
P = Pattern(Check::CheckPlain, &Cxt, ++LineNumber); |
| 1751 |
ExpressionPointer = P.parseNumericSubstitutionBlock( |
| 1752 |
GlobalNumVarRef, DefinedNumericVariable, /*IsLegacyLineExpr=*/false, |
| 1753 |
LineNumber, &Cxt, SM); |
| 1754 |
ASSERT_THAT_EXPECTED(ExpressionPointer, Succeeded()); |
| 1755 |
ExpressionVal = (*ExpressionPointer)->getAST()->eval(); |
| 1756 |
ASSERT_THAT_EXPECTED(ExpressionVal, Succeeded()); |
| 1757 |
EXPECT_EQ(cantFail(ExpressionVal->getSignedValue()), 36); |
| 1758 |
} |
| 1759 |
|
| 1760 |
TEST_F(FileCheckTest, CapturedVarDiags) { |
| 1761 |
PatternTester Tester; |
| 1762 |
ASSERT_FALSE(Tester.parsePattern("[[STRVAR:[a-z]+]] [[#NUMVAR:@LINE]]")); |
| 1763 |
EXPECT_THAT_EXPECTED(Tester.match("foobar 2"), Succeeded()); |
| 1764 |
std::vector<FileCheckDiag> Diags; |
| 1765 |
Tester.printVariableDefs(FileCheckDiag::MatchFoundAndExpected, Diags); |
| 1766 |
EXPECT_EQ(Diags.size(), 2ul); |
| 1767 |
for (FileCheckDiag Diag : Diags) { |
| 1768 |
EXPECT_EQ(Diag.CheckTy, Check::CheckPlain); |
| 1769 |
EXPECT_EQ(Diag.MatchTy, FileCheckDiag::MatchFoundAndExpected); |
| 1770 |
EXPECT_EQ(Diag.InputStartLine, 1u); |
| 1771 |
EXPECT_EQ(Diag.InputEndLine, 1u); |
| 1772 |
} |
| 1773 |
EXPECT_EQ(Diags[0].InputStartCol, 1u); |
| 1774 |
EXPECT_EQ(Diags[0].InputEndCol, 7u); |
| 1775 |
EXPECT_EQ(Diags[1].InputStartCol, 8u); |
| 1776 |
EXPECT_EQ(Diags[1].InputEndCol, 9u); |
| 1777 |
EXPECT_EQ(Diags[0].Note, "captured var \"STRVAR\""); |
| 1778 |
EXPECT_EQ(Diags[1].Note, "captured var \"NUMVAR\""); |
| 1779 |
} |
| 1780 |
} // namespace |