FileCheck.h 7.5 KB

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