Chris Lattner | 81cb8ca | 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 | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/OwningPtr.h" |
Chandler Carruth | 4ffd89f | 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" |
Chris Lattner | 81cb8ca | 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 | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Regex.h" |
Chandler Carruth | 4ffd89f | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Signals.h" |
Chris Lattner | 81cb8ca | 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 | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 30 | #include "llvm/Support/system_error.h" |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 31 | #include <algorithm> |
Eli Bendersky | 9756ca7 | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 32 | #include <map> |
| 33 | #include <string> |
| 34 | #include <vector> |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
| 37 | static cl::opt<std::string> |
| 38 | CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required); |
| 39 | |
| 40 | static cl::opt<std::string> |
| 41 | InputFilename("input-file", cl::desc("File to check (defaults to stdin)"), |
| 42 | cl::init("-"), cl::value_desc("filename")); |
| 43 | |
| 44 | static cl::opt<std::string> |
| 45 | CheckPrefix("check-prefix", cl::init("CHECK"), |
| 46 | cl::desc("Prefix to use from check file (defaults to 'CHECK')")); |
| 47 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 48 | static cl::opt<bool> |
| 49 | NoCanonicalizeWhiteSpace("strict-whitespace", |
| 50 | cl::desc("Do not treat all horizontal whitespace as equivalent")); |
| 51 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
| 53 | // Pattern Handling Code. |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 56 | class Pattern { |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 57 | SMLoc PatternLoc; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 58 | |
Jakob Stoklund Olesen | 824c10e | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 59 | /// MatchEOF - When set, this pattern only matches the end of file. This is |
| 60 | /// used for trailing CHECK-NOTs. |
| 61 | bool MatchEOF; |
| 62 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 63 | /// FixedStr - If non-empty, this pattern is a fixed string match with the |
| 64 | /// specified fixed string. |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 65 | StringRef FixedStr; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 67 | /// RegEx - If non-empty, this is a regex pattern. |
| 68 | std::string RegExStr; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 69 | |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 70 | /// \brief Contains the number of line this pattern is in. |
| 71 | unsigned LineNumber; |
| 72 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 73 | /// VariableUses - Entries in this vector map to uses of a variable in the |
| 74 | /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain |
| 75 | /// "foobaz" and we'll get an entry in this vector that tells us to insert the |
| 76 | /// value of bar at offset 3. |
| 77 | std::vector<std::pair<StringRef, unsigned> > VariableUses; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 78 | |
Eli Bendersky | 9756ca7 | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 79 | /// VariableDefs - Maps definitions of variables to their parenthesized |
| 80 | /// capture numbers. |
| 81 | /// E.g. for the pattern "foo[[bar:.*]]baz", VariableDefs will map "bar" to 1. |
| 82 | std::map<StringRef, unsigned> VariableDefs; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 84 | public: |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 85 | |
Jakob Stoklund Olesen | 824c10e | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 86 | Pattern(bool matchEOF = false) : MatchEOF(matchEOF) { } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 87 | |
Michael Liao | 0fc7137 | 2013-04-25 21:31:34 +0000 | [diff] [blame] | 88 | /// getLoc - Return the location in source code. |
| 89 | SMLoc getLoc() const { return PatternLoc; } |
| 90 | |
Eli Bendersky | 1e5cbcb | 2012-11-30 14:22:14 +0000 | [diff] [blame] | 91 | /// ParsePattern - Parse the given string into the Pattern. SM provides the |
| 92 | /// SourceMgr used for error reports, and LineNumber is the line number in |
| 93 | /// the input file from which the pattern string was read. |
| 94 | /// Returns true in case of an error, false otherwise. |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 95 | bool ParsePattern(StringRef PatternStr, SourceMgr &SM, unsigned LineNumber); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 97 | /// Match - Match the pattern string against the input buffer Buffer. This |
| 98 | /// returns the position that is matched or npos if there is no match. If |
| 99 | /// there is a match, the size of the matched string is returned in MatchLen. |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 100 | /// |
| 101 | /// The VariableTable StringMap provides the current values of filecheck |
| 102 | /// variables and is updated if this match defines new values. |
| 103 | size_t Match(StringRef Buffer, size_t &MatchLen, |
| 104 | StringMap<StringRef> &VariableTable) const; |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 105 | |
| 106 | /// PrintFailureInfo - Print additional information about a failure to match |
| 107 | /// involving this pattern. |
| 108 | void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer, |
| 109 | const StringMap<StringRef> &VariableTable) const; |
| 110 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 111 | private: |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 112 | static void AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr); |
Eli Bendersky | 9756ca7 | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 113 | bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM); |
| 114 | void AddBackrefToRegEx(unsigned BackrefNum); |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 115 | |
| 116 | /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of |
| 117 | /// matching this pattern at the start of \arg Buffer; a distance of zero |
| 118 | /// should correspond to a perfect match. |
| 119 | unsigned ComputeMatchDistance(StringRef Buffer, |
| 120 | const StringMap<StringRef> &VariableTable) const; |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 121 | |
| 122 | /// \brief Evaluates expression and stores the result to \p Value. |
| 123 | /// \return true on success. false when the expression has invalid syntax. |
| 124 | bool EvaluateExpression(StringRef Expr, std::string &Value) const; |
Eli Bendersky | 4db6511 | 2012-12-02 16:02:41 +0000 | [diff] [blame] | 125 | |
| 126 | /// \brief Finds the closing sequence of a regex variable usage or |
| 127 | /// definition. Str has to point in the beginning of the definition |
| 128 | /// (right after the opening sequence). |
| 129 | /// \return offset of the closing sequence within Str, or npos if it was not |
| 130 | /// found. |
| 131 | size_t FindRegexVarEnd(StringRef Str); |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 134 | |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 135 | bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM, |
| 136 | unsigned LineNumber) { |
| 137 | this->LineNumber = LineNumber; |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 138 | PatternLoc = SMLoc::getFromPointer(PatternStr.data()); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 139 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 140 | // Ignore trailing whitespace. |
| 141 | while (!PatternStr.empty() && |
| 142 | (PatternStr.back() == ' ' || PatternStr.back() == '\t')) |
| 143 | PatternStr = PatternStr.substr(0, PatternStr.size()-1); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 144 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 145 | // Check that there is something on the line. |
| 146 | if (PatternStr.empty()) { |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 147 | SM.PrintMessage(PatternLoc, SourceMgr::DK_Error, |
| 148 | "found empty check string with prefix '" + |
| 149 | CheckPrefix+":'"); |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 150 | return true; |
| 151 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 153 | // Check to see if this is a fixed string, or if it has regex pieces. |
Ted Kremenek | 4f50517 | 2012-09-08 04:32:13 +0000 | [diff] [blame] | 154 | if (PatternStr.size() < 2 || |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 155 | (PatternStr.find("{{") == StringRef::npos && |
| 156 | PatternStr.find("[[") == StringRef::npos)) { |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 157 | FixedStr = PatternStr; |
| 158 | return false; |
| 159 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 160 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 161 | // Paren value #0 is for the fully matched string. Any new parenthesized |
Chris Lattner | 13a38c4 | 2011-04-09 06:18:02 +0000 | [diff] [blame] | 162 | // values add from there. |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 163 | unsigned CurParen = 1; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 165 | // Otherwise, there is at least one regex piece. Build up the regex pattern |
| 166 | // by escaping scary characters in fixed strings, building up one big regex. |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 167 | while (!PatternStr.empty()) { |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 168 | // RegEx matches. |
Chris Lattner | 13a38c4 | 2011-04-09 06:18:02 +0000 | [diff] [blame] | 169 | if (PatternStr.startswith("{{")) { |
Eli Bendersky | 1e5cbcb | 2012-11-30 14:22:14 +0000 | [diff] [blame] | 170 | // This is the start of a regex match. Scan for the }}. |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 171 | size_t End = PatternStr.find("}}"); |
| 172 | if (End == StringRef::npos) { |
| 173 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 174 | SourceMgr::DK_Error, |
| 175 | "found start of regex string with no end '}}'"); |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 176 | return true; |
| 177 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 42e31df | 2011-04-09 06:37:03 +0000 | [diff] [blame] | 179 | // Enclose {{}} patterns in parens just like [[]] even though we're not |
| 180 | // capturing the result for any purpose. This is required in case the |
| 181 | // expression contains an alternation like: CHECK: abc{{x|z}}def. We |
| 182 | // want this to turn into: "abc(x|z)def" not "abcx|zdef". |
| 183 | RegExStr += '('; |
| 184 | ++CurParen; |
| 185 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 186 | if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM)) |
| 187 | return true; |
Chris Lattner | 42e31df | 2011-04-09 06:37:03 +0000 | [diff] [blame] | 188 | RegExStr += ')'; |
Chris Lattner | 13a38c4 | 2011-04-09 06:18:02 +0000 | [diff] [blame] | 189 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 190 | PatternStr = PatternStr.substr(End+2); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 191 | continue; |
| 192 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 193 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 194 | // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .* |
| 195 | // (or some other regex) and assigns it to the FileCheck variable 'foo'. The |
| 196 | // second form is [[foo]] which is a reference to foo. The variable name |
Daniel Dunbar | 964ac01 | 2009-11-22 22:07:50 +0000 | [diff] [blame] | 197 | // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 198 | // it. This is to catch some common errors. |
Chris Lattner | 13a38c4 | 2011-04-09 06:18:02 +0000 | [diff] [blame] | 199 | if (PatternStr.startswith("[[")) { |
Eli Bendersky | 4db6511 | 2012-12-02 16:02:41 +0000 | [diff] [blame] | 200 | // Find the closing bracket pair ending the match. End is going to be an |
| 201 | // offset relative to the beginning of the match string. |
| 202 | size_t End = FindRegexVarEnd(PatternStr.substr(2)); |
| 203 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 204 | if (End == StringRef::npos) { |
| 205 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 206 | SourceMgr::DK_Error, |
| 207 | "invalid named regex reference, no ]] found"); |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 208 | return true; |
| 209 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 210 | |
Eli Bendersky | 4db6511 | 2012-12-02 16:02:41 +0000 | [diff] [blame] | 211 | StringRef MatchStr = PatternStr.substr(2, End); |
| 212 | PatternStr = PatternStr.substr(End+4); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 213 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 214 | // Get the regex name (e.g. "foo"). |
| 215 | size_t NameEnd = MatchStr.find(':'); |
| 216 | StringRef Name = MatchStr.substr(0, NameEnd); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 217 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 218 | if (Name.empty()) { |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 219 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, |
| 220 | "invalid name in named regex: empty name"); |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 224 | // Verify that the name/expression is well formed. FileCheck currently |
| 225 | // supports @LINE, @LINE+number, @LINE-number expressions. The check here |
| 226 | // is relaxed, more strict check is performed in \c EvaluateExpression. |
| 227 | bool IsExpression = false; |
| 228 | for (unsigned i = 0, e = Name.size(); i != e; ++i) { |
| 229 | if (i == 0 && Name[i] == '@') { |
| 230 | if (NameEnd != StringRef::npos) { |
| 231 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), |
| 232 | SourceMgr::DK_Error, |
| 233 | "invalid name in named regex definition"); |
| 234 | return true; |
| 235 | } |
| 236 | IsExpression = true; |
| 237 | continue; |
| 238 | } |
| 239 | if (Name[i] != '_' && !isalnum(Name[i]) && |
| 240 | (!IsExpression || (Name[i] != '+' && Name[i] != '-'))) { |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 241 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i), |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 242 | SourceMgr::DK_Error, "invalid name in named regex"); |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 243 | return true; |
| 244 | } |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 245 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 246 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 247 | // Name can't start with a digit. |
Guy Benyei | 87d0b9e | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 248 | if (isdigit(static_cast<unsigned char>(Name[0]))) { |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 249 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, |
| 250 | "invalid name in named regex"); |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 251 | return true; |
| 252 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 253 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 254 | // Handle [[foo]]. |
| 255 | if (NameEnd == StringRef::npos) { |
Eli Bendersky | 9756ca7 | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 256 | // Handle variables that were defined earlier on the same line by |
| 257 | // emitting a backreference. |
| 258 | if (VariableDefs.find(Name) != VariableDefs.end()) { |
| 259 | unsigned VarParenNum = VariableDefs[Name]; |
| 260 | if (VarParenNum < 1 || VarParenNum > 9) { |
| 261 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), |
| 262 | SourceMgr::DK_Error, |
| 263 | "Can't back-reference more than 9 variables"); |
| 264 | return true; |
| 265 | } |
| 266 | AddBackrefToRegEx(VarParenNum); |
| 267 | } else { |
| 268 | VariableUses.push_back(std::make_pair(Name, RegExStr.size())); |
| 269 | } |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 270 | continue; |
| 271 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 272 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 273 | // Handle [[foo:.*]]. |
Eli Bendersky | 9756ca7 | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 274 | VariableDefs[Name] = CurParen; |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 275 | RegExStr += '('; |
| 276 | ++CurParen; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 277 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 278 | if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM)) |
| 279 | return true; |
| 280 | |
| 281 | RegExStr += ')'; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 282 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 283 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 284 | // Handle fixed string matches. |
| 285 | // Find the end, which is the start of the next regex. |
| 286 | size_t FixedMatchEnd = PatternStr.find("{{"); |
| 287 | FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[[")); |
| 288 | AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr); |
| 289 | PatternStr = PatternStr.substr(FixedMatchEnd); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 290 | } |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 291 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 292 | return false; |
| 293 | } |
| 294 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 295 | void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) { |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 296 | // Add the characters from FixedStr to the regex, escaping as needed. This |
| 297 | // avoids "leaning toothpicks" in common patterns. |
| 298 | for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) { |
| 299 | switch (FixedStr[i]) { |
| 300 | // These are the special characters matched in "p_ere_exp". |
| 301 | case '(': |
| 302 | case ')': |
| 303 | case '^': |
| 304 | case '$': |
| 305 | case '|': |
| 306 | case '*': |
| 307 | case '+': |
| 308 | case '?': |
| 309 | case '.': |
| 310 | case '[': |
| 311 | case '\\': |
| 312 | case '{': |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 313 | TheStr += '\\'; |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 314 | // FALL THROUGH. |
| 315 | default: |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 316 | TheStr += FixedStr[i]; |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Eli Bendersky | 9756ca7 | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 322 | bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 323 | SourceMgr &SM) { |
Eli Bendersky | 9756ca7 | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 324 | Regex R(RS); |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 325 | std::string Error; |
| 326 | if (!R.isValid(Error)) { |
Eli Bendersky | 9756ca7 | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 327 | SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error, |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 328 | "invalid regex: " + Error); |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 329 | return true; |
| 330 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 331 | |
Eli Bendersky | 9756ca7 | 2012-12-01 21:54:48 +0000 | [diff] [blame] | 332 | RegExStr += RS.str(); |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 333 | CurParen += R.getNumMatches(); |
| 334 | return false; |
| 335 | } |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 336 | |
Eli Bendersky | 9756ca7 | 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 | 70a870a | 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 | 5287008 | 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 | eec9695 | 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 | 824c10e | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 367 | // If this is the EOF pattern, match it immediately. |
| 368 | if (MatchEOF) { |
| 369 | MatchLen = 0; |
| 370 | return Buffer.size(); |
| 371 | } |
| 372 | |
Chris Lattner | 2702e6a | 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 | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 379 | // Regex match. |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 380 | |
Chris Lattner | eec9695 | 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 | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 387 | |
Chris Lattner | eec9695 | 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 | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 390 | std::string Value; |
Alexander Kornienko | 70a870a | 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 | |
| 402 | // Look up the value and escape it so that we can plop it into the regex. |
| 403 | AddFixedStringToRegEx(it->second, Value); |
| 404 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 405 | |
Chris Lattner | eec9695 | 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 | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 411 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 412 | // Match the newly constructed regex. |
| 413 | RegExToMatch = TmpStr; |
| 414 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 415 | |
| 416 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 417 | SmallVector<StringRef, 4> MatchInfo; |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 418 | if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo)) |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 419 | return StringRef::npos; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 5d6a05f | 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 | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 424 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 425 | // If this defines any variables, remember their values. |
Eli Bendersky | 9756ca7 | 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 | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 431 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 433 | MatchLen = FullMatch.size(); |
| 434 | return FullMatch.data()-Buffer.data(); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Daniel Dunbar | ead2dac | 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 | 0806f9f | 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 | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Daniel Dunbar | fafe93c | 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 | fafe93c | 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 | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 461 | SmallString<256> Msg; |
| 462 | raw_svector_ostream OS(Msg); |
Alexander Kornienko | 70a870a | 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 | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 474 | } else { |
Alexander Kornienko | 70a870a | 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 | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 488 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 489 | OS.str()); |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
Daniel Dunbar | ead2dac | 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 | e3a1e50 | 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 | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 503 | if (Buffer[i] == '\n') |
| 504 | ++NumLinesForward; |
| 505 | |
Dan Gohman | d8a5541 | 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 | ead2dac | 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 | 7a68e0d | 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 | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 527 | SourceMgr::DK_Note, "possible intended match here"); |
Daniel Dunbar | ead2dac | 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 | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 532 | } |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 533 | |
Eli Bendersky | 4db6511 | 2012-12-02 16:02:41 +0000 | [diff] [blame] | 534 | size_t Pattern::FindRegexVarEnd(StringRef Str) { |
| 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 ']': |
| 555 | assert(BracketDepth > 0 && "Invalid regex"); |
| 556 | BracketDepth--; |
| 557 | break; |
| 558 | } |
| 559 | Str = Str.substr(1); |
| 560 | Offset++; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return StringRef::npos; |
| 565 | } |
| 566 | |
| 567 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 568 | //===----------------------------------------------------------------------===// |
| 569 | // Check Strings. |
| 570 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 571 | |
| 572 | /// CheckString - This is a check that we found in the input file. |
| 573 | struct CheckString { |
| 574 | /// Pat - The pattern to match. |
| 575 | Pattern Pat; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 577 | /// Loc - The location in the match file that the check string was specified. |
| 578 | SMLoc Loc; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 579 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 580 | /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed |
| 581 | /// to a CHECK: directive. |
| 582 | bool IsCheckNext; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 583 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 584 | /// NotStrings - These are all of the strings that are disallowed from |
| 585 | /// occurring between this match string and the previous one (or start of |
| 586 | /// file). |
Michael Liao | 0fc7137 | 2013-04-25 21:31:34 +0000 | [diff] [blame] | 587 | std::vector<Pattern> NotStrings; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 588 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 589 | CheckString(const Pattern &P, SMLoc L, bool isCheckNext) |
| 590 | : Pat(P), Loc(L), IsCheckNext(isCheckNext) {} |
Michael Liao | 7efbbd6 | 2013-05-14 20:29:52 +0000 | [diff] [blame^] | 591 | |
| 592 | /// Check - Match check string and its "not strings". |
| 593 | size_t Check(const SourceMgr &SM, StringRef Buffer, size_t &MatchLen, |
| 594 | StringMap<StringRef> &VariableTable) const; |
| 595 | |
| 596 | /// CheckNext - Verify there is a single line in the given buffer. |
| 597 | bool CheckNext(const SourceMgr &SM, StringRef Buffer) const; |
| 598 | |
| 599 | /// CheckNot - Verify there's no "not strings" in the given buffer. |
| 600 | bool CheckNot(const SourceMgr &SM, StringRef Buffer, |
| 601 | StringMap<StringRef> &VariableTable) const; |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 602 | }; |
| 603 | |
Guy Benyei | 4cc74fc | 2013-02-06 20:40:38 +0000 | [diff] [blame] | 604 | /// Canonicalize whitespaces in the input file. Line endings are replaced |
| 605 | /// with UNIX-style '\n'. |
| 606 | /// |
| 607 | /// \param PreserveHorizontal Don't squash consecutive horizontal whitespace |
| 608 | /// characters to a single space. |
| 609 | static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB, |
| 610 | bool PreserveHorizontal) { |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 611 | SmallString<128> NewFile; |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 612 | NewFile.reserve(MB->getBufferSize()); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 613 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 614 | for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd(); |
| 615 | Ptr != End; ++Ptr) { |
NAKAMURA Takumi | 9f6e03f | 2010-11-14 03:28:22 +0000 | [diff] [blame] | 616 | // Eliminate trailing dosish \r. |
| 617 | if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') { |
| 618 | continue; |
| 619 | } |
| 620 | |
Michael Liao | c16f8c5 | 2013-04-25 18:54:02 +0000 | [diff] [blame] | 621 | // If current char is not a horizontal whitespace or if horizontal |
Guy Benyei | 4cc74fc | 2013-02-06 20:40:38 +0000 | [diff] [blame] | 622 | // whitespace canonicalization is disabled, dump it to output as is. |
| 623 | if (PreserveHorizontal || (*Ptr != ' ' && *Ptr != '\t')) { |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 624 | NewFile.push_back(*Ptr); |
| 625 | continue; |
| 626 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 627 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 628 | // Otherwise, add one space and advance over neighboring space. |
| 629 | NewFile.push_back(' '); |
| 630 | while (Ptr+1 != End && |
| 631 | (Ptr[1] == ' ' || Ptr[1] == '\t')) |
| 632 | ++Ptr; |
| 633 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 634 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 635 | // Free the old buffer and return a new one. |
| 636 | MemoryBuffer *MB2 = |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 637 | MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier()); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 638 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 639 | delete MB; |
| 640 | return MB2; |
| 641 | } |
| 642 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 644 | /// ReadCheckFile - Read the check file, which specifies the sequence of |
| 645 | /// expected strings. The strings are added to the CheckStrings vector. |
Eli Bendersky | 1e5cbcb | 2012-11-30 14:22:14 +0000 | [diff] [blame] | 646 | /// Returns true in case of an error, false otherwise. |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 647 | static bool ReadCheckFile(SourceMgr &SM, |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 648 | std::vector<CheckString> &CheckStrings) { |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 649 | OwningPtr<MemoryBuffer> File; |
| 650 | if (error_code ec = |
| 651 | MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), File)) { |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 652 | errs() << "Could not open check file '" << CheckFilename << "': " |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 653 | << ec.message() << '\n'; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 654 | return true; |
| 655 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 656 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 657 | // If we want to canonicalize whitespace, strip excess whitespace from the |
Guy Benyei | 4cc74fc | 2013-02-06 20:40:38 +0000 | [diff] [blame] | 658 | // buffer containing the CHECK lines. Remove DOS style line endings. |
Benjamin Kramer | 7cdba15 | 2013-03-23 13:56:23 +0000 | [diff] [blame] | 659 | MemoryBuffer *F = |
| 660 | CanonicalizeInputFile(File.take(), NoCanonicalizeWhiteSpace); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 662 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 663 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 664 | // Find all instances of CheckPrefix followed by : in the file. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 665 | StringRef Buffer = F->getBuffer(); |
Michael Liao | 0fc7137 | 2013-04-25 21:31:34 +0000 | [diff] [blame] | 666 | std::vector<Pattern> NotMatches; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 667 | |
Eli Bendersky | 1e5cbcb | 2012-11-30 14:22:14 +0000 | [diff] [blame] | 668 | // LineNumber keeps track of the line on which CheckPrefix instances are |
| 669 | // found. |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 670 | unsigned LineNumber = 1; |
| 671 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 672 | while (1) { |
| 673 | // See if Prefix occurs in the memory buffer. |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 674 | size_t PrefixLoc = Buffer.find(CheckPrefix); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 675 | // If we didn't find a match, we're done. |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 676 | if (PrefixLoc == StringRef::npos) |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 677 | break; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 678 | |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 679 | LineNumber += Buffer.substr(0, PrefixLoc).count('\n'); |
| 680 | |
| 681 | Buffer = Buffer.substr(PrefixLoc); |
| 682 | |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 683 | const char *CheckPrefixStart = Buffer.data(); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 685 | // When we find a check prefix, keep track of whether we find CHECK: or |
| 686 | // CHECK-NEXT: |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 687 | bool IsCheckNext = false, IsCheckNot = false; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 688 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 689 | // Verify that the : is present after the prefix. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 690 | if (Buffer[CheckPrefix.size()] == ':') { |
| 691 | Buffer = Buffer.substr(CheckPrefix.size()+1); |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 692 | } else if (Buffer.size() > CheckPrefix.size()+6 && |
| 693 | memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) { |
Benjamin Kramer | 30ce40e | 2012-09-18 20:51:39 +0000 | [diff] [blame] | 694 | Buffer = Buffer.substr(CheckPrefix.size()+6); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 695 | IsCheckNext = true; |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 696 | } else if (Buffer.size() > CheckPrefix.size()+5 && |
| 697 | memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) { |
Benjamin Kramer | 30ce40e | 2012-09-18 20:51:39 +0000 | [diff] [blame] | 698 | Buffer = Buffer.substr(CheckPrefix.size()+5); |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 699 | IsCheckNot = true; |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 700 | } else { |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 701 | Buffer = Buffer.substr(1); |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 702 | continue; |
| 703 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 705 | // Okay, we found the prefix, yay. Remember the rest of the line, but |
| 706 | // ignore leading and trailing whitespace. |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 707 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t")); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 708 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 709 | // Scan ahead to the end of line. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 710 | size_t EOL = Buffer.find_first_of("\n\r"); |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 711 | |
Dan Gohman | e546343 | 2010-01-29 21:53:18 +0000 | [diff] [blame] | 712 | // Remember the location of the start of the pattern, for diagnostics. |
| 713 | SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data()); |
| 714 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 715 | // Parse the pattern. |
| 716 | Pattern P; |
Alexander Kornienko | 70a870a | 2012-11-14 21:07:37 +0000 | [diff] [blame] | 717 | if (P.ParsePattern(Buffer.substr(0, EOL), SM, LineNumber)) |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 718 | return true; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 719 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 720 | Buffer = Buffer.substr(EOL); |
| 721 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 722 | // Verify that CHECK-NEXT lines have at least one CHECK line before them. |
| 723 | if (IsCheckNext && CheckStrings.empty()) { |
| 724 | SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart), |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 725 | SourceMgr::DK_Error, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 726 | "found '"+CheckPrefix+"-NEXT:' without previous '"+ |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 727 | CheckPrefix+ ": line"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 728 | return true; |
| 729 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 730 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 731 | // Handle CHECK-NOT. |
| 732 | if (IsCheckNot) { |
Michael Liao | 0fc7137 | 2013-04-25 21:31:34 +0000 | [diff] [blame] | 733 | NotMatches.push_back(P); |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 734 | continue; |
| 735 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 736 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 737 | // Okay, add the string we captured to the output vector and move on. |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 738 | CheckStrings.push_back(CheckString(P, |
Dan Gohman | e546343 | 2010-01-29 21:53:18 +0000 | [diff] [blame] | 739 | PatternLoc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 740 | IsCheckNext)); |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 741 | std::swap(NotMatches, CheckStrings.back().NotStrings); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 742 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 743 | |
Jakob Stoklund Olesen | 824c10e | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 744 | // Add an EOF pattern for any trailing CHECK-NOTs. |
| 745 | if (!NotMatches.empty()) { |
| 746 | CheckStrings.push_back(CheckString(Pattern(true), |
| 747 | SMLoc::getFromPointer(Buffer.data()), |
| 748 | false)); |
| 749 | std::swap(NotMatches, CheckStrings.back().NotStrings); |
| 750 | } |
| 751 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 752 | if (CheckStrings.empty()) { |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 753 | errs() << "error: no check strings found with prefix '" << CheckPrefix |
| 754 | << ":'\n"; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 755 | return true; |
| 756 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 757 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 758 | return false; |
| 759 | } |
| 760 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 761 | static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr, |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 762 | StringRef Buffer, |
| 763 | StringMap<StringRef> &VariableTable) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 764 | // Otherwise, we have an error, emit an error message. |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 765 | SM.PrintMessage(CheckStr.Loc, SourceMgr::DK_Error, |
| 766 | "expected string not found in input"); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 767 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 768 | // Print the "scanning from here" line. If the current position is at the |
| 769 | // end of a line, advance to the start of the next line. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 770 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r")); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 771 | |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 772 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 773 | "scanning from here"); |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 774 | |
| 775 | // Allow the pattern to print additional information if desired. |
| 776 | CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 779 | /// CountNumNewlinesBetween - Count the number of newlines in the specified |
| 780 | /// range. |
| 781 | static unsigned CountNumNewlinesBetween(StringRef Range) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 782 | unsigned NumNewLines = 0; |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 783 | while (1) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 784 | // Scan for newline. |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 785 | Range = Range.substr(Range.find_first_of("\n\r")); |
| 786 | if (Range.empty()) return NumNewLines; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 787 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 788 | ++NumNewLines; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 789 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 790 | // Handle \n\r and \r\n as a single newline. |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 791 | if (Range.size() > 1 && |
| 792 | (Range[1] == '\n' || Range[1] == '\r') && |
| 793 | (Range[0] != Range[1])) |
| 794 | Range = Range.substr(1); |
| 795 | Range = Range.substr(1); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 796 | } |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Michael Liao | 7efbbd6 | 2013-05-14 20:29:52 +0000 | [diff] [blame^] | 799 | size_t CheckString::Check(const SourceMgr &SM, StringRef Buffer, |
| 800 | size_t &MatchLen, |
| 801 | StringMap<StringRef> &VariableTable) const { |
| 802 | size_t MatchPos = Pat.Match(Buffer, MatchLen, VariableTable); |
| 803 | if (MatchPos == StringRef::npos) { |
| 804 | PrintCheckFailed(SM, *this, Buffer, VariableTable); |
| 805 | return StringRef::npos; |
| 806 | } |
| 807 | |
| 808 | StringRef SkippedRegion = Buffer.substr(0, MatchPos); |
| 809 | |
| 810 | // If this check is a "CHECK-NEXT", verify that the previous match was on |
| 811 | // the previous line (i.e. that there is one newline between them). |
| 812 | if (CheckNext(SM, SkippedRegion)) |
| 813 | return StringRef::npos; |
| 814 | |
| 815 | // If this match had "not strings", verify that they don't exist in the |
| 816 | // skipped region. |
| 817 | if (CheckNot(SM, SkippedRegion, VariableTable)) |
| 818 | return StringRef::npos; |
| 819 | |
| 820 | return MatchPos; |
| 821 | } |
| 822 | |
| 823 | bool CheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const { |
| 824 | if (!IsCheckNext) |
| 825 | return false; |
| 826 | |
| 827 | // Count the number of newlines between the previous match and this one. |
| 828 | assert(Buffer.data() != |
| 829 | SM.getMemoryBuffer( |
| 830 | SM.FindBufferContainingLoc( |
| 831 | SMLoc::getFromPointer(Buffer.data())))->getBufferStart() && |
| 832 | "CHECK-NEXT can't be the first check in a file"); |
| 833 | |
| 834 | unsigned NumNewLines = CountNumNewlinesBetween(Buffer); |
| 835 | |
| 836 | if (NumNewLines == 0) { |
| 837 | SM.PrintMessage(Loc, SourceMgr::DK_Error, CheckPrefix+ |
| 838 | "-NEXT: is on the same line as previous match"); |
| 839 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), |
| 840 | SourceMgr::DK_Note, "'next' match was here"); |
| 841 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 842 | "previous match ended here"); |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | if (NumNewLines != 1) { |
| 847 | SM.PrintMessage(Loc, SourceMgr::DK_Error, CheckPrefix+ |
| 848 | "-NEXT: is not on the line after the previous match"); |
| 849 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), |
| 850 | SourceMgr::DK_Note, "'next' match was here"); |
| 851 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 852 | "previous match ended here"); |
| 853 | return true; |
| 854 | } |
| 855 | |
| 856 | return false; |
| 857 | } |
| 858 | |
| 859 | bool CheckString::CheckNot(const SourceMgr &SM, StringRef Buffer, |
| 860 | StringMap<StringRef> &VariableTable) const { |
| 861 | for (unsigned ChunkNo = 0, e = NotStrings.size(); |
| 862 | ChunkNo != e; ++ChunkNo) { |
| 863 | size_t MatchLen = 0; |
| 864 | size_t Pos = NotStrings[ChunkNo].Match(Buffer, MatchLen, VariableTable); |
| 865 | |
| 866 | if (Pos == StringRef::npos) continue; |
| 867 | |
| 868 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()+Pos), |
| 869 | SourceMgr::DK_Error, |
| 870 | CheckPrefix+"-NOT: string occurred!"); |
| 871 | SM.PrintMessage(NotStrings[ChunkNo].getLoc(), SourceMgr::DK_Note, |
| 872 | CheckPrefix+"-NOT: pattern specified here"); |
| 873 | return true; |
| 874 | } |
| 875 | |
| 876 | return false; |
| 877 | } |
| 878 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 879 | int main(int argc, char **argv) { |
| 880 | sys::PrintStackTraceOnErrorSignal(); |
| 881 | PrettyStackTraceProgram X(argc, argv); |
| 882 | cl::ParseCommandLineOptions(argc, argv); |
| 883 | |
| 884 | SourceMgr SM; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 885 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 886 | // Read the expected strings from the check file. |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 887 | std::vector<CheckString> CheckStrings; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 888 | if (ReadCheckFile(SM, CheckStrings)) |
| 889 | return 2; |
| 890 | |
| 891 | // Open the file to check and add it to SourceMgr. |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 892 | OwningPtr<MemoryBuffer> File; |
| 893 | if (error_code ec = |
| 894 | MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) { |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 895 | errs() << "Could not open input file '" << InputFilename << "': " |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 896 | << ec.message() << '\n'; |
Eli Bendersky | 7f8e76f | 2012-11-30 13:51:33 +0000 | [diff] [blame] | 897 | return 2; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 898 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 899 | |
Benjamin Kramer | 7cdba15 | 2013-03-23 13:56:23 +0000 | [diff] [blame] | 900 | if (File->getBufferSize() == 0) { |
Chris Lattner | 1aac186 | 2011-02-09 16:46:02 +0000 | [diff] [blame] | 901 | errs() << "FileCheck error: '" << InputFilename << "' is empty.\n"; |
Eli Bendersky | 7f8e76f | 2012-11-30 13:51:33 +0000 | [diff] [blame] | 902 | return 2; |
Chris Lattner | 1aac186 | 2011-02-09 16:46:02 +0000 | [diff] [blame] | 903 | } |
Benjamin Kramer | 7cdba15 | 2013-03-23 13:56:23 +0000 | [diff] [blame] | 904 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 905 | // Remove duplicate spaces in the input file if requested. |
Guy Benyei | 4cc74fc | 2013-02-06 20:40:38 +0000 | [diff] [blame] | 906 | // Remove DOS style line endings. |
Benjamin Kramer | 7cdba15 | 2013-03-23 13:56:23 +0000 | [diff] [blame] | 907 | MemoryBuffer *F = |
| 908 | CanonicalizeInputFile(File.take(), NoCanonicalizeWhiteSpace); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 909 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 910 | SM.AddNewSourceBuffer(F, SMLoc()); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 911 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 912 | /// VariableTable - This holds all the current filecheck variables. |
| 913 | StringMap<StringRef> VariableTable; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 914 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 915 | // Check that we have all of the expected strings, in order, in the input |
| 916 | // file. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 917 | StringRef Buffer = F->getBuffer(); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 918 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 919 | for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) { |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 920 | const CheckString &CheckStr = CheckStrings[StrNo]; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 921 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 922 | // Find StrNo in the file. |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 923 | size_t MatchLen = 0; |
Michael Liao | 7efbbd6 | 2013-05-14 20:29:52 +0000 | [diff] [blame^] | 924 | size_t MatchPos = CheckStr.Check(SM, Buffer, MatchLen, VariableTable); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 925 | |
Michael Liao | 7efbbd6 | 2013-05-14 20:29:52 +0000 | [diff] [blame^] | 926 | if (MatchPos == StringRef::npos) |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 927 | return 1; |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 928 | |
Michael Liao | 7efbbd6 | 2013-05-14 20:29:52 +0000 | [diff] [blame^] | 929 | Buffer = Buffer.substr(MatchPos + MatchLen); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 930 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 932 | return 0; |
| 933 | } |