FileCheck.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //==-- llvm/FileCheck/FileCheck.h --------------------------------*- C++ -*-==//
  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. /// \file This file has some utilities to use FileCheck as an API
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_FILECHECK_FILECHECK_H
  13. #define LLVM_FILECHECK_FILECHECK_H
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/Support/MemoryBuffer.h"
  16. #include "llvm/Support/Regex.h"
  17. #include "llvm/Support/SourceMgr.h"
  18. #include <bitset>
  19. #include <string>
  20. #include <vector>
  21. namespace llvm {
  22. /// Contains info about various FileCheck options.
  23. struct FileCheckRequest {
  24. std::vector<StringRef> CheckPrefixes;
  25. std::vector<StringRef> CommentPrefixes;
  26. bool NoCanonicalizeWhiteSpace = false;
  27. std::vector<StringRef> ImplicitCheckNot;
  28. std::vector<StringRef> GlobalDefines;
  29. bool AllowEmptyInput = false;
  30. bool AllowUnusedPrefixes = false;
  31. bool MatchFullLines = false;
  32. bool IgnoreCase = false;
  33. bool IsDefaultCheckPrefix = false;
  34. bool EnableVarScope = false;
  35. bool AllowDeprecatedDagOverlap = false;
  36. bool Verbose = false;
  37. bool VerboseVerbose = false;
  38. };
  39. namespace Check {
  40. enum FileCheckKind {
  41. CheckNone = 0,
  42. CheckPlain,
  43. CheckNext,
  44. CheckSame,
  45. CheckNot,
  46. CheckDAG,
  47. CheckLabel,
  48. CheckEmpty,
  49. CheckComment,
  50. /// Indicates the pattern only matches the end of file. This is used for
  51. /// trailing CHECK-NOTs.
  52. CheckEOF,
  53. /// Marks when parsing found a -NOT check combined with another CHECK suffix.
  54. CheckBadNot,
  55. /// Marks when parsing found a -COUNT directive with invalid count value.
  56. CheckBadCount
  57. };
  58. enum FileCheckKindModifier {
  59. /// Modifies directive to perform literal match.
  60. ModifierLiteral = 0,
  61. // The number of modifier.
  62. Size
  63. };
  64. class FileCheckType {
  65. FileCheckKind Kind;
  66. int Count; ///< optional Count for some checks
  67. /// Modifers for the check directive.
  68. std::bitset<FileCheckKindModifier::Size> Modifiers;
  69. public:
  70. FileCheckType(FileCheckKind Kind = CheckNone)
  71. : Kind(Kind), Count(1), Modifiers() {}
  72. FileCheckType(const FileCheckType &) = default;
  73. FileCheckType &operator=(const FileCheckType &) = default;
  74. operator FileCheckKind() const { return Kind; }
  75. int getCount() const { return Count; }
  76. FileCheckType &setCount(int C);
  77. bool isLiteralMatch() const {
  78. return Modifiers[FileCheckKindModifier::ModifierLiteral];
  79. }
  80. FileCheckType &setLiteralMatch(bool Literal = true) {
  81. Modifiers.set(FileCheckKindModifier::ModifierLiteral, Literal);
  82. return *this;
  83. }
  84. // \returns a description of \p Prefix.
  85. std::string getDescription(StringRef Prefix) const;
  86. // \returns a description of \p Modifiers.
  87. std::string getModifiersDescription() const;
  88. };
  89. } // namespace Check
  90. /// Summary of a FileCheck diagnostic.
  91. struct FileCheckDiag {
  92. /// What is the FileCheck directive for this diagnostic?
  93. Check::FileCheckType CheckTy;
  94. /// Where is the FileCheck directive for this diagnostic?
  95. SMLoc CheckLoc;
  96. /// What type of match result does this diagnostic describe?
  97. ///
  98. /// A directive's supplied pattern is said to be either expected or excluded
  99. /// depending on whether the pattern must have or must not have a match in
  100. /// order for the directive to succeed. For example, a CHECK directive's
  101. /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
  102. ///
  103. /// There might be more than one match result for a single pattern. For
  104. /// example, there might be several discarded matches
  105. /// (MatchFoundButDiscarded) before either a good match
  106. /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
  107. /// and there might be a fuzzy match (MatchFuzzy) after the latter.
  108. enum MatchType {
  109. /// Indicates a good match for an expected pattern.
  110. MatchFoundAndExpected,
  111. /// Indicates a match for an excluded pattern.
  112. MatchFoundButExcluded,
  113. /// Indicates a match for an expected pattern, but the match is on the
  114. /// wrong line.
  115. MatchFoundButWrongLine,
  116. /// Indicates a discarded match for an expected pattern.
  117. MatchFoundButDiscarded,
  118. /// Indicates an error while processing a match after the match was found
  119. /// for an expected or excluded pattern. The error is specified by \c Note,
  120. /// to which it should be appropriate to prepend "error: " later. The full
  121. /// match itself should be recorded in a preceding diagnostic of a different
  122. /// \c MatchFound match type.
  123. MatchFoundErrorNote,
  124. /// Indicates no match for an excluded pattern.
  125. MatchNoneAndExcluded,
  126. /// Indicates no match for an expected pattern, but this might follow good
  127. /// matches when multiple matches are expected for the pattern, or it might
  128. /// follow discarded matches for the pattern.
  129. MatchNoneButExpected,
  130. /// Indicates no match due to an expected or excluded pattern that has
  131. /// proven to be invalid at match time. The exact problems are usually
  132. /// reported in subsequent diagnostics of the same match type but with
  133. /// \c Note set.
  134. MatchNoneForInvalidPattern,
  135. /// Indicates a fuzzy match that serves as a suggestion for the next
  136. /// intended match for an expected pattern with too few or no good matches.
  137. MatchFuzzy,
  138. } MatchTy;
  139. /// The search range if MatchTy starts with MatchNone, or the match range
  140. /// otherwise.
  141. unsigned InputStartLine;
  142. unsigned InputStartCol;
  143. unsigned InputEndLine;
  144. unsigned InputEndCol;
  145. /// A note to replace the one normally indicated by MatchTy, or the empty
  146. /// string if none.
  147. std::string Note;
  148. FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy,
  149. SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange,
  150. StringRef Note = "");
  151. };
  152. class FileCheckPatternContext;
  153. struct FileCheckString;
  154. /// FileCheck class takes the request and exposes various methods that
  155. /// use information from the request.
  156. class FileCheck {
  157. FileCheckRequest Req;
  158. std::unique_ptr<FileCheckPatternContext> PatternContext;
  159. // C++17 TODO: make this a plain std::vector.
  160. std::unique_ptr<std::vector<FileCheckString>> CheckStrings;
  161. public:
  162. explicit FileCheck(FileCheckRequest Req);
  163. ~FileCheck();
  164. // Combines the check prefixes into a single regex so that we can efficiently
  165. // scan for any of the set.
  166. //
  167. // The semantics are that the longest-match wins which matches our regex
  168. // library.
  169. Regex buildCheckPrefixRegex();
  170. /// Reads the check file from \p Buffer and records the expected strings it
  171. /// contains. Errors are reported against \p SM.
  172. ///
  173. /// Only expected strings whose prefix is one of those listed in \p PrefixRE
  174. /// are recorded. \returns true in case of an error, false otherwise.
  175. ///
  176. /// If \p ImpPatBufferIDRange, then the range (inclusive start, exclusive end)
  177. /// of IDs for source buffers added to \p SM for implicit patterns are
  178. /// recorded in it. The range is empty if there are none.
  179. bool
  180. readCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
  181. std::pair<unsigned, unsigned> *ImpPatBufferIDRange = nullptr);
  182. bool ValidateCheckPrefixes();
  183. /// Canonicalizes whitespaces in the file. Line endings are replaced with
  184. /// UNIX-style '\n'.
  185. StringRef CanonicalizeFile(MemoryBuffer &MB,
  186. SmallVectorImpl<char> &OutputBuffer);
  187. /// Checks the input to FileCheck provided in the \p Buffer against the
  188. /// expected strings read from the check file and record diagnostics emitted
  189. /// in \p Diags. Errors are recorded against \p SM.
  190. ///
  191. /// \returns false if the input fails to satisfy the checks.
  192. bool checkInput(SourceMgr &SM, StringRef Buffer,
  193. std::vector<FileCheckDiag> *Diags = nullptr);
  194. };
  195. } // namespace llvm
  196. #endif