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