Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:55

0001 //==-- llvm/FileCheck/FileCheck.h --------------------------------*- C++ -*-==//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file This file has some utilities to use FileCheck as an API
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_FILECHECK_FILECHECK_H
0014 #define LLVM_FILECHECK_FILECHECK_H
0015 
0016 #include "llvm/ADT/StringRef.h"
0017 #include "llvm/Support/Regex.h"
0018 #include "llvm/Support/SMLoc.h"
0019 #include <bitset>
0020 #include <memory>
0021 #include <string>
0022 #include <vector>
0023 
0024 namespace llvm {
0025 class MemoryBuffer;
0026 class SourceMgr;
0027 template <typename T> class SmallVectorImpl;
0028 
0029 /// Contains info about various FileCheck options.
0030 struct FileCheckRequest {
0031   std::vector<StringRef> CheckPrefixes;
0032   std::vector<StringRef> CommentPrefixes;
0033   bool NoCanonicalizeWhiteSpace = false;
0034   std::vector<StringRef> ImplicitCheckNot;
0035   std::vector<StringRef> GlobalDefines;
0036   bool AllowEmptyInput = false;
0037   bool AllowUnusedPrefixes = false;
0038   bool MatchFullLines = false;
0039   bool IgnoreCase = false;
0040   bool IsDefaultCheckPrefix = false;
0041   bool EnableVarScope = false;
0042   bool AllowDeprecatedDagOverlap = false;
0043   bool Verbose = false;
0044   bool VerboseVerbose = false;
0045 };
0046 
0047 namespace Check {
0048 
0049 enum FileCheckKind {
0050   CheckNone = 0,
0051   CheckMisspelled,
0052   CheckPlain,
0053   CheckNext,
0054   CheckSame,
0055   CheckNot,
0056   CheckDAG,
0057   CheckLabel,
0058   CheckEmpty,
0059   CheckComment,
0060 
0061   /// Indicates the pattern only matches the end of file. This is used for
0062   /// trailing CHECK-NOTs.
0063   CheckEOF,
0064 
0065   /// Marks when parsing found a -NOT check combined with another CHECK suffix.
0066   CheckBadNot,
0067 
0068   /// Marks when parsing found a -COUNT directive with invalid count value.
0069   CheckBadCount
0070 };
0071 
0072 enum FileCheckKindModifier {
0073   /// Modifies directive to perform literal match.
0074   ModifierLiteral = 0,
0075 
0076   // The number of modifier.
0077   Size
0078 };
0079 
0080 class FileCheckType {
0081   FileCheckKind Kind;
0082   int Count; ///< optional Count for some checks
0083   /// Modifers for the check directive.
0084   std::bitset<FileCheckKindModifier::Size> Modifiers;
0085 
0086 public:
0087   FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
0088   FileCheckType(const FileCheckType &) = default;
0089   FileCheckType &operator=(const FileCheckType &) = default;
0090 
0091   operator FileCheckKind() const { return Kind; }
0092 
0093   int getCount() const { return Count; }
0094   FileCheckType &setCount(int C);
0095 
0096   bool isLiteralMatch() const {
0097     return Modifiers[FileCheckKindModifier::ModifierLiteral];
0098   }
0099   FileCheckType &setLiteralMatch(bool Literal = true) {
0100     Modifiers.set(FileCheckKindModifier::ModifierLiteral, Literal);
0101     return *this;
0102   }
0103 
0104   // \returns a description of \p Prefix.
0105   std::string getDescription(StringRef Prefix) const;
0106 
0107   // \returns a description of \p Modifiers.
0108   std::string getModifiersDescription() const;
0109 };
0110 } // namespace Check
0111 
0112 /// Summary of a FileCheck diagnostic.
0113 struct FileCheckDiag {
0114   /// What is the FileCheck directive for this diagnostic?
0115   Check::FileCheckType CheckTy;
0116   /// Where is the FileCheck directive for this diagnostic?
0117   SMLoc CheckLoc;
0118   /// What type of match result does this diagnostic describe?
0119   ///
0120   /// A directive's supplied pattern is said to be either expected or excluded
0121   /// depending on whether the pattern must have or must not have a match in
0122   /// order for the directive to succeed.  For example, a CHECK directive's
0123   /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
0124   ///
0125   /// There might be more than one match result for a single pattern.  For
0126   /// example, there might be several discarded matches
0127   /// (MatchFoundButDiscarded) before either a good match
0128   /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
0129   /// and there might be a fuzzy match (MatchFuzzy) after the latter.
0130   enum MatchType {
0131     /// Indicates a good match for an expected pattern.
0132     MatchFoundAndExpected,
0133     /// Indicates a match for an excluded pattern.
0134     MatchFoundButExcluded,
0135     /// Indicates a match for an expected pattern, but the match is on the
0136     /// wrong line.
0137     MatchFoundButWrongLine,
0138     /// Indicates a discarded match for an expected pattern.
0139     MatchFoundButDiscarded,
0140     /// Indicates an error while processing a match after the match was found
0141     /// for an expected or excluded pattern.  The error is specified by \c Note,
0142     /// to which it should be appropriate to prepend "error: " later.  The full
0143     /// match itself should be recorded in a preceding diagnostic of a different
0144     /// \c MatchFound match type.
0145     MatchFoundErrorNote,
0146     /// Indicates no match for an excluded pattern.
0147     MatchNoneAndExcluded,
0148     /// Indicates no match for an expected pattern, but this might follow good
0149     /// matches when multiple matches are expected for the pattern, or it might
0150     /// follow discarded matches for the pattern.
0151     MatchNoneButExpected,
0152     /// Indicates no match due to an expected or excluded pattern that has
0153     /// proven to be invalid at match time.  The exact problems are usually
0154     /// reported in subsequent diagnostics of the same match type but with
0155     /// \c Note set.
0156     MatchNoneForInvalidPattern,
0157     /// Indicates a fuzzy match that serves as a suggestion for the next
0158     /// intended match for an expected pattern with too few or no good matches.
0159     MatchFuzzy,
0160   } MatchTy;
0161   /// The search range if MatchTy starts with MatchNone, or the match range
0162   /// otherwise.
0163   unsigned InputStartLine;
0164   unsigned InputStartCol;
0165   unsigned InputEndLine;
0166   unsigned InputEndCol;
0167   /// A note to replace the one normally indicated by MatchTy, or the empty
0168   /// string if none.
0169   std::string Note;
0170   FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy,
0171                 SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange,
0172                 StringRef Note = "");
0173 };
0174 
0175 class FileCheckPatternContext;
0176 struct FileCheckString;
0177 
0178 /// FileCheck class takes the request and exposes various methods that
0179 /// use information from the request.
0180 class FileCheck {
0181   FileCheckRequest Req;
0182   std::unique_ptr<FileCheckPatternContext> PatternContext;
0183   std::vector<FileCheckString> CheckStrings;
0184 
0185 public:
0186   explicit FileCheck(FileCheckRequest Req);
0187   ~FileCheck();
0188 
0189   /// Reads the check file from \p Buffer and records the expected strings it
0190   /// contains. Errors are reported against \p SM.
0191   ///
0192   /// If \p ImpPatBufferIDRange, then the range (inclusive start, exclusive end)
0193   /// of IDs for source buffers added to \p SM for implicit patterns are
0194   /// recorded in it.  The range is empty if there are none.
0195   bool
0196   readCheckFile(SourceMgr &SM, StringRef Buffer,
0197                 std::pair<unsigned, unsigned> *ImpPatBufferIDRange = nullptr);
0198 
0199   bool ValidateCheckPrefixes();
0200 
0201   /// Canonicalizes whitespaces in the file. Line endings are replaced with
0202   /// UNIX-style '\n'.
0203   StringRef CanonicalizeFile(MemoryBuffer &MB,
0204                              SmallVectorImpl<char> &OutputBuffer);
0205 
0206   /// Checks the input to FileCheck provided in the \p Buffer against the
0207   /// expected strings read from the check file and record diagnostics emitted
0208   /// in \p Diags. Errors are recorded against \p SM.
0209   ///
0210   /// \returns false if the input fails to satisfy the checks.
0211   bool checkInput(SourceMgr &SM, StringRef Buffer,
0212                   std::vector<FileCheckDiag> *Diags = nullptr);
0213 };
0214 
0215 } // namespace llvm
0216 
0217 #endif