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 | |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Regex.h" |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/SourceMgr.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/System/Signals.h" |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringMap.h" |
| 28 | #include <algorithm> |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | static cl::opt<std::string> |
| 32 | CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required); |
| 33 | |
| 34 | static cl::opt<std::string> |
| 35 | InputFilename("input-file", cl::desc("File to check (defaults to stdin)"), |
| 36 | cl::init("-"), cl::value_desc("filename")); |
| 37 | |
| 38 | static cl::opt<std::string> |
| 39 | CheckPrefix("check-prefix", cl::init("CHECK"), |
| 40 | cl::desc("Prefix to use from check file (defaults to 'CHECK')")); |
| 41 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 42 | static cl::opt<bool> |
| 43 | NoCanonicalizeWhiteSpace("strict-whitespace", |
| 44 | cl::desc("Do not treat all horizontal whitespace as equivalent")); |
| 45 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | // Pattern Handling Code. |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 50 | class Pattern { |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 51 | SMLoc PatternLoc; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 52 | |
Jakob Stoklund Olesen | 824c10e | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 53 | /// MatchEOF - When set, this pattern only matches the end of file. This is |
| 54 | /// used for trailing CHECK-NOTs. |
| 55 | bool MatchEOF; |
| 56 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 57 | /// FixedStr - If non-empty, this pattern is a fixed string match with the |
| 58 | /// specified fixed string. |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 59 | StringRef FixedStr; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 61 | /// RegEx - If non-empty, this is a regex pattern. |
| 62 | std::string RegExStr; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 63 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 64 | /// VariableUses - Entries in this vector map to uses of a variable in the |
| 65 | /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain |
| 66 | /// "foobaz" and we'll get an entry in this vector that tells us to insert the |
| 67 | /// value of bar at offset 3. |
| 68 | std::vector<std::pair<StringRef, unsigned> > VariableUses; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 69 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 70 | /// VariableDefs - Entries in this vector map to definitions of a variable in |
| 71 | /// the pattern, e.g. "foo[[bar:.*]]baz". In this case, the RegExStr will |
| 72 | /// contain "foo(.*)baz" and VariableDefs will contain the pair "bar",1. The |
| 73 | /// index indicates what parenthesized value captures the variable value. |
| 74 | std::vector<std::pair<StringRef, unsigned> > VariableDefs; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 76 | public: |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 77 | |
Jakob Stoklund Olesen | 824c10e | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 78 | Pattern(bool matchEOF = false) : MatchEOF(matchEOF) { } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 79 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 80 | bool ParsePattern(StringRef PatternStr, SourceMgr &SM); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 82 | /// Match - Match the pattern string against the input buffer Buffer. This |
| 83 | /// returns the position that is matched or npos if there is no match. If |
| 84 | /// 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] | 85 | /// |
| 86 | /// The VariableTable StringMap provides the current values of filecheck |
| 87 | /// variables and is updated if this match defines new values. |
| 88 | size_t Match(StringRef Buffer, size_t &MatchLen, |
| 89 | StringMap<StringRef> &VariableTable) const; |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 90 | |
| 91 | /// PrintFailureInfo - Print additional information about a failure to match |
| 92 | /// involving this pattern. |
| 93 | void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer, |
| 94 | const StringMap<StringRef> &VariableTable) const; |
| 95 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 96 | private: |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 97 | static void AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr); |
| 98 | bool AddRegExToRegEx(StringRef RegExStr, unsigned &CurParen, SourceMgr &SM); |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 99 | |
| 100 | /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of |
| 101 | /// matching this pattern at the start of \arg Buffer; a distance of zero |
| 102 | /// should correspond to a perfect match. |
| 103 | unsigned ComputeMatchDistance(StringRef Buffer, |
| 104 | const StringMap<StringRef> &VariableTable) const; |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 107 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 108 | bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) { |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 109 | PatternLoc = SMLoc::getFromPointer(PatternStr.data()); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 110 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 111 | // Ignore trailing whitespace. |
| 112 | while (!PatternStr.empty() && |
| 113 | (PatternStr.back() == ' ' || PatternStr.back() == '\t')) |
| 114 | PatternStr = PatternStr.substr(0, PatternStr.size()-1); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 115 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 116 | // Check that there is something on the line. |
| 117 | if (PatternStr.empty()) { |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 118 | SM.PrintMessage(PatternLoc, "found empty check string with prefix '" + |
| 119 | CheckPrefix+":'", "error"); |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 123 | // Check to see if this is a fixed string, or if it has regex pieces. |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 124 | if (PatternStr.size() < 2 || |
| 125 | (PatternStr.find("{{") == StringRef::npos && |
| 126 | PatternStr.find("[[") == StringRef::npos)) { |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 127 | FixedStr = PatternStr; |
| 128 | return false; |
| 129 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 130 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 131 | // Paren value #0 is for the fully matched string. Any new parenthesized |
| 132 | // values add from their. |
| 133 | unsigned CurParen = 1; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 135 | // Otherwise, there is at least one regex piece. Build up the regex pattern |
| 136 | // by escaping scary characters in fixed strings, building up one big regex. |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 137 | while (!PatternStr.empty()) { |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 138 | // RegEx matches. |
| 139 | if (PatternStr.size() >= 2 && |
| 140 | PatternStr[0] == '{' && PatternStr[1] == '{') { |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 141 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 142 | // Otherwise, this is the start of a regex match. Scan for the }}. |
| 143 | size_t End = PatternStr.find("}}"); |
| 144 | if (End == StringRef::npos) { |
| 145 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
| 146 | "found start of regex string with no end '}}'", "error"); |
| 147 | return true; |
| 148 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 149 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 150 | if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM)) |
| 151 | return true; |
| 152 | PatternStr = PatternStr.substr(End+2); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 153 | continue; |
| 154 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 155 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 156 | // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .* |
| 157 | // (or some other regex) and assigns it to the FileCheck variable 'foo'. The |
| 158 | // 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] | 159 | // 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] | 160 | // it. This is to catch some common errors. |
| 161 | if (PatternStr.size() >= 2 && |
| 162 | PatternStr[0] == '[' && PatternStr[1] == '[') { |
| 163 | // Verify that it is terminated properly. |
| 164 | size_t End = PatternStr.find("]]"); |
| 165 | if (End == StringRef::npos) { |
| 166 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
| 167 | "invalid named regex reference, no ]] found", "error"); |
| 168 | return true; |
| 169 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 170 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 171 | StringRef MatchStr = PatternStr.substr(2, End-2); |
| 172 | PatternStr = PatternStr.substr(End+2); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 173 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 174 | // Get the regex name (e.g. "foo"). |
| 175 | size_t NameEnd = MatchStr.find(':'); |
| 176 | StringRef Name = MatchStr.substr(0, NameEnd); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 177 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 178 | if (Name.empty()) { |
| 179 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), |
| 180 | "invalid name in named regex: empty name", "error"); |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | // Verify that the name is well formed. |
| 185 | for (unsigned i = 0, e = Name.size(); i != e; ++i) |
Daniel Dunbar | 964ac01 | 2009-11-22 22:07:50 +0000 | [diff] [blame] | 186 | if (Name[i] != '_' && |
| 187 | (Name[i] < 'a' || Name[i] > 'z') && |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 188 | (Name[i] < 'A' || Name[i] > 'Z') && |
| 189 | (Name[i] < '0' || Name[i] > '9')) { |
| 190 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i), |
| 191 | "invalid name in named regex", "error"); |
| 192 | return true; |
| 193 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 194 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 195 | // Name can't start with a digit. |
| 196 | if (isdigit(Name[0])) { |
| 197 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), |
| 198 | "invalid name in named regex", "error"); |
| 199 | return true; |
| 200 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 201 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 202 | // Handle [[foo]]. |
| 203 | if (NameEnd == StringRef::npos) { |
| 204 | VariableUses.push_back(std::make_pair(Name, RegExStr.size())); |
| 205 | continue; |
| 206 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 207 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 208 | // Handle [[foo:.*]]. |
| 209 | VariableDefs.push_back(std::make_pair(Name, CurParen)); |
| 210 | RegExStr += '('; |
| 211 | ++CurParen; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 212 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 213 | if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM)) |
| 214 | return true; |
| 215 | |
| 216 | RegExStr += ')'; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 217 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 218 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 219 | // Handle fixed string matches. |
| 220 | // Find the end, which is the start of the next regex. |
| 221 | size_t FixedMatchEnd = PatternStr.find("{{"); |
| 222 | FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[[")); |
| 223 | AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr); |
| 224 | PatternStr = PatternStr.substr(FixedMatchEnd); |
| 225 | continue; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 226 | } |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 227 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 231 | void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) { |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 232 | // Add the characters from FixedStr to the regex, escaping as needed. This |
| 233 | // avoids "leaning toothpicks" in common patterns. |
| 234 | for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) { |
| 235 | switch (FixedStr[i]) { |
| 236 | // These are the special characters matched in "p_ere_exp". |
| 237 | case '(': |
| 238 | case ')': |
| 239 | case '^': |
| 240 | case '$': |
| 241 | case '|': |
| 242 | case '*': |
| 243 | case '+': |
| 244 | case '?': |
| 245 | case '.': |
| 246 | case '[': |
| 247 | case '\\': |
| 248 | case '{': |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 249 | TheStr += '\\'; |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 250 | // FALL THROUGH. |
| 251 | default: |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 252 | TheStr += FixedStr[i]; |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 253 | break; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 258 | bool Pattern::AddRegExToRegEx(StringRef RegexStr, unsigned &CurParen, |
| 259 | SourceMgr &SM) { |
| 260 | Regex R(RegexStr); |
| 261 | std::string Error; |
| 262 | if (!R.isValid(Error)) { |
| 263 | SM.PrintMessage(SMLoc::getFromPointer(RegexStr.data()), |
| 264 | "invalid regex: " + Error, "error"); |
| 265 | return true; |
| 266 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 267 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 268 | RegExStr += RegexStr.str(); |
| 269 | CurParen += R.getNumMatches(); |
| 270 | return false; |
| 271 | } |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 273 | /// Match - Match the pattern string against the input buffer Buffer. This |
| 274 | /// returns the position that is matched or npos if there is no match. If |
| 275 | /// 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] | 276 | size_t Pattern::Match(StringRef Buffer, size_t &MatchLen, |
| 277 | StringMap<StringRef> &VariableTable) const { |
Jakob Stoklund Olesen | 824c10e | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 278 | // If this is the EOF pattern, match it immediately. |
| 279 | if (MatchEOF) { |
| 280 | MatchLen = 0; |
| 281 | return Buffer.size(); |
| 282 | } |
| 283 | |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 284 | // If this is a fixed string pattern, just match it now. |
| 285 | if (!FixedStr.empty()) { |
| 286 | MatchLen = FixedStr.size(); |
| 287 | return Buffer.find(FixedStr); |
| 288 | } |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 290 | // Regex match. |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 291 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 292 | // If there are variable uses, we need to create a temporary string with the |
| 293 | // actual value. |
| 294 | StringRef RegExToMatch = RegExStr; |
| 295 | std::string TmpStr; |
| 296 | if (!VariableUses.empty()) { |
| 297 | TmpStr = RegExStr; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 298 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 299 | unsigned InsertOffset = 0; |
| 300 | for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) { |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 301 | StringMap<StringRef>::iterator it = |
| 302 | VariableTable.find(VariableUses[i].first); |
| 303 | // If the variable is undefined, return an error. |
| 304 | if (it == VariableTable.end()) |
| 305 | return StringRef::npos; |
| 306 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 307 | // Look up the value and escape it so that we can plop it into the regex. |
| 308 | std::string Value; |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 309 | AddFixedStringToRegEx(it->second, Value); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 310 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 311 | // Plop it into the regex at the adjusted offset. |
| 312 | TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset, |
| 313 | Value.begin(), Value.end()); |
| 314 | InsertOffset += Value.size(); |
| 315 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 316 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 317 | // Match the newly constructed regex. |
| 318 | RegExToMatch = TmpStr; |
| 319 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 320 | |
| 321 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 322 | SmallVector<StringRef, 4> MatchInfo; |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 323 | if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo)) |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 324 | return StringRef::npos; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 326 | // Successful regex match. |
| 327 | assert(!MatchInfo.empty() && "Didn't get any match"); |
| 328 | StringRef FullMatch = MatchInfo[0]; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 329 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 330 | // If this defines any variables, remember their values. |
| 331 | for (unsigned i = 0, e = VariableDefs.size(); i != e; ++i) { |
| 332 | assert(VariableDefs[i].second < MatchInfo.size() && |
| 333 | "Internal paren error"); |
| 334 | VariableTable[VariableDefs[i].first] = MatchInfo[VariableDefs[i].second]; |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 335 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 337 | MatchLen = FullMatch.size(); |
| 338 | return FullMatch.data()-Buffer.data(); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 341 | unsigned Pattern::ComputeMatchDistance(StringRef Buffer, |
| 342 | const StringMap<StringRef> &VariableTable) const { |
| 343 | // Just compute the number of matching characters. For regular expressions, we |
| 344 | // just compare against the regex itself and hope for the best. |
| 345 | // |
| 346 | // FIXME: One easy improvement here is have the regex lib generate a single |
| 347 | // example regular expression which matches, and use that as the example |
| 348 | // string. |
| 349 | StringRef ExampleString(FixedStr); |
| 350 | if (ExampleString.empty()) |
| 351 | ExampleString = RegExStr; |
| 352 | |
Daniel Dunbar | 0806f9f | 2010-01-30 00:24:06 +0000 | [diff] [blame] | 353 | // Only compare up to the first line in the buffer, or the string size. |
| 354 | StringRef BufferPrefix = Buffer.substr(0, ExampleString.size()); |
| 355 | BufferPrefix = BufferPrefix.split('\n').first; |
| 356 | return BufferPrefix.edit_distance(ExampleString); |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 359 | void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer, |
| 360 | const StringMap<StringRef> &VariableTable) const{ |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 361 | // If this was a regular expression using variables, print the current |
| 362 | // variable values. |
| 363 | if (!VariableUses.empty()) { |
| 364 | for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) { |
| 365 | StringRef Var = VariableUses[i].first; |
| 366 | StringMap<StringRef>::const_iterator it = VariableTable.find(Var); |
| 367 | SmallString<256> Msg; |
| 368 | raw_svector_ostream OS(Msg); |
| 369 | |
| 370 | // Check for undefined variable references. |
| 371 | if (it == VariableTable.end()) { |
| 372 | OS << "uses undefined variable \""; |
| 373 | OS.write_escaped(Var) << "\"";; |
| 374 | } else { |
| 375 | OS << "with variable \""; |
| 376 | OS.write_escaped(Var) << "\" equal to \""; |
| 377 | OS.write_escaped(it->second) << "\""; |
| 378 | } |
| 379 | |
| 380 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), OS.str(), "note", |
| 381 | /*ShowLine=*/false); |
| 382 | } |
| 383 | } |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 384 | |
| 385 | // Attempt to find the closest/best fuzzy match. Usually an error happens |
| 386 | // because some string in the output didn't exactly match. In these cases, we |
| 387 | // would like to show the user a best guess at what "should have" matched, to |
| 388 | // save them having to actually check the input manually. |
| 389 | size_t NumLinesForward = 0; |
| 390 | size_t Best = StringRef::npos; |
| 391 | double BestQuality = 0; |
| 392 | |
| 393 | // Use an arbitrary 4k limit on how far we will search. |
Dan Gohman | e3a1e50 | 2010-01-29 21:57:46 +0000 | [diff] [blame] | 394 | 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] | 395 | if (Buffer[i] == '\n') |
| 396 | ++NumLinesForward; |
| 397 | |
Dan Gohman | d8a5541 | 2010-01-29 21:55:16 +0000 | [diff] [blame] | 398 | // Patterns have leading whitespace stripped, so skip whitespace when |
| 399 | // looking for something which looks like a pattern. |
| 400 | if (Buffer[i] == ' ' || Buffer[i] == '\t') |
| 401 | continue; |
| 402 | |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 403 | // Compute the "quality" of this match as an arbitrary combination of the |
| 404 | // match distance and the number of lines skipped to get to this match. |
| 405 | unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable); |
| 406 | double Quality = Distance + (NumLinesForward / 100.); |
| 407 | |
| 408 | if (Quality < BestQuality || Best == StringRef::npos) { |
| 409 | Best = i; |
| 410 | BestQuality = Quality; |
| 411 | } |
| 412 | } |
| 413 | |
Daniel Dunbar | 7a68e0d | 2010-03-19 18:07:43 +0000 | [diff] [blame] | 414 | // Print the "possible intended match here" line if we found something |
| 415 | // reasonable and not equal to what we showed in the "scanning from here" |
| 416 | // line. |
| 417 | if (Best && Best != StringRef::npos && BestQuality < 50) { |
| 418 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best), |
| 419 | "possible intended match here", "note"); |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 420 | |
| 421 | // FIXME: If we wanted to be really friendly we would show why the match |
| 422 | // failed, as it can be hard to spot simple one character differences. |
| 423 | } |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 424 | } |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 425 | |
| 426 | //===----------------------------------------------------------------------===// |
| 427 | // Check Strings. |
| 428 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 429 | |
| 430 | /// CheckString - This is a check that we found in the input file. |
| 431 | struct CheckString { |
| 432 | /// Pat - The pattern to match. |
| 433 | Pattern Pat; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 435 | /// Loc - The location in the match file that the check string was specified. |
| 436 | SMLoc Loc; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 438 | /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed |
| 439 | /// to a CHECK: directive. |
| 440 | bool IsCheckNext; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 441 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 442 | /// NotStrings - These are all of the strings that are disallowed from |
| 443 | /// occurring between this match string and the previous one (or start of |
| 444 | /// file). |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 445 | std::vector<std::pair<SMLoc, Pattern> > NotStrings; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 447 | CheckString(const Pattern &P, SMLoc L, bool isCheckNext) |
| 448 | : Pat(P), Loc(L), IsCheckNext(isCheckNext) {} |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 449 | }; |
| 450 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 451 | /// CanonicalizeInputFile - Remove duplicate horizontal space from the specified |
| 452 | /// memory buffer, free it, and return a new one. |
| 453 | static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) { |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 454 | SmallString<128> NewFile; |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 455 | NewFile.reserve(MB->getBufferSize()); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 456 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 457 | for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd(); |
| 458 | Ptr != End; ++Ptr) { |
NAKAMURA Takumi | 9f6e03f | 2010-11-14 03:28:22 +0000 | [diff] [blame] | 459 | // Eliminate trailing dosish \r. |
| 460 | if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') { |
| 461 | continue; |
| 462 | } |
| 463 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 464 | // If C is not a horizontal whitespace, skip it. |
| 465 | if (*Ptr != ' ' && *Ptr != '\t') { |
| 466 | NewFile.push_back(*Ptr); |
| 467 | continue; |
| 468 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 469 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 470 | // Otherwise, add one space and advance over neighboring space. |
| 471 | NewFile.push_back(' '); |
| 472 | while (Ptr+1 != End && |
| 473 | (Ptr[1] == ' ' || Ptr[1] == '\t')) |
| 474 | ++Ptr; |
| 475 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 476 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 477 | // Free the old buffer and return a new one. |
| 478 | MemoryBuffer *MB2 = |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 479 | MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier()); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 480 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 481 | delete MB; |
| 482 | return MB2; |
| 483 | } |
| 484 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 486 | /// ReadCheckFile - Read the check file, which specifies the sequence of |
| 487 | /// expected strings. The strings are added to the CheckStrings vector. |
| 488 | static bool ReadCheckFile(SourceMgr &SM, |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 489 | std::vector<CheckString> &CheckStrings) { |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 490 | // Open the check file, and tell SourceMgr about it. |
| 491 | std::string ErrorStr; |
| 492 | MemoryBuffer *F = |
| 493 | MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr); |
| 494 | if (F == 0) { |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 495 | errs() << "Could not open check file '" << CheckFilename << "': " |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 496 | << ErrorStr << '\n'; |
| 497 | return true; |
| 498 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 499 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 500 | // If we want to canonicalize whitespace, strip excess whitespace from the |
| 501 | // buffer containing the CHECK lines. |
| 502 | if (!NoCanonicalizeWhiteSpace) |
| 503 | F = CanonicalizeInputFile(F); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 504 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 505 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 506 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 507 | // Find all instances of CheckPrefix followed by : in the file. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 508 | StringRef Buffer = F->getBuffer(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 509 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 510 | std::vector<std::pair<SMLoc, Pattern> > NotMatches; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 511 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 512 | while (1) { |
| 513 | // See if Prefix occurs in the memory buffer. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 514 | Buffer = Buffer.substr(Buffer.find(CheckPrefix)); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 516 | // If we didn't find a match, we're done. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 517 | if (Buffer.empty()) |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 518 | break; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 519 | |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 520 | const char *CheckPrefixStart = Buffer.data(); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 521 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 522 | // When we find a check prefix, keep track of whether we find CHECK: or |
| 523 | // CHECK-NEXT: |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 524 | bool IsCheckNext = false, IsCheckNot = false; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 525 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 526 | // Verify that the : is present after the prefix. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 527 | if (Buffer[CheckPrefix.size()] == ':') { |
| 528 | Buffer = Buffer.substr(CheckPrefix.size()+1); |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 529 | } else if (Buffer.size() > CheckPrefix.size()+6 && |
| 530 | memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) { |
| 531 | Buffer = Buffer.substr(CheckPrefix.size()+7); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 532 | IsCheckNext = true; |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 533 | } else if (Buffer.size() > CheckPrefix.size()+5 && |
| 534 | memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) { |
| 535 | Buffer = Buffer.substr(CheckPrefix.size()+6); |
| 536 | IsCheckNot = true; |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 537 | } else { |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 538 | Buffer = Buffer.substr(1); |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 539 | continue; |
| 540 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 541 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 542 | // Okay, we found the prefix, yay. Remember the rest of the line, but |
| 543 | // ignore leading and trailing whitespace. |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 544 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t")); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 546 | // Scan ahead to the end of line. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 547 | size_t EOL = Buffer.find_first_of("\n\r"); |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 548 | |
Dan Gohman | e546343 | 2010-01-29 21:53:18 +0000 | [diff] [blame] | 549 | // Remember the location of the start of the pattern, for diagnostics. |
| 550 | SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data()); |
| 551 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 552 | // Parse the pattern. |
| 553 | Pattern P; |
| 554 | if (P.ParsePattern(Buffer.substr(0, EOL), SM)) |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 555 | return true; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 556 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 557 | Buffer = Buffer.substr(EOL); |
| 558 | |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 560 | // Verify that CHECK-NEXT lines have at least one CHECK line before them. |
| 561 | if (IsCheckNext && CheckStrings.empty()) { |
| 562 | SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart), |
| 563 | "found '"+CheckPrefix+"-NEXT:' without previous '"+ |
| 564 | CheckPrefix+ ": line", "error"); |
| 565 | return true; |
| 566 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 567 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 568 | // Handle CHECK-NOT. |
| 569 | if (IsCheckNot) { |
| 570 | NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()), |
| 571 | P)); |
| 572 | continue; |
| 573 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 574 | |
| 575 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 576 | // 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] | 577 | CheckStrings.push_back(CheckString(P, |
Dan Gohman | e546343 | 2010-01-29 21:53:18 +0000 | [diff] [blame] | 578 | PatternLoc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 579 | IsCheckNext)); |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 580 | std::swap(NotMatches, CheckStrings.back().NotStrings); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 581 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 582 | |
Jakob Stoklund Olesen | 824c10e | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 583 | // Add an EOF pattern for any trailing CHECK-NOTs. |
| 584 | if (!NotMatches.empty()) { |
| 585 | CheckStrings.push_back(CheckString(Pattern(true), |
| 586 | SMLoc::getFromPointer(Buffer.data()), |
| 587 | false)); |
| 588 | std::swap(NotMatches, CheckStrings.back().NotStrings); |
| 589 | } |
| 590 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 591 | if (CheckStrings.empty()) { |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 592 | errs() << "error: no check strings found with prefix '" << CheckPrefix |
| 593 | << ":'\n"; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 594 | return true; |
| 595 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 597 | return false; |
| 598 | } |
| 599 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 600 | static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr, |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 601 | StringRef Buffer, |
| 602 | StringMap<StringRef> &VariableTable) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 603 | // Otherwise, we have an error, emit an error message. |
| 604 | SM.PrintMessage(CheckStr.Loc, "expected string not found in input", |
| 605 | "error"); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 607 | // Print the "scanning from here" line. If the current position is at the |
| 608 | // end of a line, advance to the start of the next line. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 609 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r")); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 611 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here", |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 612 | "note"); |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 613 | |
| 614 | // Allow the pattern to print additional information if desired. |
| 615 | CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 618 | /// CountNumNewlinesBetween - Count the number of newlines in the specified |
| 619 | /// range. |
| 620 | static unsigned CountNumNewlinesBetween(StringRef Range) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 621 | unsigned NumNewLines = 0; |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 622 | while (1) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 623 | // Scan for newline. |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 624 | Range = Range.substr(Range.find_first_of("\n\r")); |
| 625 | if (Range.empty()) return NumNewLines; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 627 | ++NumNewLines; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 629 | // Handle \n\r and \r\n as a single newline. |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 630 | if (Range.size() > 1 && |
| 631 | (Range[1] == '\n' || Range[1] == '\r') && |
| 632 | (Range[0] != Range[1])) |
| 633 | Range = Range.substr(1); |
| 634 | Range = Range.substr(1); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 635 | } |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 638 | int main(int argc, char **argv) { |
| 639 | sys::PrintStackTraceOnErrorSignal(); |
| 640 | PrettyStackTraceProgram X(argc, argv); |
| 641 | cl::ParseCommandLineOptions(argc, argv); |
| 642 | |
| 643 | SourceMgr SM; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 644 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 645 | // Read the expected strings from the check file. |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 646 | std::vector<CheckString> CheckStrings; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 647 | if (ReadCheckFile(SM, CheckStrings)) |
| 648 | return 2; |
| 649 | |
| 650 | // Open the file to check and add it to SourceMgr. |
| 651 | std::string ErrorStr; |
| 652 | MemoryBuffer *F = |
| 653 | MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr); |
| 654 | if (F == 0) { |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 655 | errs() << "Could not open input file '" << InputFilename << "': " |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 656 | << ErrorStr << '\n'; |
| 657 | return true; |
| 658 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 660 | // Remove duplicate spaces in the input file if requested. |
| 661 | if (!NoCanonicalizeWhiteSpace) |
| 662 | F = CanonicalizeInputFile(F); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 664 | SM.AddNewSourceBuffer(F, SMLoc()); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 665 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 666 | /// VariableTable - This holds all the current filecheck variables. |
| 667 | StringMap<StringRef> VariableTable; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 668 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 669 | // Check that we have all of the expected strings, in order, in the input |
| 670 | // file. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 671 | StringRef Buffer = F->getBuffer(); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 672 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 673 | const char *LastMatch = Buffer.data(); |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 675 | for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) { |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 676 | const CheckString &CheckStr = CheckStrings[StrNo]; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 677 | |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 678 | StringRef SearchFrom = Buffer; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 680 | // Find StrNo in the file. |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 681 | size_t MatchLen = 0; |
Jakob Stoklund Olesen | 824c10e | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 682 | size_t MatchPos = CheckStr.Pat.Match(Buffer, MatchLen, VariableTable); |
| 683 | Buffer = Buffer.substr(MatchPos); |
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 | // If we didn't find a match, reject the input. |
Jakob Stoklund Olesen | 824c10e | 2010-10-15 17:47:12 +0000 | [diff] [blame] | 686 | if (MatchPos == StringRef::npos) { |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 687 | PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 688 | return 1; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 689 | } |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 690 | |
| 691 | StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch); |
| 692 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 693 | // If this check is a "CHECK-NEXT", verify that the previous match was on |
| 694 | // the previous line (i.e. that there is one newline between them). |
| 695 | if (CheckStr.IsCheckNext) { |
| 696 | // Count the number of newlines between the previous match and this one. |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 697 | assert(LastMatch != F->getBufferStart() && |
| 698 | "CHECK-NEXT can't be the first check in a file"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 700 | unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 701 | if (NumNewLines == 0) { |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 702 | SM.PrintMessage(CheckStr.Loc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 703 | CheckPrefix+"-NEXT: is on the same line as previous match", |
| 704 | "error"); |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 705 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 706 | "'next' match was here", "note"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 707 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch), |
| 708 | "previous match was here", "note"); |
| 709 | return 1; |
| 710 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 712 | if (NumNewLines != 1) { |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 713 | SM.PrintMessage(CheckStr.Loc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 714 | CheckPrefix+ |
| 715 | "-NEXT: is not on the line after the previous match", |
| 716 | "error"); |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 717 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 718 | "'next' match was here", "note"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 719 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch), |
| 720 | "previous match was here", "note"); |
| 721 | return 1; |
| 722 | } |
| 723 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 724 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 725 | // If this match had "not strings", verify that they don't exist in the |
| 726 | // skipped region. |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 727 | for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size(); |
| 728 | ChunkNo != e; ++ChunkNo) { |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 729 | size_t MatchLen = 0; |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 730 | size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion, |
| 731 | MatchLen, |
| 732 | VariableTable); |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 733 | if (Pos == StringRef::npos) continue; |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 734 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 735 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos), |
| 736 | CheckPrefix+"-NOT: string occurred!", "error"); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 737 | SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first, |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 738 | CheckPrefix+"-NOT: pattern specified here", "note"); |
| 739 | return 1; |
| 740 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 741 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 8111576 | 2009-09-21 02:30:42 +0000 | [diff] [blame] | 743 | // Otherwise, everything is good. Step over the matched text and remember |
| 744 | // the position after the match as the end of the last match. |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 745 | Buffer = Buffer.substr(MatchLen); |
Chris Lattner | 8111576 | 2009-09-21 02:30:42 +0000 | [diff] [blame] | 746 | LastMatch = Buffer.data(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 747 | } |
Mikhail Glushenkov | 7112c86 | 2010-08-20 17:38:38 +0000 | [diff] [blame] | 748 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 749 | return 0; |
| 750 | } |