Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 1 | //===- FileCheck.cpp - Check that File's Contents match what is expected --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // FileCheck does a line-by line check of a file that validates whether it |
| 11 | // contains the expected content. This is useful for regression tests etc. |
| 12 | // |
| 13 | // This program exits with an error status of 2 on error, exit status of 0 if |
| 14 | // the file matched the expected contents, and exit status of 1 if it did not |
| 15 | // contain the expected contents. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chandler Carruth | 91d19d8 | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/ADT/StringMap.h" |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringSet.h" |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | f08d2db | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Regex.h" |
Chandler Carruth | 91d19d8 | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Signals.h" |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 28 | #include "llvm/Support/SourceMgr.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 31 | #include <cctype> |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 32 | #include <map> |
| 33 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 34 | #include <system_error> |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 35 | #include <vector> |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
| 38 | static cl::opt<std::string> |
| 39 | CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required); |
| 40 | |
| 41 | static cl::opt<std::string> |
| 42 | InputFilename("input-file", cl::desc("File to check (defaults to stdin)"), |
| 43 | cl::init("-"), cl::value_desc("filename")); |
| 44 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 45 | static cl::list<std::string> |
| 46 | CheckPrefixes("check-prefix", |
| 47 | cl::desc("Prefix to use from check file (defaults to 'CHECK')")); |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 2c3e5cd | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 49 | static cl::opt<bool> |
| 50 | NoCanonicalizeWhiteSpace("strict-whitespace", |
| 51 | cl::desc("Do not treat all horizontal whitespace as equivalent")); |
| 52 | |
Alexander Kornienko | 56ccdbb | 2014-07-11 12:39:32 +0000 | [diff] [blame] | 53 | static cl::list<std::string> ImplicitCheckNot( |
| 54 | "implicit-check-not", |
| 55 | cl::desc("Add an implicit negative check with this pattern to every\n" |
| 56 | "positive check. This can be used to ensure that no instances of\n" |
| 57 | "this pattern occur which are not matched by a positive pattern"), |
| 58 | cl::value_desc("pattern")); |
| 59 | |
Justin Bogner | 1b9f936 | 2014-08-07 18:40:37 +0000 | [diff] [blame] | 60 | static cl::opt<bool> AllowEmptyInput( |
| 61 | "allow-empty", cl::init(false), |
| 62 | cl::desc("Allow the input file to be empty. This is useful when making\n" |
| 63 | "checks that some error message does not occur, for example.")); |
| 64 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 65 | typedef cl::list<std::string>::const_iterator prefix_iterator; |
| 66 | |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // Pattern Handling Code. |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 71 | namespace Check { |
| 72 | enum CheckType { |
| 73 | CheckNone = 0, |
| 74 | CheckPlain, |
| 75 | CheckNext, |
| 76 | CheckNot, |
| 77 | CheckDAG, |
| 78 | CheckLabel, |
| 79 | |
| 80 | /// MatchEOF - When set, this pattern only matches the end of file. This is |
| 81 | /// used for trailing CHECK-NOTs. |
| 82 | CheckEOF |
| 83 | }; |
| 84 | } |
| 85 | |
Chris Lattner | 3b40b44 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 86 | class Pattern { |
Chris Lattner | 0a4c44b | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 87 | SMLoc PatternLoc; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 88 | |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 89 | Check::CheckType CheckTy; |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 90 | |
Chris Lattner | b16ab0c | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 91 | /// FixedStr - If non-empty, this pattern is a fixed string match with the |
| 92 | /// specified fixed string. |
Chris Lattner | 221460e | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 93 | StringRef FixedStr; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 94 | |
Chris Lattner | b16ab0c | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 95 | /// RegEx - If non-empty, this is a regex pattern. |
| 96 | std::string RegExStr; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 97 | |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 98 | /// \brief Contains the number of line this pattern is in. |
| 99 | unsigned LineNumber; |
| 100 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 101 | /// VariableUses - Entries in this vector map to uses of a variable in the |
| 102 | /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain |
| 103 | /// "foobaz" and we'll get an entry in this vector that tells us to insert the |
| 104 | /// value of bar at offset 3. |
| 105 | std::vector<std::pair<StringRef, unsigned> > VariableUses; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 106 | |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 107 | /// VariableDefs - Maps definitions of variables to their parenthesized |
| 108 | /// capture numbers. |
| 109 | /// E.g. for the pattern "foo[[bar:.*]]baz", VariableDefs will map "bar" to 1. |
| 110 | std::map<StringRef, unsigned> VariableDefs; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 3b40b44 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 112 | public: |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 113 | |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 114 | Pattern(Check::CheckType Ty) |
| 115 | : CheckTy(Ty) { } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 116 | |
Michael Liao | 0b707eb | 2013-04-25 21:31:34 +0000 | [diff] [blame] | 117 | /// getLoc - Return the location in source code. |
| 118 | SMLoc getLoc() const { return PatternLoc; } |
| 119 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 120 | /// ParsePattern - Parse the given string into the Pattern. Prefix provides |
| 121 | /// which prefix is being matched, SM provides the SourceMgr used for error |
| 122 | /// reports, and LineNumber is the line number in the input file from which |
| 123 | /// the pattern string was read. Returns true in case of an error, false |
| 124 | /// otherwise. |
| 125 | bool ParsePattern(StringRef PatternStr, |
| 126 | StringRef Prefix, |
| 127 | SourceMgr &SM, |
| 128 | unsigned LineNumber); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 3b40b44 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 130 | /// Match - Match the pattern string against the input buffer Buffer. This |
| 131 | /// returns the position that is matched or npos if there is no match. If |
| 132 | /// there is a match, the size of the matched string is returned in MatchLen. |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 133 | /// |
| 134 | /// The VariableTable StringMap provides the current values of filecheck |
| 135 | /// variables and is updated if this match defines new values. |
| 136 | size_t Match(StringRef Buffer, size_t &MatchLen, |
| 137 | StringMap<StringRef> &VariableTable) const; |
Daniel Dunbar | e0ef65a | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 138 | |
| 139 | /// PrintFailureInfo - Print additional information about a failure to match |
| 140 | /// involving this pattern. |
| 141 | void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer, |
| 142 | const StringMap<StringRef> &VariableTable) const; |
| 143 | |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 144 | bool hasVariable() const { return !(VariableUses.empty() && |
| 145 | VariableDefs.empty()); } |
| 146 | |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 147 | Check::CheckType getCheckTy() const { return CheckTy; } |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 148 | |
Chris Lattner | b16ab0c | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 149 | private: |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 150 | bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM); |
| 151 | void AddBackrefToRegEx(unsigned BackrefNum); |
Daniel Dunbar | fd29d88 | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 152 | |
| 153 | /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of |
| 154 | /// matching this pattern at the start of \arg Buffer; a distance of zero |
| 155 | /// should correspond to a perfect match. |
| 156 | unsigned ComputeMatchDistance(StringRef Buffer, |
| 157 | const StringMap<StringRef> &VariableTable) const; |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 158 | |
| 159 | /// \brief Evaluates expression and stores the result to \p Value. |
| 160 | /// \return true on success. false when the expression has invalid syntax. |
| 161 | bool EvaluateExpression(StringRef Expr, std::string &Value) const; |
Eli Bendersky | 061d2ba | 2012-12-02 16:02:41 +0000 | [diff] [blame] | 162 | |
| 163 | /// \brief Finds the closing sequence of a regex variable usage or |
| 164 | /// definition. Str has to point in the beginning of the definition |
| 165 | /// (right after the opening sequence). |
| 166 | /// \return offset of the closing sequence within Str, or npos if it was not |
| 167 | /// found. |
Adrian Prantl | 81e5cd9 | 2014-01-03 21:49:09 +0000 | [diff] [blame] | 168 | size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM); |
Chris Lattner | 3b40b44 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 171 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 172 | bool Pattern::ParsePattern(StringRef PatternStr, |
| 173 | StringRef Prefix, |
| 174 | SourceMgr &SM, |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 175 | unsigned LineNumber) { |
| 176 | this->LineNumber = LineNumber; |
Chris Lattner | 0a4c44b | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 177 | PatternLoc = SMLoc::getFromPointer(PatternStr.data()); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 179 | // Ignore trailing whitespace. |
| 180 | while (!PatternStr.empty() && |
| 181 | (PatternStr.back() == ' ' || PatternStr.back() == '\t')) |
| 182 | PatternStr = PatternStr.substr(0, PatternStr.size()-1); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 184 | // Check that there is something on the line. |
| 185 | if (PatternStr.empty()) { |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 186 | SM.PrintMessage(PatternLoc, SourceMgr::DK_Error, |
| 187 | "found empty check string with prefix '" + |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 188 | Prefix + ":'"); |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 189 | return true; |
| 190 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 221460e | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 192 | // Check to see if this is a fixed string, or if it has regex pieces. |
Ted Kremenek | d946696 | 2012-09-08 04:32:13 +0000 | [diff] [blame] | 193 | if (PatternStr.size() < 2 || |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 194 | (PatternStr.find("{{") == StringRef::npos && |
| 195 | PatternStr.find("[[") == StringRef::npos)) { |
Chris Lattner | 221460e | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 196 | FixedStr = PatternStr; |
| 197 | return false; |
| 198 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 200 | // Paren value #0 is for the fully matched string. Any new parenthesized |
Chris Lattner | 53e0679 | 2011-04-09 06:18:02 +0000 | [diff] [blame] | 201 | // values add from there. |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 202 | unsigned CurParen = 1; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 203 | |
Chris Lattner | b16ab0c | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 204 | // Otherwise, there is at least one regex piece. Build up the regex pattern |
| 205 | // by escaping scary characters in fixed strings, building up one big regex. |
Chris Lattner | f08d2db | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 206 | while (!PatternStr.empty()) { |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 207 | // RegEx matches. |
Chris Lattner | 53e0679 | 2011-04-09 06:18:02 +0000 | [diff] [blame] | 208 | if (PatternStr.startswith("{{")) { |
Eli Bendersky | 43d50d4 | 2012-11-30 14:22:14 +0000 | [diff] [blame] | 209 | // This is the start of a regex match. Scan for the }}. |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 210 | size_t End = PatternStr.find("}}"); |
| 211 | if (End == StringRef::npos) { |
| 212 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 213 | SourceMgr::DK_Error, |
| 214 | "found start of regex string with no end '}}'"); |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 215 | return true; |
| 216 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 217 | |
Chris Lattner | e53c95f | 2011-04-09 06:37:03 +0000 | [diff] [blame] | 218 | // Enclose {{}} patterns in parens just like [[]] even though we're not |
| 219 | // capturing the result for any purpose. This is required in case the |
| 220 | // expression contains an alternation like: CHECK: abc{{x|z}}def. We |
| 221 | // want this to turn into: "abc(x|z)def" not "abcx|zdef". |
| 222 | RegExStr += '('; |
| 223 | ++CurParen; |
| 224 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 225 | if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM)) |
| 226 | return true; |
Chris Lattner | e53c95f | 2011-04-09 06:37:03 +0000 | [diff] [blame] | 227 | RegExStr += ')'; |
Chris Lattner | 53e0679 | 2011-04-09 06:18:02 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 229 | PatternStr = PatternStr.substr(End+2); |
Chris Lattner | f08d2db | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 230 | continue; |
| 231 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 233 | // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .* |
| 234 | // (or some other regex) and assigns it to the FileCheck variable 'foo'. The |
| 235 | // second form is [[foo]] which is a reference to foo. The variable name |
Daniel Dunbar | 57cb733 | 2009-11-22 22:07:50 +0000 | [diff] [blame] | 236 | // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 237 | // it. This is to catch some common errors. |
Chris Lattner | 53e0679 | 2011-04-09 06:18:02 +0000 | [diff] [blame] | 238 | if (PatternStr.startswith("[[")) { |
Eli Bendersky | 061d2ba | 2012-12-02 16:02:41 +0000 | [diff] [blame] | 239 | // Find the closing bracket pair ending the match. End is going to be an |
| 240 | // offset relative to the beginning of the match string. |
Adrian Prantl | 81e5cd9 | 2014-01-03 21:49:09 +0000 | [diff] [blame] | 241 | size_t End = FindRegexVarEnd(PatternStr.substr(2), SM); |
Eli Bendersky | 061d2ba | 2012-12-02 16:02:41 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 243 | if (End == StringRef::npos) { |
| 244 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 245 | SourceMgr::DK_Error, |
| 246 | "invalid named regex reference, no ]] found"); |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 247 | return true; |
| 248 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 249 | |
Eli Bendersky | 061d2ba | 2012-12-02 16:02:41 +0000 | [diff] [blame] | 250 | StringRef MatchStr = PatternStr.substr(2, End); |
| 251 | PatternStr = PatternStr.substr(End+4); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 253 | // Get the regex name (e.g. "foo"). |
| 254 | size_t NameEnd = MatchStr.find(':'); |
| 255 | StringRef Name = MatchStr.substr(0, NameEnd); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 257 | if (Name.empty()) { |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 258 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, |
| 259 | "invalid name in named regex: empty name"); |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 260 | return true; |
| 261 | } |
| 262 | |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 263 | // Verify that the name/expression is well formed. FileCheck currently |
| 264 | // supports @LINE, @LINE+number, @LINE-number expressions. The check here |
| 265 | // is relaxed, more strict check is performed in \c EvaluateExpression. |
| 266 | bool IsExpression = false; |
| 267 | for (unsigned i = 0, e = Name.size(); i != e; ++i) { |
| 268 | if (i == 0 && Name[i] == '@') { |
| 269 | if (NameEnd != StringRef::npos) { |
| 270 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), |
| 271 | SourceMgr::DK_Error, |
| 272 | "invalid name in named regex definition"); |
| 273 | return true; |
| 274 | } |
| 275 | IsExpression = true; |
| 276 | continue; |
| 277 | } |
| 278 | if (Name[i] != '_' && !isalnum(Name[i]) && |
| 279 | (!IsExpression || (Name[i] != '+' && Name[i] != '-'))) { |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 280 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i), |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 281 | SourceMgr::DK_Error, "invalid name in named regex"); |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 282 | return true; |
| 283 | } |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 284 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 286 | // Name can't start with a digit. |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 287 | if (isdigit(static_cast<unsigned char>(Name[0]))) { |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 288 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, |
| 289 | "invalid name in named regex"); |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 290 | return true; |
| 291 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 293 | // Handle [[foo]]. |
| 294 | if (NameEnd == StringRef::npos) { |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 295 | // Handle variables that were defined earlier on the same line by |
| 296 | // emitting a backreference. |
| 297 | if (VariableDefs.find(Name) != VariableDefs.end()) { |
| 298 | unsigned VarParenNum = VariableDefs[Name]; |
| 299 | if (VarParenNum < 1 || VarParenNum > 9) { |
| 300 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), |
| 301 | SourceMgr::DK_Error, |
| 302 | "Can't back-reference more than 9 variables"); |
| 303 | return true; |
| 304 | } |
| 305 | AddBackrefToRegEx(VarParenNum); |
| 306 | } else { |
| 307 | VariableUses.push_back(std::make_pair(Name, RegExStr.size())); |
| 308 | } |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 309 | continue; |
| 310 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 312 | // Handle [[foo:.*]]. |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 313 | VariableDefs[Name] = CurParen; |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 314 | RegExStr += '('; |
| 315 | ++CurParen; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 317 | if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM)) |
| 318 | return true; |
| 319 | |
| 320 | RegExStr += ')'; |
Chris Lattner | f08d2db | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 321 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 323 | // Handle fixed string matches. |
| 324 | // Find the end, which is the start of the next regex. |
| 325 | size_t FixedMatchEnd = PatternStr.find("{{"); |
| 326 | FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[[")); |
Hans Wennborg | 6f4f77b | 2013-12-12 00:06:41 +0000 | [diff] [blame] | 327 | RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd)); |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 328 | PatternStr = PatternStr.substr(FixedMatchEnd); |
Chris Lattner | f08d2db | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | a2f8fc5 | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 334 | bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 335 | SourceMgr &SM) { |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 336 | Regex R(RS); |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 337 | std::string Error; |
| 338 | if (!R.isValid(Error)) { |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 339 | SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error, |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 340 | "invalid regex: " + Error); |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 341 | return true; |
| 342 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 343 | |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 344 | RegExStr += RS.str(); |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 345 | CurParen += R.getNumMatches(); |
| 346 | return false; |
| 347 | } |
Chris Lattner | b16ab0c | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 348 | |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 349 | void Pattern::AddBackrefToRegEx(unsigned BackrefNum) { |
| 350 | assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number"); |
| 351 | std::string Backref = std::string("\\") + |
| 352 | std::string(1, '0' + BackrefNum); |
| 353 | RegExStr += Backref; |
| 354 | } |
| 355 | |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 356 | bool Pattern::EvaluateExpression(StringRef Expr, std::string &Value) const { |
| 357 | // The only supported expression is @LINE([\+-]\d+)? |
| 358 | if (!Expr.startswith("@LINE")) |
| 359 | return false; |
| 360 | Expr = Expr.substr(StringRef("@LINE").size()); |
| 361 | int Offset = 0; |
| 362 | if (!Expr.empty()) { |
| 363 | if (Expr[0] == '+') |
| 364 | Expr = Expr.substr(1); |
| 365 | else if (Expr[0] != '-') |
| 366 | return false; |
| 367 | if (Expr.getAsInteger(10, Offset)) |
| 368 | return false; |
| 369 | } |
| 370 | Value = llvm::itostr(LineNumber + Offset); |
| 371 | return true; |
| 372 | } |
| 373 | |
Chris Lattner | f08d2db | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 374 | /// Match - Match the pattern string against the input buffer Buffer. This |
| 375 | /// returns the position that is matched or npos if there is no match. If |
| 376 | /// there is a match, the size of the matched string is returned in MatchLen. |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 377 | size_t Pattern::Match(StringRef Buffer, size_t &MatchLen, |
| 378 | StringMap<StringRef> &VariableTable) const { |
Jakob Stoklund Olesen | eba5582 | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 379 | // If this is the EOF pattern, match it immediately. |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 380 | if (CheckTy == Check::CheckEOF) { |
Jakob Stoklund Olesen | eba5582 | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 381 | MatchLen = 0; |
| 382 | return Buffer.size(); |
| 383 | } |
| 384 | |
Chris Lattner | 221460e | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 385 | // If this is a fixed string pattern, just match it now. |
| 386 | if (!FixedStr.empty()) { |
| 387 | MatchLen = FixedStr.size(); |
| 388 | return Buffer.find(FixedStr); |
| 389 | } |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 390 | |
Chris Lattner | b16ab0c | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 391 | // Regex match. |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 393 | // If there are variable uses, we need to create a temporary string with the |
| 394 | // actual value. |
| 395 | StringRef RegExToMatch = RegExStr; |
| 396 | std::string TmpStr; |
| 397 | if (!VariableUses.empty()) { |
| 398 | TmpStr = RegExStr; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 400 | unsigned InsertOffset = 0; |
| 401 | for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) { |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 402 | std::string Value; |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 403 | |
| 404 | if (VariableUses[i].first[0] == '@') { |
| 405 | if (!EvaluateExpression(VariableUses[i].first, Value)) |
| 406 | return StringRef::npos; |
| 407 | } else { |
| 408 | StringMap<StringRef>::iterator it = |
| 409 | VariableTable.find(VariableUses[i].first); |
| 410 | // If the variable is undefined, return an error. |
| 411 | if (it == VariableTable.end()) |
| 412 | return StringRef::npos; |
| 413 | |
Hans Wennborg | 6f4f77b | 2013-12-12 00:06:41 +0000 | [diff] [blame] | 414 | // Look up the value and escape it so that we can put it into the regex. |
| 415 | Value += Regex::escape(it->second); |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 416 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 418 | // Plop it into the regex at the adjusted offset. |
| 419 | TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset, |
| 420 | Value.begin(), Value.end()); |
| 421 | InsertOffset += Value.size(); |
| 422 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 424 | // Match the newly constructed regex. |
| 425 | RegExToMatch = TmpStr; |
| 426 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 427 | |
| 428 | |
Chris Lattner | b16ab0c | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 429 | SmallVector<StringRef, 4> MatchInfo; |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 430 | if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo)) |
Chris Lattner | b16ab0c | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 431 | return StringRef::npos; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 432 | |
Chris Lattner | b16ab0c | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 433 | // Successful regex match. |
| 434 | assert(!MatchInfo.empty() && "Didn't get any match"); |
| 435 | StringRef FullMatch = MatchInfo[0]; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 437 | // If this defines any variables, remember their values. |
Eli Bendersky | e8b8f1b | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 438 | for (std::map<StringRef, unsigned>::const_iterator I = VariableDefs.begin(), |
| 439 | E = VariableDefs.end(); |
| 440 | I != E; ++I) { |
| 441 | assert(I->second < MatchInfo.size() && "Internal paren error"); |
| 442 | VariableTable[I->first] = MatchInfo[I->second]; |
Chris Lattner | 0a4c44b | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 443 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 444 | |
Chris Lattner | b16ab0c | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 445 | MatchLen = FullMatch.size(); |
| 446 | return FullMatch.data()-Buffer.data(); |
Chris Lattner | f08d2db | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Daniel Dunbar | fd29d88 | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 449 | unsigned Pattern::ComputeMatchDistance(StringRef Buffer, |
| 450 | const StringMap<StringRef> &VariableTable) const { |
| 451 | // Just compute the number of matching characters. For regular expressions, we |
| 452 | // just compare against the regex itself and hope for the best. |
| 453 | // |
| 454 | // FIXME: One easy improvement here is have the regex lib generate a single |
| 455 | // example regular expression which matches, and use that as the example |
| 456 | // string. |
| 457 | StringRef ExampleString(FixedStr); |
| 458 | if (ExampleString.empty()) |
| 459 | ExampleString = RegExStr; |
| 460 | |
Daniel Dunbar | e9aa36c | 2010-01-30 00:24:06 +0000 | [diff] [blame] | 461 | // Only compare up to the first line in the buffer, or the string size. |
| 462 | StringRef BufferPrefix = Buffer.substr(0, ExampleString.size()); |
| 463 | BufferPrefix = BufferPrefix.split('\n').first; |
| 464 | return BufferPrefix.edit_distance(ExampleString); |
Daniel Dunbar | fd29d88 | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Daniel Dunbar | e0ef65a | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 467 | void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer, |
| 468 | const StringMap<StringRef> &VariableTable) const{ |
Daniel Dunbar | e0ef65a | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 469 | // If this was a regular expression using variables, print the current |
| 470 | // variable values. |
| 471 | if (!VariableUses.empty()) { |
| 472 | for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 473 | SmallString<256> Msg; |
| 474 | raw_svector_ostream OS(Msg); |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 475 | StringRef Var = VariableUses[i].first; |
| 476 | if (Var[0] == '@') { |
| 477 | std::string Value; |
| 478 | if (EvaluateExpression(Var, Value)) { |
| 479 | OS << "with expression \""; |
| 480 | OS.write_escaped(Var) << "\" equal to \""; |
| 481 | OS.write_escaped(Value) << "\""; |
| 482 | } else { |
| 483 | OS << "uses incorrect expression \""; |
| 484 | OS.write_escaped(Var) << "\""; |
| 485 | } |
Daniel Dunbar | e0ef65a | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 486 | } else { |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 487 | StringMap<StringRef>::const_iterator it = VariableTable.find(Var); |
| 488 | |
| 489 | // Check for undefined variable references. |
| 490 | if (it == VariableTable.end()) { |
| 491 | OS << "uses undefined variable \""; |
| 492 | OS.write_escaped(Var) << "\""; |
| 493 | } else { |
| 494 | OS << "with variable \""; |
| 495 | OS.write_escaped(Var) << "\" equal to \""; |
| 496 | OS.write_escaped(it->second) << "\""; |
| 497 | } |
Daniel Dunbar | e0ef65a | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 500 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 501 | OS.str()); |
Daniel Dunbar | e0ef65a | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
Daniel Dunbar | fd29d88 | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 504 | |
| 505 | // Attempt to find the closest/best fuzzy match. Usually an error happens |
| 506 | // because some string in the output didn't exactly match. In these cases, we |
| 507 | // would like to show the user a best guess at what "should have" matched, to |
| 508 | // save them having to actually check the input manually. |
| 509 | size_t NumLinesForward = 0; |
| 510 | size_t Best = StringRef::npos; |
| 511 | double BestQuality = 0; |
| 512 | |
| 513 | // Use an arbitrary 4k limit on how far we will search. |
Dan Gohman | 2bf486e | 2010-01-29 21:57:46 +0000 | [diff] [blame] | 514 | for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) { |
Daniel Dunbar | fd29d88 | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 515 | if (Buffer[i] == '\n') |
| 516 | ++NumLinesForward; |
| 517 | |
Dan Gohman | df22bbf | 2010-01-29 21:55:16 +0000 | [diff] [blame] | 518 | // Patterns have leading whitespace stripped, so skip whitespace when |
| 519 | // looking for something which looks like a pattern. |
| 520 | if (Buffer[i] == ' ' || Buffer[i] == '\t') |
| 521 | continue; |
| 522 | |
Daniel Dunbar | fd29d88 | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 523 | // Compute the "quality" of this match as an arbitrary combination of the |
| 524 | // match distance and the number of lines skipped to get to this match. |
| 525 | unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable); |
| 526 | double Quality = Distance + (NumLinesForward / 100.); |
| 527 | |
| 528 | if (Quality < BestQuality || Best == StringRef::npos) { |
| 529 | Best = i; |
| 530 | BestQuality = Quality; |
| 531 | } |
| 532 | } |
| 533 | |
Daniel Dunbar | c069cc8 | 2010-03-19 18:07:43 +0000 | [diff] [blame] | 534 | // Print the "possible intended match here" line if we found something |
| 535 | // reasonable and not equal to what we showed in the "scanning from here" |
| 536 | // line. |
| 537 | if (Best && Best != StringRef::npos && BestQuality < 50) { |
| 538 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best), |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 539 | SourceMgr::DK_Note, "possible intended match here"); |
Daniel Dunbar | fd29d88 | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 540 | |
| 541 | // FIXME: If we wanted to be really friendly we would show why the match |
| 542 | // failed, as it can be hard to spot simple one character differences. |
| 543 | } |
Daniel Dunbar | e0ef65a | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 544 | } |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 545 | |
Adrian Prantl | 81e5cd9 | 2014-01-03 21:49:09 +0000 | [diff] [blame] | 546 | size_t Pattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) { |
Eli Bendersky | 061d2ba | 2012-12-02 16:02:41 +0000 | [diff] [blame] | 547 | // Offset keeps track of the current offset within the input Str |
| 548 | size_t Offset = 0; |
| 549 | // [...] Nesting depth |
| 550 | size_t BracketDepth = 0; |
| 551 | |
| 552 | while (!Str.empty()) { |
| 553 | if (Str.startswith("]]") && BracketDepth == 0) |
| 554 | return Offset; |
| 555 | if (Str[0] == '\\') { |
| 556 | // Backslash escapes the next char within regexes, so skip them both. |
| 557 | Str = Str.substr(2); |
| 558 | Offset += 2; |
| 559 | } else { |
| 560 | switch (Str[0]) { |
| 561 | default: |
| 562 | break; |
| 563 | case '[': |
| 564 | BracketDepth++; |
| 565 | break; |
| 566 | case ']': |
Adrian Prantl | 81e5cd9 | 2014-01-03 21:49:09 +0000 | [diff] [blame] | 567 | if (BracketDepth == 0) { |
| 568 | SM.PrintMessage(SMLoc::getFromPointer(Str.data()), |
| 569 | SourceMgr::DK_Error, |
| 570 | "missing closing \"]\" for regex variable"); |
| 571 | exit(1); |
| 572 | } |
Eli Bendersky | 061d2ba | 2012-12-02 16:02:41 +0000 | [diff] [blame] | 573 | BracketDepth--; |
| 574 | break; |
| 575 | } |
| 576 | Str = Str.substr(1); |
| 577 | Offset++; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | return StringRef::npos; |
| 582 | } |
| 583 | |
| 584 | |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 585 | //===----------------------------------------------------------------------===// |
| 586 | // Check Strings. |
| 587 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3b40b44 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 588 | |
| 589 | /// CheckString - This is a check that we found in the input file. |
| 590 | struct CheckString { |
| 591 | /// Pat - The pattern to match. |
| 592 | Pattern Pat; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 593 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 594 | /// Prefix - Which prefix name this check matched. |
| 595 | StringRef Prefix; |
| 596 | |
Chris Lattner | 26cccfe | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 597 | /// Loc - The location in the match file that the check string was specified. |
| 598 | SMLoc Loc; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 599 | |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 600 | /// CheckTy - Specify what kind of check this is. e.g. CHECK-NEXT: directive, |
| 601 | /// as opposed to a CHECK: directive. |
| 602 | Check::CheckType CheckTy; |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 603 | |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 604 | /// DagNotStrings - These are all of the strings that are disallowed from |
Chris Lattner | 236d2d5 | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 605 | /// occurring between this match string and the previous one (or start of |
| 606 | /// file). |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 607 | std::vector<Pattern> DagNotStrings; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 608 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 609 | |
| 610 | CheckString(const Pattern &P, |
| 611 | StringRef S, |
| 612 | SMLoc L, |
| 613 | Check::CheckType Ty) |
| 614 | : Pat(P), Prefix(S), Loc(L), CheckTy(Ty) {} |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 615 | |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 616 | /// Check - Match check string and its "not strings" and/or "dag strings". |
Stephen Lin | e93a3a0 | 2013-10-11 18:38:36 +0000 | [diff] [blame] | 617 | size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode, |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 618 | size_t &MatchLen, StringMap<StringRef> &VariableTable) const; |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 619 | |
| 620 | /// CheckNext - Verify there is a single line in the given buffer. |
| 621 | bool CheckNext(const SourceMgr &SM, StringRef Buffer) const; |
| 622 | |
| 623 | /// CheckNot - Verify there's no "not strings" in the given buffer. |
| 624 | bool CheckNot(const SourceMgr &SM, StringRef Buffer, |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 625 | const std::vector<const Pattern *> &NotStrings, |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 626 | StringMap<StringRef> &VariableTable) const; |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 627 | |
| 628 | /// CheckDag - Match "dag strings" and their mixed "not strings". |
| 629 | size_t CheckDag(const SourceMgr &SM, StringRef Buffer, |
| 630 | std::vector<const Pattern *> &NotStrings, |
| 631 | StringMap<StringRef> &VariableTable) const; |
Chris Lattner | 26cccfe | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 632 | }; |
| 633 | |
Guy Benyei | 5ea04c3 | 2013-02-06 20:40:38 +0000 | [diff] [blame] | 634 | /// Canonicalize whitespaces in the input file. Line endings are replaced |
| 635 | /// with UNIX-style '\n'. |
| 636 | /// |
| 637 | /// \param PreserveHorizontal Don't squash consecutive horizontal whitespace |
| 638 | /// characters to a single space. |
Rafael Espindola | ce5dd1a | 2014-08-01 14:11:14 +0000 | [diff] [blame] | 639 | static MemoryBuffer *CanonicalizeInputFile(std::unique_ptr<MemoryBuffer> MB, |
Guy Benyei | 5ea04c3 | 2013-02-06 20:40:38 +0000 | [diff] [blame] | 640 | bool PreserveHorizontal) { |
Chris Lattner | 0e45d24 | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 641 | SmallString<128> NewFile; |
Chris Lattner | a2f8fc5 | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 642 | NewFile.reserve(MB->getBufferSize()); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 643 | |
Chris Lattner | a2f8fc5 | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 644 | for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd(); |
| 645 | Ptr != End; ++Ptr) { |
NAKAMURA Takumi | fd781bf | 2010-11-14 03:28:22 +0000 | [diff] [blame] | 646 | // Eliminate trailing dosish \r. |
| 647 | if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') { |
| 648 | continue; |
| 649 | } |
| 650 | |
Michael Liao | 61bed2f | 2013-04-25 18:54:02 +0000 | [diff] [blame] | 651 | // If current char is not a horizontal whitespace or if horizontal |
Guy Benyei | 5ea04c3 | 2013-02-06 20:40:38 +0000 | [diff] [blame] | 652 | // whitespace canonicalization is disabled, dump it to output as is. |
| 653 | if (PreserveHorizontal || (*Ptr != ' ' && *Ptr != '\t')) { |
Chris Lattner | a2f8fc5 | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 654 | NewFile.push_back(*Ptr); |
| 655 | continue; |
| 656 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 657 | |
Chris Lattner | a2f8fc5 | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 658 | // Otherwise, add one space and advance over neighboring space. |
| 659 | NewFile.push_back(' '); |
| 660 | while (Ptr+1 != End && |
| 661 | (Ptr[1] == ' ' || Ptr[1] == '\t')) |
| 662 | ++Ptr; |
| 663 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 664 | |
Rafael Espindola | ce5dd1a | 2014-08-01 14:11:14 +0000 | [diff] [blame] | 665 | return MemoryBuffer::getMemBufferCopy(NewFile.str(), |
| 666 | MB->getBufferIdentifier()); |
Chris Lattner | a2f8fc5 | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 669 | static bool IsPartOfWord(char c) { |
| 670 | return (isalnum(c) || c == '-' || c == '_'); |
| 671 | } |
| 672 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 673 | // Get the size of the prefix extension. |
| 674 | static size_t CheckTypeSize(Check::CheckType Ty) { |
| 675 | switch (Ty) { |
| 676 | case Check::CheckNone: |
| 677 | return 0; |
| 678 | |
| 679 | case Check::CheckPlain: |
| 680 | return sizeof(":") - 1; |
| 681 | |
| 682 | case Check::CheckNext: |
| 683 | return sizeof("-NEXT:") - 1; |
| 684 | |
| 685 | case Check::CheckNot: |
| 686 | return sizeof("-NOT:") - 1; |
| 687 | |
| 688 | case Check::CheckDAG: |
| 689 | return sizeof("-DAG:") - 1; |
| 690 | |
| 691 | case Check::CheckLabel: |
| 692 | return sizeof("-LABEL:") - 1; |
| 693 | |
| 694 | case Check::CheckEOF: |
| 695 | llvm_unreachable("Should not be using EOF size"); |
| 696 | } |
| 697 | |
| 698 | llvm_unreachable("Bad check type"); |
| 699 | } |
| 700 | |
| 701 | static Check::CheckType FindCheckType(StringRef Buffer, StringRef Prefix) { |
Matt Arsenault | c4d2d47 | 2013-09-17 22:45:57 +0000 | [diff] [blame] | 702 | char NextChar = Buffer[Prefix.size()]; |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 703 | |
| 704 | // Verify that the : is present after the prefix. |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 705 | if (NextChar == ':') |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 706 | return Check::CheckPlain; |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 707 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 708 | if (NextChar != '-') |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 709 | return Check::CheckNone; |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 710 | |
Matt Arsenault | c4d2d47 | 2013-09-17 22:45:57 +0000 | [diff] [blame] | 711 | StringRef Rest = Buffer.drop_front(Prefix.size() + 1); |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 712 | if (Rest.startswith("NEXT:")) |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 713 | return Check::CheckNext; |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 714 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 715 | if (Rest.startswith("NOT:")) |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 716 | return Check::CheckNot; |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 717 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 718 | if (Rest.startswith("DAG:")) |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 719 | return Check::CheckDAG; |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 720 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 721 | if (Rest.startswith("LABEL:")) |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 722 | return Check::CheckLabel; |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 723 | |
| 724 | return Check::CheckNone; |
| 725 | } |
| 726 | |
| 727 | // From the given position, find the next character after the word. |
| 728 | static size_t SkipWord(StringRef Str, size_t Loc) { |
| 729 | while (Loc < Str.size() && IsPartOfWord(Str[Loc])) |
| 730 | ++Loc; |
| 731 | return Loc; |
| 732 | } |
| 733 | |
| 734 | // Try to find the first match in buffer for any prefix. If a valid match is |
| 735 | // found, return that prefix and set its type and location. If there are almost |
| 736 | // matches (e.g. the actual prefix string is found, but is not an actual check |
| 737 | // string), but no valid match, return an empty string and set the position to |
| 738 | // resume searching from. If no partial matches are found, return an empty |
| 739 | // string and the location will be StringRef::npos. If one prefix is a substring |
| 740 | // of another, the maximal match should be found. e.g. if "A" and "AA" are |
| 741 | // prefixes then AA-CHECK: should match the second one. |
| 742 | static StringRef FindFirstCandidateMatch(StringRef &Buffer, |
| 743 | Check::CheckType &CheckTy, |
| 744 | size_t &CheckLoc) { |
| 745 | StringRef FirstPrefix; |
| 746 | size_t FirstLoc = StringRef::npos; |
| 747 | size_t SearchLoc = StringRef::npos; |
| 748 | Check::CheckType FirstTy = Check::CheckNone; |
| 749 | |
| 750 | CheckTy = Check::CheckNone; |
| 751 | CheckLoc = StringRef::npos; |
| 752 | |
| 753 | for (prefix_iterator I = CheckPrefixes.begin(), E = CheckPrefixes.end(); |
| 754 | I != E; ++I) { |
| 755 | StringRef Prefix(*I); |
| 756 | size_t PrefixLoc = Buffer.find(Prefix); |
| 757 | |
| 758 | if (PrefixLoc == StringRef::npos) |
| 759 | continue; |
| 760 | |
| 761 | // Track where we are searching for invalid prefixes that look almost right. |
| 762 | // We need to only advance to the first partial match on the next attempt |
| 763 | // since a partial match could be a substring of a later, valid prefix. |
| 764 | // Need to skip to the end of the word, otherwise we could end up |
| 765 | // matching a prefix in a substring later. |
| 766 | if (PrefixLoc < SearchLoc) |
| 767 | SearchLoc = SkipWord(Buffer, PrefixLoc); |
| 768 | |
| 769 | // We only want to find the first match to avoid skipping some. |
| 770 | if (PrefixLoc > FirstLoc) |
| 771 | continue; |
Alexey Samsonov | a7181a1 | 2013-11-13 14:12:52 +0000 | [diff] [blame] | 772 | // If one matching check-prefix is a prefix of another, choose the |
| 773 | // longer one. |
| 774 | if (PrefixLoc == FirstLoc && Prefix.size() < FirstPrefix.size()) |
| 775 | continue; |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 776 | |
| 777 | StringRef Rest = Buffer.drop_front(PrefixLoc); |
| 778 | // Make sure we have actually found the prefix, and not a word containing |
| 779 | // it. This should also prevent matching the wrong prefix when one is a |
| 780 | // substring of another. |
| 781 | if (PrefixLoc != 0 && IsPartOfWord(Buffer[PrefixLoc - 1])) |
Daniel Sanders | 43b5f57 | 2013-11-20 13:25:05 +0000 | [diff] [blame] | 782 | FirstTy = Check::CheckNone; |
| 783 | else |
| 784 | FirstTy = FindCheckType(Rest, Prefix); |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 785 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 786 | FirstLoc = PrefixLoc; |
Alexey Samsonov | a7181a1 | 2013-11-13 14:12:52 +0000 | [diff] [blame] | 787 | FirstPrefix = Prefix; |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Alexey Samsonov | a7181a1 | 2013-11-13 14:12:52 +0000 | [diff] [blame] | 790 | // If the first prefix is invalid, we should continue the search after it. |
| 791 | if (FirstTy == Check::CheckNone) { |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 792 | CheckLoc = SearchLoc; |
Alexey Samsonov | a7181a1 | 2013-11-13 14:12:52 +0000 | [diff] [blame] | 793 | return ""; |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Alexey Samsonov | a7181a1 | 2013-11-13 14:12:52 +0000 | [diff] [blame] | 796 | CheckTy = FirstTy; |
| 797 | CheckLoc = FirstLoc; |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 798 | return FirstPrefix; |
| 799 | } |
| 800 | |
| 801 | static StringRef FindFirstMatchingPrefix(StringRef &Buffer, |
| 802 | unsigned &LineNumber, |
| 803 | Check::CheckType &CheckTy, |
| 804 | size_t &CheckLoc) { |
| 805 | while (!Buffer.empty()) { |
| 806 | StringRef Prefix = FindFirstCandidateMatch(Buffer, CheckTy, CheckLoc); |
| 807 | // If we found a real match, we are done. |
| 808 | if (!Prefix.empty()) { |
| 809 | LineNumber += Buffer.substr(0, CheckLoc).count('\n'); |
| 810 | return Prefix; |
| 811 | } |
| 812 | |
| 813 | // We didn't find any almost matches either, we are also done. |
| 814 | if (CheckLoc == StringRef::npos) |
| 815 | return StringRef(); |
| 816 | |
| 817 | LineNumber += Buffer.substr(0, CheckLoc + 1).count('\n'); |
| 818 | |
| 819 | // Advance to the last possible match we found and try again. |
| 820 | Buffer = Buffer.drop_front(CheckLoc + 1); |
| 821 | } |
| 822 | |
| 823 | return StringRef(); |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 824 | } |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 825 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 826 | /// ReadCheckFile - Read the check file, which specifies the sequence of |
| 827 | /// expected strings. The strings are added to the CheckStrings vector. |
Eli Bendersky | 43d50d4 | 2012-11-30 14:22:14 +0000 | [diff] [blame] | 828 | /// Returns true in case of an error, false otherwise. |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 829 | static bool ReadCheckFile(SourceMgr &SM, |
Chris Lattner | 26cccfe | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 830 | std::vector<CheckString> &CheckStrings) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 831 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 832 | MemoryBuffer::getFileOrSTDIN(CheckFilename); |
| 833 | if (std::error_code EC = FileOrErr.getError()) { |
| 834 | errs() << "Could not open check file '" << CheckFilename |
| 835 | << "': " << EC.message() << '\n'; |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 836 | return true; |
| 837 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 838 | |
Chris Lattner | a2f8fc5 | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 839 | // If we want to canonicalize whitespace, strip excess whitespace from the |
Guy Benyei | 5ea04c3 | 2013-02-06 20:40:38 +0000 | [diff] [blame] | 840 | // buffer containing the CHECK lines. Remove DOS style line endings. |
Rafael Espindola | ce5dd1a | 2014-08-01 14:11:14 +0000 | [diff] [blame] | 841 | MemoryBuffer *F = CanonicalizeInputFile(std::move(FileOrErr.get()), |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 842 | NoCanonicalizeWhiteSpace); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 843 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 844 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 845 | |
Chris Lattner | 10f10ce | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 846 | // Find all instances of CheckPrefix followed by : in the file. |
Chris Lattner | caa5fc0 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 847 | StringRef Buffer = F->getBuffer(); |
Alexander Kornienko | 56ccdbb | 2014-07-11 12:39:32 +0000 | [diff] [blame] | 848 | |
| 849 | std::vector<Pattern> ImplicitNegativeChecks; |
| 850 | for (const auto &PatternString : ImplicitCheckNot) { |
| 851 | // Create a buffer with fake command line content in order to display the |
| 852 | // command line option responsible for the specific implicit CHECK-NOT. |
| 853 | std::string Prefix = std::string("-") + ImplicitCheckNot.ArgStr + "='"; |
| 854 | std::string Suffix = "'"; |
| 855 | MemoryBuffer *CmdLine = MemoryBuffer::getMemBufferCopy( |
| 856 | Prefix + PatternString + Suffix, "command line"); |
| 857 | StringRef PatternInBuffer = |
| 858 | CmdLine->getBuffer().substr(Prefix.size(), PatternString.size()); |
| 859 | SM.AddNewSourceBuffer(CmdLine, SMLoc()); |
| 860 | |
| 861 | ImplicitNegativeChecks.push_back(Pattern(Check::CheckNot)); |
| 862 | ImplicitNegativeChecks.back().ParsePattern(PatternInBuffer, |
| 863 | "IMPLICIT-CHECK", SM, 0); |
| 864 | } |
| 865 | |
| 866 | |
| 867 | std::vector<Pattern> DagNotMatches = ImplicitNegativeChecks; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 868 | |
Eli Bendersky | 43d50d4 | 2012-11-30 14:22:14 +0000 | [diff] [blame] | 869 | // LineNumber keeps track of the line on which CheckPrefix instances are |
| 870 | // found. |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 871 | unsigned LineNumber = 1; |
| 872 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 873 | while (1) { |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 874 | Check::CheckType CheckTy; |
| 875 | size_t PrefixLoc; |
| 876 | |
| 877 | // See if a prefix occurs in the memory buffer. |
| 878 | StringRef UsedPrefix = FindFirstMatchingPrefix(Buffer, |
| 879 | LineNumber, |
| 880 | CheckTy, |
| 881 | PrefixLoc); |
| 882 | if (UsedPrefix.empty()) |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 883 | break; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 884 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 885 | Buffer = Buffer.drop_front(PrefixLoc); |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 886 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 887 | // Location to use for error messages. |
| 888 | const char *UsedPrefixStart = Buffer.data() + (PrefixLoc == 0 ? 0 : 1); |
Alexander Kornienko | 92987fb | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 889 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 890 | // PrefixLoc is to the start of the prefix. Skip to the end. |
| 891 | Buffer = Buffer.drop_front(UsedPrefix.size() + CheckTypeSize(CheckTy)); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 892 | |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 893 | // Okay, we found the prefix, yay. Remember the rest of the line, but ignore |
| 894 | // leading and trailing whitespace. |
Chris Lattner | 236d2d5 | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 895 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t")); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 896 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 897 | // Scan ahead to the end of line. |
Chris Lattner | caa5fc0 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 898 | size_t EOL = Buffer.find_first_of("\n\r"); |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 899 | |
Dan Gohman | 838fb09 | 2010-01-29 21:53:18 +0000 | [diff] [blame] | 900 | // Remember the location of the start of the pattern, for diagnostics. |
| 901 | SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data()); |
| 902 | |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 903 | // Parse the pattern. |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 904 | Pattern P(CheckTy); |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 905 | if (P.ParsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, LineNumber)) |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 906 | return true; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 907 | |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 908 | // Verify that CHECK-LABEL lines do not define or use variables |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 909 | if ((CheckTy == Check::CheckLabel) && P.hasVariable()) { |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 910 | SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart), |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 911 | SourceMgr::DK_Error, |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 912 | "found '" + UsedPrefix + "-LABEL:'" |
| 913 | " with variable definition or use"); |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 914 | return true; |
| 915 | } |
| 916 | |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 917 | Buffer = Buffer.substr(EOL); |
| 918 | |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 919 | // Verify that CHECK-NEXT lines have at least one CHECK line before them. |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 920 | if ((CheckTy == Check::CheckNext) && CheckStrings.empty()) { |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 921 | SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart), |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 922 | SourceMgr::DK_Error, |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 923 | "found '" + UsedPrefix + "-NEXT:' without previous '" |
| 924 | + UsedPrefix + ": line"); |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 925 | return true; |
| 926 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 927 | |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 928 | // Handle CHECK-DAG/-NOT. |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 929 | if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) { |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 930 | DagNotMatches.push_back(P); |
Chris Lattner | 74d5073 | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 931 | continue; |
| 932 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 933 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 934 | // Okay, add the string we captured to the output vector and move on. |
Chris Lattner | 3b40b44 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 935 | CheckStrings.push_back(CheckString(P, |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 936 | UsedPrefix, |
Dan Gohman | 838fb09 | 2010-01-29 21:53:18 +0000 | [diff] [blame] | 937 | PatternLoc, |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 938 | CheckTy)); |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 939 | std::swap(DagNotMatches, CheckStrings.back().DagNotStrings); |
Alexander Kornienko | 56ccdbb | 2014-07-11 12:39:32 +0000 | [diff] [blame] | 940 | DagNotMatches = ImplicitNegativeChecks; |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 941 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 942 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 943 | // Add an EOF pattern for any trailing CHECK-DAG/-NOTs, and use the first |
| 944 | // prefix as a filler for the error message. |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 945 | if (!DagNotMatches.empty()) { |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 946 | CheckStrings.push_back(CheckString(Pattern(Check::CheckEOF), |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 947 | CheckPrefixes[0], |
Jakob Stoklund Olesen | eba5582 | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 948 | SMLoc::getFromPointer(Buffer.data()), |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 949 | Check::CheckEOF)); |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 950 | std::swap(DagNotMatches, CheckStrings.back().DagNotStrings); |
Jakob Stoklund Olesen | eba5582 | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 953 | if (CheckStrings.empty()) { |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 954 | errs() << "error: no check strings found with prefix" |
| 955 | << (CheckPrefixes.size() > 1 ? "es " : " "); |
| 956 | for (size_t I = 0, N = CheckPrefixes.size(); I != N; ++I) { |
| 957 | StringRef Prefix(CheckPrefixes[I]); |
| 958 | errs() << '\'' << Prefix << ":'"; |
| 959 | if (I != N - 1) |
| 960 | errs() << ", "; |
| 961 | } |
| 962 | |
| 963 | errs() << '\n'; |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 964 | return true; |
| 965 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 966 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 967 | return false; |
| 968 | } |
| 969 | |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 970 | static void PrintCheckFailed(const SourceMgr &SM, const SMLoc &Loc, |
| 971 | const Pattern &Pat, StringRef Buffer, |
Daniel Dunbar | e0ef65a | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 972 | StringMap<StringRef> &VariableTable) { |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 973 | // Otherwise, we have an error, emit an error message. |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 974 | SM.PrintMessage(Loc, SourceMgr::DK_Error, |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 975 | "expected string not found in input"); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 976 | |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 977 | // Print the "scanning from here" line. If the current position is at the |
| 978 | // end of a line, advance to the start of the next line. |
Chris Lattner | caa5fc0 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 979 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r")); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 980 | |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 981 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 982 | "scanning from here"); |
Daniel Dunbar | e0ef65a | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 983 | |
| 984 | // Allow the pattern to print additional information if desired. |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 985 | Pat.PrintFailureInfo(SM, Buffer, VariableTable); |
| 986 | } |
| 987 | |
| 988 | static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr, |
| 989 | StringRef Buffer, |
| 990 | StringMap<StringRef> &VariableTable) { |
| 991 | PrintCheckFailed(SM, CheckStr.Loc, CheckStr.Pat, Buffer, VariableTable); |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Chris Lattner | 3718358 | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 994 | /// CountNumNewlinesBetween - Count the number of newlines in the specified |
| 995 | /// range. |
Richard Smith | 592fe88 | 2014-04-07 17:09:53 +0000 | [diff] [blame] | 996 | static unsigned CountNumNewlinesBetween(StringRef Range, |
| 997 | const char *&FirstNewLine) { |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 998 | unsigned NumNewLines = 0; |
Chris Lattner | 3718358 | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 999 | while (1) { |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 1000 | // Scan for newline. |
Chris Lattner | 3718358 | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 1001 | Range = Range.substr(Range.find_first_of("\n\r")); |
| 1002 | if (Range.empty()) return NumNewLines; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1003 | |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 1004 | ++NumNewLines; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 1006 | // Handle \n\r and \r\n as a single newline. |
Chris Lattner | 3718358 | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 1007 | if (Range.size() > 1 && |
| 1008 | (Range[1] == '\n' || Range[1] == '\r') && |
| 1009 | (Range[0] != Range[1])) |
| 1010 | Range = Range.substr(1); |
| 1011 | Range = Range.substr(1); |
Richard Smith | 592fe88 | 2014-04-07 17:09:53 +0000 | [diff] [blame] | 1012 | |
| 1013 | if (NumNewLines == 1) |
| 1014 | FirstNewLine = Range.begin(); |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 1015 | } |
Chris Lattner | da108b4 | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1018 | size_t CheckString::Check(const SourceMgr &SM, StringRef Buffer, |
Stephen Lin | e93a3a0 | 2013-10-11 18:38:36 +0000 | [diff] [blame] | 1019 | bool IsLabelScanMode, size_t &MatchLen, |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1020 | StringMap<StringRef> &VariableTable) const { |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1021 | size_t LastPos = 0; |
| 1022 | std::vector<const Pattern *> NotStrings; |
| 1023 | |
Stephen Lin | e93a3a0 | 2013-10-11 18:38:36 +0000 | [diff] [blame] | 1024 | // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL |
| 1025 | // bounds; we have not processed variable definitions within the bounded block |
| 1026 | // yet so cannot handle any final CHECK-DAG yet; this is handled when going |
| 1027 | // over the block again (including the last CHECK-LABEL) in normal mode. |
| 1028 | if (!IsLabelScanMode) { |
| 1029 | // Match "dag strings" (with mixed "not strings" if any). |
| 1030 | LastPos = CheckDag(SM, Buffer, NotStrings, VariableTable); |
| 1031 | if (LastPos == StringRef::npos) |
| 1032 | return StringRef::npos; |
| 1033 | } |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1034 | |
| 1035 | // Match itself from the last position after matching CHECK-DAG. |
| 1036 | StringRef MatchBuffer = Buffer.substr(LastPos); |
| 1037 | size_t MatchPos = Pat.Match(MatchBuffer, MatchLen, VariableTable); |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1038 | if (MatchPos == StringRef::npos) { |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1039 | PrintCheckFailed(SM, *this, MatchBuffer, VariableTable); |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1040 | return StringRef::npos; |
| 1041 | } |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1042 | MatchPos += LastPos; |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1043 | |
Stephen Lin | e93a3a0 | 2013-10-11 18:38:36 +0000 | [diff] [blame] | 1044 | // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT |
| 1045 | // or CHECK-NOT |
| 1046 | if (!IsLabelScanMode) { |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1047 | StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos); |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1048 | |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1049 | // If this check is a "CHECK-NEXT", verify that the previous match was on |
| 1050 | // the previous line (i.e. that there is one newline between them). |
| 1051 | if (CheckNext(SM, SkippedRegion)) |
| 1052 | return StringRef::npos; |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1053 | |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1054 | // If this match had "not strings", verify that they don't exist in the |
| 1055 | // skipped region. |
| 1056 | if (CheckNot(SM, SkippedRegion, NotStrings, VariableTable)) |
| 1057 | return StringRef::npos; |
| 1058 | } |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1059 | |
| 1060 | return MatchPos; |
| 1061 | } |
| 1062 | |
| 1063 | bool CheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const { |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 1064 | if (CheckTy != Check::CheckNext) |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1065 | return false; |
| 1066 | |
| 1067 | // Count the number of newlines between the previous match and this one. |
| 1068 | assert(Buffer.data() != |
| 1069 | SM.getMemoryBuffer( |
| 1070 | SM.FindBufferContainingLoc( |
| 1071 | SMLoc::getFromPointer(Buffer.data())))->getBufferStart() && |
| 1072 | "CHECK-NEXT can't be the first check in a file"); |
| 1073 | |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 1074 | const char *FirstNewLine = nullptr; |
Richard Smith | 592fe88 | 2014-04-07 17:09:53 +0000 | [diff] [blame] | 1075 | unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine); |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1076 | |
| 1077 | if (NumNewLines == 0) { |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1078 | SM.PrintMessage(Loc, SourceMgr::DK_Error, Prefix + |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1079 | "-NEXT: is on the same line as previous match"); |
| 1080 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), |
| 1081 | SourceMgr::DK_Note, "'next' match was here"); |
| 1082 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 1083 | "previous match ended here"); |
| 1084 | return true; |
| 1085 | } |
| 1086 | |
| 1087 | if (NumNewLines != 1) { |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1088 | SM.PrintMessage(Loc, SourceMgr::DK_Error, Prefix + |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1089 | "-NEXT: is not on the line after the previous match"); |
| 1090 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), |
| 1091 | SourceMgr::DK_Note, "'next' match was here"); |
| 1092 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 1093 | "previous match ended here"); |
Richard Smith | 592fe88 | 2014-04-07 17:09:53 +0000 | [diff] [blame] | 1094 | SM.PrintMessage(SMLoc::getFromPointer(FirstNewLine), SourceMgr::DK_Note, |
| 1095 | "non-matching line after previous match is here"); |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1096 | return true; |
| 1097 | } |
| 1098 | |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
| 1102 | bool CheckString::CheckNot(const SourceMgr &SM, StringRef Buffer, |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1103 | const std::vector<const Pattern *> &NotStrings, |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1104 | StringMap<StringRef> &VariableTable) const { |
| 1105 | for (unsigned ChunkNo = 0, e = NotStrings.size(); |
| 1106 | ChunkNo != e; ++ChunkNo) { |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1107 | const Pattern *Pat = NotStrings[ChunkNo]; |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 1108 | assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!"); |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1109 | |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1110 | size_t MatchLen = 0; |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1111 | size_t Pos = Pat->Match(Buffer, MatchLen, VariableTable); |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1112 | |
| 1113 | if (Pos == StringRef::npos) continue; |
| 1114 | |
| 1115 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()+Pos), |
| 1116 | SourceMgr::DK_Error, |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1117 | Prefix + "-NOT: string occurred!"); |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1118 | SM.PrintMessage(Pat->getLoc(), SourceMgr::DK_Note, |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1119 | Prefix + "-NOT: pattern specified here"); |
Michael Liao | dcc7d48 | 2013-05-14 20:29:52 +0000 | [diff] [blame] | 1120 | return true; |
| 1121 | } |
| 1122 | |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1126 | size_t CheckString::CheckDag(const SourceMgr &SM, StringRef Buffer, |
| 1127 | std::vector<const Pattern *> &NotStrings, |
| 1128 | StringMap<StringRef> &VariableTable) const { |
| 1129 | if (DagNotStrings.empty()) |
| 1130 | return 0; |
| 1131 | |
| 1132 | size_t LastPos = 0; |
| 1133 | size_t StartPos = LastPos; |
| 1134 | |
| 1135 | for (unsigned ChunkNo = 0, e = DagNotStrings.size(); |
| 1136 | ChunkNo != e; ++ChunkNo) { |
| 1137 | const Pattern &Pat = DagNotStrings[ChunkNo]; |
| 1138 | |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 1139 | assert((Pat.getCheckTy() == Check::CheckDAG || |
| 1140 | Pat.getCheckTy() == Check::CheckNot) && |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1141 | "Invalid CHECK-DAG or CHECK-NOT!"); |
| 1142 | |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 1143 | if (Pat.getCheckTy() == Check::CheckNot) { |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1144 | NotStrings.push_back(&Pat); |
| 1145 | continue; |
| 1146 | } |
| 1147 | |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 1148 | assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!"); |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1149 | |
| 1150 | size_t MatchLen = 0, MatchPos; |
| 1151 | |
| 1152 | // CHECK-DAG always matches from the start. |
| 1153 | StringRef MatchBuffer = Buffer.substr(StartPos); |
| 1154 | MatchPos = Pat.Match(MatchBuffer, MatchLen, VariableTable); |
| 1155 | // With a group of CHECK-DAGs, a single mismatching means the match on |
| 1156 | // that group of CHECK-DAGs fails immediately. |
| 1157 | if (MatchPos == StringRef::npos) { |
| 1158 | PrintCheckFailed(SM, Pat.getLoc(), Pat, MatchBuffer, VariableTable); |
| 1159 | return StringRef::npos; |
| 1160 | } |
| 1161 | // Re-calc it as the offset relative to the start of the original string. |
| 1162 | MatchPos += StartPos; |
| 1163 | |
| 1164 | if (!NotStrings.empty()) { |
| 1165 | if (MatchPos < LastPos) { |
| 1166 | // Reordered? |
| 1167 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + MatchPos), |
| 1168 | SourceMgr::DK_Error, |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1169 | Prefix + "-DAG: found a match of CHECK-DAG" |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1170 | " reordering across a CHECK-NOT"); |
| 1171 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + LastPos), |
| 1172 | SourceMgr::DK_Note, |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1173 | Prefix + "-DAG: the farthest match of CHECK-DAG" |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1174 | " is found here"); |
| 1175 | SM.PrintMessage(NotStrings[0]->getLoc(), SourceMgr::DK_Note, |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1176 | Prefix + "-NOT: the crossed pattern specified" |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1177 | " here"); |
| 1178 | SM.PrintMessage(Pat.getLoc(), SourceMgr::DK_Note, |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1179 | Prefix + "-DAG: the reordered pattern specified" |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1180 | " here"); |
| 1181 | return StringRef::npos; |
| 1182 | } |
| 1183 | // All subsequent CHECK-DAGs should be matched from the farthest |
| 1184 | // position of all precedent CHECK-DAGs (including this one.) |
| 1185 | StartPos = LastPos; |
| 1186 | // If there's CHECK-NOTs between two CHECK-DAGs or from CHECK to |
| 1187 | // CHECK-DAG, verify that there's no 'not' strings occurred in that |
| 1188 | // region. |
| 1189 | StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos); |
Tim Northover | cf708c3 | 2013-08-02 11:32:50 +0000 | [diff] [blame] | 1190 | if (CheckNot(SM, SkippedRegion, NotStrings, VariableTable)) |
Michael Liao | 91a1b2c | 2013-05-14 20:34:12 +0000 | [diff] [blame] | 1191 | return StringRef::npos; |
| 1192 | // Clear "not strings". |
| 1193 | NotStrings.clear(); |
| 1194 | } |
| 1195 | |
| 1196 | // Update the last position with CHECK-DAG matches. |
| 1197 | LastPos = std::max(MatchPos + MatchLen, LastPos); |
| 1198 | } |
| 1199 | |
| 1200 | return LastPos; |
| 1201 | } |
| 1202 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1203 | // A check prefix must contain only alphanumeric, hyphens and underscores. |
| 1204 | static bool ValidateCheckPrefix(StringRef CheckPrefix) { |
| 1205 | Regex Validator("^[a-zA-Z0-9_-]*$"); |
| 1206 | return Validator.match(CheckPrefix); |
| 1207 | } |
| 1208 | |
| 1209 | static bool ValidateCheckPrefixes() { |
| 1210 | StringSet<> PrefixSet; |
| 1211 | |
| 1212 | for (prefix_iterator I = CheckPrefixes.begin(), E = CheckPrefixes.end(); |
| 1213 | I != E; ++I) { |
| 1214 | StringRef Prefix(*I); |
| 1215 | |
Eli Bendersky | 24412b1 | 2014-07-29 20:30:53 +0000 | [diff] [blame] | 1216 | // Reject empty prefixes. |
| 1217 | if (Prefix == "") |
| 1218 | return false; |
| 1219 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1220 | if (!PrefixSet.insert(Prefix)) |
| 1221 | return false; |
| 1222 | |
| 1223 | if (!ValidateCheckPrefix(Prefix)) |
| 1224 | return false; |
| 1225 | } |
| 1226 | |
| 1227 | return true; |
| 1228 | } |
| 1229 | |
| 1230 | // I don't think there's a way to specify an initial value for cl::list, |
| 1231 | // so if nothing was specified, add the default |
| 1232 | static void AddCheckPrefixIfNeeded() { |
| 1233 | if (CheckPrefixes.empty()) |
| 1234 | CheckPrefixes.push_back("CHECK"); |
Rui Ueyama | c2735158 | 2013-08-12 23:05:59 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 1237 | int main(int argc, char **argv) { |
| 1238 | sys::PrintStackTraceOnErrorSignal(); |
| 1239 | PrettyStackTraceProgram X(argc, argv); |
| 1240 | cl::ParseCommandLineOptions(argc, argv); |
| 1241 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1242 | if (!ValidateCheckPrefixes()) { |
| 1243 | errs() << "Supplied check-prefix is invalid! Prefixes must be unique and " |
| 1244 | "start with a letter and contain only alphanumeric characters, " |
| 1245 | "hyphens and underscores\n"; |
Rui Ueyama | c2735158 | 2013-08-12 23:05:59 +0000 | [diff] [blame] | 1246 | return 2; |
| 1247 | } |
| 1248 | |
Matt Arsenault | 13df462 | 2013-11-10 02:04:09 +0000 | [diff] [blame] | 1249 | AddCheckPrefixIfNeeded(); |
| 1250 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 1251 | SourceMgr SM; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 1253 | // Read the expected strings from the check file. |
Chris Lattner | 26cccfe | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 1254 | std::vector<CheckString> CheckStrings; |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 1255 | if (ReadCheckFile(SM, CheckStrings)) |
| 1256 | return 2; |
| 1257 | |
| 1258 | // Open the file to check and add it to SourceMgr. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 1259 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 1260 | MemoryBuffer::getFileOrSTDIN(InputFilename); |
| 1261 | if (std::error_code EC = FileOrErr.getError()) { |
| 1262 | errs() << "Could not open input file '" << InputFilename |
| 1263 | << "': " << EC.message() << '\n'; |
Eli Bendersky | 8e1c647 | 2012-11-30 13:51:33 +0000 | [diff] [blame] | 1264 | return 2; |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 1265 | } |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 1266 | std::unique_ptr<MemoryBuffer> &File = FileOrErr.get(); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1267 | |
Justin Bogner | 1b9f936 | 2014-08-07 18:40:37 +0000 | [diff] [blame] | 1268 | if (File->getBufferSize() == 0 && !AllowEmptyInput) { |
Chris Lattner | b692bed | 2011-02-09 16:46:02 +0000 | [diff] [blame] | 1269 | errs() << "FileCheck error: '" << InputFilename << "' is empty.\n"; |
Eli Bendersky | 8e1c647 | 2012-11-30 13:51:33 +0000 | [diff] [blame] | 1270 | return 2; |
Chris Lattner | b692bed | 2011-02-09 16:46:02 +0000 | [diff] [blame] | 1271 | } |
Benjamin Kramer | e963d66 | 2013-03-23 13:56:23 +0000 | [diff] [blame] | 1272 | |
Chris Lattner | 2c3e5cd | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 1273 | // Remove duplicate spaces in the input file if requested. |
Guy Benyei | 5ea04c3 | 2013-02-06 20:40:38 +0000 | [diff] [blame] | 1274 | // Remove DOS style line endings. |
Benjamin Kramer | e963d66 | 2013-03-23 13:56:23 +0000 | [diff] [blame] | 1275 | MemoryBuffer *F = |
Rafael Espindola | ce5dd1a | 2014-08-01 14:11:14 +0000 | [diff] [blame] | 1276 | CanonicalizeInputFile(std::move(File), NoCanonicalizeWhiteSpace); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1277 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 1278 | SM.AddNewSourceBuffer(F, SMLoc()); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1279 | |
Chris Lattner | 8879e06 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 1280 | /// VariableTable - This holds all the current filecheck variables. |
| 1281 | StringMap<StringRef> VariableTable; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1282 | |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 1283 | // Check that we have all of the expected strings, in order, in the input |
| 1284 | // file. |
Chris Lattner | caa5fc0 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 1285 | StringRef Buffer = F->getBuffer(); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1286 | |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1287 | bool hasError = false; |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1288 | |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1289 | unsigned i = 0, j = 0, e = CheckStrings.size(); |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1290 | |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1291 | while (true) { |
| 1292 | StringRef CheckRegion; |
| 1293 | if (j == e) { |
| 1294 | CheckRegion = Buffer; |
| 1295 | } else { |
| 1296 | const CheckString &CheckLabelStr = CheckStrings[j]; |
Matt Arsenault | 3882097 | 2013-09-17 22:30:02 +0000 | [diff] [blame] | 1297 | if (CheckLabelStr.CheckTy != Check::CheckLabel) { |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1298 | ++j; |
| 1299 | continue; |
| 1300 | } |
Chris Lattner | 3718358 | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 1301 | |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1302 | // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG |
| 1303 | size_t MatchLabelLen = 0; |
Stephen Lin | e93a3a0 | 2013-10-11 18:38:36 +0000 | [diff] [blame] | 1304 | size_t MatchLabelPos = CheckLabelStr.Check(SM, Buffer, true, |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1305 | MatchLabelLen, VariableTable); |
| 1306 | if (MatchLabelPos == StringRef::npos) { |
| 1307 | hasError = true; |
| 1308 | break; |
| 1309 | } |
| 1310 | |
| 1311 | CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen); |
| 1312 | Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen); |
| 1313 | ++j; |
| 1314 | } |
| 1315 | |
| 1316 | for ( ; i != j; ++i) { |
| 1317 | const CheckString &CheckStr = CheckStrings[i]; |
| 1318 | |
| 1319 | // Check each string within the scanned region, including a second check |
| 1320 | // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG) |
| 1321 | size_t MatchLen = 0; |
Stephen Lin | e93a3a0 | 2013-10-11 18:38:36 +0000 | [diff] [blame] | 1322 | size_t MatchPos = CheckStr.Check(SM, CheckRegion, false, MatchLen, |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1323 | VariableTable); |
| 1324 | |
| 1325 | if (MatchPos == StringRef::npos) { |
| 1326 | hasError = true; |
| 1327 | i = j; |
| 1328 | break; |
| 1329 | } |
| 1330 | |
| 1331 | CheckRegion = CheckRegion.substr(MatchPos + MatchLen); |
| 1332 | } |
| 1333 | |
| 1334 | if (j == e) |
| 1335 | break; |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 1336 | } |
Mikhail Glushenkov | defcda2 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 1337 | |
Stephen Lin | f8bd2e5 | 2013-07-12 14:51:05 +0000 | [diff] [blame] | 1338 | return hasError ? 1 : 0; |
Chris Lattner | ee3c74f | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 1339 | } |