FileCheck.cpp 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861
  1. //===- FileCheck.cpp - Check that File's Contents match what is expected --===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // FileCheck does a line-by line check of a file that validates whether it
  10. // contains the expected content. This is useful for regression tests etc.
  11. //
  12. // This file implements most of the API that will be used by the FileCheck utility
  13. // as well as various unittests.
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/FileCheck/FileCheck.h"
  16. #include "FileCheckImpl.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/StringSet.h"
  19. #include "llvm/ADT/Twine.h"
  20. #include "llvm/Support/CheckedArithmetic.h"
  21. #include "llvm/Support/FormatVariadic.h"
  22. #include <cstdint>
  23. #include <list>
  24. #include <set>
  25. #include <tuple>
  26. #include <utility>
  27. using namespace llvm;
  28. StringRef ExpressionFormat::toString() const {
  29. switch (Value) {
  30. case Kind::NoFormat:
  31. return StringRef("<none>");
  32. case Kind::Unsigned:
  33. return StringRef("%u");
  34. case Kind::Signed:
  35. return StringRef("%d");
  36. case Kind::HexUpper:
  37. return StringRef("%X");
  38. case Kind::HexLower:
  39. return StringRef("%x");
  40. }
  41. llvm_unreachable("unknown expression format");
  42. }
  43. Expected<std::string> ExpressionFormat::getWildcardRegex() const {
  44. StringRef AlternateFormPrefix = AlternateForm ? StringRef("0x") : StringRef();
  45. auto CreatePrecisionRegex = [&](StringRef S) {
  46. return (Twine(AlternateFormPrefix) + S + Twine('{') + Twine(Precision) +
  47. "}")
  48. .str();
  49. };
  50. switch (Value) {
  51. case Kind::Unsigned:
  52. if (Precision)
  53. return CreatePrecisionRegex("([1-9][0-9]*)?[0-9]");
  54. return std::string("[0-9]+");
  55. case Kind::Signed:
  56. if (Precision)
  57. return CreatePrecisionRegex("-?([1-9][0-9]*)?[0-9]");
  58. return std::string("-?[0-9]+");
  59. case Kind::HexUpper:
  60. if (Precision)
  61. return CreatePrecisionRegex("([1-9A-F][0-9A-F]*)?[0-9A-F]");
  62. return (Twine(AlternateFormPrefix) + Twine("[0-9A-F]+")).str();
  63. case Kind::HexLower:
  64. if (Precision)
  65. return CreatePrecisionRegex("([1-9a-f][0-9a-f]*)?[0-9a-f]");
  66. return (Twine(AlternateFormPrefix) + Twine("[0-9a-f]+")).str();
  67. default:
  68. return createStringError(std::errc::invalid_argument,
  69. "trying to match value with invalid format");
  70. }
  71. }
  72. Expected<std::string>
  73. ExpressionFormat::getMatchingString(ExpressionValue IntegerValue) const {
  74. uint64_t AbsoluteValue;
  75. StringRef SignPrefix = IntegerValue.isNegative() ? "-" : "";
  76. if (Value == Kind::Signed) {
  77. Expected<int64_t> SignedValue = IntegerValue.getSignedValue();
  78. if (!SignedValue)
  79. return SignedValue.takeError();
  80. if (*SignedValue < 0)
  81. AbsoluteValue = cantFail(IntegerValue.getAbsolute().getUnsignedValue());
  82. else
  83. AbsoluteValue = *SignedValue;
  84. } else {
  85. Expected<uint64_t> UnsignedValue = IntegerValue.getUnsignedValue();
  86. if (!UnsignedValue)
  87. return UnsignedValue.takeError();
  88. AbsoluteValue = *UnsignedValue;
  89. }
  90. std::string AbsoluteValueStr;
  91. switch (Value) {
  92. case Kind::Unsigned:
  93. case Kind::Signed:
  94. AbsoluteValueStr = utostr(AbsoluteValue);
  95. break;
  96. case Kind::HexUpper:
  97. case Kind::HexLower:
  98. AbsoluteValueStr = utohexstr(AbsoluteValue, Value == Kind::HexLower);
  99. break;
  100. default:
  101. return createStringError(std::errc::invalid_argument,
  102. "trying to match value with invalid format");
  103. }
  104. StringRef AlternateFormPrefix = AlternateForm ? StringRef("0x") : StringRef();
  105. if (Precision > AbsoluteValueStr.size()) {
  106. unsigned LeadingZeros = Precision - AbsoluteValueStr.size();
  107. return (Twine(SignPrefix) + Twine(AlternateFormPrefix) +
  108. std::string(LeadingZeros, '0') + AbsoluteValueStr)
  109. .str();
  110. }
  111. return (Twine(SignPrefix) + Twine(AlternateFormPrefix) + AbsoluteValueStr)
  112. .str();
  113. }
  114. Expected<ExpressionValue>
  115. ExpressionFormat::valueFromStringRepr(StringRef StrVal,
  116. const SourceMgr &SM) const {
  117. bool ValueIsSigned = Value == Kind::Signed;
  118. // Both the FileCheck utility and library only call this method with a valid
  119. // value in StrVal. This is guaranteed by the regex returned by
  120. // getWildcardRegex() above. Only underflow and overflow errors can thus
  121. // occur. However new uses of this method could be added in the future so
  122. // the error message does not make assumptions about StrVal.
  123. StringRef IntegerParseErrorStr = "unable to represent numeric value";
  124. if (ValueIsSigned) {
  125. int64_t SignedValue;
  126. if (StrVal.getAsInteger(10, SignedValue))
  127. return ErrorDiagnostic::get(SM, StrVal, IntegerParseErrorStr);
  128. return ExpressionValue(SignedValue);
  129. }
  130. bool Hex = Value == Kind::HexUpper || Value == Kind::HexLower;
  131. uint64_t UnsignedValue;
  132. bool MissingFormPrefix = AlternateForm && !StrVal.consume_front("0x");
  133. if (StrVal.getAsInteger(Hex ? 16 : 10, UnsignedValue))
  134. return ErrorDiagnostic::get(SM, StrVal, IntegerParseErrorStr);
  135. // Error out for a missing prefix only now that we know we have an otherwise
  136. // valid integer. For example, "-0x18" is reported above instead.
  137. if (MissingFormPrefix)
  138. return ErrorDiagnostic::get(SM, StrVal, "missing alternate form prefix");
  139. return ExpressionValue(UnsignedValue);
  140. }
  141. static int64_t getAsSigned(uint64_t UnsignedValue) {
  142. // Use memcpy to reinterpret the bitpattern in Value since casting to
  143. // signed is implementation-defined if the unsigned value is too big to be
  144. // represented in the signed type and using an union violates type aliasing
  145. // rules.
  146. int64_t SignedValue;
  147. memcpy(&SignedValue, &UnsignedValue, sizeof(SignedValue));
  148. return SignedValue;
  149. }
  150. Expected<int64_t> ExpressionValue::getSignedValue() const {
  151. if (Negative)
  152. return getAsSigned(Value);
  153. if (Value > (uint64_t)std::numeric_limits<int64_t>::max())
  154. return make_error<OverflowError>();
  155. // Value is in the representable range of int64_t so we can use cast.
  156. return static_cast<int64_t>(Value);
  157. }
  158. Expected<uint64_t> ExpressionValue::getUnsignedValue() const {
  159. if (Negative)
  160. return make_error<OverflowError>();
  161. return Value;
  162. }
  163. ExpressionValue ExpressionValue::getAbsolute() const {
  164. if (!Negative)
  165. return *this;
  166. int64_t SignedValue = getAsSigned(Value);
  167. int64_t MaxInt64 = std::numeric_limits<int64_t>::max();
  168. // Absolute value can be represented as int64_t.
  169. if (SignedValue >= -MaxInt64)
  170. return ExpressionValue(-getAsSigned(Value));
  171. // -X == -(max int64_t + Rem), negate each component independently.
  172. SignedValue += MaxInt64;
  173. uint64_t RemainingValueAbsolute = -SignedValue;
  174. return ExpressionValue(MaxInt64 + RemainingValueAbsolute);
  175. }
  176. Expected<ExpressionValue> llvm::operator+(const ExpressionValue &LeftOperand,
  177. const ExpressionValue &RightOperand) {
  178. if (LeftOperand.isNegative() && RightOperand.isNegative()) {
  179. int64_t LeftValue = cantFail(LeftOperand.getSignedValue());
  180. int64_t RightValue = cantFail(RightOperand.getSignedValue());
  181. std::optional<int64_t> Result = checkedAdd<int64_t>(LeftValue, RightValue);
  182. if (!Result)
  183. return make_error<OverflowError>();
  184. return ExpressionValue(*Result);
  185. }
  186. // (-A) + B == B - A.
  187. if (LeftOperand.isNegative())
  188. return RightOperand - LeftOperand.getAbsolute();
  189. // A + (-B) == A - B.
  190. if (RightOperand.isNegative())
  191. return LeftOperand - RightOperand.getAbsolute();
  192. // Both values are positive at this point.
  193. uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue());
  194. uint64_t RightValue = cantFail(RightOperand.getUnsignedValue());
  195. std::optional<uint64_t> Result =
  196. checkedAddUnsigned<uint64_t>(LeftValue, RightValue);
  197. if (!Result)
  198. return make_error<OverflowError>();
  199. return ExpressionValue(*Result);
  200. }
  201. Expected<ExpressionValue> llvm::operator-(const ExpressionValue &LeftOperand,
  202. const ExpressionValue &RightOperand) {
  203. // Result will be negative and thus might underflow.
  204. if (LeftOperand.isNegative() && !RightOperand.isNegative()) {
  205. int64_t LeftValue = cantFail(LeftOperand.getSignedValue());
  206. uint64_t RightValue = cantFail(RightOperand.getUnsignedValue());
  207. // Result <= -1 - (max int64_t) which overflows on 1- and 2-complement.
  208. if (RightValue > (uint64_t)std::numeric_limits<int64_t>::max())
  209. return make_error<OverflowError>();
  210. std::optional<int64_t> Result =
  211. checkedSub(LeftValue, static_cast<int64_t>(RightValue));
  212. if (!Result)
  213. return make_error<OverflowError>();
  214. return ExpressionValue(*Result);
  215. }
  216. // (-A) - (-B) == B - A.
  217. if (LeftOperand.isNegative())
  218. return RightOperand.getAbsolute() - LeftOperand.getAbsolute();
  219. // A - (-B) == A + B.
  220. if (RightOperand.isNegative())
  221. return LeftOperand + RightOperand.getAbsolute();
  222. // Both values are positive at this point.
  223. uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue());
  224. uint64_t RightValue = cantFail(RightOperand.getUnsignedValue());
  225. if (LeftValue >= RightValue)
  226. return ExpressionValue(LeftValue - RightValue);
  227. else {
  228. uint64_t AbsoluteDifference = RightValue - LeftValue;
  229. uint64_t MaxInt64 = std::numeric_limits<int64_t>::max();
  230. // Value might underflow.
  231. if (AbsoluteDifference > MaxInt64) {
  232. AbsoluteDifference -= MaxInt64;
  233. int64_t Result = -MaxInt64;
  234. int64_t MinInt64 = std::numeric_limits<int64_t>::min();
  235. // Underflow, tested by:
  236. // abs(Result + (max int64_t)) > abs((min int64_t) + (max int64_t))
  237. if (AbsoluteDifference > static_cast<uint64_t>(-(MinInt64 - Result)))
  238. return make_error<OverflowError>();
  239. Result -= static_cast<int64_t>(AbsoluteDifference);
  240. return ExpressionValue(Result);
  241. }
  242. return ExpressionValue(-static_cast<int64_t>(AbsoluteDifference));
  243. }
  244. }
  245. Expected<ExpressionValue> llvm::operator*(const ExpressionValue &LeftOperand,
  246. const ExpressionValue &RightOperand) {
  247. // -A * -B == A * B
  248. if (LeftOperand.isNegative() && RightOperand.isNegative())
  249. return LeftOperand.getAbsolute() * RightOperand.getAbsolute();
  250. // A * -B == -B * A
  251. if (RightOperand.isNegative())
  252. return RightOperand * LeftOperand;
  253. assert(!RightOperand.isNegative() && "Unexpected negative operand!");
  254. // Result will be negative and can underflow.
  255. if (LeftOperand.isNegative()) {
  256. auto Result = LeftOperand.getAbsolute() * RightOperand.getAbsolute();
  257. if (!Result)
  258. return Result;
  259. return ExpressionValue(0) - *Result;
  260. }
  261. // Result will be positive and can overflow.
  262. uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue());
  263. uint64_t RightValue = cantFail(RightOperand.getUnsignedValue());
  264. std::optional<uint64_t> Result =
  265. checkedMulUnsigned<uint64_t>(LeftValue, RightValue);
  266. if (!Result)
  267. return make_error<OverflowError>();
  268. return ExpressionValue(*Result);
  269. }
  270. Expected<ExpressionValue> llvm::operator/(const ExpressionValue &LeftOperand,
  271. const ExpressionValue &RightOperand) {
  272. // -A / -B == A / B
  273. if (LeftOperand.isNegative() && RightOperand.isNegative())
  274. return LeftOperand.getAbsolute() / RightOperand.getAbsolute();
  275. // Check for divide by zero.
  276. if (RightOperand == ExpressionValue(0))
  277. return make_error<OverflowError>();
  278. // Result will be negative and can underflow.
  279. if (LeftOperand.isNegative() || RightOperand.isNegative())
  280. return ExpressionValue(0) -
  281. cantFail(LeftOperand.getAbsolute() / RightOperand.getAbsolute());
  282. uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue());
  283. uint64_t RightValue = cantFail(RightOperand.getUnsignedValue());
  284. return ExpressionValue(LeftValue / RightValue);
  285. }
  286. Expected<ExpressionValue> llvm::max(const ExpressionValue &LeftOperand,
  287. const ExpressionValue &RightOperand) {
  288. if (LeftOperand.isNegative() && RightOperand.isNegative()) {
  289. int64_t LeftValue = cantFail(LeftOperand.getSignedValue());
  290. int64_t RightValue = cantFail(RightOperand.getSignedValue());
  291. return ExpressionValue(std::max(LeftValue, RightValue));
  292. }
  293. if (!LeftOperand.isNegative() && !RightOperand.isNegative()) {
  294. uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue());
  295. uint64_t RightValue = cantFail(RightOperand.getUnsignedValue());
  296. return ExpressionValue(std::max(LeftValue, RightValue));
  297. }
  298. if (LeftOperand.isNegative())
  299. return RightOperand;
  300. return LeftOperand;
  301. }
  302. Expected<ExpressionValue> llvm::min(const ExpressionValue &LeftOperand,
  303. const ExpressionValue &RightOperand) {
  304. if (cantFail(max(LeftOperand, RightOperand)) == LeftOperand)
  305. return RightOperand;
  306. return LeftOperand;
  307. }
  308. Expected<ExpressionValue> NumericVariableUse::eval() const {
  309. std::optional<ExpressionValue> Value = Variable->getValue();
  310. if (Value)
  311. return *Value;
  312. return make_error<UndefVarError>(getExpressionStr());
  313. }
  314. Expected<ExpressionValue> BinaryOperation::eval() const {
  315. Expected<ExpressionValue> LeftOp = LeftOperand->eval();
  316. Expected<ExpressionValue> RightOp = RightOperand->eval();
  317. // Bubble up any error (e.g. undefined variables) in the recursive
  318. // evaluation.
  319. if (!LeftOp || !RightOp) {
  320. Error Err = Error::success();
  321. if (!LeftOp)
  322. Err = joinErrors(std::move(Err), LeftOp.takeError());
  323. if (!RightOp)
  324. Err = joinErrors(std::move(Err), RightOp.takeError());
  325. return std::move(Err);
  326. }
  327. return EvalBinop(*LeftOp, *RightOp);
  328. }
  329. Expected<ExpressionFormat>
  330. BinaryOperation::getImplicitFormat(const SourceMgr &SM) const {
  331. Expected<ExpressionFormat> LeftFormat = LeftOperand->getImplicitFormat(SM);
  332. Expected<ExpressionFormat> RightFormat = RightOperand->getImplicitFormat(SM);
  333. if (!LeftFormat || !RightFormat) {
  334. Error Err = Error::success();
  335. if (!LeftFormat)
  336. Err = joinErrors(std::move(Err), LeftFormat.takeError());
  337. if (!RightFormat)
  338. Err = joinErrors(std::move(Err), RightFormat.takeError());
  339. return std::move(Err);
  340. }
  341. if (*LeftFormat != ExpressionFormat::Kind::NoFormat &&
  342. *RightFormat != ExpressionFormat::Kind::NoFormat &&
  343. *LeftFormat != *RightFormat)
  344. return ErrorDiagnostic::get(
  345. SM, getExpressionStr(),
  346. "implicit format conflict between '" + LeftOperand->getExpressionStr() +
  347. "' (" + LeftFormat->toString() + ") and '" +
  348. RightOperand->getExpressionStr() + "' (" + RightFormat->toString() +
  349. "), need an explicit format specifier");
  350. return *LeftFormat != ExpressionFormat::Kind::NoFormat ? *LeftFormat
  351. : *RightFormat;
  352. }
  353. Expected<std::string> NumericSubstitution::getResult() const {
  354. assert(ExpressionPointer->getAST() != nullptr &&
  355. "Substituting empty expression");
  356. Expected<ExpressionValue> EvaluatedValue =
  357. ExpressionPointer->getAST()->eval();
  358. if (!EvaluatedValue)
  359. return EvaluatedValue.takeError();
  360. ExpressionFormat Format = ExpressionPointer->getFormat();
  361. return Format.getMatchingString(*EvaluatedValue);
  362. }
  363. Expected<std::string> StringSubstitution::getResult() const {
  364. // Look up the value and escape it so that we can put it into the regex.
  365. Expected<StringRef> VarVal = Context->getPatternVarValue(FromStr);
  366. if (!VarVal)
  367. return VarVal.takeError();
  368. return Regex::escape(*VarVal);
  369. }
  370. bool Pattern::isValidVarNameStart(char C) { return C == '_' || isAlpha(C); }
  371. Expected<Pattern::VariableProperties>
  372. Pattern::parseVariable(StringRef &Str, const SourceMgr &SM) {
  373. if (Str.empty())
  374. return ErrorDiagnostic::get(SM, Str, "empty variable name");
  375. size_t I = 0;
  376. bool IsPseudo = Str[0] == '@';
  377. // Global vars start with '$'.
  378. if (Str[0] == '$' || IsPseudo)
  379. ++I;
  380. if (!isValidVarNameStart(Str[I++]))
  381. return ErrorDiagnostic::get(SM, Str, "invalid variable name");
  382. for (size_t E = Str.size(); I != E; ++I)
  383. // Variable names are composed of alphanumeric characters and underscores.
  384. if (Str[I] != '_' && !isAlnum(Str[I]))
  385. break;
  386. StringRef Name = Str.take_front(I);
  387. Str = Str.substr(I);
  388. return VariableProperties {Name, IsPseudo};
  389. }
  390. // StringRef holding all characters considered as horizontal whitespaces by
  391. // FileCheck input canonicalization.
  392. constexpr StringLiteral SpaceChars = " \t";
  393. // Parsing helper function that strips the first character in S and returns it.
  394. static char popFront(StringRef &S) {
  395. char C = S.front();
  396. S = S.drop_front();
  397. return C;
  398. }
  399. char OverflowError::ID = 0;
  400. char UndefVarError::ID = 0;
  401. char ErrorDiagnostic::ID = 0;
  402. char NotFoundError::ID = 0;
  403. char ErrorReported::ID = 0;
  404. Expected<NumericVariable *> Pattern::parseNumericVariableDefinition(
  405. StringRef &Expr, FileCheckPatternContext *Context,
  406. std::optional<size_t> LineNumber, ExpressionFormat ImplicitFormat,
  407. const SourceMgr &SM) {
  408. Expected<VariableProperties> ParseVarResult = parseVariable(Expr, SM);
  409. if (!ParseVarResult)
  410. return ParseVarResult.takeError();
  411. StringRef Name = ParseVarResult->Name;
  412. if (ParseVarResult->IsPseudo)
  413. return ErrorDiagnostic::get(
  414. SM, Name, "definition of pseudo numeric variable unsupported");
  415. // Detect collisions between string and numeric variables when the latter
  416. // is created later than the former.
  417. if (Context->DefinedVariableTable.find(Name) !=
  418. Context->DefinedVariableTable.end())
  419. return ErrorDiagnostic::get(
  420. SM, Name, "string variable with name '" + Name + "' already exists");
  421. Expr = Expr.ltrim(SpaceChars);
  422. if (!Expr.empty())
  423. return ErrorDiagnostic::get(
  424. SM, Expr, "unexpected characters after numeric variable name");
  425. NumericVariable *DefinedNumericVariable;
  426. auto VarTableIter = Context->GlobalNumericVariableTable.find(Name);
  427. if (VarTableIter != Context->GlobalNumericVariableTable.end()) {
  428. DefinedNumericVariable = VarTableIter->second;
  429. if (DefinedNumericVariable->getImplicitFormat() != ImplicitFormat)
  430. return ErrorDiagnostic::get(
  431. SM, Expr, "format different from previous variable definition");
  432. } else
  433. DefinedNumericVariable =
  434. Context->makeNumericVariable(Name, ImplicitFormat, LineNumber);
  435. return DefinedNumericVariable;
  436. }
  437. Expected<std::unique_ptr<NumericVariableUse>> Pattern::parseNumericVariableUse(
  438. StringRef Name, bool IsPseudo, std::optional<size_t> LineNumber,
  439. FileCheckPatternContext *Context, const SourceMgr &SM) {
  440. if (IsPseudo && !Name.equals("@LINE"))
  441. return ErrorDiagnostic::get(
  442. SM, Name, "invalid pseudo numeric variable '" + Name + "'");
  443. // Numeric variable definitions and uses are parsed in the order in which
  444. // they appear in the CHECK patterns. For each definition, the pointer to the
  445. // class instance of the corresponding numeric variable definition is stored
  446. // in GlobalNumericVariableTable in parsePattern. Therefore, if the pointer
  447. // we get below is null, it means no such variable was defined before. When
  448. // that happens, we create a dummy variable so that parsing can continue. All
  449. // uses of undefined variables, whether string or numeric, are then diagnosed
  450. // in printNoMatch() after failing to match.
  451. auto VarTableIter = Context->GlobalNumericVariableTable.find(Name);
  452. NumericVariable *NumericVariable;
  453. if (VarTableIter != Context->GlobalNumericVariableTable.end())
  454. NumericVariable = VarTableIter->second;
  455. else {
  456. NumericVariable = Context->makeNumericVariable(
  457. Name, ExpressionFormat(ExpressionFormat::Kind::Unsigned));
  458. Context->GlobalNumericVariableTable[Name] = NumericVariable;
  459. }
  460. std::optional<size_t> DefLineNumber = NumericVariable->getDefLineNumber();
  461. if (DefLineNumber && LineNumber && *DefLineNumber == *LineNumber)
  462. return ErrorDiagnostic::get(
  463. SM, Name,
  464. "numeric variable '" + Name +
  465. "' defined earlier in the same CHECK directive");
  466. return std::make_unique<NumericVariableUse>(Name, NumericVariable);
  467. }
  468. Expected<std::unique_ptr<ExpressionAST>> Pattern::parseNumericOperand(
  469. StringRef &Expr, AllowedOperand AO, bool MaybeInvalidConstraint,
  470. std::optional<size_t> LineNumber, FileCheckPatternContext *Context,
  471. const SourceMgr &SM) {
  472. if (Expr.startswith("(")) {
  473. if (AO != AllowedOperand::Any)
  474. return ErrorDiagnostic::get(
  475. SM, Expr, "parenthesized expression not permitted here");
  476. return parseParenExpr(Expr, LineNumber, Context, SM);
  477. }
  478. if (AO == AllowedOperand::LineVar || AO == AllowedOperand::Any) {
  479. // Try to parse as a numeric variable use.
  480. Expected<Pattern::VariableProperties> ParseVarResult =
  481. parseVariable(Expr, SM);
  482. if (ParseVarResult) {
  483. // Try to parse a function call.
  484. if (Expr.ltrim(SpaceChars).startswith("(")) {
  485. if (AO != AllowedOperand::Any)
  486. return ErrorDiagnostic::get(SM, ParseVarResult->Name,
  487. "unexpected function call");
  488. return parseCallExpr(Expr, ParseVarResult->Name, LineNumber, Context,
  489. SM);
  490. }
  491. return parseNumericVariableUse(ParseVarResult->Name,
  492. ParseVarResult->IsPseudo, LineNumber,
  493. Context, SM);
  494. }
  495. if (AO == AllowedOperand::LineVar)
  496. return ParseVarResult.takeError();
  497. // Ignore the error and retry parsing as a literal.
  498. consumeError(ParseVarResult.takeError());
  499. }
  500. // Otherwise, parse it as a literal.
  501. int64_t SignedLiteralValue;
  502. uint64_t UnsignedLiteralValue;
  503. StringRef SaveExpr = Expr;
  504. // Accept both signed and unsigned literal, default to signed literal.
  505. if (!Expr.consumeInteger((AO == AllowedOperand::LegacyLiteral) ? 10 : 0,
  506. UnsignedLiteralValue))
  507. return std::make_unique<ExpressionLiteral>(SaveExpr.drop_back(Expr.size()),
  508. UnsignedLiteralValue);
  509. Expr = SaveExpr;
  510. if (AO == AllowedOperand::Any && !Expr.consumeInteger(0, SignedLiteralValue))
  511. return std::make_unique<ExpressionLiteral>(SaveExpr.drop_back(Expr.size()),
  512. SignedLiteralValue);
  513. return ErrorDiagnostic::get(
  514. SM, Expr,
  515. Twine("invalid ") +
  516. (MaybeInvalidConstraint ? "matching constraint or " : "") +
  517. "operand format");
  518. }
  519. Expected<std::unique_ptr<ExpressionAST>>
  520. Pattern::parseParenExpr(StringRef &Expr, std::optional<size_t> LineNumber,
  521. FileCheckPatternContext *Context, const SourceMgr &SM) {
  522. Expr = Expr.ltrim(SpaceChars);
  523. assert(Expr.startswith("("));
  524. // Parse right operand.
  525. Expr.consume_front("(");
  526. Expr = Expr.ltrim(SpaceChars);
  527. if (Expr.empty())
  528. return ErrorDiagnostic::get(SM, Expr, "missing operand in expression");
  529. // Note: parseNumericOperand handles nested opening parentheses.
  530. Expected<std::unique_ptr<ExpressionAST>> SubExprResult = parseNumericOperand(
  531. Expr, AllowedOperand::Any, /*MaybeInvalidConstraint=*/false, LineNumber,
  532. Context, SM);
  533. Expr = Expr.ltrim(SpaceChars);
  534. while (SubExprResult && !Expr.empty() && !Expr.startswith(")")) {
  535. StringRef OrigExpr = Expr;
  536. SubExprResult = parseBinop(OrigExpr, Expr, std::move(*SubExprResult), false,
  537. LineNumber, Context, SM);
  538. Expr = Expr.ltrim(SpaceChars);
  539. }
  540. if (!SubExprResult)
  541. return SubExprResult;
  542. if (!Expr.consume_front(")")) {
  543. return ErrorDiagnostic::get(SM, Expr,
  544. "missing ')' at end of nested expression");
  545. }
  546. return SubExprResult;
  547. }
  548. Expected<std::unique_ptr<ExpressionAST>>
  549. Pattern::parseBinop(StringRef Expr, StringRef &RemainingExpr,
  550. std::unique_ptr<ExpressionAST> LeftOp,
  551. bool IsLegacyLineExpr, std::optional<size_t> LineNumber,
  552. FileCheckPatternContext *Context, const SourceMgr &SM) {
  553. RemainingExpr = RemainingExpr.ltrim(SpaceChars);
  554. if (RemainingExpr.empty())
  555. return std::move(LeftOp);
  556. // Check if this is a supported operation and select a function to perform
  557. // it.
  558. SMLoc OpLoc = SMLoc::getFromPointer(RemainingExpr.data());
  559. char Operator = popFront(RemainingExpr);
  560. binop_eval_t EvalBinop;
  561. switch (Operator) {
  562. case '+':
  563. EvalBinop = operator+;
  564. break;
  565. case '-':
  566. EvalBinop = operator-;
  567. break;
  568. default:
  569. return ErrorDiagnostic::get(
  570. SM, OpLoc, Twine("unsupported operation '") + Twine(Operator) + "'");
  571. }
  572. // Parse right operand.
  573. RemainingExpr = RemainingExpr.ltrim(SpaceChars);
  574. if (RemainingExpr.empty())
  575. return ErrorDiagnostic::get(SM, RemainingExpr,
  576. "missing operand in expression");
  577. // The second operand in a legacy @LINE expression is always a literal.
  578. AllowedOperand AO =
  579. IsLegacyLineExpr ? AllowedOperand::LegacyLiteral : AllowedOperand::Any;
  580. Expected<std::unique_ptr<ExpressionAST>> RightOpResult =
  581. parseNumericOperand(RemainingExpr, AO, /*MaybeInvalidConstraint=*/false,
  582. LineNumber, Context, SM);
  583. if (!RightOpResult)
  584. return RightOpResult;
  585. Expr = Expr.drop_back(RemainingExpr.size());
  586. return std::make_unique<BinaryOperation>(Expr, EvalBinop, std::move(LeftOp),
  587. std::move(*RightOpResult));
  588. }
  589. Expected<std::unique_ptr<ExpressionAST>>
  590. Pattern::parseCallExpr(StringRef &Expr, StringRef FuncName,
  591. std::optional<size_t> LineNumber,
  592. FileCheckPatternContext *Context, const SourceMgr &SM) {
  593. Expr = Expr.ltrim(SpaceChars);
  594. assert(Expr.startswith("("));
  595. auto OptFunc = StringSwitch<binop_eval_t>(FuncName)
  596. .Case("add", operator+)
  597. .Case("div", operator/)
  598. .Case("max", max)
  599. .Case("min", min)
  600. .Case("mul", operator*)
  601. .Case("sub", operator-)
  602. .Default(nullptr);
  603. if (!OptFunc)
  604. return ErrorDiagnostic::get(
  605. SM, FuncName, Twine("call to undefined function '") + FuncName + "'");
  606. Expr.consume_front("(");
  607. Expr = Expr.ltrim(SpaceChars);
  608. // Parse call arguments, which are comma separated.
  609. SmallVector<std::unique_ptr<ExpressionAST>, 4> Args;
  610. while (!Expr.empty() && !Expr.startswith(")")) {
  611. if (Expr.startswith(","))
  612. return ErrorDiagnostic::get(SM, Expr, "missing argument");
  613. // Parse the argument, which is an arbitary expression.
  614. StringRef OuterBinOpExpr = Expr;
  615. Expected<std::unique_ptr<ExpressionAST>> Arg = parseNumericOperand(
  616. Expr, AllowedOperand::Any, /*MaybeInvalidConstraint=*/false, LineNumber,
  617. Context, SM);
  618. while (Arg && !Expr.empty()) {
  619. Expr = Expr.ltrim(SpaceChars);
  620. // Have we reached an argument terminator?
  621. if (Expr.startswith(",") || Expr.startswith(")"))
  622. break;
  623. // Arg = Arg <op> <expr>
  624. Arg = parseBinop(OuterBinOpExpr, Expr, std::move(*Arg), false, LineNumber,
  625. Context, SM);
  626. }
  627. // Prefer an expression error over a generic invalid argument message.
  628. if (!Arg)
  629. return Arg.takeError();
  630. Args.push_back(std::move(*Arg));
  631. // Have we parsed all available arguments?
  632. Expr = Expr.ltrim(SpaceChars);
  633. if (!Expr.consume_front(","))
  634. break;
  635. Expr = Expr.ltrim(SpaceChars);
  636. if (Expr.startswith(")"))
  637. return ErrorDiagnostic::get(SM, Expr, "missing argument");
  638. }
  639. if (!Expr.consume_front(")"))
  640. return ErrorDiagnostic::get(SM, Expr,
  641. "missing ')' at end of call expression");
  642. const unsigned NumArgs = Args.size();
  643. if (NumArgs == 2)
  644. return std::make_unique<BinaryOperation>(Expr, *OptFunc, std::move(Args[0]),
  645. std::move(Args[1]));
  646. // TODO: Support more than binop_eval_t.
  647. return ErrorDiagnostic::get(SM, FuncName,
  648. Twine("function '") + FuncName +
  649. Twine("' takes 2 arguments but ") +
  650. Twine(NumArgs) + " given");
  651. }
  652. Expected<std::unique_ptr<Expression>> Pattern::parseNumericSubstitutionBlock(
  653. StringRef Expr, std::optional<NumericVariable *> &DefinedNumericVariable,
  654. bool IsLegacyLineExpr, std::optional<size_t> LineNumber,
  655. FileCheckPatternContext *Context, const SourceMgr &SM) {
  656. std::unique_ptr<ExpressionAST> ExpressionASTPointer = nullptr;
  657. StringRef DefExpr = StringRef();
  658. DefinedNumericVariable = std::nullopt;
  659. ExpressionFormat ExplicitFormat = ExpressionFormat();
  660. unsigned Precision = 0;
  661. // Parse format specifier (NOTE: ',' is also an argument seperator).
  662. size_t FormatSpecEnd = Expr.find(',');
  663. size_t FunctionStart = Expr.find('(');
  664. if (FormatSpecEnd != StringRef::npos && FormatSpecEnd < FunctionStart) {
  665. StringRef FormatExpr = Expr.take_front(FormatSpecEnd);
  666. Expr = Expr.drop_front(FormatSpecEnd + 1);
  667. FormatExpr = FormatExpr.trim(SpaceChars);
  668. if (!FormatExpr.consume_front("%"))
  669. return ErrorDiagnostic::get(
  670. SM, FormatExpr,
  671. "invalid matching format specification in expression");
  672. // Parse alternate form flag.
  673. SMLoc AlternateFormFlagLoc = SMLoc::getFromPointer(FormatExpr.data());
  674. bool AlternateForm = FormatExpr.consume_front("#");
  675. // Parse precision.
  676. if (FormatExpr.consume_front(".")) {
  677. if (FormatExpr.consumeInteger(10, Precision))
  678. return ErrorDiagnostic::get(SM, FormatExpr,
  679. "invalid precision in format specifier");
  680. }
  681. if (!FormatExpr.empty()) {
  682. // Check for unknown matching format specifier and set matching format in
  683. // class instance representing this expression.
  684. SMLoc FmtLoc = SMLoc::getFromPointer(FormatExpr.data());
  685. switch (popFront(FormatExpr)) {
  686. case 'u':
  687. ExplicitFormat =
  688. ExpressionFormat(ExpressionFormat::Kind::Unsigned, Precision);
  689. break;
  690. case 'd':
  691. ExplicitFormat =
  692. ExpressionFormat(ExpressionFormat::Kind::Signed, Precision);
  693. break;
  694. case 'x':
  695. ExplicitFormat = ExpressionFormat(ExpressionFormat::Kind::HexLower,
  696. Precision, AlternateForm);
  697. break;
  698. case 'X':
  699. ExplicitFormat = ExpressionFormat(ExpressionFormat::Kind::HexUpper,
  700. Precision, AlternateForm);
  701. break;
  702. default:
  703. return ErrorDiagnostic::get(SM, FmtLoc,
  704. "invalid format specifier in expression");
  705. }
  706. }
  707. if (AlternateForm && ExplicitFormat != ExpressionFormat::Kind::HexLower &&
  708. ExplicitFormat != ExpressionFormat::Kind::HexUpper)
  709. return ErrorDiagnostic::get(
  710. SM, AlternateFormFlagLoc,
  711. "alternate form only supported for hex values");
  712. FormatExpr = FormatExpr.ltrim(SpaceChars);
  713. if (!FormatExpr.empty())
  714. return ErrorDiagnostic::get(
  715. SM, FormatExpr,
  716. "invalid matching format specification in expression");
  717. }
  718. // Save variable definition expression if any.
  719. size_t DefEnd = Expr.find(':');
  720. if (DefEnd != StringRef::npos) {
  721. DefExpr = Expr.substr(0, DefEnd);
  722. Expr = Expr.substr(DefEnd + 1);
  723. }
  724. // Parse matching constraint.
  725. Expr = Expr.ltrim(SpaceChars);
  726. bool HasParsedValidConstraint = false;
  727. if (Expr.consume_front("=="))
  728. HasParsedValidConstraint = true;
  729. // Parse the expression itself.
  730. Expr = Expr.ltrim(SpaceChars);
  731. if (Expr.empty()) {
  732. if (HasParsedValidConstraint)
  733. return ErrorDiagnostic::get(
  734. SM, Expr, "empty numeric expression should not have a constraint");
  735. } else {
  736. Expr = Expr.rtrim(SpaceChars);
  737. StringRef OuterBinOpExpr = Expr;
  738. // The first operand in a legacy @LINE expression is always the @LINE
  739. // pseudo variable.
  740. AllowedOperand AO =
  741. IsLegacyLineExpr ? AllowedOperand::LineVar : AllowedOperand::Any;
  742. Expected<std::unique_ptr<ExpressionAST>> ParseResult = parseNumericOperand(
  743. Expr, AO, !HasParsedValidConstraint, LineNumber, Context, SM);
  744. while (ParseResult && !Expr.empty()) {
  745. ParseResult = parseBinop(OuterBinOpExpr, Expr, std::move(*ParseResult),
  746. IsLegacyLineExpr, LineNumber, Context, SM);
  747. // Legacy @LINE expressions only allow 2 operands.
  748. if (ParseResult && IsLegacyLineExpr && !Expr.empty())
  749. return ErrorDiagnostic::get(
  750. SM, Expr,
  751. "unexpected characters at end of expression '" + Expr + "'");
  752. }
  753. if (!ParseResult)
  754. return ParseResult.takeError();
  755. ExpressionASTPointer = std::move(*ParseResult);
  756. }
  757. // Select format of the expression, i.e. (i) its explicit format, if any,
  758. // otherwise (ii) its implicit format, if any, otherwise (iii) the default
  759. // format (unsigned). Error out in case of conflicting implicit format
  760. // without explicit format.
  761. ExpressionFormat Format;
  762. if (ExplicitFormat)
  763. Format = ExplicitFormat;
  764. else if (ExpressionASTPointer) {
  765. Expected<ExpressionFormat> ImplicitFormat =
  766. ExpressionASTPointer->getImplicitFormat(SM);
  767. if (!ImplicitFormat)
  768. return ImplicitFormat.takeError();
  769. Format = *ImplicitFormat;
  770. }
  771. if (!Format)
  772. Format = ExpressionFormat(ExpressionFormat::Kind::Unsigned, Precision);
  773. std::unique_ptr<Expression> ExpressionPointer =
  774. std::make_unique<Expression>(std::move(ExpressionASTPointer), Format);
  775. // Parse the numeric variable definition.
  776. if (DefEnd != StringRef::npos) {
  777. DefExpr = DefExpr.ltrim(SpaceChars);
  778. Expected<NumericVariable *> ParseResult = parseNumericVariableDefinition(
  779. DefExpr, Context, LineNumber, ExpressionPointer->getFormat(), SM);
  780. if (!ParseResult)
  781. return ParseResult.takeError();
  782. DefinedNumericVariable = *ParseResult;
  783. }
  784. return std::move(ExpressionPointer);
  785. }
  786. bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix,
  787. SourceMgr &SM, const FileCheckRequest &Req) {
  788. bool MatchFullLinesHere = Req.MatchFullLines && CheckTy != Check::CheckNot;
  789. IgnoreCase = Req.IgnoreCase;
  790. PatternLoc = SMLoc::getFromPointer(PatternStr.data());
  791. if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
  792. // Ignore trailing whitespace.
  793. while (!PatternStr.empty() &&
  794. (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
  795. PatternStr = PatternStr.substr(0, PatternStr.size() - 1);
  796. // Check that there is something on the line.
  797. if (PatternStr.empty() && CheckTy != Check::CheckEmpty) {
  798. SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
  799. "found empty check string with prefix '" + Prefix + ":'");
  800. return true;
  801. }
  802. if (!PatternStr.empty() && CheckTy == Check::CheckEmpty) {
  803. SM.PrintMessage(
  804. PatternLoc, SourceMgr::DK_Error,
  805. "found non-empty check string for empty check with prefix '" + Prefix +
  806. ":'");
  807. return true;
  808. }
  809. if (CheckTy == Check::CheckEmpty) {
  810. RegExStr = "(\n$)";
  811. return false;
  812. }
  813. // If literal check, set fixed string.
  814. if (CheckTy.isLiteralMatch()) {
  815. FixedStr = PatternStr;
  816. return false;
  817. }
  818. // Check to see if this is a fixed string, or if it has regex pieces.
  819. if (!MatchFullLinesHere &&
  820. (PatternStr.size() < 2 ||
  821. (!PatternStr.contains("{{") && !PatternStr.contains("[[")))) {
  822. FixedStr = PatternStr;
  823. return false;
  824. }
  825. if (MatchFullLinesHere) {
  826. RegExStr += '^';
  827. if (!Req.NoCanonicalizeWhiteSpace)
  828. RegExStr += " *";
  829. }
  830. // Paren value #0 is for the fully matched string. Any new parenthesized
  831. // values add from there.
  832. unsigned CurParen = 1;
  833. // Otherwise, there is at least one regex piece. Build up the regex pattern
  834. // by escaping scary characters in fixed strings, building up one big regex.
  835. while (!PatternStr.empty()) {
  836. // RegEx matches.
  837. if (PatternStr.startswith("{{")) {
  838. // This is the start of a regex match. Scan for the }}.
  839. size_t End = PatternStr.find("}}");
  840. if (End == StringRef::npos) {
  841. SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
  842. SourceMgr::DK_Error,
  843. "found start of regex string with no end '}}'");
  844. return true;
  845. }
  846. // Enclose {{}} patterns in parens just like [[]] even though we're not
  847. // capturing the result for any purpose. This is required in case the
  848. // expression contains an alternation like: CHECK: abc{{x|z}}def. We
  849. // want this to turn into: "abc(x|z)def" not "abcx|zdef".
  850. RegExStr += '(';
  851. ++CurParen;
  852. if (AddRegExToRegEx(PatternStr.substr(2, End - 2), CurParen, SM))
  853. return true;
  854. RegExStr += ')';
  855. PatternStr = PatternStr.substr(End + 2);
  856. continue;
  857. }
  858. // String and numeric substitution blocks. Pattern substitution blocks come
  859. // in two forms: [[foo:.*]] and [[foo]]. The former matches .* (or some
  860. // other regex) and assigns it to the string variable 'foo'. The latter
  861. // substitutes foo's value. Numeric substitution blocks recognize the same
  862. // form as string ones, but start with a '#' sign after the double
  863. // brackets. They also accept a combined form which sets a numeric variable
  864. // to the evaluation of an expression. Both string and numeric variable
  865. // names must satisfy the regular expression "[a-zA-Z_][0-9a-zA-Z_]*" to be
  866. // valid, as this helps catch some common errors. If there are extra '['s
  867. // before the "[[", treat them literally.
  868. if (PatternStr.startswith("[[") && !PatternStr.startswith("[[[")) {
  869. StringRef UnparsedPatternStr = PatternStr.substr(2);
  870. // Find the closing bracket pair ending the match. End is going to be an
  871. // offset relative to the beginning of the match string.
  872. size_t End = FindRegexVarEnd(UnparsedPatternStr, SM);
  873. StringRef MatchStr = UnparsedPatternStr.substr(0, End);
  874. bool IsNumBlock = MatchStr.consume_front("#");
  875. if (End == StringRef::npos) {
  876. SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
  877. SourceMgr::DK_Error,
  878. "Invalid substitution block, no ]] found");
  879. return true;
  880. }
  881. // Strip the substitution block we are parsing. End points to the start
  882. // of the "]]" closing the expression so account for it in computing the
  883. // index of the first unparsed character.
  884. PatternStr = UnparsedPatternStr.substr(End + 2);
  885. bool IsDefinition = false;
  886. bool SubstNeeded = false;
  887. // Whether the substitution block is a legacy use of @LINE with string
  888. // substitution block syntax.
  889. bool IsLegacyLineExpr = false;
  890. StringRef DefName;
  891. StringRef SubstStr;
  892. StringRef MatchRegexp;
  893. std::string WildcardRegexp;
  894. size_t SubstInsertIdx = RegExStr.size();
  895. // Parse string variable or legacy @LINE expression.
  896. if (!IsNumBlock) {
  897. size_t VarEndIdx = MatchStr.find(':');
  898. size_t SpacePos = MatchStr.substr(0, VarEndIdx).find_first_of(" \t");
  899. if (SpacePos != StringRef::npos) {
  900. SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data() + SpacePos),
  901. SourceMgr::DK_Error, "unexpected whitespace");
  902. return true;
  903. }
  904. // Get the name (e.g. "foo") and verify it is well formed.
  905. StringRef OrigMatchStr = MatchStr;
  906. Expected<Pattern::VariableProperties> ParseVarResult =
  907. parseVariable(MatchStr, SM);
  908. if (!ParseVarResult) {
  909. logAllUnhandledErrors(ParseVarResult.takeError(), errs());
  910. return true;
  911. }
  912. StringRef Name = ParseVarResult->Name;
  913. bool IsPseudo = ParseVarResult->IsPseudo;
  914. IsDefinition = (VarEndIdx != StringRef::npos);
  915. SubstNeeded = !IsDefinition;
  916. if (IsDefinition) {
  917. if ((IsPseudo || !MatchStr.consume_front(":"))) {
  918. SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
  919. SourceMgr::DK_Error,
  920. "invalid name in string variable definition");
  921. return true;
  922. }
  923. // Detect collisions between string and numeric variables when the
  924. // former is created later than the latter.
  925. if (Context->GlobalNumericVariableTable.find(Name) !=
  926. Context->GlobalNumericVariableTable.end()) {
  927. SM.PrintMessage(
  928. SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
  929. "numeric variable with name '" + Name + "' already exists");
  930. return true;
  931. }
  932. DefName = Name;
  933. MatchRegexp = MatchStr;
  934. } else {
  935. if (IsPseudo) {
  936. MatchStr = OrigMatchStr;
  937. IsLegacyLineExpr = IsNumBlock = true;
  938. } else {
  939. if (!MatchStr.empty()) {
  940. SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
  941. SourceMgr::DK_Error,
  942. "invalid name in string variable use");
  943. return true;
  944. }
  945. SubstStr = Name;
  946. }
  947. }
  948. }
  949. // Parse numeric substitution block.
  950. std::unique_ptr<Expression> ExpressionPointer;
  951. std::optional<NumericVariable *> DefinedNumericVariable;
  952. if (IsNumBlock) {
  953. Expected<std::unique_ptr<Expression>> ParseResult =
  954. parseNumericSubstitutionBlock(MatchStr, DefinedNumericVariable,
  955. IsLegacyLineExpr, LineNumber, Context,
  956. SM);
  957. if (!ParseResult) {
  958. logAllUnhandledErrors(ParseResult.takeError(), errs());
  959. return true;
  960. }
  961. ExpressionPointer = std::move(*ParseResult);
  962. SubstNeeded = ExpressionPointer->getAST() != nullptr;
  963. if (DefinedNumericVariable) {
  964. IsDefinition = true;
  965. DefName = (*DefinedNumericVariable)->getName();
  966. }
  967. if (SubstNeeded)
  968. SubstStr = MatchStr;
  969. else {
  970. ExpressionFormat Format = ExpressionPointer->getFormat();
  971. WildcardRegexp = cantFail(Format.getWildcardRegex());
  972. MatchRegexp = WildcardRegexp;
  973. }
  974. }
  975. // Handle variable definition: [[<def>:(...)]] and [[#(...)<def>:(...)]].
  976. if (IsDefinition) {
  977. RegExStr += '(';
  978. ++SubstInsertIdx;
  979. if (IsNumBlock) {
  980. NumericVariableMatch NumericVariableDefinition = {
  981. *DefinedNumericVariable, CurParen};
  982. NumericVariableDefs[DefName] = NumericVariableDefinition;
  983. // This store is done here rather than in match() to allow
  984. // parseNumericVariableUse() to get the pointer to the class instance
  985. // of the right variable definition corresponding to a given numeric
  986. // variable use.
  987. Context->GlobalNumericVariableTable[DefName] =
  988. *DefinedNumericVariable;
  989. } else {
  990. VariableDefs[DefName] = CurParen;
  991. // Mark string variable as defined to detect collisions between
  992. // string and numeric variables in parseNumericVariableUse() and
  993. // defineCmdlineVariables() when the latter is created later than the
  994. // former. We cannot reuse GlobalVariableTable for this by populating
  995. // it with an empty string since we would then lose the ability to
  996. // detect the use of an undefined variable in match().
  997. Context->DefinedVariableTable[DefName] = true;
  998. }
  999. ++CurParen;
  1000. }
  1001. if (!MatchRegexp.empty() && AddRegExToRegEx(MatchRegexp, CurParen, SM))
  1002. return true;
  1003. if (IsDefinition)
  1004. RegExStr += ')';
  1005. // Handle substitutions: [[foo]] and [[#<foo expr>]].
  1006. if (SubstNeeded) {
  1007. // Handle substitution of string variables that were defined earlier on
  1008. // the same line by emitting a backreference. Expressions do not
  1009. // support substituting a numeric variable defined on the same line.
  1010. if (!IsNumBlock && VariableDefs.find(SubstStr) != VariableDefs.end()) {
  1011. unsigned CaptureParenGroup = VariableDefs[SubstStr];
  1012. if (CaptureParenGroup < 1 || CaptureParenGroup > 9) {
  1013. SM.PrintMessage(SMLoc::getFromPointer(SubstStr.data()),
  1014. SourceMgr::DK_Error,
  1015. "Can't back-reference more than 9 variables");
  1016. return true;
  1017. }
  1018. AddBackrefToRegEx(CaptureParenGroup);
  1019. } else {
  1020. // Handle substitution of string variables ([[<var>]]) defined in
  1021. // previous CHECK patterns, and substitution of expressions.
  1022. Substitution *Substitution =
  1023. IsNumBlock
  1024. ? Context->makeNumericSubstitution(
  1025. SubstStr, std::move(ExpressionPointer), SubstInsertIdx)
  1026. : Context->makeStringSubstitution(SubstStr, SubstInsertIdx);
  1027. Substitutions.push_back(Substitution);
  1028. }
  1029. }
  1030. continue;
  1031. }
  1032. // Handle fixed string matches.
  1033. // Find the end, which is the start of the next regex.
  1034. size_t FixedMatchEnd =
  1035. std::min(PatternStr.find("{{", 1), PatternStr.find("[[", 1));
  1036. RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd));
  1037. PatternStr = PatternStr.substr(FixedMatchEnd);
  1038. }
  1039. if (MatchFullLinesHere) {
  1040. if (!Req.NoCanonicalizeWhiteSpace)
  1041. RegExStr += " *";
  1042. RegExStr += '$';
  1043. }
  1044. return false;
  1045. }
  1046. bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) {
  1047. Regex R(RS);
  1048. std::string Error;
  1049. if (!R.isValid(Error)) {
  1050. SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error,
  1051. "invalid regex: " + Error);
  1052. return true;
  1053. }
  1054. RegExStr += RS.str();
  1055. CurParen += R.getNumMatches();
  1056. return false;
  1057. }
  1058. void Pattern::AddBackrefToRegEx(unsigned BackrefNum) {
  1059. assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
  1060. std::string Backref = std::string("\\") + std::string(1, '0' + BackrefNum);
  1061. RegExStr += Backref;
  1062. }
  1063. Pattern::MatchResult Pattern::match(StringRef Buffer,
  1064. const SourceMgr &SM) const {
  1065. // If this is the EOF pattern, match it immediately.
  1066. if (CheckTy == Check::CheckEOF)
  1067. return MatchResult(Buffer.size(), 0, Error::success());
  1068. // If this is a fixed string pattern, just match it now.
  1069. if (!FixedStr.empty()) {
  1070. size_t Pos =
  1071. IgnoreCase ? Buffer.find_insensitive(FixedStr) : Buffer.find(FixedStr);
  1072. if (Pos == StringRef::npos)
  1073. return make_error<NotFoundError>();
  1074. return MatchResult(Pos, /*MatchLen=*/FixedStr.size(), Error::success());
  1075. }
  1076. // Regex match.
  1077. // If there are substitutions, we need to create a temporary string with the
  1078. // actual value.
  1079. StringRef RegExToMatch = RegExStr;
  1080. std::string TmpStr;
  1081. if (!Substitutions.empty()) {
  1082. TmpStr = RegExStr;
  1083. if (LineNumber)
  1084. Context->LineVariable->setValue(ExpressionValue(*LineNumber));
  1085. size_t InsertOffset = 0;
  1086. // Substitute all string variables and expressions whose values are only
  1087. // now known. Use of string variables defined on the same line are handled
  1088. // by back-references.
  1089. Error Errs = Error::success();
  1090. for (const auto &Substitution : Substitutions) {
  1091. // Substitute and check for failure (e.g. use of undefined variable).
  1092. Expected<std::string> Value = Substitution->getResult();
  1093. if (!Value) {
  1094. // Convert to an ErrorDiagnostic to get location information. This is
  1095. // done here rather than printMatch/printNoMatch since now we know which
  1096. // substitution block caused the overflow.
  1097. Errs = joinErrors(std::move(Errs),
  1098. handleErrors(
  1099. Value.takeError(),
  1100. [&](const OverflowError &E) {
  1101. return ErrorDiagnostic::get(
  1102. SM, Substitution->getFromString(),
  1103. "unable to substitute variable or "
  1104. "numeric expression: overflow error");
  1105. },
  1106. [&SM](const UndefVarError &E) {
  1107. return ErrorDiagnostic::get(SM, E.getVarName(),
  1108. E.message());
  1109. }));
  1110. continue;
  1111. }
  1112. // Plop it into the regex at the adjusted offset.
  1113. TmpStr.insert(TmpStr.begin() + Substitution->getIndex() + InsertOffset,
  1114. Value->begin(), Value->end());
  1115. InsertOffset += Value->size();
  1116. }
  1117. if (Errs)
  1118. return std::move(Errs);
  1119. // Match the newly constructed regex.
  1120. RegExToMatch = TmpStr;
  1121. }
  1122. SmallVector<StringRef, 4> MatchInfo;
  1123. unsigned int Flags = Regex::Newline;
  1124. if (IgnoreCase)
  1125. Flags |= Regex::IgnoreCase;
  1126. if (!Regex(RegExToMatch, Flags).match(Buffer, &MatchInfo))
  1127. return make_error<NotFoundError>();
  1128. // Successful regex match.
  1129. assert(!MatchInfo.empty() && "Didn't get any match");
  1130. StringRef FullMatch = MatchInfo[0];
  1131. // If this defines any string variables, remember their values.
  1132. for (const auto &VariableDef : VariableDefs) {
  1133. assert(VariableDef.second < MatchInfo.size() && "Internal paren error");
  1134. Context->GlobalVariableTable[VariableDef.first] =
  1135. MatchInfo[VariableDef.second];
  1136. }
  1137. // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after
  1138. // the required preceding newline, which is consumed by the pattern in the
  1139. // case of CHECK-EMPTY but not CHECK-NEXT.
  1140. size_t MatchStartSkip = CheckTy == Check::CheckEmpty;
  1141. Match TheMatch;
  1142. TheMatch.Pos = FullMatch.data() - Buffer.data() + MatchStartSkip;
  1143. TheMatch.Len = FullMatch.size() - MatchStartSkip;
  1144. // If this defines any numeric variables, remember their values.
  1145. for (const auto &NumericVariableDef : NumericVariableDefs) {
  1146. const NumericVariableMatch &NumericVariableMatch =
  1147. NumericVariableDef.getValue();
  1148. unsigned CaptureParenGroup = NumericVariableMatch.CaptureParenGroup;
  1149. assert(CaptureParenGroup < MatchInfo.size() && "Internal paren error");
  1150. NumericVariable *DefinedNumericVariable =
  1151. NumericVariableMatch.DefinedNumericVariable;
  1152. StringRef MatchedValue = MatchInfo[CaptureParenGroup];
  1153. ExpressionFormat Format = DefinedNumericVariable->getImplicitFormat();
  1154. Expected<ExpressionValue> Value =
  1155. Format.valueFromStringRepr(MatchedValue, SM);
  1156. if (!Value)
  1157. return MatchResult(TheMatch, Value.takeError());
  1158. DefinedNumericVariable->setValue(*Value, MatchedValue);
  1159. }
  1160. return MatchResult(TheMatch, Error::success());
  1161. }
  1162. unsigned Pattern::computeMatchDistance(StringRef Buffer) const {
  1163. // Just compute the number of matching characters. For regular expressions, we
  1164. // just compare against the regex itself and hope for the best.
  1165. //
  1166. // FIXME: One easy improvement here is have the regex lib generate a single
  1167. // example regular expression which matches, and use that as the example
  1168. // string.
  1169. StringRef ExampleString(FixedStr);
  1170. if (ExampleString.empty())
  1171. ExampleString = RegExStr;
  1172. // Only compare up to the first line in the buffer, or the string size.
  1173. StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
  1174. BufferPrefix = BufferPrefix.split('\n').first;
  1175. return BufferPrefix.edit_distance(ExampleString);
  1176. }
  1177. void Pattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer,
  1178. SMRange Range,
  1179. FileCheckDiag::MatchType MatchTy,
  1180. std::vector<FileCheckDiag> *Diags) const {
  1181. // Print what we know about substitutions.
  1182. if (!Substitutions.empty()) {
  1183. for (const auto &Substitution : Substitutions) {
  1184. SmallString<256> Msg;
  1185. raw_svector_ostream OS(Msg);
  1186. Expected<std::string> MatchedValue = Substitution->getResult();
  1187. // Substitution failures are handled in printNoMatch().
  1188. if (!MatchedValue) {
  1189. consumeError(MatchedValue.takeError());
  1190. continue;
  1191. }
  1192. OS << "with \"";
  1193. OS.write_escaped(Substitution->getFromString()) << "\" equal to \"";
  1194. OS.write_escaped(*MatchedValue) << "\"";
  1195. // We report only the start of the match/search range to suggest we are
  1196. // reporting the substitutions as set at the start of the match/search.
  1197. // Indicating a non-zero-length range might instead seem to imply that the
  1198. // substitution matches or was captured from exactly that range.
  1199. if (Diags)
  1200. Diags->emplace_back(SM, CheckTy, getLoc(), MatchTy,
  1201. SMRange(Range.Start, Range.Start), OS.str());
  1202. else
  1203. SM.PrintMessage(Range.Start, SourceMgr::DK_Note, OS.str());
  1204. }
  1205. }
  1206. }
  1207. void Pattern::printVariableDefs(const SourceMgr &SM,
  1208. FileCheckDiag::MatchType MatchTy,
  1209. std::vector<FileCheckDiag> *Diags) const {
  1210. if (VariableDefs.empty() && NumericVariableDefs.empty())
  1211. return;
  1212. // Build list of variable captures.
  1213. struct VarCapture {
  1214. StringRef Name;
  1215. SMRange Range;
  1216. };
  1217. SmallVector<VarCapture, 2> VarCaptures;
  1218. for (const auto &VariableDef : VariableDefs) {
  1219. VarCapture VC;
  1220. VC.Name = VariableDef.first;
  1221. StringRef Value = Context->GlobalVariableTable[VC.Name];
  1222. SMLoc Start = SMLoc::getFromPointer(Value.data());
  1223. SMLoc End = SMLoc::getFromPointer(Value.data() + Value.size());
  1224. VC.Range = SMRange(Start, End);
  1225. VarCaptures.push_back(VC);
  1226. }
  1227. for (const auto &VariableDef : NumericVariableDefs) {
  1228. VarCapture VC;
  1229. VC.Name = VariableDef.getKey();
  1230. std::optional<StringRef> StrValue =
  1231. VariableDef.getValue().DefinedNumericVariable->getStringValue();
  1232. if (!StrValue)
  1233. continue;
  1234. SMLoc Start = SMLoc::getFromPointer(StrValue->data());
  1235. SMLoc End = SMLoc::getFromPointer(StrValue->data() + StrValue->size());
  1236. VC.Range = SMRange(Start, End);
  1237. VarCaptures.push_back(VC);
  1238. }
  1239. // Sort variable captures by the order in which they matched the input.
  1240. // Ranges shouldn't be overlapping, so we can just compare the start.
  1241. llvm::sort(VarCaptures, [](const VarCapture &A, const VarCapture &B) {
  1242. if (&A == &B)
  1243. return false;
  1244. assert(A.Range.Start != B.Range.Start &&
  1245. "unexpected overlapping variable captures");
  1246. return A.Range.Start.getPointer() < B.Range.Start.getPointer();
  1247. });
  1248. // Create notes for the sorted captures.
  1249. for (const VarCapture &VC : VarCaptures) {
  1250. SmallString<256> Msg;
  1251. raw_svector_ostream OS(Msg);
  1252. OS << "captured var \"" << VC.Name << "\"";
  1253. if (Diags)
  1254. Diags->emplace_back(SM, CheckTy, getLoc(), MatchTy, VC.Range, OS.str());
  1255. else
  1256. SM.PrintMessage(VC.Range.Start, SourceMgr::DK_Note, OS.str(), VC.Range);
  1257. }
  1258. }
  1259. static SMRange ProcessMatchResult(FileCheckDiag::MatchType MatchTy,
  1260. const SourceMgr &SM, SMLoc Loc,
  1261. Check::FileCheckType CheckTy,
  1262. StringRef Buffer, size_t Pos, size_t Len,
  1263. std::vector<FileCheckDiag> *Diags,
  1264. bool AdjustPrevDiags = false) {
  1265. SMLoc Start = SMLoc::getFromPointer(Buffer.data() + Pos);
  1266. SMLoc End = SMLoc::getFromPointer(Buffer.data() + Pos + Len);
  1267. SMRange Range(Start, End);
  1268. if (Diags) {
  1269. if (AdjustPrevDiags) {
  1270. SMLoc CheckLoc = Diags->rbegin()->CheckLoc;
  1271. for (auto I = Diags->rbegin(), E = Diags->rend();
  1272. I != E && I->CheckLoc == CheckLoc; ++I)
  1273. I->MatchTy = MatchTy;
  1274. } else
  1275. Diags->emplace_back(SM, CheckTy, Loc, MatchTy, Range);
  1276. }
  1277. return Range;
  1278. }
  1279. void Pattern::printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
  1280. std::vector<FileCheckDiag> *Diags) const {
  1281. // Attempt to find the closest/best fuzzy match. Usually an error happens
  1282. // because some string in the output didn't exactly match. In these cases, we
  1283. // would like to show the user a best guess at what "should have" matched, to
  1284. // save them having to actually check the input manually.
  1285. size_t NumLinesForward = 0;
  1286. size_t Best = StringRef::npos;
  1287. double BestQuality = 0;
  1288. // Use an arbitrary 4k limit on how far we will search.
  1289. for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
  1290. if (Buffer[i] == '\n')
  1291. ++NumLinesForward;
  1292. // Patterns have leading whitespace stripped, so skip whitespace when
  1293. // looking for something which looks like a pattern.
  1294. if (Buffer[i] == ' ' || Buffer[i] == '\t')
  1295. continue;
  1296. // Compute the "quality" of this match as an arbitrary combination of the
  1297. // match distance and the number of lines skipped to get to this match.
  1298. unsigned Distance = computeMatchDistance(Buffer.substr(i));
  1299. double Quality = Distance + (NumLinesForward / 100.);
  1300. if (Quality < BestQuality || Best == StringRef::npos) {
  1301. Best = i;
  1302. BestQuality = Quality;
  1303. }
  1304. }
  1305. // Print the "possible intended match here" line if we found something
  1306. // reasonable and not equal to what we showed in the "scanning from here"
  1307. // line.
  1308. if (Best && Best != StringRef::npos && BestQuality < 50) {
  1309. SMRange MatchRange =
  1310. ProcessMatchResult(FileCheckDiag::MatchFuzzy, SM, getLoc(),
  1311. getCheckTy(), Buffer, Best, 0, Diags);
  1312. SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note,
  1313. "possible intended match here");
  1314. // FIXME: If we wanted to be really friendly we would show why the match
  1315. // failed, as it can be hard to spot simple one character differences.
  1316. }
  1317. }
  1318. Expected<StringRef>
  1319. FileCheckPatternContext::getPatternVarValue(StringRef VarName) {
  1320. auto VarIter = GlobalVariableTable.find(VarName);
  1321. if (VarIter == GlobalVariableTable.end())
  1322. return make_error<UndefVarError>(VarName);
  1323. return VarIter->second;
  1324. }
  1325. template <class... Types>
  1326. NumericVariable *FileCheckPatternContext::makeNumericVariable(Types... args) {
  1327. NumericVariables.push_back(std::make_unique<NumericVariable>(args...));
  1328. return NumericVariables.back().get();
  1329. }
  1330. Substitution *
  1331. FileCheckPatternContext::makeStringSubstitution(StringRef VarName,
  1332. size_t InsertIdx) {
  1333. Substitutions.push_back(
  1334. std::make_unique<StringSubstitution>(this, VarName, InsertIdx));
  1335. return Substitutions.back().get();
  1336. }
  1337. Substitution *FileCheckPatternContext::makeNumericSubstitution(
  1338. StringRef ExpressionStr, std::unique_ptr<Expression> Expression,
  1339. size_t InsertIdx) {
  1340. Substitutions.push_back(std::make_unique<NumericSubstitution>(
  1341. this, ExpressionStr, std::move(Expression), InsertIdx));
  1342. return Substitutions.back().get();
  1343. }
  1344. size_t Pattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) {
  1345. // Offset keeps track of the current offset within the input Str
  1346. size_t Offset = 0;
  1347. // [...] Nesting depth
  1348. size_t BracketDepth = 0;
  1349. while (!Str.empty()) {
  1350. if (Str.startswith("]]") && BracketDepth == 0)
  1351. return Offset;
  1352. if (Str[0] == '\\') {
  1353. // Backslash escapes the next char within regexes, so skip them both.
  1354. Str = Str.substr(2);
  1355. Offset += 2;
  1356. } else {
  1357. switch (Str[0]) {
  1358. default:
  1359. break;
  1360. case '[':
  1361. BracketDepth++;
  1362. break;
  1363. case ']':
  1364. if (BracketDepth == 0) {
  1365. SM.PrintMessage(SMLoc::getFromPointer(Str.data()),
  1366. SourceMgr::DK_Error,
  1367. "missing closing \"]\" for regex variable");
  1368. exit(1);
  1369. }
  1370. BracketDepth--;
  1371. break;
  1372. }
  1373. Str = Str.substr(1);
  1374. Offset++;
  1375. }
  1376. }
  1377. return StringRef::npos;
  1378. }
  1379. StringRef FileCheck::CanonicalizeFile(MemoryBuffer &MB,
  1380. SmallVectorImpl<char> &OutputBuffer) {
  1381. OutputBuffer.reserve(MB.getBufferSize());
  1382. for (const char *Ptr = MB.getBufferStart(), *End = MB.getBufferEnd();
  1383. Ptr != End; ++Ptr) {
  1384. // Eliminate trailing dosish \r.
  1385. if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
  1386. continue;
  1387. }
  1388. // If current char is not a horizontal whitespace or if horizontal
  1389. // whitespace canonicalization is disabled, dump it to output as is.
  1390. if (Req.NoCanonicalizeWhiteSpace || (*Ptr != ' ' && *Ptr != '\t')) {
  1391. OutputBuffer.push_back(*Ptr);
  1392. continue;
  1393. }
  1394. // Otherwise, add one space and advance over neighboring space.
  1395. OutputBuffer.push_back(' ');
  1396. while (Ptr + 1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t'))
  1397. ++Ptr;
  1398. }
  1399. // Add a null byte and then return all but that byte.
  1400. OutputBuffer.push_back('\0');
  1401. return StringRef(OutputBuffer.data(), OutputBuffer.size() - 1);
  1402. }
  1403. FileCheckDiag::FileCheckDiag(const SourceMgr &SM,
  1404. const Check::FileCheckType &CheckTy,
  1405. SMLoc CheckLoc, MatchType MatchTy,
  1406. SMRange InputRange, StringRef Note)
  1407. : CheckTy(CheckTy), CheckLoc(CheckLoc), MatchTy(MatchTy), Note(Note) {
  1408. auto Start = SM.getLineAndColumn(InputRange.Start);
  1409. auto End = SM.getLineAndColumn(InputRange.End);
  1410. InputStartLine = Start.first;
  1411. InputStartCol = Start.second;
  1412. InputEndLine = End.first;
  1413. InputEndCol = End.second;
  1414. }
  1415. static bool IsPartOfWord(char c) {
  1416. return (isAlnum(c) || c == '-' || c == '_');
  1417. }
  1418. Check::FileCheckType &Check::FileCheckType::setCount(int C) {
  1419. assert(Count > 0 && "zero and negative counts are not supported");
  1420. assert((C == 1 || Kind == CheckPlain) &&
  1421. "count supported only for plain CHECK directives");
  1422. Count = C;
  1423. return *this;
  1424. }
  1425. std::string Check::FileCheckType::getModifiersDescription() const {
  1426. if (Modifiers.none())
  1427. return "";
  1428. std::string Ret;
  1429. raw_string_ostream OS(Ret);
  1430. OS << '{';
  1431. if (isLiteralMatch())
  1432. OS << "LITERAL";
  1433. OS << '}';
  1434. return OS.str();
  1435. }
  1436. std::string Check::FileCheckType::getDescription(StringRef Prefix) const {
  1437. // Append directive modifiers.
  1438. auto WithModifiers = [this, Prefix](StringRef Str) -> std::string {
  1439. return (Prefix + Str + getModifiersDescription()).str();
  1440. };
  1441. switch (Kind) {
  1442. case Check::CheckNone:
  1443. return "invalid";
  1444. case Check::CheckMisspelled:
  1445. return "misspelled";
  1446. case Check::CheckPlain:
  1447. if (Count > 1)
  1448. return WithModifiers("-COUNT");
  1449. return WithModifiers("");
  1450. case Check::CheckNext:
  1451. return WithModifiers("-NEXT");
  1452. case Check::CheckSame:
  1453. return WithModifiers("-SAME");
  1454. case Check::CheckNot:
  1455. return WithModifiers("-NOT");
  1456. case Check::CheckDAG:
  1457. return WithModifiers("-DAG");
  1458. case Check::CheckLabel:
  1459. return WithModifiers("-LABEL");
  1460. case Check::CheckEmpty:
  1461. return WithModifiers("-EMPTY");
  1462. case Check::CheckComment:
  1463. return std::string(Prefix);
  1464. case Check::CheckEOF:
  1465. return "implicit EOF";
  1466. case Check::CheckBadNot:
  1467. return "bad NOT";
  1468. case Check::CheckBadCount:
  1469. return "bad COUNT";
  1470. }
  1471. llvm_unreachable("unknown FileCheckType");
  1472. }
  1473. static std::pair<Check::FileCheckType, StringRef>
  1474. FindCheckType(const FileCheckRequest &Req, StringRef Buffer, StringRef Prefix,
  1475. bool &Misspelled) {
  1476. if (Buffer.size() <= Prefix.size())
  1477. return {Check::CheckNone, StringRef()};
  1478. StringRef Rest = Buffer.drop_front(Prefix.size());
  1479. // Check for comment.
  1480. if (llvm::is_contained(Req.CommentPrefixes, Prefix)) {
  1481. if (Rest.consume_front(":"))
  1482. return {Check::CheckComment, Rest};
  1483. // Ignore a comment prefix if it has a suffix like "-NOT".
  1484. return {Check::CheckNone, StringRef()};
  1485. }
  1486. auto ConsumeModifiers = [&](Check::FileCheckType Ret)
  1487. -> std::pair<Check::FileCheckType, StringRef> {
  1488. if (Rest.consume_front(":"))
  1489. return {Ret, Rest};
  1490. if (!Rest.consume_front("{"))
  1491. return {Check::CheckNone, StringRef()};
  1492. // Parse the modifiers, speparated by commas.
  1493. do {
  1494. // Allow whitespace in modifiers list.
  1495. Rest = Rest.ltrim();
  1496. if (Rest.consume_front("LITERAL"))
  1497. Ret.setLiteralMatch();
  1498. else
  1499. return {Check::CheckNone, Rest};
  1500. // Allow whitespace in modifiers list.
  1501. Rest = Rest.ltrim();
  1502. } while (Rest.consume_front(","));
  1503. if (!Rest.consume_front("}:"))
  1504. return {Check::CheckNone, Rest};
  1505. return {Ret, Rest};
  1506. };
  1507. // Verify that the prefix is followed by directive modifiers or a colon.
  1508. if (Rest.consume_front(":"))
  1509. return {Check::CheckPlain, Rest};
  1510. if (Rest.front() == '{')
  1511. return ConsumeModifiers(Check::CheckPlain);
  1512. if (Rest.consume_front("_"))
  1513. Misspelled = true;
  1514. else if (!Rest.consume_front("-"))
  1515. return {Check::CheckNone, StringRef()};
  1516. if (Rest.consume_front("COUNT-")) {
  1517. int64_t Count;
  1518. if (Rest.consumeInteger(10, Count))
  1519. // Error happened in parsing integer.
  1520. return {Check::CheckBadCount, Rest};
  1521. if (Count <= 0 || Count > INT32_MAX)
  1522. return {Check::CheckBadCount, Rest};
  1523. if (Rest.front() != ':' && Rest.front() != '{')
  1524. return {Check::CheckBadCount, Rest};
  1525. return ConsumeModifiers(
  1526. Check::FileCheckType(Check::CheckPlain).setCount(Count));
  1527. }
  1528. // You can't combine -NOT with another suffix.
  1529. if (Rest.startswith("DAG-NOT:") || Rest.startswith("NOT-DAG:") ||
  1530. Rest.startswith("NEXT-NOT:") || Rest.startswith("NOT-NEXT:") ||
  1531. Rest.startswith("SAME-NOT:") || Rest.startswith("NOT-SAME:") ||
  1532. Rest.startswith("EMPTY-NOT:") || Rest.startswith("NOT-EMPTY:"))
  1533. return {Check::CheckBadNot, Rest};
  1534. if (Rest.consume_front("NEXT"))
  1535. return ConsumeModifiers(Check::CheckNext);
  1536. if (Rest.consume_front("SAME"))
  1537. return ConsumeModifiers(Check::CheckSame);
  1538. if (Rest.consume_front("NOT"))
  1539. return ConsumeModifiers(Check::CheckNot);
  1540. if (Rest.consume_front("DAG"))
  1541. return ConsumeModifiers(Check::CheckDAG);
  1542. if (Rest.consume_front("LABEL"))
  1543. return ConsumeModifiers(Check::CheckLabel);
  1544. if (Rest.consume_front("EMPTY"))
  1545. return ConsumeModifiers(Check::CheckEmpty);
  1546. return {Check::CheckNone, Rest};
  1547. }
  1548. static std::pair<Check::FileCheckType, StringRef>
  1549. FindCheckType(const FileCheckRequest &Req, StringRef Buffer, StringRef Prefix) {
  1550. bool Misspelled = false;
  1551. auto Res = FindCheckType(Req, Buffer, Prefix, Misspelled);
  1552. if (Res.first != Check::CheckNone && Misspelled)
  1553. return {Check::CheckMisspelled, Res.second};
  1554. return Res;
  1555. }
  1556. // From the given position, find the next character after the word.
  1557. static size_t SkipWord(StringRef Str, size_t Loc) {
  1558. while (Loc < Str.size() && IsPartOfWord(Str[Loc]))
  1559. ++Loc;
  1560. return Loc;
  1561. }
  1562. /// Searches the buffer for the first prefix in the prefix regular expression.
  1563. ///
  1564. /// This searches the buffer using the provided regular expression, however it
  1565. /// enforces constraints beyond that:
  1566. /// 1) The found prefix must not be a suffix of something that looks like
  1567. /// a valid prefix.
  1568. /// 2) The found prefix must be followed by a valid check type suffix using \c
  1569. /// FindCheckType above.
  1570. ///
  1571. /// \returns a pair of StringRefs into the Buffer, which combines:
  1572. /// - the first match of the regular expression to satisfy these two is
  1573. /// returned,
  1574. /// otherwise an empty StringRef is returned to indicate failure.
  1575. /// - buffer rewound to the location right after parsed suffix, for parsing
  1576. /// to continue from
  1577. ///
  1578. /// If this routine returns a valid prefix, it will also shrink \p Buffer to
  1579. /// start at the beginning of the returned prefix, increment \p LineNumber for
  1580. /// each new line consumed from \p Buffer, and set \p CheckTy to the type of
  1581. /// check found by examining the suffix.
  1582. ///
  1583. /// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy
  1584. /// is unspecified.
  1585. static std::pair<StringRef, StringRef>
  1586. FindFirstMatchingPrefix(const FileCheckRequest &Req, Regex &PrefixRE,
  1587. StringRef &Buffer, unsigned &LineNumber,
  1588. Check::FileCheckType &CheckTy) {
  1589. SmallVector<StringRef, 2> Matches;
  1590. while (!Buffer.empty()) {
  1591. // Find the first (longest) match using the RE.
  1592. if (!PrefixRE.match(Buffer, &Matches))
  1593. // No match at all, bail.
  1594. return {StringRef(), StringRef()};
  1595. StringRef Prefix = Matches[0];
  1596. Matches.clear();
  1597. assert(Prefix.data() >= Buffer.data() &&
  1598. Prefix.data() < Buffer.data() + Buffer.size() &&
  1599. "Prefix doesn't start inside of buffer!");
  1600. size_t Loc = Prefix.data() - Buffer.data();
  1601. StringRef Skipped = Buffer.substr(0, Loc);
  1602. Buffer = Buffer.drop_front(Loc);
  1603. LineNumber += Skipped.count('\n');
  1604. // Check that the matched prefix isn't a suffix of some other check-like
  1605. // word.
  1606. // FIXME: This is a very ad-hoc check. it would be better handled in some
  1607. // other way. Among other things it seems hard to distinguish between
  1608. // intentional and unintentional uses of this feature.
  1609. if (Skipped.empty() || !IsPartOfWord(Skipped.back())) {
  1610. // Now extract the type.
  1611. StringRef AfterSuffix;
  1612. std::tie(CheckTy, AfterSuffix) = FindCheckType(Req, Buffer, Prefix);
  1613. // If we've found a valid check type for this prefix, we're done.
  1614. if (CheckTy != Check::CheckNone)
  1615. return {Prefix, AfterSuffix};
  1616. }
  1617. // If we didn't successfully find a prefix, we need to skip this invalid
  1618. // prefix and continue scanning. We directly skip the prefix that was
  1619. // matched and any additional parts of that check-like word.
  1620. Buffer = Buffer.drop_front(SkipWord(Buffer, Prefix.size()));
  1621. }
  1622. // We ran out of buffer while skipping partial matches so give up.
  1623. return {StringRef(), StringRef()};
  1624. }
  1625. void FileCheckPatternContext::createLineVariable() {
  1626. assert(!LineVariable && "@LINE pseudo numeric variable already created");
  1627. StringRef LineName = "@LINE";
  1628. LineVariable = makeNumericVariable(
  1629. LineName, ExpressionFormat(ExpressionFormat::Kind::Unsigned));
  1630. GlobalNumericVariableTable[LineName] = LineVariable;
  1631. }
  1632. FileCheck::FileCheck(FileCheckRequest Req)
  1633. : Req(Req), PatternContext(std::make_unique<FileCheckPatternContext>()),
  1634. CheckStrings(std::make_unique<std::vector<FileCheckString>>()) {}
  1635. FileCheck::~FileCheck() = default;
  1636. bool FileCheck::readCheckFile(
  1637. SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
  1638. std::pair<unsigned, unsigned> *ImpPatBufferIDRange) {
  1639. if (ImpPatBufferIDRange)
  1640. ImpPatBufferIDRange->first = ImpPatBufferIDRange->second = 0;
  1641. Error DefineError =
  1642. PatternContext->defineCmdlineVariables(Req.GlobalDefines, SM);
  1643. if (DefineError) {
  1644. logAllUnhandledErrors(std::move(DefineError), errs());
  1645. return true;
  1646. }
  1647. PatternContext->createLineVariable();
  1648. std::vector<Pattern> ImplicitNegativeChecks;
  1649. for (StringRef PatternString : Req.ImplicitCheckNot) {
  1650. // Create a buffer with fake command line content in order to display the
  1651. // command line option responsible for the specific implicit CHECK-NOT.
  1652. std::string Prefix = "-implicit-check-not='";
  1653. std::string Suffix = "'";
  1654. std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
  1655. (Prefix + PatternString + Suffix).str(), "command line");
  1656. StringRef PatternInBuffer =
  1657. CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
  1658. unsigned BufferID = SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
  1659. if (ImpPatBufferIDRange) {
  1660. if (ImpPatBufferIDRange->first == ImpPatBufferIDRange->second) {
  1661. ImpPatBufferIDRange->first = BufferID;
  1662. ImpPatBufferIDRange->second = BufferID + 1;
  1663. } else {
  1664. assert(BufferID == ImpPatBufferIDRange->second &&
  1665. "expected consecutive source buffer IDs");
  1666. ++ImpPatBufferIDRange->second;
  1667. }
  1668. }
  1669. ImplicitNegativeChecks.push_back(
  1670. Pattern(Check::CheckNot, PatternContext.get()));
  1671. ImplicitNegativeChecks.back().parsePattern(PatternInBuffer,
  1672. "IMPLICIT-CHECK", SM, Req);
  1673. }
  1674. std::vector<Pattern> DagNotMatches = ImplicitNegativeChecks;
  1675. // LineNumber keeps track of the line on which CheckPrefix instances are
  1676. // found.
  1677. unsigned LineNumber = 1;
  1678. std::set<StringRef> PrefixesNotFound(Req.CheckPrefixes.begin(),
  1679. Req.CheckPrefixes.end());
  1680. const size_t DistinctPrefixes = PrefixesNotFound.size();
  1681. while (true) {
  1682. Check::FileCheckType CheckTy;
  1683. // See if a prefix occurs in the memory buffer.
  1684. StringRef UsedPrefix;
  1685. StringRef AfterSuffix;
  1686. std::tie(UsedPrefix, AfterSuffix) =
  1687. FindFirstMatchingPrefix(Req, PrefixRE, Buffer, LineNumber, CheckTy);
  1688. if (UsedPrefix.empty())
  1689. break;
  1690. if (CheckTy != Check::CheckComment)
  1691. PrefixesNotFound.erase(UsedPrefix);
  1692. assert(UsedPrefix.data() == Buffer.data() &&
  1693. "Failed to move Buffer's start forward, or pointed prefix outside "
  1694. "of the buffer!");
  1695. assert(AfterSuffix.data() >= Buffer.data() &&
  1696. AfterSuffix.data() < Buffer.data() + Buffer.size() &&
  1697. "Parsing after suffix doesn't start inside of buffer!");
  1698. // Location to use for error messages.
  1699. const char *UsedPrefixStart = UsedPrefix.data();
  1700. // Skip the buffer to the end of parsed suffix (or just prefix, if no good
  1701. // suffix was processed).
  1702. Buffer = AfterSuffix.empty() ? Buffer.drop_front(UsedPrefix.size())
  1703. : AfterSuffix;
  1704. // Complain about misspelled directives.
  1705. if (CheckTy == Check::CheckMisspelled) {
  1706. StringRef UsedDirective(UsedPrefix.data(),
  1707. AfterSuffix.data() - UsedPrefix.data());
  1708. SM.PrintMessage(SMLoc::getFromPointer(UsedDirective.data()),
  1709. SourceMgr::DK_Error,
  1710. "misspelled directive '" + UsedDirective + "'");
  1711. return true;
  1712. }
  1713. // Complain about useful-looking but unsupported suffixes.
  1714. if (CheckTy == Check::CheckBadNot) {
  1715. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
  1716. "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
  1717. return true;
  1718. }
  1719. // Complain about invalid count specification.
  1720. if (CheckTy == Check::CheckBadCount) {
  1721. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
  1722. "invalid count in -COUNT specification on prefix '" +
  1723. UsedPrefix + "'");
  1724. return true;
  1725. }
  1726. // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
  1727. // leading whitespace.
  1728. if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
  1729. Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
  1730. // Scan ahead to the end of line.
  1731. size_t EOL = Buffer.find_first_of("\n\r");
  1732. // Remember the location of the start of the pattern, for diagnostics.
  1733. SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
  1734. // Extract the pattern from the buffer.
  1735. StringRef PatternBuffer = Buffer.substr(0, EOL);
  1736. Buffer = Buffer.substr(EOL);
  1737. // If this is a comment, we're done.
  1738. if (CheckTy == Check::CheckComment)
  1739. continue;
  1740. // Parse the pattern.
  1741. Pattern P(CheckTy, PatternContext.get(), LineNumber);
  1742. if (P.parsePattern(PatternBuffer, UsedPrefix, SM, Req))
  1743. return true;
  1744. // Verify that CHECK-LABEL lines do not define or use variables
  1745. if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
  1746. SM.PrintMessage(
  1747. SMLoc::getFromPointer(UsedPrefixStart), SourceMgr::DK_Error,
  1748. "found '" + UsedPrefix + "-LABEL:'"
  1749. " with variable definition or use");
  1750. return true;
  1751. }
  1752. // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
  1753. if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame ||
  1754. CheckTy == Check::CheckEmpty) &&
  1755. CheckStrings->empty()) {
  1756. StringRef Type = CheckTy == Check::CheckNext
  1757. ? "NEXT"
  1758. : CheckTy == Check::CheckEmpty ? "EMPTY" : "SAME";
  1759. SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
  1760. SourceMgr::DK_Error,
  1761. "found '" + UsedPrefix + "-" + Type +
  1762. "' without previous '" + UsedPrefix + ": line");
  1763. return true;
  1764. }
  1765. // Handle CHECK-DAG/-NOT.
  1766. if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
  1767. DagNotMatches.push_back(P);
  1768. continue;
  1769. }
  1770. // Okay, add the string we captured to the output vector and move on.
  1771. CheckStrings->emplace_back(P, UsedPrefix, PatternLoc);
  1772. std::swap(DagNotMatches, CheckStrings->back().DagNotStrings);
  1773. DagNotMatches = ImplicitNegativeChecks;
  1774. }
  1775. // When there are no used prefixes we report an error except in the case that
  1776. // no prefix is specified explicitly but -implicit-check-not is specified.
  1777. const bool NoPrefixesFound = PrefixesNotFound.size() == DistinctPrefixes;
  1778. const bool SomePrefixesUnexpectedlyNotUsed =
  1779. !Req.AllowUnusedPrefixes && !PrefixesNotFound.empty();
  1780. if ((NoPrefixesFound || SomePrefixesUnexpectedlyNotUsed) &&
  1781. (ImplicitNegativeChecks.empty() || !Req.IsDefaultCheckPrefix)) {
  1782. errs() << "error: no check strings found with prefix"
  1783. << (PrefixesNotFound.size() > 1 ? "es " : " ");
  1784. bool First = true;
  1785. for (StringRef MissingPrefix : PrefixesNotFound) {
  1786. if (!First)
  1787. errs() << ", ";
  1788. errs() << "\'" << MissingPrefix << ":'";
  1789. First = false;
  1790. }
  1791. errs() << '\n';
  1792. return true;
  1793. }
  1794. // Add an EOF pattern for any trailing --implicit-check-not/CHECK-DAG/-NOTs,
  1795. // and use the first prefix as a filler for the error message.
  1796. if (!DagNotMatches.empty()) {
  1797. CheckStrings->emplace_back(
  1798. Pattern(Check::CheckEOF, PatternContext.get(), LineNumber + 1),
  1799. *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data()));
  1800. std::swap(DagNotMatches, CheckStrings->back().DagNotStrings);
  1801. }
  1802. return false;
  1803. }
  1804. /// Returns either (1) \c ErrorSuccess if there was no error or (2)
  1805. /// \c ErrorReported if an error was reported, such as an unexpected match.
  1806. static Error printMatch(bool ExpectedMatch, const SourceMgr &SM,
  1807. StringRef Prefix, SMLoc Loc, const Pattern &Pat,
  1808. int MatchedCount, StringRef Buffer,
  1809. Pattern::MatchResult MatchResult,
  1810. const FileCheckRequest &Req,
  1811. std::vector<FileCheckDiag> *Diags) {
  1812. // Suppress some verbosity if there's no error.
  1813. bool HasError = !ExpectedMatch || MatchResult.TheError;
  1814. bool PrintDiag = true;
  1815. if (!HasError) {
  1816. if (!Req.Verbose)
  1817. return ErrorReported::reportedOrSuccess(HasError);
  1818. if (!Req.VerboseVerbose && Pat.getCheckTy() == Check::CheckEOF)
  1819. return ErrorReported::reportedOrSuccess(HasError);
  1820. // Due to their verbosity, we don't print verbose diagnostics here if we're
  1821. // gathering them for Diags to be rendered elsewhere, but we always print
  1822. // other diagnostics.
  1823. PrintDiag = !Diags;
  1824. }
  1825. // Add "found" diagnostic, substitutions, and variable definitions to Diags.
  1826. FileCheckDiag::MatchType MatchTy = ExpectedMatch
  1827. ? FileCheckDiag::MatchFoundAndExpected
  1828. : FileCheckDiag::MatchFoundButExcluded;
  1829. SMRange MatchRange = ProcessMatchResult(MatchTy, SM, Loc, Pat.getCheckTy(),
  1830. Buffer, MatchResult.TheMatch->Pos,
  1831. MatchResult.TheMatch->Len, Diags);
  1832. if (Diags) {
  1833. Pat.printSubstitutions(SM, Buffer, MatchRange, MatchTy, Diags);
  1834. Pat.printVariableDefs(SM, MatchTy, Diags);
  1835. }
  1836. if (!PrintDiag) {
  1837. assert(!HasError && "expected to report more diagnostics for error");
  1838. return ErrorReported::reportedOrSuccess(HasError);
  1839. }
  1840. // Print the match.
  1841. std::string Message = formatv("{0}: {1} string found in input",
  1842. Pat.getCheckTy().getDescription(Prefix),
  1843. (ExpectedMatch ? "expected" : "excluded"))
  1844. .str();
  1845. if (Pat.getCount() > 1)
  1846. Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
  1847. SM.PrintMessage(
  1848. Loc, ExpectedMatch ? SourceMgr::DK_Remark : SourceMgr::DK_Error, Message);
  1849. SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, "found here",
  1850. {MatchRange});
  1851. // Print additional information, which can be useful even if there are errors.
  1852. Pat.printSubstitutions(SM, Buffer, MatchRange, MatchTy, nullptr);
  1853. Pat.printVariableDefs(SM, MatchTy, nullptr);
  1854. // Print errors and add them to Diags. We report these errors after the match
  1855. // itself because we found them after the match. If we had found them before
  1856. // the match, we'd be in printNoMatch.
  1857. handleAllErrors(std::move(MatchResult.TheError),
  1858. [&](const ErrorDiagnostic &E) {
  1859. E.log(errs());
  1860. if (Diags) {
  1861. Diags->emplace_back(SM, Pat.getCheckTy(), Loc,
  1862. FileCheckDiag::MatchFoundErrorNote,
  1863. E.getRange(), E.getMessage().str());
  1864. }
  1865. });
  1866. return ErrorReported::reportedOrSuccess(HasError);
  1867. }
  1868. /// Returns either (1) \c ErrorSuccess if there was no error, or (2)
  1869. /// \c ErrorReported if an error was reported, such as an expected match not
  1870. /// found.
  1871. static Error printNoMatch(bool ExpectedMatch, const SourceMgr &SM,
  1872. StringRef Prefix, SMLoc Loc, const Pattern &Pat,
  1873. int MatchedCount, StringRef Buffer, Error MatchError,
  1874. bool VerboseVerbose,
  1875. std::vector<FileCheckDiag> *Diags) {
  1876. // Print any pattern errors, and record them to be added to Diags later.
  1877. bool HasError = ExpectedMatch;
  1878. bool HasPatternError = false;
  1879. FileCheckDiag::MatchType MatchTy = ExpectedMatch
  1880. ? FileCheckDiag::MatchNoneButExpected
  1881. : FileCheckDiag::MatchNoneAndExcluded;
  1882. SmallVector<std::string, 4> ErrorMsgs;
  1883. handleAllErrors(
  1884. std::move(MatchError),
  1885. [&](const ErrorDiagnostic &E) {
  1886. HasError = HasPatternError = true;
  1887. MatchTy = FileCheckDiag::MatchNoneForInvalidPattern;
  1888. E.log(errs());
  1889. if (Diags)
  1890. ErrorMsgs.push_back(E.getMessage().str());
  1891. },
  1892. // NotFoundError is why printNoMatch was invoked.
  1893. [](const NotFoundError &E) {});
  1894. // Suppress some verbosity if there's no error.
  1895. bool PrintDiag = true;
  1896. if (!HasError) {
  1897. if (!VerboseVerbose)
  1898. return ErrorReported::reportedOrSuccess(HasError);
  1899. // Due to their verbosity, we don't print verbose diagnostics here if we're
  1900. // gathering them for Diags to be rendered elsewhere, but we always print
  1901. // other diagnostics.
  1902. PrintDiag = !Diags;
  1903. }
  1904. // Add "not found" diagnostic, substitutions, and pattern errors to Diags.
  1905. //
  1906. // We handle Diags a little differently than the errors we print directly:
  1907. // we add the "not found" diagnostic to Diags even if there are pattern
  1908. // errors. The reason is that we need to attach pattern errors as notes
  1909. // somewhere in the input, and the input search range from the "not found"
  1910. // diagnostic is all we have to anchor them.
  1911. SMRange SearchRange = ProcessMatchResult(MatchTy, SM, Loc, Pat.getCheckTy(),
  1912. Buffer, 0, Buffer.size(), Diags);
  1913. if (Diags) {
  1914. SMRange NoteRange = SMRange(SearchRange.Start, SearchRange.Start);
  1915. for (StringRef ErrorMsg : ErrorMsgs)
  1916. Diags->emplace_back(SM, Pat.getCheckTy(), Loc, MatchTy, NoteRange,
  1917. ErrorMsg);
  1918. Pat.printSubstitutions(SM, Buffer, SearchRange, MatchTy, Diags);
  1919. }
  1920. if (!PrintDiag) {
  1921. assert(!HasError && "expected to report more diagnostics for error");
  1922. return ErrorReported::reportedOrSuccess(HasError);
  1923. }
  1924. // Print "not found" diagnostic, except that's implied if we already printed a
  1925. // pattern error.
  1926. if (!HasPatternError) {
  1927. std::string Message = formatv("{0}: {1} string not found in input",
  1928. Pat.getCheckTy().getDescription(Prefix),
  1929. (ExpectedMatch ? "expected" : "excluded"))
  1930. .str();
  1931. if (Pat.getCount() > 1)
  1932. Message +=
  1933. formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
  1934. SM.PrintMessage(Loc,
  1935. ExpectedMatch ? SourceMgr::DK_Error : SourceMgr::DK_Remark,
  1936. Message);
  1937. SM.PrintMessage(SearchRange.Start, SourceMgr::DK_Note,
  1938. "scanning from here");
  1939. }
  1940. // Print additional information, which can be useful even after a pattern
  1941. // error.
  1942. Pat.printSubstitutions(SM, Buffer, SearchRange, MatchTy, nullptr);
  1943. if (ExpectedMatch)
  1944. Pat.printFuzzyMatch(SM, Buffer, Diags);
  1945. return ErrorReported::reportedOrSuccess(HasError);
  1946. }
  1947. /// Returns either (1) \c ErrorSuccess if there was no error, or (2)
  1948. /// \c ErrorReported if an error was reported.
  1949. static Error reportMatchResult(bool ExpectedMatch, const SourceMgr &SM,
  1950. StringRef Prefix, SMLoc Loc, const Pattern &Pat,
  1951. int MatchedCount, StringRef Buffer,
  1952. Pattern::MatchResult MatchResult,
  1953. const FileCheckRequest &Req,
  1954. std::vector<FileCheckDiag> *Diags) {
  1955. if (MatchResult.TheMatch)
  1956. return printMatch(ExpectedMatch, SM, Prefix, Loc, Pat, MatchedCount, Buffer,
  1957. std::move(MatchResult), Req, Diags);
  1958. return printNoMatch(ExpectedMatch, SM, Prefix, Loc, Pat, MatchedCount, Buffer,
  1959. std::move(MatchResult.TheError), Req.VerboseVerbose,
  1960. Diags);
  1961. }
  1962. /// Counts the number of newlines in the specified range.
  1963. static unsigned CountNumNewlinesBetween(StringRef Range,
  1964. const char *&FirstNewLine) {
  1965. unsigned NumNewLines = 0;
  1966. while (true) {
  1967. // Scan for newline.
  1968. Range = Range.substr(Range.find_first_of("\n\r"));
  1969. if (Range.empty())
  1970. return NumNewLines;
  1971. ++NumNewLines;
  1972. // Handle \n\r and \r\n as a single newline.
  1973. if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') &&
  1974. (Range[0] != Range[1]))
  1975. Range = Range.substr(1);
  1976. Range = Range.substr(1);
  1977. if (NumNewLines == 1)
  1978. FirstNewLine = Range.begin();
  1979. }
  1980. }
  1981. size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
  1982. bool IsLabelScanMode, size_t &MatchLen,
  1983. FileCheckRequest &Req,
  1984. std::vector<FileCheckDiag> *Diags) const {
  1985. size_t LastPos = 0;
  1986. std::vector<const Pattern *> NotStrings;
  1987. // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
  1988. // bounds; we have not processed variable definitions within the bounded block
  1989. // yet so cannot handle any final CHECK-DAG yet; this is handled when going
  1990. // over the block again (including the last CHECK-LABEL) in normal mode.
  1991. if (!IsLabelScanMode) {
  1992. // Match "dag strings" (with mixed "not strings" if any).
  1993. LastPos = CheckDag(SM, Buffer, NotStrings, Req, Diags);
  1994. if (LastPos == StringRef::npos)
  1995. return StringRef::npos;
  1996. }
  1997. // Match itself from the last position after matching CHECK-DAG.
  1998. size_t LastMatchEnd = LastPos;
  1999. size_t FirstMatchPos = 0;
  2000. // Go match the pattern Count times. Majority of patterns only match with
  2001. // count 1 though.
  2002. assert(Pat.getCount() != 0 && "pattern count can not be zero");
  2003. for (int i = 1; i <= Pat.getCount(); i++) {
  2004. StringRef MatchBuffer = Buffer.substr(LastMatchEnd);
  2005. // get a match at current start point
  2006. Pattern::MatchResult MatchResult = Pat.match(MatchBuffer, SM);
  2007. // report
  2008. if (Error Err = reportMatchResult(/*ExpectedMatch=*/true, SM, Prefix, Loc,
  2009. Pat, i, MatchBuffer,
  2010. std::move(MatchResult), Req, Diags)) {
  2011. cantFail(handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
  2012. return StringRef::npos;
  2013. }
  2014. size_t MatchPos = MatchResult.TheMatch->Pos;
  2015. if (i == 1)
  2016. FirstMatchPos = LastPos + MatchPos;
  2017. // move start point after the match
  2018. LastMatchEnd += MatchPos + MatchResult.TheMatch->Len;
  2019. }
  2020. // Full match len counts from first match pos.
  2021. MatchLen = LastMatchEnd - FirstMatchPos;
  2022. // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
  2023. // or CHECK-NOT
  2024. if (!IsLabelScanMode) {
  2025. size_t MatchPos = FirstMatchPos - LastPos;
  2026. StringRef MatchBuffer = Buffer.substr(LastPos);
  2027. StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
  2028. // If this check is a "CHECK-NEXT", verify that the previous match was on
  2029. // the previous line (i.e. that there is one newline between them).
  2030. if (CheckNext(SM, SkippedRegion)) {
  2031. ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
  2032. Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
  2033. Diags, Req.Verbose);
  2034. return StringRef::npos;
  2035. }
  2036. // If this check is a "CHECK-SAME", verify that the previous match was on
  2037. // the same line (i.e. that there is no newline between them).
  2038. if (CheckSame(SM, SkippedRegion)) {
  2039. ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
  2040. Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
  2041. Diags, Req.Verbose);
  2042. return StringRef::npos;
  2043. }
  2044. // If this match had "not strings", verify that they don't exist in the
  2045. // skipped region.
  2046. if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
  2047. return StringRef::npos;
  2048. }
  2049. return FirstMatchPos;
  2050. }
  2051. bool FileCheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
  2052. if (Pat.getCheckTy() != Check::CheckNext &&
  2053. Pat.getCheckTy() != Check::CheckEmpty)
  2054. return false;
  2055. Twine CheckName =
  2056. Prefix +
  2057. Twine(Pat.getCheckTy() == Check::CheckEmpty ? "-EMPTY" : "-NEXT");
  2058. // Count the number of newlines between the previous match and this one.
  2059. const char *FirstNewLine = nullptr;
  2060. unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
  2061. if (NumNewLines == 0) {
  2062. SM.PrintMessage(Loc, SourceMgr::DK_Error,
  2063. CheckName + ": is on the same line as previous match");
  2064. SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
  2065. "'next' match was here");
  2066. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
  2067. "previous match ended here");
  2068. return true;
  2069. }
  2070. if (NumNewLines != 1) {
  2071. SM.PrintMessage(Loc, SourceMgr::DK_Error,
  2072. CheckName +
  2073. ": is not on the line after the previous match");
  2074. SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
  2075. "'next' match was here");
  2076. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
  2077. "previous match ended here");
  2078. SM.PrintMessage(SMLoc::getFromPointer(FirstNewLine), SourceMgr::DK_Note,
  2079. "non-matching line after previous match is here");
  2080. return true;
  2081. }
  2082. return false;
  2083. }
  2084. bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
  2085. if (Pat.getCheckTy() != Check::CheckSame)
  2086. return false;
  2087. // Count the number of newlines between the previous match and this one.
  2088. const char *FirstNewLine = nullptr;
  2089. unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
  2090. if (NumNewLines != 0) {
  2091. SM.PrintMessage(Loc, SourceMgr::DK_Error,
  2092. Prefix +
  2093. "-SAME: is not on the same line as the previous match");
  2094. SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
  2095. "'next' match was here");
  2096. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
  2097. "previous match ended here");
  2098. return true;
  2099. }
  2100. return false;
  2101. }
  2102. bool FileCheckString::CheckNot(const SourceMgr &SM, StringRef Buffer,
  2103. const std::vector<const Pattern *> &NotStrings,
  2104. const FileCheckRequest &Req,
  2105. std::vector<FileCheckDiag> *Diags) const {
  2106. bool DirectiveFail = false;
  2107. for (const Pattern *Pat : NotStrings) {
  2108. assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!");
  2109. Pattern::MatchResult MatchResult = Pat->match(Buffer, SM);
  2110. if (Error Err = reportMatchResult(/*ExpectedMatch=*/false, SM, Prefix,
  2111. Pat->getLoc(), *Pat, 1, Buffer,
  2112. std::move(MatchResult), Req, Diags)) {
  2113. cantFail(handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
  2114. DirectiveFail = true;
  2115. continue;
  2116. }
  2117. }
  2118. return DirectiveFail;
  2119. }
  2120. size_t FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
  2121. std::vector<const Pattern *> &NotStrings,
  2122. const FileCheckRequest &Req,
  2123. std::vector<FileCheckDiag> *Diags) const {
  2124. if (DagNotStrings.empty())
  2125. return 0;
  2126. // The start of the search range.
  2127. size_t StartPos = 0;
  2128. struct MatchRange {
  2129. size_t Pos;
  2130. size_t End;
  2131. };
  2132. // A sorted list of ranges for non-overlapping CHECK-DAG matches. Match
  2133. // ranges are erased from this list once they are no longer in the search
  2134. // range.
  2135. std::list<MatchRange> MatchRanges;
  2136. // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG
  2137. // group, so we don't use a range-based for loop here.
  2138. for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end();
  2139. PatItr != PatEnd; ++PatItr) {
  2140. const Pattern &Pat = *PatItr;
  2141. assert((Pat.getCheckTy() == Check::CheckDAG ||
  2142. Pat.getCheckTy() == Check::CheckNot) &&
  2143. "Invalid CHECK-DAG or CHECK-NOT!");
  2144. if (Pat.getCheckTy() == Check::CheckNot) {
  2145. NotStrings.push_back(&Pat);
  2146. continue;
  2147. }
  2148. assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
  2149. // CHECK-DAG always matches from the start.
  2150. size_t MatchLen = 0, MatchPos = StartPos;
  2151. // Search for a match that doesn't overlap a previous match in this
  2152. // CHECK-DAG group.
  2153. for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) {
  2154. StringRef MatchBuffer = Buffer.substr(MatchPos);
  2155. Pattern::MatchResult MatchResult = Pat.match(MatchBuffer, SM);
  2156. // With a group of CHECK-DAGs, a single mismatching means the match on
  2157. // that group of CHECK-DAGs fails immediately.
  2158. if (MatchResult.TheError || Req.VerboseVerbose) {
  2159. if (Error Err = reportMatchResult(/*ExpectedMatch=*/true, SM, Prefix,
  2160. Pat.getLoc(), Pat, 1, MatchBuffer,
  2161. std::move(MatchResult), Req, Diags)) {
  2162. cantFail(
  2163. handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
  2164. return StringRef::npos;
  2165. }
  2166. }
  2167. MatchLen = MatchResult.TheMatch->Len;
  2168. // Re-calc it as the offset relative to the start of the original
  2169. // string.
  2170. MatchPos += MatchResult.TheMatch->Pos;
  2171. MatchRange M{MatchPos, MatchPos + MatchLen};
  2172. if (Req.AllowDeprecatedDagOverlap) {
  2173. // We don't need to track all matches in this mode, so we just maintain
  2174. // one match range that encompasses the current CHECK-DAG group's
  2175. // matches.
  2176. if (MatchRanges.empty())
  2177. MatchRanges.insert(MatchRanges.end(), M);
  2178. else {
  2179. auto Block = MatchRanges.begin();
  2180. Block->Pos = std::min(Block->Pos, M.Pos);
  2181. Block->End = std::max(Block->End, M.End);
  2182. }
  2183. break;
  2184. }
  2185. // Iterate previous matches until overlapping match or insertion point.
  2186. bool Overlap = false;
  2187. for (; MI != ME; ++MI) {
  2188. if (M.Pos < MI->End) {
  2189. // !Overlap => New match has no overlap and is before this old match.
  2190. // Overlap => New match overlaps this old match.
  2191. Overlap = MI->Pos < M.End;
  2192. break;
  2193. }
  2194. }
  2195. if (!Overlap) {
  2196. // Insert non-overlapping match into list.
  2197. MatchRanges.insert(MI, M);
  2198. break;
  2199. }
  2200. if (Req.VerboseVerbose) {
  2201. // Due to their verbosity, we don't print verbose diagnostics here if
  2202. // we're gathering them for a different rendering, but we always print
  2203. // other diagnostics.
  2204. if (!Diags) {
  2205. SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos);
  2206. SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End);
  2207. SMRange OldRange(OldStart, OldEnd);
  2208. SM.PrintMessage(OldStart, SourceMgr::DK_Note,
  2209. "match discarded, overlaps earlier DAG match here",
  2210. {OldRange});
  2211. } else {
  2212. SMLoc CheckLoc = Diags->rbegin()->CheckLoc;
  2213. for (auto I = Diags->rbegin(), E = Diags->rend();
  2214. I != E && I->CheckLoc == CheckLoc; ++I)
  2215. I->MatchTy = FileCheckDiag::MatchFoundButDiscarded;
  2216. }
  2217. }
  2218. MatchPos = MI->End;
  2219. }
  2220. if (!Req.VerboseVerbose)
  2221. cantFail(printMatch(
  2222. /*ExpectedMatch=*/true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer,
  2223. Pattern::MatchResult(MatchPos, MatchLen, Error::success()), Req,
  2224. Diags));
  2225. // Handle the end of a CHECK-DAG group.
  2226. if (std::next(PatItr) == PatEnd ||
  2227. std::next(PatItr)->getCheckTy() == Check::CheckNot) {
  2228. if (!NotStrings.empty()) {
  2229. // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to
  2230. // CHECK-DAG, verify that there are no 'not' strings occurred in that
  2231. // region.
  2232. StringRef SkippedRegion =
  2233. Buffer.slice(StartPos, MatchRanges.begin()->Pos);
  2234. if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
  2235. return StringRef::npos;
  2236. // Clear "not strings".
  2237. NotStrings.clear();
  2238. }
  2239. // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the
  2240. // end of this CHECK-DAG group's match range.
  2241. StartPos = MatchRanges.rbegin()->End;
  2242. // Don't waste time checking for (impossible) overlaps before that.
  2243. MatchRanges.clear();
  2244. }
  2245. }
  2246. return StartPos;
  2247. }
  2248. static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes,
  2249. ArrayRef<StringRef> SuppliedPrefixes) {
  2250. for (StringRef Prefix : SuppliedPrefixes) {
  2251. if (Prefix.empty()) {
  2252. errs() << "error: supplied " << Kind << " prefix must not be the empty "
  2253. << "string\n";
  2254. return false;
  2255. }
  2256. static const Regex Validator("^[a-zA-Z0-9_-]*$");
  2257. if (!Validator.match(Prefix)) {
  2258. errs() << "error: supplied " << Kind << " prefix must start with a "
  2259. << "letter and contain only alphanumeric characters, hyphens, and "
  2260. << "underscores: '" << Prefix << "'\n";
  2261. return false;
  2262. }
  2263. if (!UniquePrefixes.insert(Prefix).second) {
  2264. errs() << "error: supplied " << Kind << " prefix must be unique among "
  2265. << "check and comment prefixes: '" << Prefix << "'\n";
  2266. return false;
  2267. }
  2268. }
  2269. return true;
  2270. }
  2271. static const char *DefaultCheckPrefixes[] = {"CHECK"};
  2272. static const char *DefaultCommentPrefixes[] = {"COM", "RUN"};
  2273. bool FileCheck::ValidateCheckPrefixes() {
  2274. StringSet<> UniquePrefixes;
  2275. // Add default prefixes to catch user-supplied duplicates of them below.
  2276. if (Req.CheckPrefixes.empty()) {
  2277. for (const char *Prefix : DefaultCheckPrefixes)
  2278. UniquePrefixes.insert(Prefix);
  2279. }
  2280. if (Req.CommentPrefixes.empty()) {
  2281. for (const char *Prefix : DefaultCommentPrefixes)
  2282. UniquePrefixes.insert(Prefix);
  2283. }
  2284. // Do not validate the default prefixes, or diagnostics about duplicates might
  2285. // incorrectly indicate that they were supplied by the user.
  2286. if (!ValidatePrefixes("check", UniquePrefixes, Req.CheckPrefixes))
  2287. return false;
  2288. if (!ValidatePrefixes("comment", UniquePrefixes, Req.CommentPrefixes))
  2289. return false;
  2290. return true;
  2291. }
  2292. Regex FileCheck::buildCheckPrefixRegex() {
  2293. if (Req.CheckPrefixes.empty()) {
  2294. for (const char *Prefix : DefaultCheckPrefixes)
  2295. Req.CheckPrefixes.push_back(Prefix);
  2296. Req.IsDefaultCheckPrefix = true;
  2297. }
  2298. if (Req.CommentPrefixes.empty()) {
  2299. for (const char *Prefix : DefaultCommentPrefixes)
  2300. Req.CommentPrefixes.push_back(Prefix);
  2301. }
  2302. // We already validated the contents of CheckPrefixes and CommentPrefixes so
  2303. // just concatenate them as alternatives.
  2304. SmallString<32> PrefixRegexStr;
  2305. for (size_t I = 0, E = Req.CheckPrefixes.size(); I != E; ++I) {
  2306. if (I != 0)
  2307. PrefixRegexStr.push_back('|');
  2308. PrefixRegexStr.append(Req.CheckPrefixes[I]);
  2309. }
  2310. for (StringRef Prefix : Req.CommentPrefixes) {
  2311. PrefixRegexStr.push_back('|');
  2312. PrefixRegexStr.append(Prefix);
  2313. }
  2314. return Regex(PrefixRegexStr);
  2315. }
  2316. Error FileCheckPatternContext::defineCmdlineVariables(
  2317. ArrayRef<StringRef> CmdlineDefines, SourceMgr &SM) {
  2318. assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() &&
  2319. "Overriding defined variable with command-line variable definitions");
  2320. if (CmdlineDefines.empty())
  2321. return Error::success();
  2322. // Create a string representing the vector of command-line definitions. Each
  2323. // definition is on its own line and prefixed with a definition number to
  2324. // clarify which definition a given diagnostic corresponds to.
  2325. unsigned I = 0;
  2326. Error Errs = Error::success();
  2327. std::string CmdlineDefsDiag;
  2328. SmallVector<std::pair<size_t, size_t>, 4> CmdlineDefsIndices;
  2329. for (StringRef CmdlineDef : CmdlineDefines) {
  2330. std::string DefPrefix = ("Global define #" + Twine(++I) + ": ").str();
  2331. size_t EqIdx = CmdlineDef.find('=');
  2332. if (EqIdx == StringRef::npos) {
  2333. CmdlineDefsIndices.push_back(std::make_pair(CmdlineDefsDiag.size(), 0));
  2334. continue;
  2335. }
  2336. // Numeric variable definition.
  2337. if (CmdlineDef[0] == '#') {
  2338. // Append a copy of the command-line definition adapted to use the same
  2339. // format as in the input file to be able to reuse
  2340. // parseNumericSubstitutionBlock.
  2341. CmdlineDefsDiag += (DefPrefix + CmdlineDef + " (parsed as: [[").str();
  2342. std::string SubstitutionStr = std::string(CmdlineDef);
  2343. SubstitutionStr[EqIdx] = ':';
  2344. CmdlineDefsIndices.push_back(
  2345. std::make_pair(CmdlineDefsDiag.size(), SubstitutionStr.size()));
  2346. CmdlineDefsDiag += (SubstitutionStr + Twine("]])\n")).str();
  2347. } else {
  2348. CmdlineDefsDiag += DefPrefix;
  2349. CmdlineDefsIndices.push_back(
  2350. std::make_pair(CmdlineDefsDiag.size(), CmdlineDef.size()));
  2351. CmdlineDefsDiag += (CmdlineDef + "\n").str();
  2352. }
  2353. }
  2354. // Create a buffer with fake command line content in order to display
  2355. // parsing diagnostic with location information and point to the
  2356. // global definition with invalid syntax.
  2357. std::unique_ptr<MemoryBuffer> CmdLineDefsDiagBuffer =
  2358. MemoryBuffer::getMemBufferCopy(CmdlineDefsDiag, "Global defines");
  2359. StringRef CmdlineDefsDiagRef = CmdLineDefsDiagBuffer->getBuffer();
  2360. SM.AddNewSourceBuffer(std::move(CmdLineDefsDiagBuffer), SMLoc());
  2361. for (std::pair<size_t, size_t> CmdlineDefIndices : CmdlineDefsIndices) {
  2362. StringRef CmdlineDef = CmdlineDefsDiagRef.substr(CmdlineDefIndices.first,
  2363. CmdlineDefIndices.second);
  2364. if (CmdlineDef.empty()) {
  2365. Errs = joinErrors(
  2366. std::move(Errs),
  2367. ErrorDiagnostic::get(SM, CmdlineDef,
  2368. "missing equal sign in global definition"));
  2369. continue;
  2370. }
  2371. // Numeric variable definition.
  2372. if (CmdlineDef[0] == '#') {
  2373. // Now parse the definition both to check that the syntax is correct and
  2374. // to create the necessary class instance.
  2375. StringRef CmdlineDefExpr = CmdlineDef.substr(1);
  2376. std::optional<NumericVariable *> DefinedNumericVariable;
  2377. Expected<std::unique_ptr<Expression>> ExpressionResult =
  2378. Pattern::parseNumericSubstitutionBlock(CmdlineDefExpr,
  2379. DefinedNumericVariable, false,
  2380. std::nullopt, this, SM);
  2381. if (!ExpressionResult) {
  2382. Errs = joinErrors(std::move(Errs), ExpressionResult.takeError());
  2383. continue;
  2384. }
  2385. std::unique_ptr<Expression> Expression = std::move(*ExpressionResult);
  2386. // Now evaluate the expression whose value this variable should be set
  2387. // to, since the expression of a command-line variable definition should
  2388. // only use variables defined earlier on the command-line. If not, this
  2389. // is an error and we report it.
  2390. Expected<ExpressionValue> Value = Expression->getAST()->eval();
  2391. if (!Value) {
  2392. Errs = joinErrors(std::move(Errs), Value.takeError());
  2393. continue;
  2394. }
  2395. assert(DefinedNumericVariable && "No variable defined");
  2396. (*DefinedNumericVariable)->setValue(*Value);
  2397. // Record this variable definition.
  2398. GlobalNumericVariableTable[(*DefinedNumericVariable)->getName()] =
  2399. *DefinedNumericVariable;
  2400. } else {
  2401. // String variable definition.
  2402. std::pair<StringRef, StringRef> CmdlineNameVal = CmdlineDef.split('=');
  2403. StringRef CmdlineName = CmdlineNameVal.first;
  2404. StringRef OrigCmdlineName = CmdlineName;
  2405. Expected<Pattern::VariableProperties> ParseVarResult =
  2406. Pattern::parseVariable(CmdlineName, SM);
  2407. if (!ParseVarResult) {
  2408. Errs = joinErrors(std::move(Errs), ParseVarResult.takeError());
  2409. continue;
  2410. }
  2411. // Check that CmdlineName does not denote a pseudo variable is only
  2412. // composed of the parsed numeric variable. This catches cases like
  2413. // "FOO+2" in a "FOO+2=10" definition.
  2414. if (ParseVarResult->IsPseudo || !CmdlineName.empty()) {
  2415. Errs = joinErrors(std::move(Errs),
  2416. ErrorDiagnostic::get(
  2417. SM, OrigCmdlineName,
  2418. "invalid name in string variable definition '" +
  2419. OrigCmdlineName + "'"));
  2420. continue;
  2421. }
  2422. StringRef Name = ParseVarResult->Name;
  2423. // Detect collisions between string and numeric variables when the former
  2424. // is created later than the latter.
  2425. if (GlobalNumericVariableTable.find(Name) !=
  2426. GlobalNumericVariableTable.end()) {
  2427. Errs = joinErrors(std::move(Errs),
  2428. ErrorDiagnostic::get(SM, Name,
  2429. "numeric variable with name '" +
  2430. Name + "' already exists"));
  2431. continue;
  2432. }
  2433. GlobalVariableTable.insert(CmdlineNameVal);
  2434. // Mark the string variable as defined to detect collisions between
  2435. // string and numeric variables in defineCmdlineVariables when the latter
  2436. // is created later than the former. We cannot reuse GlobalVariableTable
  2437. // for this by populating it with an empty string since we would then
  2438. // lose the ability to detect the use of an undefined variable in
  2439. // match().
  2440. DefinedVariableTable[Name] = true;
  2441. }
  2442. }
  2443. return Errs;
  2444. }
  2445. void FileCheckPatternContext::clearLocalVars() {
  2446. SmallVector<StringRef, 16> LocalPatternVars, LocalNumericVars;
  2447. for (const StringMapEntry<StringRef> &Var : GlobalVariableTable)
  2448. if (Var.first()[0] != '$')
  2449. LocalPatternVars.push_back(Var.first());
  2450. // Numeric substitution reads the value of a variable directly, not via
  2451. // GlobalNumericVariableTable. Therefore, we clear local variables by
  2452. // clearing their value which will lead to a numeric substitution failure. We
  2453. // also mark the variable for removal from GlobalNumericVariableTable since
  2454. // this is what defineCmdlineVariables checks to decide that no global
  2455. // variable has been defined.
  2456. for (const auto &Var : GlobalNumericVariableTable)
  2457. if (Var.first()[0] != '$') {
  2458. Var.getValue()->clearValue();
  2459. LocalNumericVars.push_back(Var.first());
  2460. }
  2461. for (const auto &Var : LocalPatternVars)
  2462. GlobalVariableTable.erase(Var);
  2463. for (const auto &Var : LocalNumericVars)
  2464. GlobalNumericVariableTable.erase(Var);
  2465. }
  2466. bool FileCheck::checkInput(SourceMgr &SM, StringRef Buffer,
  2467. std::vector<FileCheckDiag> *Diags) {
  2468. bool ChecksFailed = false;
  2469. unsigned i = 0, j = 0, e = CheckStrings->size();
  2470. while (true) {
  2471. StringRef CheckRegion;
  2472. if (j == e) {
  2473. CheckRegion = Buffer;
  2474. } else {
  2475. const FileCheckString &CheckLabelStr = (*CheckStrings)[j];
  2476. if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) {
  2477. ++j;
  2478. continue;
  2479. }
  2480. // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
  2481. size_t MatchLabelLen = 0;
  2482. size_t MatchLabelPos =
  2483. CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, Req, Diags);
  2484. if (MatchLabelPos == StringRef::npos)
  2485. // Immediately bail if CHECK-LABEL fails, nothing else we can do.
  2486. return false;
  2487. CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
  2488. Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
  2489. ++j;
  2490. }
  2491. // Do not clear the first region as it's the one before the first
  2492. // CHECK-LABEL and it would clear variables defined on the command-line
  2493. // before they get used.
  2494. if (i != 0 && Req.EnableVarScope)
  2495. PatternContext->clearLocalVars();
  2496. for (; i != j; ++i) {
  2497. const FileCheckString &CheckStr = (*CheckStrings)[i];
  2498. // Check each string within the scanned region, including a second check
  2499. // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
  2500. size_t MatchLen = 0;
  2501. size_t MatchPos =
  2502. CheckStr.Check(SM, CheckRegion, false, MatchLen, Req, Diags);
  2503. if (MatchPos == StringRef::npos) {
  2504. ChecksFailed = true;
  2505. i = j;
  2506. break;
  2507. }
  2508. CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
  2509. }
  2510. if (j == e)
  2511. break;
  2512. }
  2513. // Success if no checks failed.
  2514. return !ChecksFailed;
  2515. }