blob: 0b6861009fb3e6b97686480944bac6707d047d3a [file] [log] [blame]
//==-- llvm/Support/FileCheck.h ---------------------------*- C++ -*-==//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
/// \file This file has some utilities to use FileCheck as an API
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/SourceMgr.h"
#include <vector>
#include <map>
namespace llvm {
/// Contains info about various FileCheck options.
struct FileCheckRequest {
std::vector<std::string> CheckPrefixes;
bool NoCanonicalizeWhiteSpace = false;
std::vector<std::string> ImplicitCheckNot;
std::vector<std::string> GlobalDefines;
bool AllowEmptyInput = false;
bool MatchFullLines = false;
bool EnableVarScope = false;
bool AllowDeprecatedDagOverlap = false;
bool Verbose = false;
bool VerboseVerbose = false;
// Pattern Handling Code.
namespace Check {
enum FileCheckKind {
CheckNone = 0,
/// Indicates the pattern only matches the end of file. This is used for
/// trailing CHECK-NOTs.
/// Marks when parsing found a -NOT check combined with another CHECK suffix.
/// Marks when parsing found a -COUNT directive with invalid count value.
class FileCheckType {
FileCheckKind Kind;
int Count; ///< optional Count for some checks
FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
FileCheckType(const FileCheckType &) = default;
operator FileCheckKind() const { return Kind; }
int getCount() const { return Count; }
FileCheckType &setCount(int C);
std::string getDescription(StringRef Prefix) const;
} // namespace Check
struct FileCheckDiag;
/// Class holding the FileCheckPattern 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.
class FileCheckPatternContext {
friend class FileCheckPattern;
/// When matching a given pattern, this holds the value of all the FileCheck
/// 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;
/// Return the value of variable \p VarName or None if no such variable has
/// been defined.
llvm::Optional<StringRef> getVarValue(StringRef VarName);
/// Define variables from definitions given on the command line passed as a
/// vector of VAR=VAL strings in \p CmdlineDefines.
void defineCmdlineVariables(std::vector<std::string> &CmdlineDefines);
/// Undefine local variables (variables whose name does not start with a '$'
/// sign), i.e. remove them from GlobalVariableTable.
void clearLocalVars();
class FileCheckPattern {
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 map to uses of a variable in the pattern, e.g.
/// "foo[[bar]]baz". In this case, the RegExStr will contain "foobaz" and
/// we'll get an entry in this vector that tells us to insert the value of
/// bar at offset 3.
std::vector<std::pair<StringRef, unsigned>> VariableUses;
/// Maps definitions of variables to their parenthesized capture numbers.
/// E.g. for the pattern "foo[[bar:.*]]baz", VariableDefs will map "bar" to
/// 1.
std::map<StringRef, unsigned> VariableDefs;
/// Pointer to the class instance shared by all patterns holding a table with
/// the values of live variables at the start of any given CHECK line.
FileCheckPatternContext *Context;
Check::FileCheckType CheckTy;
/// Contains the number of line this pattern is in.
unsigned LineNumber;
explicit FileCheckPattern(Check::FileCheckType Ty,
FileCheckPatternContext *Context)
: Context(Context), CheckTy(Ty) {}
/// 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; }
bool ParsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
unsigned LineNumber, const FileCheckRequest &Req);
size_t match(StringRef Buffer, size_t &MatchLen) const;
/// Print value of successful substitutions or name of undefined pattern
/// variables preventing such a successful substitution.
void printVariableUses(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 !(VariableUses.empty() && VariableDefs.empty());
Check::FileCheckType getCheckTy() const { return CheckTy; }
int getCount() const { return CheckTy.getCount(); }
bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
void AddBackrefToRegEx(unsigned BackrefNum);
unsigned computeMatchDistance(StringRef Buffer) const;
bool EvaluateExpression(StringRef Expr, std::string &Value) const;
size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
/// Summary of a FileCheck diagnostic.
struct FileCheckDiag {
/// What is the FileCheck directive for this diagnostic?
Check::FileCheckType CheckTy;
/// Where is the FileCheck directive for this diagnostic?
unsigned CheckLine, CheckCol;
/// What type of match result does this diagnostic describe?
/// A directive's supplied pattern is said to be either expected or excluded
/// depending on whether the pattern must have or must not have a match in
/// order for the directive to succeed. For example, a CHECK directive's
/// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
/// All match result types whose names end with "Excluded" are for excluded
/// patterns, and all others are for expected patterns.
/// There might be more than one match result for a single pattern. For
/// example, there might be several discarded matches
/// (MatchFoundButDiscarded) before either a good match
/// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
/// and there might be a fuzzy match (MatchFuzzy) after the latter.
enum MatchType {
/// Indicates a good match for an expected pattern.
/// Indicates a match for an excluded pattern.
/// Indicates a match for an expected pattern, but the match is on the
/// wrong line.
/// Indicates a discarded match for an expected pattern.
/// Indicates no match for an excluded pattern.
/// Indicates no match for an expected pattern, but this might follow good
/// matches when multiple matches are expected for the pattern, or it might
/// follow discarded matches for the pattern.
/// Indicates a fuzzy match that serves as a suggestion for the next
/// intended match for an expected pattern with too few or no good matches.
} MatchTy;
/// The search range if MatchTy is MatchNoneAndExcluded or
/// MatchNoneButExpected, or the match range otherwise.
unsigned InputStartLine;
unsigned InputStartCol;
unsigned InputEndLine;
unsigned InputEndCol;
FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy,
SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange);
// Check Strings.
/// A check that we found in the input file.
struct FileCheckString {
/// The pattern to match.
FileCheckPattern Pat;
/// 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).
std::vector<FileCheckPattern> DagNotStrings;
FileCheckString(const FileCheckPattern &P, StringRef S, SMLoc L)
: Pat(P), Prefix(S), Loc(L) {}
size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
size_t &MatchLen, FileCheckRequest &Req,
std::vector<FileCheckDiag> *Diags) const;
bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
bool CheckNot(const SourceMgr &SM, StringRef Buffer,
const std::vector<const FileCheckPattern *> &NotStrings,
const FileCheckRequest &Req,
std::vector<FileCheckDiag> *Diags) const;
size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
std::vector<const FileCheckPattern *> &NotStrings,
const FileCheckRequest &Req,
std::vector<FileCheckDiag> *Diags) const;
/// FileCheck class takes the request and exposes various methods that
/// use information from the request.
class FileCheck {
FileCheckRequest Req;
FileCheckPatternContext PatternContext;
FileCheck(FileCheckRequest Req) : Req(Req) {}
// Combines the check prefixes into a single regex so that we can efficiently
// scan for any of the set.
// The semantics are that the longest-match wins which matches our regex
// library.
Regex buildCheckPrefixRegex();
/// Read the check file, which specifies the sequence of expected strings.
/// The strings are added to the CheckStrings vector. Returns true in case of
/// an error, false otherwise.
bool ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
std::vector<FileCheckString> &CheckStrings);
bool ValidateCheckPrefixes();
/// Canonicalize whitespaces in the file. Line endings are replaced with
/// UNIX-style '\n'.
StringRef CanonicalizeFile(MemoryBuffer &MB,
SmallVectorImpl<char> &OutputBuffer);
/// Check the input to FileCheck provided in the \p Buffer against the \p
/// CheckStrings read from the check file.
/// Returns false if the input fails to satisfy the checks.
bool CheckInput(SourceMgr &SM, StringRef Buffer,
ArrayRef<FileCheckString> CheckStrings,
std::vector<FileCheckDiag> *Diags = nullptr);
} // namespace llvm