FileCheck.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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 file implements most of the API that will be used by the FileCheck utility
  14. // as well as various unittests.
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Support/FileCheck.h"
  17. #include "llvm/ADT/StringSet.h"
  18. #include <list>
  19. #include <map>
  20. using namespace llvm;
  21. /// Parses the given string into the Pattern.
  22. ///
  23. /// \p Prefix provides which prefix is being matched, \p SM provides the
  24. /// SourceMgr used for error reports, and \p LineNumber is the line number in
  25. /// the input file from which the pattern string was read. Returns true in
  26. /// case of an error, false otherwise.
  27. bool FileCheckPattern::ParsePattern(StringRef PatternStr, StringRef Prefix,
  28. SourceMgr &SM, unsigned LineNumber,
  29. const FileCheckRequest &Req) {
  30. bool MatchFullLinesHere = Req.MatchFullLines && CheckTy != Check::CheckNot;
  31. this->LineNumber = LineNumber;
  32. PatternLoc = SMLoc::getFromPointer(PatternStr.data());
  33. if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
  34. // Ignore trailing whitespace.
  35. while (!PatternStr.empty() &&
  36. (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
  37. PatternStr = PatternStr.substr(0, PatternStr.size() - 1);
  38. // Check that there is something on the line.
  39. if (PatternStr.empty() && CheckTy != Check::CheckEmpty) {
  40. SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
  41. "found empty check string with prefix '" + Prefix + ":'");
  42. return true;
  43. }
  44. if (!PatternStr.empty() && CheckTy == Check::CheckEmpty) {
  45. SM.PrintMessage(
  46. PatternLoc, SourceMgr::DK_Error,
  47. "found non-empty check string for empty check with prefix '" + Prefix +
  48. ":'");
  49. return true;
  50. }
  51. if (CheckTy == Check::CheckEmpty) {
  52. RegExStr = "(\n$)";
  53. return false;
  54. }
  55. // Check to see if this is a fixed string, or if it has regex pieces.
  56. if (!MatchFullLinesHere &&
  57. (PatternStr.size() < 2 || (PatternStr.find("{{") == StringRef::npos &&
  58. PatternStr.find("[[") == StringRef::npos))) {
  59. FixedStr = PatternStr;
  60. return false;
  61. }
  62. if (MatchFullLinesHere) {
  63. RegExStr += '^';
  64. if (!Req.NoCanonicalizeWhiteSpace)
  65. RegExStr += " *";
  66. }
  67. // Paren value #0 is for the fully matched string. Any new parenthesized
  68. // values add from there.
  69. unsigned CurParen = 1;
  70. // Otherwise, there is at least one regex piece. Build up the regex pattern
  71. // by escaping scary characters in fixed strings, building up one big regex.
  72. while (!PatternStr.empty()) {
  73. // RegEx matches.
  74. if (PatternStr.startswith("{{")) {
  75. // This is the start of a regex match. Scan for the }}.
  76. size_t End = PatternStr.find("}}");
  77. if (End == StringRef::npos) {
  78. SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
  79. SourceMgr::DK_Error,
  80. "found start of regex string with no end '}}'");
  81. return true;
  82. }
  83. // Enclose {{}} patterns in parens just like [[]] even though we're not
  84. // capturing the result for any purpose. This is required in case the
  85. // expression contains an alternation like: CHECK: abc{{x|z}}def. We
  86. // want this to turn into: "abc(x|z)def" not "abcx|zdef".
  87. RegExStr += '(';
  88. ++CurParen;
  89. if (AddRegExToRegEx(PatternStr.substr(2, End - 2), CurParen, SM))
  90. return true;
  91. RegExStr += ')';
  92. PatternStr = PatternStr.substr(End + 2);
  93. continue;
  94. }
  95. // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
  96. // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
  97. // second form is [[foo]] which is a reference to foo. The variable name
  98. // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
  99. // it. This is to catch some common errors.
  100. if (PatternStr.startswith("[[")) {
  101. // Find the closing bracket pair ending the match. End is going to be an
  102. // offset relative to the beginning of the match string.
  103. size_t End = FindRegexVarEnd(PatternStr.substr(2), SM);
  104. if (End == StringRef::npos) {
  105. SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
  106. SourceMgr::DK_Error,
  107. "invalid named regex reference, no ]] found");
  108. return true;
  109. }
  110. StringRef MatchStr = PatternStr.substr(2, End);
  111. PatternStr = PatternStr.substr(End + 4);
  112. // Get the regex name (e.g. "foo").
  113. size_t NameEnd = MatchStr.find(':');
  114. StringRef Name = MatchStr.substr(0, NameEnd);
  115. if (Name.empty()) {
  116. SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
  117. "invalid name in named regex: empty name");
  118. return true;
  119. }
  120. // Verify that the name/expression is well formed. FileCheck currently
  121. // supports @LINE, @LINE+number, @LINE-number expressions. The check here
  122. // is relaxed, more strict check is performed in \c EvaluateExpression.
  123. bool IsExpression = false;
  124. for (unsigned i = 0, e = Name.size(); i != e; ++i) {
  125. if (i == 0) {
  126. if (Name[i] == '$') // Global vars start with '$'
  127. continue;
  128. if (Name[i] == '@') {
  129. if (NameEnd != StringRef::npos) {
  130. SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
  131. SourceMgr::DK_Error,
  132. "invalid name in named regex definition");
  133. return true;
  134. }
  135. IsExpression = true;
  136. continue;
  137. }
  138. }
  139. if (Name[i] != '_' && !isalnum(Name[i]) &&
  140. (!IsExpression || (Name[i] != '+' && Name[i] != '-'))) {
  141. SM.PrintMessage(SMLoc::getFromPointer(Name.data() + i),
  142. SourceMgr::DK_Error, "invalid name in named regex");
  143. return true;
  144. }
  145. }
  146. // Name can't start with a digit.
  147. if (isdigit(static_cast<unsigned char>(Name[0]))) {
  148. SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
  149. "invalid name in named regex");
  150. return true;
  151. }
  152. // Handle [[foo]].
  153. if (NameEnd == StringRef::npos) {
  154. // Handle variables that were defined earlier on the same line by
  155. // emitting a backreference.
  156. if (VariableDefs.find(Name) != VariableDefs.end()) {
  157. unsigned VarParenNum = VariableDefs[Name];
  158. if (VarParenNum < 1 || VarParenNum > 9) {
  159. SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
  160. SourceMgr::DK_Error,
  161. "Can't back-reference more than 9 variables");
  162. return true;
  163. }
  164. AddBackrefToRegEx(VarParenNum);
  165. } else {
  166. VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
  167. }
  168. continue;
  169. }
  170. // Handle [[foo:.*]].
  171. VariableDefs[Name] = CurParen;
  172. RegExStr += '(';
  173. ++CurParen;
  174. if (AddRegExToRegEx(MatchStr.substr(NameEnd + 1), CurParen, SM))
  175. return true;
  176. RegExStr += ')';
  177. }
  178. // Handle fixed string matches.
  179. // Find the end, which is the start of the next regex.
  180. size_t FixedMatchEnd = PatternStr.find("{{");
  181. FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
  182. RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd));
  183. PatternStr = PatternStr.substr(FixedMatchEnd);
  184. }
  185. if (MatchFullLinesHere) {
  186. if (!Req.NoCanonicalizeWhiteSpace)
  187. RegExStr += " *";
  188. RegExStr += '$';
  189. }
  190. return false;
  191. }
  192. bool FileCheckPattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) {
  193. Regex R(RS);
  194. std::string Error;
  195. if (!R.isValid(Error)) {
  196. SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error,
  197. "invalid regex: " + Error);
  198. return true;
  199. }
  200. RegExStr += RS.str();
  201. CurParen += R.getNumMatches();
  202. return false;
  203. }
  204. void FileCheckPattern::AddBackrefToRegEx(unsigned BackrefNum) {
  205. assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
  206. std::string Backref = std::string("\\") + std::string(1, '0' + BackrefNum);
  207. RegExStr += Backref;
  208. }
  209. /// Evaluates expression and stores the result to \p Value.
  210. ///
  211. /// Returns true on success and false when the expression has invalid syntax.
  212. bool FileCheckPattern::EvaluateExpression(StringRef Expr, std::string &Value) const {
  213. // The only supported expression is @LINE([\+-]\d+)?
  214. if (!Expr.startswith("@LINE"))
  215. return false;
  216. Expr = Expr.substr(StringRef("@LINE").size());
  217. int Offset = 0;
  218. if (!Expr.empty()) {
  219. if (Expr[0] == '+')
  220. Expr = Expr.substr(1);
  221. else if (Expr[0] != '-')
  222. return false;
  223. if (Expr.getAsInteger(10, Offset))
  224. return false;
  225. }
  226. Value = llvm::itostr(LineNumber + Offset);
  227. return true;
  228. }
  229. /// Matches the pattern string against the input buffer \p Buffer
  230. ///
  231. /// This returns the position that is matched or npos if there is no match. If
  232. /// there is a match, the size of the matched string is returned in \p
  233. /// MatchLen.
  234. ///
  235. /// The \p VariableTable StringMap provides the current values of filecheck
  236. /// variables and is updated if this match defines new values.
  237. size_t FileCheckPattern::Match(StringRef Buffer, size_t &MatchLen,
  238. StringMap<StringRef> &VariableTable) const {
  239. // If this is the EOF pattern, match it immediately.
  240. if (CheckTy == Check::CheckEOF) {
  241. MatchLen = 0;
  242. return Buffer.size();
  243. }
  244. // If this is a fixed string pattern, just match it now.
  245. if (!FixedStr.empty()) {
  246. MatchLen = FixedStr.size();
  247. return Buffer.find(FixedStr);
  248. }
  249. // Regex match.
  250. // If there are variable uses, we need to create a temporary string with the
  251. // actual value.
  252. StringRef RegExToMatch = RegExStr;
  253. std::string TmpStr;
  254. if (!VariableUses.empty()) {
  255. TmpStr = RegExStr;
  256. unsigned InsertOffset = 0;
  257. for (const auto &VariableUse : VariableUses) {
  258. std::string Value;
  259. if (VariableUse.first[0] == '@') {
  260. if (!EvaluateExpression(VariableUse.first, Value))
  261. return StringRef::npos;
  262. } else {
  263. StringMap<StringRef>::iterator it =
  264. VariableTable.find(VariableUse.first);
  265. // If the variable is undefined, return an error.
  266. if (it == VariableTable.end())
  267. return StringRef::npos;
  268. // Look up the value and escape it so that we can put it into the regex.
  269. Value += Regex::escape(it->second);
  270. }
  271. // Plop it into the regex at the adjusted offset.
  272. TmpStr.insert(TmpStr.begin() + VariableUse.second + InsertOffset,
  273. Value.begin(), Value.end());
  274. InsertOffset += Value.size();
  275. }
  276. // Match the newly constructed regex.
  277. RegExToMatch = TmpStr;
  278. }
  279. SmallVector<StringRef, 4> MatchInfo;
  280. if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
  281. return StringRef::npos;
  282. // Successful regex match.
  283. assert(!MatchInfo.empty() && "Didn't get any match");
  284. StringRef FullMatch = MatchInfo[0];
  285. // If this defines any variables, remember their values.
  286. for (const auto &VariableDef : VariableDefs) {
  287. assert(VariableDef.second < MatchInfo.size() && "Internal paren error");
  288. VariableTable[VariableDef.first] = MatchInfo[VariableDef.second];
  289. }
  290. // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after
  291. // the required preceding newline, which is consumed by the pattern in the
  292. // case of CHECK-EMPTY but not CHECK-NEXT.
  293. size_t MatchStartSkip = CheckTy == Check::CheckEmpty;
  294. MatchLen = FullMatch.size() - MatchStartSkip;
  295. return FullMatch.data() - Buffer.data() + MatchStartSkip;
  296. }
  297. /// Computes an arbitrary estimate for the quality of matching this pattern at
  298. /// the start of \p Buffer; a distance of zero should correspond to a perfect
  299. /// match.
  300. unsigned
  301. FileCheckPattern::ComputeMatchDistance(StringRef Buffer,
  302. const StringMap<StringRef> &VariableTable) const {
  303. // Just compute the number of matching characters. For regular expressions, we
  304. // just compare against the regex itself and hope for the best.
  305. //
  306. // FIXME: One easy improvement here is have the regex lib generate a single
  307. // example regular expression which matches, and use that as the example
  308. // string.
  309. StringRef ExampleString(FixedStr);
  310. if (ExampleString.empty())
  311. ExampleString = RegExStr;
  312. // Only compare up to the first line in the buffer, or the string size.
  313. StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
  314. BufferPrefix = BufferPrefix.split('\n').first;
  315. return BufferPrefix.edit_distance(ExampleString);
  316. }
  317. void FileCheckPattern::PrintVariableUses(const SourceMgr &SM, StringRef Buffer,
  318. const StringMap<StringRef> &VariableTable,
  319. SMRange MatchRange) const {
  320. // If this was a regular expression using variables, print the current
  321. // variable values.
  322. if (!VariableUses.empty()) {
  323. for (const auto &VariableUse : VariableUses) {
  324. SmallString<256> Msg;
  325. raw_svector_ostream OS(Msg);
  326. StringRef Var = VariableUse.first;
  327. if (Var[0] == '@') {
  328. std::string Value;
  329. if (EvaluateExpression(Var, Value)) {
  330. OS << "with expression \"";
  331. OS.write_escaped(Var) << "\" equal to \"";
  332. OS.write_escaped(Value) << "\"";
  333. } else {
  334. OS << "uses incorrect expression \"";
  335. OS.write_escaped(Var) << "\"";
  336. }
  337. } else {
  338. StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
  339. // Check for undefined variable references.
  340. if (it == VariableTable.end()) {
  341. OS << "uses undefined variable \"";
  342. OS.write_escaped(Var) << "\"";
  343. } else {
  344. OS << "with variable \"";
  345. OS.write_escaped(Var) << "\" equal to \"";
  346. OS.write_escaped(it->second) << "\"";
  347. }
  348. }
  349. if (MatchRange.isValid())
  350. SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, OS.str(),
  351. {MatchRange});
  352. else
  353. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
  354. SourceMgr::DK_Note, OS.str());
  355. }
  356. }
  357. }
  358. void FileCheckPattern::PrintFuzzyMatch(
  359. const SourceMgr &SM, StringRef Buffer,
  360. const StringMap<StringRef> &VariableTable) const {
  361. // Attempt to find the closest/best fuzzy match. Usually an error happens
  362. // because some string in the output didn't exactly match. In these cases, we
  363. // would like to show the user a best guess at what "should have" matched, to
  364. // save them having to actually check the input manually.
  365. size_t NumLinesForward = 0;
  366. size_t Best = StringRef::npos;
  367. double BestQuality = 0;
  368. // Use an arbitrary 4k limit on how far we will search.
  369. for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
  370. if (Buffer[i] == '\n')
  371. ++NumLinesForward;
  372. // Patterns have leading whitespace stripped, so skip whitespace when
  373. // looking for something which looks like a pattern.
  374. if (Buffer[i] == ' ' || Buffer[i] == '\t')
  375. continue;
  376. // Compute the "quality" of this match as an arbitrary combination of the
  377. // match distance and the number of lines skipped to get to this match.
  378. unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
  379. double Quality = Distance + (NumLinesForward / 100.);
  380. if (Quality < BestQuality || Best == StringRef::npos) {
  381. Best = i;
  382. BestQuality = Quality;
  383. }
  384. }
  385. // Print the "possible intended match here" line if we found something
  386. // reasonable and not equal to what we showed in the "scanning from here"
  387. // line.
  388. if (Best && Best != StringRef::npos && BestQuality < 50) {
  389. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
  390. SourceMgr::DK_Note, "possible intended match here");
  391. // FIXME: If we wanted to be really friendly we would show why the match
  392. // failed, as it can be hard to spot simple one character differences.
  393. }
  394. }
  395. /// Finds the closing sequence of a regex variable usage or definition.
  396. ///
  397. /// \p Str has to point in the beginning of the definition (right after the
  398. /// opening sequence). Returns the offset of the closing sequence within Str,
  399. /// or npos if it was not found.
  400. size_t FileCheckPattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) {
  401. // Offset keeps track of the current offset within the input Str
  402. size_t Offset = 0;
  403. // [...] Nesting depth
  404. size_t BracketDepth = 0;
  405. while (!Str.empty()) {
  406. if (Str.startswith("]]") && BracketDepth == 0)
  407. return Offset;
  408. if (Str[0] == '\\') {
  409. // Backslash escapes the next char within regexes, so skip them both.
  410. Str = Str.substr(2);
  411. Offset += 2;
  412. } else {
  413. switch (Str[0]) {
  414. default:
  415. break;
  416. case '[':
  417. BracketDepth++;
  418. break;
  419. case ']':
  420. if (BracketDepth == 0) {
  421. SM.PrintMessage(SMLoc::getFromPointer(Str.data()),
  422. SourceMgr::DK_Error,
  423. "missing closing \"]\" for regex variable");
  424. exit(1);
  425. }
  426. BracketDepth--;
  427. break;
  428. }
  429. Str = Str.substr(1);
  430. Offset++;
  431. }
  432. }
  433. return StringRef::npos;
  434. }
  435. /// Canonicalize whitespaces in the file. Line endings are replaced with
  436. /// UNIX-style '\n'.
  437. StringRef
  438. llvm::FileCheck::CanonicalizeFile(MemoryBuffer &MB,
  439. SmallVectorImpl<char> &OutputBuffer) {
  440. OutputBuffer.reserve(MB.getBufferSize());
  441. for (const char *Ptr = MB.getBufferStart(), *End = MB.getBufferEnd();
  442. Ptr != End; ++Ptr) {
  443. // Eliminate trailing dosish \r.
  444. if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
  445. continue;
  446. }
  447. // If current char is not a horizontal whitespace or if horizontal
  448. // whitespace canonicalization is disabled, dump it to output as is.
  449. if (Req.NoCanonicalizeWhiteSpace || (*Ptr != ' ' && *Ptr != '\t')) {
  450. OutputBuffer.push_back(*Ptr);
  451. continue;
  452. }
  453. // Otherwise, add one space and advance over neighboring space.
  454. OutputBuffer.push_back(' ');
  455. while (Ptr + 1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t'))
  456. ++Ptr;
  457. }
  458. // Add a null byte and then return all but that byte.
  459. OutputBuffer.push_back('\0');
  460. return StringRef(OutputBuffer.data(), OutputBuffer.size() - 1);
  461. }
  462. static bool IsPartOfWord(char c) {
  463. return (isalnum(c) || c == '-' || c == '_');
  464. }
  465. // Get the size of the prefix extension.
  466. static size_t CheckTypeSize(Check::FileCheckType Ty) {
  467. switch (Ty) {
  468. case Check::CheckNone:
  469. case Check::CheckBadNot:
  470. return 0;
  471. case Check::CheckPlain:
  472. return sizeof(":") - 1;
  473. case Check::CheckNext:
  474. return sizeof("-NEXT:") - 1;
  475. case Check::CheckSame:
  476. return sizeof("-SAME:") - 1;
  477. case Check::CheckNot:
  478. return sizeof("-NOT:") - 1;
  479. case Check::CheckDAG:
  480. return sizeof("-DAG:") - 1;
  481. case Check::CheckLabel:
  482. return sizeof("-LABEL:") - 1;
  483. case Check::CheckEmpty:
  484. return sizeof("-EMPTY:") - 1;
  485. case Check::CheckEOF:
  486. llvm_unreachable("Should not be using EOF size");
  487. }
  488. llvm_unreachable("Bad check type");
  489. }
  490. // Get a description of the type.
  491. static std::string CheckTypeName(StringRef Prefix, Check::FileCheckType Ty) {
  492. switch (Ty) {
  493. case Check::CheckNone:
  494. return "invalid";
  495. case Check::CheckPlain:
  496. return Prefix;
  497. case Check::CheckNext:
  498. return Prefix.str() + "-NEXT";
  499. case Check::CheckSame:
  500. return Prefix.str() + "-SAME";
  501. case Check::CheckNot:
  502. return Prefix.str() + "-NOT";
  503. case Check::CheckDAG:
  504. return Prefix.str() + "-DAG";
  505. case Check::CheckLabel:
  506. return Prefix.str() + "-LABEL";
  507. case Check::CheckEmpty:
  508. return Prefix.str() + "-EMPTY";
  509. case Check::CheckEOF:
  510. return "implicit EOF";
  511. case Check::CheckBadNot:
  512. return "bad NOT";
  513. }
  514. llvm_unreachable("unknown FileCheckType");
  515. }
  516. static Check::FileCheckType FindCheckType(StringRef Buffer, StringRef Prefix) {
  517. if (Buffer.size() <= Prefix.size())
  518. return Check::CheckNone;
  519. char NextChar = Buffer[Prefix.size()];
  520. // Verify that the : is present after the prefix.
  521. if (NextChar == ':')
  522. return Check::CheckPlain;
  523. if (NextChar != '-')
  524. return Check::CheckNone;
  525. StringRef Rest = Buffer.drop_front(Prefix.size() + 1);
  526. if (Rest.startswith("NEXT:"))
  527. return Check::CheckNext;
  528. if (Rest.startswith("SAME:"))
  529. return Check::CheckSame;
  530. if (Rest.startswith("NOT:"))
  531. return Check::CheckNot;
  532. if (Rest.startswith("DAG:"))
  533. return Check::CheckDAG;
  534. if (Rest.startswith("LABEL:"))
  535. return Check::CheckLabel;
  536. if (Rest.startswith("EMPTY:"))
  537. return Check::CheckEmpty;
  538. // You can't combine -NOT with another suffix.
  539. if (Rest.startswith("DAG-NOT:") || Rest.startswith("NOT-DAG:") ||
  540. Rest.startswith("NEXT-NOT:") || Rest.startswith("NOT-NEXT:") ||
  541. Rest.startswith("SAME-NOT:") || Rest.startswith("NOT-SAME:") ||
  542. Rest.startswith("EMPTY-NOT:") || Rest.startswith("NOT-EMPTY:"))
  543. return Check::CheckBadNot;
  544. return Check::CheckNone;
  545. }
  546. // From the given position, find the next character after the word.
  547. static size_t SkipWord(StringRef Str, size_t Loc) {
  548. while (Loc < Str.size() && IsPartOfWord(Str[Loc]))
  549. ++Loc;
  550. return Loc;
  551. }
  552. /// Search the buffer for the first prefix in the prefix regular expression.
  553. ///
  554. /// This searches the buffer using the provided regular expression, however it
  555. /// enforces constraints beyond that:
  556. /// 1) The found prefix must not be a suffix of something that looks like
  557. /// a valid prefix.
  558. /// 2) The found prefix must be followed by a valid check type suffix using \c
  559. /// FindCheckType above.
  560. ///
  561. /// The first match of the regular expression to satisfy these two is returned,
  562. /// otherwise an empty StringRef is returned to indicate failure.
  563. ///
  564. /// If this routine returns a valid prefix, it will also shrink \p Buffer to
  565. /// start at the beginning of the returned prefix, increment \p LineNumber for
  566. /// each new line consumed from \p Buffer, and set \p CheckTy to the type of
  567. /// check found by examining the suffix.
  568. ///
  569. /// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy
  570. /// is unspecified.
  571. static StringRef FindFirstMatchingPrefix(Regex &PrefixRE, StringRef &Buffer,
  572. unsigned &LineNumber,
  573. Check::FileCheckType &CheckTy) {
  574. SmallVector<StringRef, 2> Matches;
  575. while (!Buffer.empty()) {
  576. // Find the first (longest) match using the RE.
  577. if (!PrefixRE.match(Buffer, &Matches))
  578. // No match at all, bail.
  579. return StringRef();
  580. StringRef Prefix = Matches[0];
  581. Matches.clear();
  582. assert(Prefix.data() >= Buffer.data() &&
  583. Prefix.data() < Buffer.data() + Buffer.size() &&
  584. "Prefix doesn't start inside of buffer!");
  585. size_t Loc = Prefix.data() - Buffer.data();
  586. StringRef Skipped = Buffer.substr(0, Loc);
  587. Buffer = Buffer.drop_front(Loc);
  588. LineNumber += Skipped.count('\n');
  589. // Check that the matched prefix isn't a suffix of some other check-like
  590. // word.
  591. // FIXME: This is a very ad-hoc check. it would be better handled in some
  592. // other way. Among other things it seems hard to distinguish between
  593. // intentional and unintentional uses of this feature.
  594. if (Skipped.empty() || !IsPartOfWord(Skipped.back())) {
  595. // Now extract the type.
  596. CheckTy = FindCheckType(Buffer, Prefix);
  597. // If we've found a valid check type for this prefix, we're done.
  598. if (CheckTy != Check::CheckNone)
  599. return Prefix;
  600. }
  601. // If we didn't successfully find a prefix, we need to skip this invalid
  602. // prefix and continue scanning. We directly skip the prefix that was
  603. // matched and any additional parts of that check-like word.
  604. Buffer = Buffer.drop_front(SkipWord(Buffer, Prefix.size()));
  605. }
  606. // We ran out of buffer while skipping partial matches so give up.
  607. return StringRef();
  608. }
  609. /// Read the check file, which specifies the sequence of expected strings.
  610. ///
  611. /// The strings are added to the CheckStrings vector. Returns true in case of
  612. /// an error, false otherwise.
  613. bool llvm::FileCheck::ReadCheckFile(SourceMgr &SM, StringRef Buffer,
  614. Regex &PrefixRE,
  615. std::vector<FileCheckString> &CheckStrings) {
  616. std::vector<FileCheckPattern> ImplicitNegativeChecks;
  617. for (const auto &PatternString : Req.ImplicitCheckNot) {
  618. // Create a buffer with fake command line content in order to display the
  619. // command line option responsible for the specific implicit CHECK-NOT.
  620. std::string Prefix = "-implicit-check-not='";
  621. std::string Suffix = "'";
  622. std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
  623. Prefix + PatternString + Suffix, "command line");
  624. StringRef PatternInBuffer =
  625. CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
  626. SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
  627. ImplicitNegativeChecks.push_back(FileCheckPattern(Check::CheckNot));
  628. ImplicitNegativeChecks.back().ParsePattern(PatternInBuffer,
  629. "IMPLICIT-CHECK", SM, 0, Req);
  630. }
  631. std::vector<FileCheckPattern> DagNotMatches = ImplicitNegativeChecks;
  632. // LineNumber keeps track of the line on which CheckPrefix instances are
  633. // found.
  634. unsigned LineNumber = 1;
  635. while (1) {
  636. Check::FileCheckType CheckTy;
  637. // See if a prefix occurs in the memory buffer.
  638. StringRef UsedPrefix = FindFirstMatchingPrefix(PrefixRE, Buffer, LineNumber,
  639. CheckTy);
  640. if (UsedPrefix.empty())
  641. break;
  642. assert(UsedPrefix.data() == Buffer.data() &&
  643. "Failed to move Buffer's start forward, or pointed prefix outside "
  644. "of the buffer!");
  645. // Location to use for error messages.
  646. const char *UsedPrefixStart = UsedPrefix.data();
  647. // Skip the buffer to the end.
  648. Buffer = Buffer.drop_front(UsedPrefix.size() + CheckTypeSize(CheckTy));
  649. // Complain about useful-looking but unsupported suffixes.
  650. if (CheckTy == Check::CheckBadNot) {
  651. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
  652. "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
  653. return true;
  654. }
  655. // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
  656. // leading whitespace.
  657. if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
  658. Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
  659. // Scan ahead to the end of line.
  660. size_t EOL = Buffer.find_first_of("\n\r");
  661. // Remember the location of the start of the pattern, for diagnostics.
  662. SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
  663. // Parse the pattern.
  664. FileCheckPattern P(CheckTy);
  665. if (P.ParsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, LineNumber, Req))
  666. return true;
  667. // Verify that CHECK-LABEL lines do not define or use variables
  668. if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
  669. SM.PrintMessage(
  670. SMLoc::getFromPointer(UsedPrefixStart), SourceMgr::DK_Error,
  671. "found '" + UsedPrefix + "-LABEL:'"
  672. " with variable definition or use");
  673. return true;
  674. }
  675. Buffer = Buffer.substr(EOL);
  676. // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
  677. if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame ||
  678. CheckTy == Check::CheckEmpty) &&
  679. CheckStrings.empty()) {
  680. StringRef Type = CheckTy == Check::CheckNext
  681. ? "NEXT"
  682. : CheckTy == Check::CheckEmpty ? "EMPTY" : "SAME";
  683. SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
  684. SourceMgr::DK_Error,
  685. "found '" + UsedPrefix + "-" + Type +
  686. "' without previous '" + UsedPrefix + ": line");
  687. return true;
  688. }
  689. // Handle CHECK-DAG/-NOT.
  690. if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
  691. DagNotMatches.push_back(P);
  692. continue;
  693. }
  694. // Okay, add the string we captured to the output vector and move on.
  695. CheckStrings.emplace_back(P, UsedPrefix, PatternLoc);
  696. std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
  697. DagNotMatches = ImplicitNegativeChecks;
  698. }
  699. // Add an EOF pattern for any trailing CHECK-DAG/-NOTs, and use the first
  700. // prefix as a filler for the error message.
  701. if (!DagNotMatches.empty()) {
  702. CheckStrings.emplace_back(FileCheckPattern(Check::CheckEOF), *Req.CheckPrefixes.begin(),
  703. SMLoc::getFromPointer(Buffer.data()));
  704. std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
  705. }
  706. if (CheckStrings.empty()) {
  707. errs() << "error: no check strings found with prefix"
  708. << (Req.CheckPrefixes.size() > 1 ? "es " : " ");
  709. auto I = Req.CheckPrefixes.begin();
  710. auto E = Req.CheckPrefixes.end();
  711. if (I != E) {
  712. errs() << "\'" << *I << ":'";
  713. ++I;
  714. }
  715. for (; I != E; ++I)
  716. errs() << ", \'" << *I << ":'";
  717. errs() << '\n';
  718. return true;
  719. }
  720. return false;
  721. }
  722. static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM,
  723. StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat,
  724. StringRef Buffer, StringMap<StringRef> &VariableTable,
  725. size_t MatchPos, size_t MatchLen,
  726. const FileCheckRequest &Req) {
  727. if (ExpectedMatch) {
  728. if (!Req.Verbose)
  729. return;
  730. if (!Req.VerboseVerbose && Pat.getCheckTy() == Check::CheckEOF)
  731. return;
  732. }
  733. SMLoc MatchStart = SMLoc::getFromPointer(Buffer.data() + MatchPos);
  734. SMLoc MatchEnd = SMLoc::getFromPointer(Buffer.data() + MatchPos + MatchLen);
  735. SMRange MatchRange(MatchStart, MatchEnd);
  736. SM.PrintMessage(
  737. Loc, ExpectedMatch ? SourceMgr::DK_Remark : SourceMgr::DK_Error,
  738. CheckTypeName(Prefix, Pat.getCheckTy()) + ": " +
  739. (ExpectedMatch ? "expected" : "excluded") +
  740. " string found in input");
  741. SM.PrintMessage(MatchStart, SourceMgr::DK_Note, "found here", {MatchRange});
  742. Pat.PrintVariableUses(SM, Buffer, VariableTable, MatchRange);
  743. }
  744. static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM,
  745. const FileCheckString &CheckStr, StringRef Buffer,
  746. StringMap<StringRef> &VariableTable, size_t MatchPos,
  747. size_t MatchLen, FileCheckRequest &Req) {
  748. PrintMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat,
  749. Buffer, VariableTable, MatchPos, MatchLen, Req);
  750. }
  751. static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM,
  752. StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat,
  753. StringRef Buffer,
  754. StringMap<StringRef> &VariableTable,
  755. bool VerboseVerbose) {
  756. if (!ExpectedMatch && !VerboseVerbose)
  757. return;
  758. // Otherwise, we have an error, emit an error message.
  759. SM.PrintMessage(Loc,
  760. ExpectedMatch ? SourceMgr::DK_Error : SourceMgr::DK_Remark,
  761. CheckTypeName(Prefix, Pat.getCheckTy()) + ": " +
  762. (ExpectedMatch ? "expected" : "excluded") +
  763. " string not found in input");
  764. // Print the "scanning from here" line. If the current position is at the
  765. // end of a line, advance to the start of the next line.
  766. Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
  767. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
  768. "scanning from here");
  769. // Allow the pattern to print additional information if desired.
  770. Pat.PrintVariableUses(SM, Buffer, VariableTable);
  771. if (ExpectedMatch)
  772. Pat.PrintFuzzyMatch(SM, Buffer, VariableTable);
  773. }
  774. static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM,
  775. const FileCheckString &CheckStr, StringRef Buffer,
  776. StringMap<StringRef> &VariableTable,
  777. bool VerboseVerbose) {
  778. PrintNoMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat,
  779. Buffer, VariableTable, VerboseVerbose);
  780. }
  781. /// Count the number of newlines in the specified range.
  782. static unsigned CountNumNewlinesBetween(StringRef Range,
  783. const char *&FirstNewLine) {
  784. unsigned NumNewLines = 0;
  785. while (1) {
  786. // Scan for newline.
  787. Range = Range.substr(Range.find_first_of("\n\r"));
  788. if (Range.empty())
  789. return NumNewLines;
  790. ++NumNewLines;
  791. // Handle \n\r and \r\n as a single newline.
  792. if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') &&
  793. (Range[0] != Range[1]))
  794. Range = Range.substr(1);
  795. Range = Range.substr(1);
  796. if (NumNewLines == 1)
  797. FirstNewLine = Range.begin();
  798. }
  799. }
  800. /// Match check string and its "not strings" and/or "dag strings".
  801. size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
  802. bool IsLabelScanMode, size_t &MatchLen,
  803. StringMap<StringRef> &VariableTable,
  804. FileCheckRequest &Req) const {
  805. size_t LastPos = 0;
  806. std::vector<const FileCheckPattern *> NotStrings;
  807. // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
  808. // bounds; we have not processed variable definitions within the bounded block
  809. // yet so cannot handle any final CHECK-DAG yet; this is handled when going
  810. // over the block again (including the last CHECK-LABEL) in normal mode.
  811. if (!IsLabelScanMode) {
  812. // Match "dag strings" (with mixed "not strings" if any).
  813. LastPos = CheckDag(SM, Buffer, NotStrings, VariableTable, Req);
  814. if (LastPos == StringRef::npos)
  815. return StringRef::npos;
  816. }
  817. // Match itself from the last position after matching CHECK-DAG.
  818. StringRef MatchBuffer = Buffer.substr(LastPos);
  819. size_t MatchPos = Pat.Match(MatchBuffer, MatchLen, VariableTable);
  820. if (MatchPos == StringRef::npos) {
  821. PrintNoMatch(true, SM, *this, MatchBuffer, VariableTable, Req.VerboseVerbose);
  822. return StringRef::npos;
  823. }
  824. PrintMatch(true, SM, *this, MatchBuffer, VariableTable, MatchPos, MatchLen, Req);
  825. // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
  826. // or CHECK-NOT
  827. if (!IsLabelScanMode) {
  828. StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
  829. // If this check is a "CHECK-NEXT", verify that the previous match was on
  830. // the previous line (i.e. that there is one newline between them).
  831. if (CheckNext(SM, SkippedRegion))
  832. return StringRef::npos;
  833. // If this check is a "CHECK-SAME", verify that the previous match was on
  834. // the same line (i.e. that there is no newline between them).
  835. if (CheckSame(SM, SkippedRegion))
  836. return StringRef::npos;
  837. // If this match had "not strings", verify that they don't exist in the
  838. // skipped region.
  839. if (CheckNot(SM, SkippedRegion, NotStrings, VariableTable, Req))
  840. return StringRef::npos;
  841. }
  842. return LastPos + MatchPos;
  843. }
  844. /// Verify there is a single line in the given buffer.
  845. bool FileCheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
  846. if (Pat.getCheckTy() != Check::CheckNext &&
  847. Pat.getCheckTy() != Check::CheckEmpty)
  848. return false;
  849. Twine CheckName =
  850. Prefix +
  851. Twine(Pat.getCheckTy() == Check::CheckEmpty ? "-EMPTY" : "-NEXT");
  852. // Count the number of newlines between the previous match and this one.
  853. assert(Buffer.data() !=
  854. SM.getMemoryBuffer(SM.FindBufferContainingLoc(
  855. SMLoc::getFromPointer(Buffer.data())))
  856. ->getBufferStart() &&
  857. "CHECK-NEXT and CHECK-EMPTY can't be the first check in a file");
  858. const char *FirstNewLine = nullptr;
  859. unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
  860. if (NumNewLines == 0) {
  861. SM.PrintMessage(Loc, SourceMgr::DK_Error,
  862. CheckName + ": is on the same line as previous match");
  863. SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
  864. "'next' match was here");
  865. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
  866. "previous match ended here");
  867. return true;
  868. }
  869. if (NumNewLines != 1) {
  870. SM.PrintMessage(Loc, SourceMgr::DK_Error,
  871. CheckName +
  872. ": is not on the line after the previous match");
  873. SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
  874. "'next' match was here");
  875. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
  876. "previous match ended here");
  877. SM.PrintMessage(SMLoc::getFromPointer(FirstNewLine), SourceMgr::DK_Note,
  878. "non-matching line after previous match is here");
  879. return true;
  880. }
  881. return false;
  882. }
  883. /// Verify there is no newline in the given buffer.
  884. bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
  885. if (Pat.getCheckTy() != Check::CheckSame)
  886. return false;
  887. // Count the number of newlines between the previous match and this one.
  888. assert(Buffer.data() !=
  889. SM.getMemoryBuffer(SM.FindBufferContainingLoc(
  890. SMLoc::getFromPointer(Buffer.data())))
  891. ->getBufferStart() &&
  892. "CHECK-SAME can't be the first check in a file");
  893. const char *FirstNewLine = nullptr;
  894. unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
  895. if (NumNewLines != 0) {
  896. SM.PrintMessage(Loc, SourceMgr::DK_Error,
  897. Prefix +
  898. "-SAME: is not on the same line as the previous match");
  899. SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
  900. "'next' match was here");
  901. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
  902. "previous match ended here");
  903. return true;
  904. }
  905. return false;
  906. }
  907. /// Verify there's no "not strings" in the given buffer.
  908. bool FileCheckString::CheckNot(const SourceMgr &SM, StringRef Buffer,
  909. const std::vector<const FileCheckPattern *> &NotStrings,
  910. StringMap<StringRef> &VariableTable,
  911. const FileCheckRequest &Req) const {
  912. for (const FileCheckPattern *Pat : NotStrings) {
  913. assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!");
  914. size_t MatchLen = 0;
  915. size_t Pos = Pat->Match(Buffer, MatchLen, VariableTable);
  916. if (Pos == StringRef::npos) {
  917. PrintNoMatch(false, SM, Prefix, Pat->getLoc(), *Pat, Buffer,
  918. VariableTable, Req.VerboseVerbose);
  919. continue;
  920. }
  921. PrintMatch(false, SM, Prefix, Pat->getLoc(), *Pat, Buffer, VariableTable,
  922. Pos, MatchLen, Req);
  923. return true;
  924. }
  925. return false;
  926. }
  927. /// Match "dag strings" and their mixed "not strings".
  928. size_t FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
  929. std::vector<const FileCheckPattern *> &NotStrings,
  930. StringMap<StringRef> &VariableTable,
  931. const FileCheckRequest &Req) const {
  932. if (DagNotStrings.empty())
  933. return 0;
  934. // The start of the search range.
  935. size_t StartPos = 0;
  936. struct MatchRange {
  937. size_t Pos;
  938. size_t End;
  939. };
  940. // A sorted list of ranges for non-overlapping CHECK-DAG matches. Match
  941. // ranges are erased from this list once they are no longer in the search
  942. // range.
  943. std::list<MatchRange> MatchRanges;
  944. // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG
  945. // group, so we don't use a range-based for loop here.
  946. for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end();
  947. PatItr != PatEnd; ++PatItr) {
  948. const FileCheckPattern &Pat = *PatItr;
  949. assert((Pat.getCheckTy() == Check::CheckDAG ||
  950. Pat.getCheckTy() == Check::CheckNot) &&
  951. "Invalid CHECK-DAG or CHECK-NOT!");
  952. if (Pat.getCheckTy() == Check::CheckNot) {
  953. NotStrings.push_back(&Pat);
  954. continue;
  955. }
  956. assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
  957. // CHECK-DAG always matches from the start.
  958. size_t MatchLen = 0, MatchPos = StartPos;
  959. // Search for a match that doesn't overlap a previous match in this
  960. // CHECK-DAG group.
  961. for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) {
  962. StringRef MatchBuffer = Buffer.substr(MatchPos);
  963. size_t MatchPosBuf = Pat.Match(MatchBuffer, MatchLen, VariableTable);
  964. // With a group of CHECK-DAGs, a single mismatching means the match on
  965. // that group of CHECK-DAGs fails immediately.
  966. if (MatchPosBuf == StringRef::npos) {
  967. PrintNoMatch(true, SM, Prefix, Pat.getLoc(), Pat, MatchBuffer,
  968. VariableTable, Req.VerboseVerbose);
  969. return StringRef::npos;
  970. }
  971. // Re-calc it as the offset relative to the start of the original string.
  972. MatchPos += MatchPosBuf;
  973. if (Req.VerboseVerbose)
  974. PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, Buffer, VariableTable,
  975. MatchPos, MatchLen, Req);
  976. MatchRange M{MatchPos, MatchPos + MatchLen};
  977. if (Req.AllowDeprecatedDagOverlap) {
  978. // We don't need to track all matches in this mode, so we just maintain
  979. // one match range that encompasses the current CHECK-DAG group's
  980. // matches.
  981. if (MatchRanges.empty())
  982. MatchRanges.insert(MatchRanges.end(), M);
  983. else {
  984. auto Block = MatchRanges.begin();
  985. Block->Pos = std::min(Block->Pos, M.Pos);
  986. Block->End = std::max(Block->End, M.End);
  987. }
  988. break;
  989. }
  990. // Iterate previous matches until overlapping match or insertion point.
  991. bool Overlap = false;
  992. for (; MI != ME; ++MI) {
  993. if (M.Pos < MI->End) {
  994. // !Overlap => New match has no overlap and is before this old match.
  995. // Overlap => New match overlaps this old match.
  996. Overlap = MI->Pos < M.End;
  997. break;
  998. }
  999. }
  1000. if (!Overlap) {
  1001. // Insert non-overlapping match into list.
  1002. MatchRanges.insert(MI, M);
  1003. break;
  1004. }
  1005. if (Req.VerboseVerbose) {
  1006. SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos);
  1007. SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End);
  1008. SMRange OldRange(OldStart, OldEnd);
  1009. SM.PrintMessage(OldStart, SourceMgr::DK_Note,
  1010. "match discarded, overlaps earlier DAG match here",
  1011. {OldRange});
  1012. }
  1013. MatchPos = MI->End;
  1014. }
  1015. if (!Req.VerboseVerbose)
  1016. PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, Buffer, VariableTable,
  1017. MatchPos, MatchLen, Req);
  1018. // Handle the end of a CHECK-DAG group.
  1019. if (std::next(PatItr) == PatEnd ||
  1020. std::next(PatItr)->getCheckTy() == Check::CheckNot) {
  1021. if (!NotStrings.empty()) {
  1022. // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to
  1023. // CHECK-DAG, verify that there are no 'not' strings occurred in that
  1024. // region.
  1025. StringRef SkippedRegion =
  1026. Buffer.slice(StartPos, MatchRanges.begin()->Pos);
  1027. if (CheckNot(SM, SkippedRegion, NotStrings, VariableTable, Req))
  1028. return StringRef::npos;
  1029. // Clear "not strings".
  1030. NotStrings.clear();
  1031. }
  1032. // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the
  1033. // end of this CHECK-DAG group's match range.
  1034. StartPos = MatchRanges.rbegin()->End;
  1035. // Don't waste time checking for (impossible) overlaps before that.
  1036. MatchRanges.clear();
  1037. }
  1038. }
  1039. return StartPos;
  1040. }
  1041. // A check prefix must contain only alphanumeric, hyphens and underscores.
  1042. static bool ValidateCheckPrefix(StringRef CheckPrefix) {
  1043. Regex Validator("^[a-zA-Z0-9_-]*$");
  1044. return Validator.match(CheckPrefix);
  1045. }
  1046. bool llvm::FileCheck::ValidateCheckPrefixes() {
  1047. StringSet<> PrefixSet;
  1048. for (StringRef Prefix : Req.CheckPrefixes) {
  1049. // Reject empty prefixes.
  1050. if (Prefix == "")
  1051. return false;
  1052. if (!PrefixSet.insert(Prefix).second)
  1053. return false;
  1054. if (!ValidateCheckPrefix(Prefix))
  1055. return false;
  1056. }
  1057. return true;
  1058. }
  1059. // Combines the check prefixes into a single regex so that we can efficiently
  1060. // scan for any of the set.
  1061. //
  1062. // The semantics are that the longest-match wins which matches our regex
  1063. // library.
  1064. Regex llvm::FileCheck::buildCheckPrefixRegex() {
  1065. // I don't think there's a way to specify an initial value for cl::list,
  1066. // so if nothing was specified, add the default
  1067. if (Req.CheckPrefixes.empty())
  1068. Req.CheckPrefixes.push_back("CHECK");
  1069. // We already validated the contents of CheckPrefixes so just concatenate
  1070. // them as alternatives.
  1071. SmallString<32> PrefixRegexStr;
  1072. for (StringRef Prefix : Req.CheckPrefixes) {
  1073. if (Prefix != Req.CheckPrefixes.front())
  1074. PrefixRegexStr.push_back('|');
  1075. PrefixRegexStr.append(Prefix);
  1076. }
  1077. return Regex(PrefixRegexStr);
  1078. }
  1079. // Remove local variables from \p VariableTable. Global variables
  1080. // (start with '$') are preserved.
  1081. static void ClearLocalVars(StringMap<StringRef> &VariableTable) {
  1082. SmallVector<StringRef, 16> LocalVars;
  1083. for (const auto &Var : VariableTable)
  1084. if (Var.first()[0] != '$')
  1085. LocalVars.push_back(Var.first());
  1086. for (const auto &Var : LocalVars)
  1087. VariableTable.erase(Var);
  1088. }
  1089. /// Check the input to FileCheck provided in the \p Buffer against the \p
  1090. /// CheckStrings read from the check file.
  1091. ///
  1092. /// Returns false if the input fails to satisfy the checks.
  1093. bool llvm::FileCheck::CheckInput(SourceMgr &SM, StringRef Buffer,
  1094. ArrayRef<FileCheckString> CheckStrings) {
  1095. bool ChecksFailed = false;
  1096. /// VariableTable - This holds all the current filecheck variables.
  1097. StringMap<StringRef> VariableTable;
  1098. for (const auto& Def : Req.GlobalDefines)
  1099. VariableTable.insert(StringRef(Def).split('='));
  1100. unsigned i = 0, j = 0, e = CheckStrings.size();
  1101. while (true) {
  1102. StringRef CheckRegion;
  1103. if (j == e) {
  1104. CheckRegion = Buffer;
  1105. } else {
  1106. const FileCheckString &CheckLabelStr = CheckStrings[j];
  1107. if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) {
  1108. ++j;
  1109. continue;
  1110. }
  1111. // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
  1112. size_t MatchLabelLen = 0;
  1113. size_t MatchLabelPos =
  1114. CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, VariableTable,
  1115. Req);
  1116. if (MatchLabelPos == StringRef::npos)
  1117. // Immediately bail of CHECK-LABEL fails, nothing else we can do.
  1118. return false;
  1119. CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
  1120. Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
  1121. ++j;
  1122. }
  1123. if (Req.EnableVarScope)
  1124. ClearLocalVars(VariableTable);
  1125. for (; i != j; ++i) {
  1126. const FileCheckString &CheckStr = CheckStrings[i];
  1127. // Check each string within the scanned region, including a second check
  1128. // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
  1129. size_t MatchLen = 0;
  1130. size_t MatchPos =
  1131. CheckStr.Check(SM, CheckRegion, false, MatchLen, VariableTable, Req);
  1132. if (MatchPos == StringRef::npos) {
  1133. ChecksFailed = true;
  1134. i = j;
  1135. break;
  1136. }
  1137. CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
  1138. }
  1139. if (j == e)
  1140. break;
  1141. }
  1142. // Success if no checks failed.
  1143. return !ChecksFailed;
  1144. }