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