2019-10-10 22:27:14 +08:00
|
|
|
//===-- FileCheckImpl.h - Private FileCheck Interface ------------*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the private interfaces of FileCheck. Its purpose is to
|
|
|
|
// allow unit testing of FileCheck and to separate the interface from the
|
|
|
|
// implementation. It is only meant to be used by FileCheck.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_LIB_SUPPORT_FILECHECKIMPL_H
|
|
|
|
#define LLVM_LIB_SUPPORT_FILECHECKIMPL_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Numeric substitution handling code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
/// Type representing the format an expression value should be textualized into
|
|
|
|
/// for matching. Used to represent both explicit format specifiers as well as
|
|
|
|
/// implicit format from using numeric variables.
|
|
|
|
struct ExpressionFormat {
|
|
|
|
enum class Kind {
|
|
|
|
/// Denote absence of format. Used for implicit format of literals and
|
|
|
|
/// empty expressions.
|
|
|
|
NoFormat,
|
|
|
|
/// Used when there are several conflicting implicit formats in an
|
|
|
|
/// expression.
|
|
|
|
Conflict,
|
|
|
|
/// Value is an unsigned integer and should be printed as a decimal number.
|
|
|
|
Unsigned,
|
|
|
|
/// Value should be printed as an uppercase hex number.
|
|
|
|
HexUpper,
|
|
|
|
/// Value should be printed as a lowercase hex number.
|
|
|
|
HexLower
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
Kind Value;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Evaluates a format to true if it can be used in a match.
|
|
|
|
explicit operator bool() const {
|
|
|
|
return Value != Kind::NoFormat && Value != Kind::Conflict;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Define format equality: formats are equal if neither is NoFormat and
|
|
|
|
/// their kinds are the same.
|
|
|
|
bool operator==(const ExpressionFormat &Other) const {
|
|
|
|
return Value != Kind::NoFormat && Value == Other.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const ExpressionFormat &other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(Kind OtherValue) const { return Value == OtherValue; }
|
|
|
|
|
|
|
|
bool operator!=(Kind OtherValue) const { return !(*this == OtherValue); }
|
|
|
|
|
|
|
|
ExpressionFormat() : Value(Kind::NoFormat){};
|
|
|
|
explicit ExpressionFormat(Kind Value) : Value(Value){};
|
|
|
|
|
|
|
|
/// \returns a wildcard regular expression StringRef that matches any value
|
|
|
|
/// in the format represented by this instance, or an error if the format is
|
|
|
|
/// NoFormat or Conflict.
|
|
|
|
Expected<StringRef> getWildcardRegex() const;
|
|
|
|
|
|
|
|
/// \returns the string representation of \p Value in the format represented
|
|
|
|
/// by this instance, or an error if the format is NoFormat or Conflict.
|
|
|
|
Expected<std::string> getMatchingString(uint64_t Value) const;
|
|
|
|
|
|
|
|
/// \returns the value corresponding to string representation \p StrVal
|
|
|
|
/// according to the matching format represented by this instance or an error
|
|
|
|
/// with diagnostic against \p SM if \p StrVal does not correspond to a valid
|
|
|
|
/// and representable value.
|
|
|
|
Expected<uint64_t> valueFromStringRepr(StringRef StrVal,
|
|
|
|
const SourceMgr &SM) const;
|
|
|
|
};
|
|
|
|
|
2019-10-10 22:27:14 +08:00
|
|
|
/// Base class representing the AST of a given expression.
|
2019-12-12 07:48:01 +08:00
|
|
|
class ExpressionAST {
|
2019-10-10 22:27:14 +08:00
|
|
|
public:
|
2019-12-12 07:48:01 +08:00
|
|
|
virtual ~ExpressionAST() = default;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Evaluates and \returns the value of the expression represented by this
|
|
|
|
/// AST or an error if evaluation fails.
|
|
|
|
virtual Expected<uint64_t> eval() const = 0;
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
|
|
|
|
/// \returns either the implicit format of this AST, FormatConflict if
|
|
|
|
/// implicit formats of the AST's components conflict, or NoFormat if the AST
|
|
|
|
/// has no implicit format (e.g. AST is made up of a single literal).
|
|
|
|
virtual ExpressionFormat getImplicitFormat() const {
|
|
|
|
return ExpressionFormat();
|
|
|
|
}
|
2019-10-10 22:27:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Class representing an unsigned literal in the AST of an expression.
|
2019-12-12 07:48:01 +08:00
|
|
|
class ExpressionLiteral : public ExpressionAST {
|
2019-10-10 22:27:14 +08:00
|
|
|
private:
|
|
|
|
/// Actual value of the literal.
|
|
|
|
uint64_t Value;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Constructs a literal with the specified value.
|
2019-12-12 07:48:01 +08:00
|
|
|
ExpressionLiteral(uint64_t Val) : Value(Val) {}
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// \returns the literal's value.
|
2019-11-12 02:51:14 +08:00
|
|
|
Expected<uint64_t> eval() const override { return Value; }
|
2019-10-10 22:27:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Class to represent an undefined variable error, which quotes that
|
|
|
|
/// variable's name when printed.
|
2019-12-12 07:48:01 +08:00
|
|
|
class UndefVarError : public ErrorInfo<UndefVarError> {
|
2019-10-10 22:27:14 +08:00
|
|
|
private:
|
|
|
|
StringRef VarName;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2019-12-12 07:48:01 +08:00
|
|
|
UndefVarError(StringRef VarName) : VarName(VarName) {}
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
StringRef getVarName() const { return VarName; }
|
|
|
|
|
|
|
|
std::error_code convertToErrorCode() const override {
|
|
|
|
return inconvertibleErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print name of variable associated with this error.
|
|
|
|
void log(raw_ostream &OS) const override {
|
|
|
|
OS << "\"";
|
|
|
|
OS.write_escaped(VarName) << "\"";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
/// Class representing an expression and its matching format.
|
|
|
|
class Expression {
|
|
|
|
private:
|
|
|
|
/// Pointer to AST of the expression.
|
|
|
|
std::unique_ptr<ExpressionAST> AST;
|
|
|
|
|
|
|
|
/// Format to use (e.g. hex upper case letters) when matching the value.
|
|
|
|
ExpressionFormat Format;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Generic constructor for an expression represented by the given \p AST and
|
|
|
|
/// whose matching format is \p Format.
|
|
|
|
Expression(std::unique_ptr<ExpressionAST> AST, ExpressionFormat Format)
|
|
|
|
: AST(std::move(AST)), Format(Format) {}
|
|
|
|
|
|
|
|
/// \returns pointer to AST of the expression. Pointer is guaranteed to be
|
|
|
|
/// valid as long as this object is.
|
|
|
|
ExpressionAST *getAST() const { return AST.get(); }
|
|
|
|
|
|
|
|
ExpressionFormat getFormat() const { return Format; }
|
|
|
|
};
|
|
|
|
|
2019-10-10 22:27:14 +08:00
|
|
|
/// Class representing a numeric variable and its associated current value.
|
2019-12-12 07:48:01 +08:00
|
|
|
class NumericVariable {
|
2019-10-10 22:27:14 +08:00
|
|
|
private:
|
|
|
|
/// Name of the numeric variable.
|
|
|
|
StringRef Name;
|
|
|
|
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
/// Format to use for expressions using this variable without an explicit
|
|
|
|
/// format.
|
|
|
|
ExpressionFormat ImplicitFormat;
|
|
|
|
|
2019-10-10 22:27:14 +08:00
|
|
|
/// Value of numeric variable, if defined, or None otherwise.
|
|
|
|
Optional<uint64_t> Value;
|
|
|
|
|
|
|
|
/// Line number where this variable is defined, or None if defined before
|
|
|
|
/// input is parsed. Used to determine whether a variable is defined on the
|
|
|
|
/// same line as a given use.
|
|
|
|
Optional<size_t> DefLineNumber;
|
|
|
|
|
|
|
|
public:
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
/// Constructor for a variable \p Name with implicit format \p ImplicitFormat
|
|
|
|
/// defined at line \p DefLineNumber or defined before input is parsed if
|
|
|
|
/// \p DefLineNumber is None.
|
|
|
|
explicit NumericVariable(StringRef Name, ExpressionFormat ImplicitFormat,
|
2019-12-12 07:48:01 +08:00
|
|
|
Optional<size_t> DefLineNumber = None)
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
: Name(Name), ImplicitFormat(ImplicitFormat),
|
|
|
|
DefLineNumber(DefLineNumber) {}
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// \returns name of this numeric variable.
|
|
|
|
StringRef getName() const { return Name; }
|
|
|
|
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
/// \returns implicit format of this numeric variable.
|
|
|
|
ExpressionFormat getImplicitFormat() const { return ImplicitFormat; }
|
|
|
|
|
2019-10-10 22:27:14 +08:00
|
|
|
/// \returns this variable's value.
|
|
|
|
Optional<uint64_t> getValue() const { return Value; }
|
|
|
|
|
|
|
|
/// Sets value of this numeric variable to \p NewValue.
|
|
|
|
void setValue(uint64_t NewValue) { Value = NewValue; }
|
|
|
|
|
|
|
|
/// Clears value of this numeric variable, regardless of whether it is
|
|
|
|
/// currently defined or not.
|
|
|
|
void clearValue() { Value = None; }
|
|
|
|
|
|
|
|
/// \returns the line number where this variable is defined, if any, or None
|
|
|
|
/// if defined before input is parsed.
|
2019-11-12 02:44:13 +08:00
|
|
|
Optional<size_t> getDefLineNumber() const { return DefLineNumber; }
|
2019-10-10 22:27:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Class representing the use of a numeric variable in the AST of an
|
|
|
|
/// expression.
|
2019-12-12 07:48:01 +08:00
|
|
|
class NumericVariableUse : public ExpressionAST {
|
2019-10-10 22:27:14 +08:00
|
|
|
private:
|
|
|
|
/// Name of the numeric variable.
|
|
|
|
StringRef Name;
|
|
|
|
|
|
|
|
/// Pointer to the class instance for the variable this use is about.
|
2019-12-12 07:48:01 +08:00
|
|
|
NumericVariable *Variable;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
public:
|
2019-12-12 07:48:01 +08:00
|
|
|
NumericVariableUse(StringRef Name, NumericVariable *Variable)
|
|
|
|
: Name(Name), Variable(Variable) {}
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// \returns the value of the variable referenced by this instance.
|
2019-11-12 02:51:14 +08:00
|
|
|
Expected<uint64_t> eval() const override;
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
|
|
|
|
/// \returns implicit format of this numeric variable.
|
|
|
|
ExpressionFormat getImplicitFormat() const override {
|
|
|
|
return Variable->getImplicitFormat();
|
|
|
|
}
|
2019-10-10 22:27:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Type of functions evaluating a given binary operation.
|
|
|
|
using binop_eval_t = uint64_t (*)(uint64_t, uint64_t);
|
|
|
|
|
|
|
|
/// Class representing a single binary operation in the AST of an expression.
|
2019-12-12 07:48:01 +08:00
|
|
|
class BinaryOperation : public ExpressionAST {
|
2019-10-10 22:27:14 +08:00
|
|
|
private:
|
|
|
|
/// Left operand.
|
2019-12-12 07:48:01 +08:00
|
|
|
std::unique_ptr<ExpressionAST> LeftOperand;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Right operand.
|
2019-12-12 07:48:01 +08:00
|
|
|
std::unique_ptr<ExpressionAST> RightOperand;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Pointer to function that can evaluate this binary operation.
|
|
|
|
binop_eval_t EvalBinop;
|
|
|
|
|
|
|
|
public:
|
2019-12-12 07:48:01 +08:00
|
|
|
BinaryOperation(binop_eval_t EvalBinop, std::unique_ptr<ExpressionAST> LeftOp,
|
|
|
|
std::unique_ptr<ExpressionAST> RightOp)
|
2019-10-10 22:27:14 +08:00
|
|
|
: EvalBinop(EvalBinop) {
|
|
|
|
LeftOperand = std::move(LeftOp);
|
|
|
|
RightOperand = std::move(RightOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluates the value of the binary operation represented by this AST,
|
|
|
|
/// using EvalBinop on the result of recursively evaluating the operands.
|
|
|
|
/// \returns the expression value or an error if an undefined numeric
|
|
|
|
/// variable is used in one of the operands.
|
2019-11-12 02:51:14 +08:00
|
|
|
Expected<uint64_t> eval() const override;
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
|
|
|
|
/// \returns the implicit format of this AST, if any, a format conflict if
|
|
|
|
/// the implicit formats of the AST's components conflict, or no format if
|
|
|
|
/// the AST has no implicit format (e.g. AST is made of a single literal).
|
|
|
|
ExpressionFormat getImplicitFormat() const override;
|
2019-10-10 22:27:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileCheckPatternContext;
|
|
|
|
|
|
|
|
/// Class representing a substitution to perform in the RegExStr string.
|
2019-12-12 07:48:01 +08:00
|
|
|
class Substitution {
|
2019-10-10 22:27:14 +08:00
|
|
|
protected:
|
|
|
|
/// Pointer to a class instance holding, among other things, the table with
|
|
|
|
/// the values of live string variables at the start of any given CHECK line.
|
|
|
|
/// Used for substituting string variables with the text they were defined
|
|
|
|
/// as. Expressions are linked to the numeric variables they use at
|
|
|
|
/// parse time and directly access the value of the numeric variable to
|
|
|
|
/// evaluate their value.
|
|
|
|
FileCheckPatternContext *Context;
|
|
|
|
|
|
|
|
/// The string that needs to be substituted for something else. For a
|
|
|
|
/// string variable this is its name, otherwise this is the whole expression.
|
|
|
|
StringRef FromStr;
|
|
|
|
|
|
|
|
// Index in RegExStr of where to do the substitution.
|
|
|
|
size_t InsertIdx;
|
|
|
|
|
|
|
|
public:
|
2019-12-12 07:48:01 +08:00
|
|
|
Substitution(FileCheckPatternContext *Context, StringRef VarName,
|
|
|
|
size_t InsertIdx)
|
2019-10-10 22:27:14 +08:00
|
|
|
: Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {}
|
|
|
|
|
2019-12-12 07:48:01 +08:00
|
|
|
virtual ~Substitution() = default;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// \returns the string to be substituted for something else.
|
|
|
|
StringRef getFromString() const { return FromStr; }
|
|
|
|
|
|
|
|
/// \returns the index where the substitution is to be performed in RegExStr.
|
|
|
|
size_t getIndex() const { return InsertIdx; }
|
|
|
|
|
|
|
|
/// \returns a string containing the result of the substitution represented
|
|
|
|
/// by this class instance or an error if substitution failed.
|
|
|
|
virtual Expected<std::string> getResult() const = 0;
|
|
|
|
};
|
|
|
|
|
2019-12-12 07:48:01 +08:00
|
|
|
class StringSubstitution : public Substitution {
|
2019-10-10 22:27:14 +08:00
|
|
|
public:
|
2019-12-12 07:48:01 +08:00
|
|
|
StringSubstitution(FileCheckPatternContext *Context, StringRef VarName,
|
|
|
|
size_t InsertIdx)
|
|
|
|
: Substitution(Context, VarName, InsertIdx) {}
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// \returns the text that the string variable in this substitution matched
|
|
|
|
/// when defined, or an error if the variable is undefined.
|
|
|
|
Expected<std::string> getResult() const override;
|
|
|
|
};
|
|
|
|
|
2019-12-12 07:48:01 +08:00
|
|
|
class NumericSubstitution : public Substitution {
|
2019-10-10 22:27:14 +08:00
|
|
|
private:
|
|
|
|
/// Pointer to the class representing the expression whose value is to be
|
|
|
|
/// substituted.
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
std::unique_ptr<Expression> ExpressionPointer;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
public:
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
NumericSubstitution(FileCheckPatternContext *Context, StringRef ExpressionStr,
|
|
|
|
std::unique_ptr<Expression> ExpressionPointer,
|
|
|
|
size_t InsertIdx)
|
|
|
|
: Substitution(Context, ExpressionStr, InsertIdx),
|
|
|
|
ExpressionPointer(std::move(ExpressionPointer)) {}
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// \returns a string containing the result of evaluating the expression in
|
|
|
|
/// this substitution, or an error if evaluation failed.
|
|
|
|
Expected<std::string> getResult() const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pattern handling code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
struct FileCheckDiag;
|
|
|
|
|
2019-12-12 07:48:01 +08:00
|
|
|
/// Class holding the Pattern global state, shared by all patterns: tables
|
|
|
|
/// holding values of variables and whether they are defined or not at any
|
|
|
|
/// given time in the matching process.
|
2019-10-10 22:27:14 +08:00
|
|
|
class FileCheckPatternContext {
|
2019-12-12 07:48:01 +08:00
|
|
|
friend class Pattern;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// When matching a given pattern, this holds the value of all the string
|
|
|
|
/// variables defined in previous patterns. In a pattern, only the last
|
|
|
|
/// definition for a given variable is recorded in this table.
|
|
|
|
/// Back-references are used for uses after any the other definition.
|
|
|
|
StringMap<StringRef> GlobalVariableTable;
|
|
|
|
|
|
|
|
/// Map of all string variables defined so far. Used at parse time to detect
|
|
|
|
/// a name conflict between a numeric variable and a string variable when
|
|
|
|
/// the former is defined on a later line than the latter.
|
|
|
|
StringMap<bool> DefinedVariableTable;
|
|
|
|
|
|
|
|
/// When matching a given pattern, this holds the pointers to the classes
|
|
|
|
/// representing the numeric variables defined in previous patterns. When
|
|
|
|
/// matching a pattern all definitions for that pattern are recorded in the
|
2019-12-12 07:48:01 +08:00
|
|
|
/// NumericVariableDefs table in the Pattern instance of that pattern.
|
|
|
|
StringMap<NumericVariable *> GlobalNumericVariableTable;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Pointer to the class instance representing the @LINE pseudo variable for
|
|
|
|
/// easily updating its value.
|
2019-12-12 07:48:01 +08:00
|
|
|
NumericVariable *LineVariable = nullptr;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Vector holding pointers to all parsed numeric variables. Used to
|
|
|
|
/// automatically free them once they are guaranteed to no longer be used.
|
2019-12-12 07:48:01 +08:00
|
|
|
std::vector<std::unique_ptr<NumericVariable>> NumericVariables;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
/// Vector holding pointers to all parsed expressions. Used to automatically
|
|
|
|
/// free the expressions once they are guaranteed to no longer be used.
|
|
|
|
std::vector<std::unique_ptr<Expression>> Expressions;
|
|
|
|
|
2019-10-10 22:27:14 +08:00
|
|
|
/// Vector holding pointers to all substitutions. Used to automatically free
|
|
|
|
/// them once they are guaranteed to no longer be used.
|
2019-12-12 07:48:01 +08:00
|
|
|
std::vector<std::unique_ptr<Substitution>> Substitutions;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// \returns the value of string variable \p VarName or an error if no such
|
|
|
|
/// variable has been defined.
|
|
|
|
Expected<StringRef> getPatternVarValue(StringRef VarName);
|
|
|
|
|
|
|
|
/// Defines string and numeric variables from definitions given on the
|
|
|
|
/// command line, passed as a vector of [#]VAR=VAL strings in
|
|
|
|
/// \p CmdlineDefines. \returns an error list containing diagnostics against
|
|
|
|
/// \p SM for all definition parsing failures, if any, or Success otherwise.
|
|
|
|
Error defineCmdlineVariables(std::vector<std::string> &CmdlineDefines,
|
|
|
|
SourceMgr &SM);
|
|
|
|
|
|
|
|
/// Create @LINE pseudo variable. Value is set when pattern are being
|
|
|
|
/// matched.
|
|
|
|
void createLineVariable();
|
|
|
|
|
|
|
|
/// Undefines local variables (variables whose name does not start with a '$'
|
|
|
|
/// sign), i.e. removes them from GlobalVariableTable and from
|
|
|
|
/// GlobalNumericVariableTable and also clears the value of numeric
|
|
|
|
/// variables.
|
|
|
|
void clearLocalVars();
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Makes a new numeric variable and registers it for destruction when the
|
|
|
|
/// context is destroyed.
|
2019-12-12 07:48:01 +08:00
|
|
|
template <class... Types> NumericVariable *makeNumericVariable(Types... args);
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Makes a new string substitution and registers it for destruction when the
|
|
|
|
/// context is destroyed.
|
2019-12-12 07:48:01 +08:00
|
|
|
Substitution *makeStringSubstitution(StringRef VarName, size_t InsertIdx);
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Makes a new numeric substitution and registers it for destruction when
|
|
|
|
/// the context is destroyed.
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
Substitution *makeNumericSubstitution(StringRef ExpressionStr,
|
|
|
|
std::unique_ptr<Expression> Expression,
|
|
|
|
size_t InsertIdx);
|
2019-10-10 22:27:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Class to represent an error holding a diagnostic with location information
|
|
|
|
/// used when printing it.
|
2019-12-12 07:48:01 +08:00
|
|
|
class ErrorDiagnostic : public ErrorInfo<ErrorDiagnostic> {
|
2019-10-10 22:27:14 +08:00
|
|
|
private:
|
|
|
|
SMDiagnostic Diagnostic;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2019-12-12 07:48:01 +08:00
|
|
|
ErrorDiagnostic(SMDiagnostic &&Diag) : Diagnostic(Diag) {}
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
std::error_code convertToErrorCode() const override {
|
|
|
|
return inconvertibleErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print diagnostic associated with this error when printing the error.
|
|
|
|
void log(raw_ostream &OS) const override { Diagnostic.print(nullptr, OS); }
|
|
|
|
|
|
|
|
static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg) {
|
2019-12-12 07:48:01 +08:00
|
|
|
return make_error<ErrorDiagnostic>(
|
2019-10-10 22:27:14 +08:00
|
|
|
SM.GetMessage(Loc, SourceMgr::DK_Error, ErrMsg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Error get(const SourceMgr &SM, StringRef Buffer, const Twine &ErrMsg) {
|
|
|
|
return get(SM, SMLoc::getFromPointer(Buffer.data()), ErrMsg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-12 07:48:01 +08:00
|
|
|
class NotFoundError : public ErrorInfo<NotFoundError> {
|
2019-10-10 22:27:14 +08:00
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
std::error_code convertToErrorCode() const override {
|
|
|
|
return inconvertibleErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print diagnostic associated with this error when printing the error.
|
|
|
|
void log(raw_ostream &OS) const override {
|
|
|
|
OS << "String not found in input";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-12 07:48:01 +08:00
|
|
|
class Pattern {
|
2019-10-10 22:27:14 +08:00
|
|
|
SMLoc PatternLoc;
|
|
|
|
|
|
|
|
/// A fixed string to match as the pattern or empty if this pattern requires
|
|
|
|
/// a regex match.
|
|
|
|
StringRef FixedStr;
|
|
|
|
|
|
|
|
/// A regex string to match as the pattern or empty if this pattern requires
|
|
|
|
/// a fixed string to match.
|
|
|
|
std::string RegExStr;
|
|
|
|
|
|
|
|
/// Entries in this vector represent a substitution of a string variable or
|
|
|
|
/// an expression in the RegExStr regex at match time. For example, in the
|
|
|
|
/// case of a CHECK directive with the pattern "foo[[bar]]baz[[#N+1]]",
|
|
|
|
/// RegExStr will contain "foobaz" and we'll get two entries in this vector
|
|
|
|
/// that tells us to insert the value of string variable "bar" at offset 3
|
|
|
|
/// and the value of expression "N+1" at offset 6.
|
2019-12-12 07:48:01 +08:00
|
|
|
std::vector<Substitution *> Substitutions;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Maps names of string variables defined in a pattern to the number of
|
|
|
|
/// their parenthesis group in RegExStr capturing their last definition.
|
|
|
|
///
|
|
|
|
/// E.g. for the pattern "foo[[bar:.*]]baz([[bar]][[QUUX]][[bar:.*]])",
|
|
|
|
/// RegExStr will be "foo(.*)baz(\1<quux value>(.*))" where <quux value> is
|
|
|
|
/// the value captured for QUUX on the earlier line where it was defined, and
|
|
|
|
/// VariableDefs will map "bar" to the third parenthesis group which captures
|
|
|
|
/// the second definition of "bar".
|
|
|
|
///
|
|
|
|
/// Note: uses std::map rather than StringMap to be able to get the key when
|
|
|
|
/// iterating over values.
|
|
|
|
std::map<StringRef, unsigned> VariableDefs;
|
|
|
|
|
|
|
|
/// Structure representing the definition of a numeric variable in a pattern.
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
/// It holds the pointer to the class instance holding the value and matching
|
|
|
|
/// format of the numeric variable whose value is being defined and the
|
|
|
|
/// number of the parenthesis group in RegExStr to capture that value.
|
2019-12-12 07:48:01 +08:00
|
|
|
struct NumericVariableMatch {
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
/// Pointer to class instance holding the value and matching format of the
|
|
|
|
/// numeric variable being defined.
|
2019-12-12 07:48:01 +08:00
|
|
|
NumericVariable *DefinedNumericVariable;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Number of the parenthesis group in RegExStr that captures the value of
|
|
|
|
/// this numeric variable definition.
|
|
|
|
unsigned CaptureParenGroup;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Holds the number of the parenthesis group in RegExStr and pointer to the
|
2019-12-12 07:48:01 +08:00
|
|
|
/// corresponding NumericVariable class instance of all numeric variable
|
|
|
|
/// definitions. Used to set the matched value of all those variables.
|
|
|
|
StringMap<NumericVariableMatch> NumericVariableDefs;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Pointer to a class instance holding the global state shared by all
|
|
|
|
/// patterns:
|
|
|
|
/// - separate tables with the values of live string and numeric variables
|
|
|
|
/// respectively at the start of any given CHECK line;
|
|
|
|
/// - table holding whether a string variable has been defined at any given
|
|
|
|
/// point during the parsing phase.
|
|
|
|
FileCheckPatternContext *Context;
|
|
|
|
|
|
|
|
Check::FileCheckType CheckTy;
|
|
|
|
|
|
|
|
/// Line number for this CHECK pattern or None if it is an implicit pattern.
|
|
|
|
/// Used to determine whether a variable definition is made on an earlier
|
|
|
|
/// line to the one with this CHECK.
|
|
|
|
Optional<size_t> LineNumber;
|
|
|
|
|
2019-10-11 19:59:14 +08:00
|
|
|
/// Ignore case while matching if set to true.
|
|
|
|
bool IgnoreCase = false;
|
|
|
|
|
2019-10-10 22:27:14 +08:00
|
|
|
public:
|
2019-12-12 07:48:01 +08:00
|
|
|
Pattern(Check::FileCheckType Ty, FileCheckPatternContext *Context,
|
|
|
|
Optional<size_t> Line = None)
|
2019-10-10 22:27:14 +08:00
|
|
|
: Context(Context), CheckTy(Ty), LineNumber(Line) {}
|
|
|
|
|
|
|
|
/// \returns the location in source code.
|
|
|
|
SMLoc getLoc() const { return PatternLoc; }
|
|
|
|
|
|
|
|
/// \returns the pointer to the global state for all patterns in this
|
|
|
|
/// FileCheck instance.
|
|
|
|
FileCheckPatternContext *getContext() const { return Context; }
|
|
|
|
|
|
|
|
/// \returns whether \p C is a valid first character for a variable name.
|
|
|
|
static bool isValidVarNameStart(char C);
|
|
|
|
|
|
|
|
/// Parsing information about a variable.
|
|
|
|
struct VariableProperties {
|
|
|
|
StringRef Name;
|
|
|
|
bool IsPseudo;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Parses the string at the start of \p Str for a variable name. \returns
|
|
|
|
/// a VariableProperties structure holding the variable name and whether it
|
|
|
|
/// is the name of a pseudo variable, or an error holding a diagnostic
|
|
|
|
/// against \p SM if parsing fail. If parsing was successful, also strips
|
|
|
|
/// \p Str from the variable name.
|
|
|
|
static Expected<VariableProperties> parseVariable(StringRef &Str,
|
|
|
|
const SourceMgr &SM);
|
|
|
|
/// Parses \p Expr for a numeric substitution block at line \p LineNumber,
|
|
|
|
/// or before input is parsed if \p LineNumber is None. Parameter
|
|
|
|
/// \p IsLegacyLineExpr indicates whether \p Expr should be a legacy @LINE
|
|
|
|
/// expression and \p Context points to the class instance holding the live
|
|
|
|
/// string and numeric variables. \returns a pointer to the class instance
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
/// representing the expression whose value must be substitued, or an error
|
|
|
|
/// holding a diagnostic against \p SM if parsing fails. If substitution was
|
|
|
|
/// successful, sets \p DefinedNumericVariable to point to the class
|
|
|
|
/// representing the numeric variable defined in this numeric substitution
|
|
|
|
/// block, or None if this block does not define any variable.
|
|
|
|
static Expected<std::unique_ptr<Expression>> parseNumericSubstitutionBlock(
|
2019-12-12 07:48:01 +08:00
|
|
|
StringRef Expr, Optional<NumericVariable *> &DefinedNumericVariable,
|
2019-10-10 22:27:14 +08:00
|
|
|
bool IsLegacyLineExpr, Optional<size_t> LineNumber,
|
|
|
|
FileCheckPatternContext *Context, const SourceMgr &SM);
|
2019-12-12 07:48:01 +08:00
|
|
|
/// Parses the pattern in \p PatternStr and initializes this Pattern instance
|
|
|
|
/// accordingly.
|
2019-10-10 22:27:14 +08:00
|
|
|
///
|
|
|
|
/// \p Prefix provides which prefix is being matched, \p Req describes the
|
|
|
|
/// global options that influence the parsing such as whitespace
|
|
|
|
/// canonicalization, \p SM provides the SourceMgr used for error reports.
|
|
|
|
/// \returns true in case of an error, false otherwise.
|
|
|
|
bool parsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
|
|
|
|
const FileCheckRequest &Req);
|
|
|
|
/// Matches the pattern string against the input buffer \p Buffer
|
|
|
|
///
|
|
|
|
/// \returns the position that is matched or an error indicating why matching
|
|
|
|
/// failed. If there is a match, updates \p MatchLen with the size of the
|
|
|
|
/// matched string.
|
|
|
|
///
|
|
|
|
/// The GlobalVariableTable StringMap in the FileCheckPatternContext class
|
2019-12-12 07:48:01 +08:00
|
|
|
/// instance provides the current values of FileCheck string variables and is
|
|
|
|
/// updated if this match defines new values. Likewise, the
|
2019-10-10 22:27:14 +08:00
|
|
|
/// GlobalNumericVariableTable StringMap in the same class provides the
|
|
|
|
/// current values of FileCheck numeric variables and is updated if this
|
|
|
|
/// match defines new numeric values.
|
|
|
|
Expected<size_t> match(StringRef Buffer, size_t &MatchLen,
|
|
|
|
const SourceMgr &SM) const;
|
|
|
|
/// Prints the value of successful substitutions or the name of the undefined
|
|
|
|
/// string or numeric variables preventing a successful substitution.
|
|
|
|
void printSubstitutions(const SourceMgr &SM, StringRef Buffer,
|
|
|
|
SMRange MatchRange = None) const;
|
|
|
|
void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
|
|
|
|
std::vector<FileCheckDiag> *Diags) const;
|
|
|
|
|
|
|
|
bool hasVariable() const {
|
|
|
|
return !(Substitutions.empty() && VariableDefs.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
Check::FileCheckType getCheckTy() const { return CheckTy; }
|
|
|
|
|
|
|
|
int getCount() const { return CheckTy.getCount(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
|
|
|
|
void AddBackrefToRegEx(unsigned BackrefNum);
|
|
|
|
/// Computes an arbitrary estimate for the quality of matching this pattern
|
|
|
|
/// at the start of \p Buffer; a distance of zero should correspond to a
|
|
|
|
/// perfect match.
|
|
|
|
unsigned computeMatchDistance(StringRef Buffer) const;
|
|
|
|
/// Finds the closing sequence of a regex variable usage or definition.
|
|
|
|
///
|
|
|
|
/// \p Str has to point in the beginning of the definition (right after the
|
2019-11-12 19:14:03 +08:00
|
|
|
/// opening sequence). \p SM holds the SourceMgr used for error reporting.
|
2019-10-10 22:27:14 +08:00
|
|
|
/// \returns the offset of the closing sequence within Str, or npos if it
|
|
|
|
/// was not found.
|
2019-11-12 19:14:03 +08:00
|
|
|
static size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Parses \p Expr for the name of a numeric variable to be defined at line
|
|
|
|
/// \p LineNumber, or before input is parsed if \p LineNumber is None.
|
|
|
|
/// \returns a pointer to the class instance representing that variable,
|
|
|
|
/// creating it if needed, or an error holding a diagnostic against \p SM
|
|
|
|
/// should defining such a variable be invalid.
|
2019-12-12 07:48:01 +08:00
|
|
|
static Expected<NumericVariable *> parseNumericVariableDefinition(
|
2019-10-10 22:27:14 +08:00
|
|
|
StringRef &Expr, FileCheckPatternContext *Context,
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
Optional<size_t> LineNumber, ExpressionFormat ImplicitFormat,
|
|
|
|
const SourceMgr &SM);
|
2019-10-10 22:27:14 +08:00
|
|
|
/// Parses \p Name as a (pseudo if \p IsPseudo is true) numeric variable use
|
|
|
|
/// at line \p LineNumber, or before input is parsed if \p LineNumber is
|
|
|
|
/// None. Parameter \p Context points to the class instance holding the live
|
|
|
|
/// string and numeric variables. \returns the pointer to the class instance
|
|
|
|
/// representing that variable if successful, or an error holding a
|
|
|
|
/// diagnostic against \p SM otherwise.
|
2019-12-12 07:48:01 +08:00
|
|
|
static Expected<std::unique_ptr<NumericVariableUse>> parseNumericVariableUse(
|
|
|
|
StringRef Name, bool IsPseudo, Optional<size_t> LineNumber,
|
|
|
|
FileCheckPatternContext *Context, const SourceMgr &SM);
|
FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
2019-03-06 07:20:29 +08:00
|
|
|
enum class AllowedOperand { LineVar, LegacyLiteral, Any };
|
2019-10-10 22:27:14 +08:00
|
|
|
/// Parses \p Expr for use of a numeric operand at line \p LineNumber, or
|
|
|
|
/// before input is parsed if \p LineNumber is None. Accepts both literal
|
|
|
|
/// values and numeric variables, depending on the value of \p AO. Parameter
|
|
|
|
/// \p Context points to the class instance holding the live string and
|
|
|
|
/// numeric variables. \returns the class representing that operand in the
|
|
|
|
/// AST of the expression or an error holding a diagnostic against \p SM
|
|
|
|
/// otherwise.
|
2019-12-12 07:48:01 +08:00
|
|
|
static Expected<std::unique_ptr<ExpressionAST>>
|
2019-10-10 22:27:14 +08:00
|
|
|
parseNumericOperand(StringRef &Expr, AllowedOperand AO,
|
|
|
|
Optional<size_t> LineNumber,
|
|
|
|
FileCheckPatternContext *Context, const SourceMgr &SM);
|
|
|
|
/// Parses \p Expr for a binary operation at line \p LineNumber, or before
|
|
|
|
/// input is parsed if \p LineNumber is None. The left operand of this binary
|
|
|
|
/// operation is given in \p LeftOp and \p IsLegacyLineExpr indicates whether
|
|
|
|
/// we are parsing a legacy @LINE expression. Parameter \p Context points to
|
|
|
|
/// the class instance holding the live string and numeric variables.
|
|
|
|
/// \returns the class representing the binary operation in the AST of the
|
|
|
|
/// expression, or an error holding a diagnostic against \p SM otherwise.
|
2019-12-12 07:48:01 +08:00
|
|
|
static Expected<std::unique_ptr<ExpressionAST>>
|
|
|
|
parseBinop(StringRef &Expr, std::unique_ptr<ExpressionAST> LeftOp,
|
2019-10-10 22:27:14 +08:00
|
|
|
bool IsLegacyLineExpr, Optional<size_t> LineNumber,
|
|
|
|
FileCheckPatternContext *Context, const SourceMgr &SM);
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check Strings.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// A check that we found in the input file.
|
|
|
|
struct FileCheckString {
|
|
|
|
/// The pattern to match.
|
2019-12-12 07:48:01 +08:00
|
|
|
Pattern Pat;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
|
|
|
/// Which prefix name this check matched.
|
|
|
|
StringRef Prefix;
|
|
|
|
|
|
|
|
/// The location in the match file that the check string was specified.
|
|
|
|
SMLoc Loc;
|
|
|
|
|
|
|
|
/// All of the strings that are disallowed from occurring between this match
|
|
|
|
/// string and the previous one (or start of file).
|
2019-12-12 07:48:01 +08:00
|
|
|
std::vector<Pattern> DagNotStrings;
|
2019-10-10 22:27:14 +08:00
|
|
|
|
2019-12-12 07:48:01 +08:00
|
|
|
FileCheckString(const Pattern &P, StringRef S, SMLoc L)
|
2019-10-10 22:27:14 +08:00
|
|
|
: Pat(P), Prefix(S), Loc(L) {}
|
|
|
|
|
|
|
|
/// Matches check string and its "not strings" and/or "dag strings".
|
|
|
|
size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
|
|
|
|
size_t &MatchLen, FileCheckRequest &Req,
|
|
|
|
std::vector<FileCheckDiag> *Diags) const;
|
|
|
|
|
|
|
|
/// Verifies that there is a single line in the given \p Buffer. Errors are
|
|
|
|
/// reported against \p SM.
|
|
|
|
bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
|
|
|
|
/// Verifies that there is no newline in the given \p Buffer. Errors are
|
|
|
|
/// reported against \p SM.
|
|
|
|
bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
|
|
|
|
/// Verifies that none of the strings in \p NotStrings are found in the given
|
|
|
|
/// \p Buffer. Errors are reported against \p SM and diagnostics recorded in
|
|
|
|
/// \p Diags according to the verbosity level set in \p Req.
|
|
|
|
bool CheckNot(const SourceMgr &SM, StringRef Buffer,
|
2019-12-12 07:48:01 +08:00
|
|
|
const std::vector<const Pattern *> &NotStrings,
|
2019-10-10 22:27:14 +08:00
|
|
|
const FileCheckRequest &Req,
|
|
|
|
std::vector<FileCheckDiag> *Diags) const;
|
|
|
|
/// Matches "dag strings" and their mixed "not strings".
|
|
|
|
size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
|
2019-12-12 07:48:01 +08:00
|
|
|
std::vector<const Pattern *> &NotStrings,
|
2019-10-10 22:27:14 +08:00
|
|
|
const FileCheckRequest &Req,
|
|
|
|
std::vector<FileCheckDiag> *Diags) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
#endif
|