FileCheck.cpp 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834
  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. 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. 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. 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. 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. 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. 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, 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. 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. 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, 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, 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. Optional<size_t> LineNumber,
  592. FileCheckPatternContext *Context, const SourceMgr &SM) {
  593. Expr = Expr.ltrim(SpaceChars);
  594. assert(Expr.startswith("("));
  595. auto OptFunc = StringSwitch<Optional<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(None);
  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, Optional<NumericVariable *> &DefinedNumericVariable,
  654. bool IsLegacyLineExpr, Optional<size_t> LineNumber,
  655. FileCheckPatternContext *Context, const SourceMgr &SM) {
  656. std::unique_ptr<ExpressionAST> ExpressionASTPointer = nullptr;
  657. StringRef DefExpr = StringRef();
  658. DefinedNumericVariable = None;
  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. 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. 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. assert(A.Range.Start != B.Range.Start &&
  1243. "unexpected overlapping variable captures");
  1244. return A.Range.Start.getPointer() < B.Range.Start.getPointer();
  1245. });
  1246. // Create notes for the sorted captures.
  1247. for (const VarCapture &VC : VarCaptures) {
  1248. SmallString<256> Msg;
  1249. raw_svector_ostream OS(Msg);
  1250. OS << "captured var \"" << VC.Name << "\"";
  1251. if (Diags)
  1252. Diags->emplace_back(SM, CheckTy, getLoc(), MatchTy, VC.Range, OS.str());
  1253. else
  1254. SM.PrintMessage(VC.Range.Start, SourceMgr::DK_Note, OS.str(), VC.Range);
  1255. }
  1256. }
  1257. static SMRange ProcessMatchResult(FileCheckDiag::MatchType MatchTy,
  1258. const SourceMgr &SM, SMLoc Loc,
  1259. Check::FileCheckType CheckTy,
  1260. StringRef Buffer, size_t Pos, size_t Len,
  1261. std::vector<FileCheckDiag> *Diags,
  1262. bool AdjustPrevDiags = false) {
  1263. SMLoc Start = SMLoc::getFromPointer(Buffer.data() + Pos);
  1264. SMLoc End = SMLoc::getFromPointer(Buffer.data() + Pos + Len);
  1265. SMRange Range(Start, End);
  1266. if (Diags) {
  1267. if (AdjustPrevDiags) {
  1268. SMLoc CheckLoc = Diags->rbegin()->CheckLoc;
  1269. for (auto I = Diags->rbegin(), E = Diags->rend();
  1270. I != E && I->CheckLoc == CheckLoc; ++I)
  1271. I->MatchTy = MatchTy;
  1272. } else
  1273. Diags->emplace_back(SM, CheckTy, Loc, MatchTy, Range);
  1274. }
  1275. return Range;
  1276. }
  1277. void Pattern::printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
  1278. std::vector<FileCheckDiag> *Diags) const {
  1279. // Attempt to find the closest/best fuzzy match. Usually an error happens
  1280. // because some string in the output didn't exactly match. In these cases, we
  1281. // would like to show the user a best guess at what "should have" matched, to
  1282. // save them having to actually check the input manually.
  1283. size_t NumLinesForward = 0;
  1284. size_t Best = StringRef::npos;
  1285. double BestQuality = 0;
  1286. // Use an arbitrary 4k limit on how far we will search.
  1287. for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
  1288. if (Buffer[i] == '\n')
  1289. ++NumLinesForward;
  1290. // Patterns have leading whitespace stripped, so skip whitespace when
  1291. // looking for something which looks like a pattern.
  1292. if (Buffer[i] == ' ' || Buffer[i] == '\t')
  1293. continue;
  1294. // Compute the "quality" of this match as an arbitrary combination of the
  1295. // match distance and the number of lines skipped to get to this match.
  1296. unsigned Distance = computeMatchDistance(Buffer.substr(i));
  1297. double Quality = Distance + (NumLinesForward / 100.);
  1298. if (Quality < BestQuality || Best == StringRef::npos) {
  1299. Best = i;
  1300. BestQuality = Quality;
  1301. }
  1302. }
  1303. // Print the "possible intended match here" line if we found something
  1304. // reasonable and not equal to what we showed in the "scanning from here"
  1305. // line.
  1306. if (Best && Best != StringRef::npos && BestQuality < 50) {
  1307. SMRange MatchRange =
  1308. ProcessMatchResult(FileCheckDiag::MatchFuzzy, SM, getLoc(),
  1309. getCheckTy(), Buffer, Best, 0, Diags);
  1310. SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note,
  1311. "possible intended match here");
  1312. // FIXME: If we wanted to be really friendly we would show why the match
  1313. // failed, as it can be hard to spot simple one character differences.
  1314. }
  1315. }
  1316. Expected<StringRef>
  1317. FileCheckPatternContext::getPatternVarValue(StringRef VarName) {
  1318. auto VarIter = GlobalVariableTable.find(VarName);
  1319. if (VarIter == GlobalVariableTable.end())
  1320. return make_error<UndefVarError>(VarName);
  1321. return VarIter->second;
  1322. }
  1323. template <class... Types>
  1324. NumericVariable *FileCheckPatternContext::makeNumericVariable(Types... args) {
  1325. NumericVariables.push_back(std::make_unique<NumericVariable>(args...));
  1326. return NumericVariables.back().get();
  1327. }
  1328. Substitution *
  1329. FileCheckPatternContext::makeStringSubstitution(StringRef VarName,
  1330. size_t InsertIdx) {
  1331. Substitutions.push_back(
  1332. std::make_unique<StringSubstitution>(this, VarName, InsertIdx));
  1333. return Substitutions.back().get();
  1334. }
  1335. Substitution *FileCheckPatternContext::makeNumericSubstitution(
  1336. StringRef ExpressionStr, std::unique_ptr<Expression> Expression,
  1337. size_t InsertIdx) {
  1338. Substitutions.push_back(std::make_unique<NumericSubstitution>(
  1339. this, ExpressionStr, std::move(Expression), InsertIdx));
  1340. return Substitutions.back().get();
  1341. }
  1342. size_t Pattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) {
  1343. // Offset keeps track of the current offset within the input Str
  1344. size_t Offset = 0;
  1345. // [...] Nesting depth
  1346. size_t BracketDepth = 0;
  1347. while (!Str.empty()) {
  1348. if (Str.startswith("]]") && BracketDepth == 0)
  1349. return Offset;
  1350. if (Str[0] == '\\') {
  1351. // Backslash escapes the next char within regexes, so skip them both.
  1352. Str = Str.substr(2);
  1353. Offset += 2;
  1354. } else {
  1355. switch (Str[0]) {
  1356. default:
  1357. break;
  1358. case '[':
  1359. BracketDepth++;
  1360. break;
  1361. case ']':
  1362. if (BracketDepth == 0) {
  1363. SM.PrintMessage(SMLoc::getFromPointer(Str.data()),
  1364. SourceMgr::DK_Error,
  1365. "missing closing \"]\" for regex variable");
  1366. exit(1);
  1367. }
  1368. BracketDepth--;
  1369. break;
  1370. }
  1371. Str = Str.substr(1);
  1372. Offset++;
  1373. }
  1374. }
  1375. return StringRef::npos;
  1376. }
  1377. StringRef FileCheck::CanonicalizeFile(MemoryBuffer &MB,
  1378. SmallVectorImpl<char> &OutputBuffer) {
  1379. OutputBuffer.reserve(MB.getBufferSize());
  1380. for (const char *Ptr = MB.getBufferStart(), *End = MB.getBufferEnd();
  1381. Ptr != End; ++Ptr) {
  1382. // Eliminate trailing dosish \r.
  1383. if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
  1384. continue;
  1385. }
  1386. // If current char is not a horizontal whitespace or if horizontal
  1387. // whitespace canonicalization is disabled, dump it to output as is.
  1388. if (Req.NoCanonicalizeWhiteSpace || (*Ptr != ' ' && *Ptr != '\t')) {
  1389. OutputBuffer.push_back(*Ptr);
  1390. continue;
  1391. }
  1392. // Otherwise, add one space and advance over neighboring space.
  1393. OutputBuffer.push_back(' ');
  1394. while (Ptr + 1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t'))
  1395. ++Ptr;
  1396. }
  1397. // Add a null byte and then return all but that byte.
  1398. OutputBuffer.push_back('\0');
  1399. return StringRef(OutputBuffer.data(), OutputBuffer.size() - 1);
  1400. }
  1401. FileCheckDiag::FileCheckDiag(const SourceMgr &SM,
  1402. const Check::FileCheckType &CheckTy,
  1403. SMLoc CheckLoc, MatchType MatchTy,
  1404. SMRange InputRange, StringRef Note)
  1405. : CheckTy(CheckTy), CheckLoc(CheckLoc), MatchTy(MatchTy), Note(Note) {
  1406. auto Start = SM.getLineAndColumn(InputRange.Start);
  1407. auto End = SM.getLineAndColumn(InputRange.End);
  1408. InputStartLine = Start.first;
  1409. InputStartCol = Start.second;
  1410. InputEndLine = End.first;
  1411. InputEndCol = End.second;
  1412. }
  1413. static bool IsPartOfWord(char c) {
  1414. return (isAlnum(c) || c == '-' || c == '_');
  1415. }
  1416. Check::FileCheckType &Check::FileCheckType::setCount(int C) {
  1417. assert(Count > 0 && "zero and negative counts are not supported");
  1418. assert((C == 1 || Kind == CheckPlain) &&
  1419. "count supported only for plain CHECK directives");
  1420. Count = C;
  1421. return *this;
  1422. }
  1423. std::string Check::FileCheckType::getModifiersDescription() const {
  1424. if (Modifiers.none())
  1425. return "";
  1426. std::string Ret;
  1427. raw_string_ostream OS(Ret);
  1428. OS << '{';
  1429. if (isLiteralMatch())
  1430. OS << "LITERAL";
  1431. OS << '}';
  1432. return OS.str();
  1433. }
  1434. std::string Check::FileCheckType::getDescription(StringRef Prefix) const {
  1435. // Append directive modifiers.
  1436. auto WithModifiers = [this, Prefix](StringRef Str) -> std::string {
  1437. return (Prefix + Str + getModifiersDescription()).str();
  1438. };
  1439. switch (Kind) {
  1440. case Check::CheckNone:
  1441. return "invalid";
  1442. case Check::CheckPlain:
  1443. if (Count > 1)
  1444. return WithModifiers("-COUNT");
  1445. return WithModifiers("");
  1446. case Check::CheckNext:
  1447. return WithModifiers("-NEXT");
  1448. case Check::CheckSame:
  1449. return WithModifiers("-SAME");
  1450. case Check::CheckNot:
  1451. return WithModifiers("-NOT");
  1452. case Check::CheckDAG:
  1453. return WithModifiers("-DAG");
  1454. case Check::CheckLabel:
  1455. return WithModifiers("-LABEL");
  1456. case Check::CheckEmpty:
  1457. return WithModifiers("-EMPTY");
  1458. case Check::CheckComment:
  1459. return std::string(Prefix);
  1460. case Check::CheckEOF:
  1461. return "implicit EOF";
  1462. case Check::CheckBadNot:
  1463. return "bad NOT";
  1464. case Check::CheckBadCount:
  1465. return "bad COUNT";
  1466. }
  1467. llvm_unreachable("unknown FileCheckType");
  1468. }
  1469. static std::pair<Check::FileCheckType, StringRef>
  1470. FindCheckType(const FileCheckRequest &Req, StringRef Buffer, StringRef Prefix) {
  1471. if (Buffer.size() <= Prefix.size())
  1472. return {Check::CheckNone, StringRef()};
  1473. StringRef Rest = Buffer.drop_front(Prefix.size());
  1474. // Check for comment.
  1475. if (llvm::is_contained(Req.CommentPrefixes, Prefix)) {
  1476. if (Rest.consume_front(":"))
  1477. return {Check::CheckComment, Rest};
  1478. // Ignore a comment prefix if it has a suffix like "-NOT".
  1479. return {Check::CheckNone, StringRef()};
  1480. }
  1481. auto ConsumeModifiers = [&](Check::FileCheckType Ret)
  1482. -> std::pair<Check::FileCheckType, StringRef> {
  1483. if (Rest.consume_front(":"))
  1484. return {Ret, Rest};
  1485. if (!Rest.consume_front("{"))
  1486. return {Check::CheckNone, StringRef()};
  1487. // Parse the modifiers, speparated by commas.
  1488. do {
  1489. // Allow whitespace in modifiers list.
  1490. Rest = Rest.ltrim();
  1491. if (Rest.consume_front("LITERAL"))
  1492. Ret.setLiteralMatch();
  1493. else
  1494. return {Check::CheckNone, Rest};
  1495. // Allow whitespace in modifiers list.
  1496. Rest = Rest.ltrim();
  1497. } while (Rest.consume_front(","));
  1498. if (!Rest.consume_front("}:"))
  1499. return {Check::CheckNone, Rest};
  1500. return {Ret, Rest};
  1501. };
  1502. // Verify that the prefix is followed by directive modifiers or a colon.
  1503. if (Rest.consume_front(":"))
  1504. return {Check::CheckPlain, Rest};
  1505. if (Rest.front() == '{')
  1506. return ConsumeModifiers(Check::CheckPlain);
  1507. if (!Rest.consume_front("-"))
  1508. return {Check::CheckNone, StringRef()};
  1509. if (Rest.consume_front("COUNT-")) {
  1510. int64_t Count;
  1511. if (Rest.consumeInteger(10, Count))
  1512. // Error happened in parsing integer.
  1513. return {Check::CheckBadCount, Rest};
  1514. if (Count <= 0 || Count > INT32_MAX)
  1515. return {Check::CheckBadCount, Rest};
  1516. if (Rest.front() != ':' && Rest.front() != '{')
  1517. return {Check::CheckBadCount, Rest};
  1518. return ConsumeModifiers(
  1519. Check::FileCheckType(Check::CheckPlain).setCount(Count));
  1520. }
  1521. // You can't combine -NOT with another suffix.
  1522. if (Rest.startswith("DAG-NOT:") || Rest.startswith("NOT-DAG:") ||
  1523. Rest.startswith("NEXT-NOT:") || Rest.startswith("NOT-NEXT:") ||
  1524. Rest.startswith("SAME-NOT:") || Rest.startswith("NOT-SAME:") ||
  1525. Rest.startswith("EMPTY-NOT:") || Rest.startswith("NOT-EMPTY:"))
  1526. return {Check::CheckBadNot, Rest};
  1527. if (Rest.consume_front("NEXT"))
  1528. return ConsumeModifiers(Check::CheckNext);
  1529. if (Rest.consume_front("SAME"))
  1530. return ConsumeModifiers(Check::CheckSame);
  1531. if (Rest.consume_front("NOT"))
  1532. return ConsumeModifiers(Check::CheckNot);
  1533. if (Rest.consume_front("DAG"))
  1534. return ConsumeModifiers(Check::CheckDAG);
  1535. if (Rest.consume_front("LABEL"))
  1536. return ConsumeModifiers(Check::CheckLabel);
  1537. if (Rest.consume_front("EMPTY"))
  1538. return ConsumeModifiers(Check::CheckEmpty);
  1539. return {Check::CheckNone, Rest};
  1540. }
  1541. // From the given position, find the next character after the word.
  1542. static size_t SkipWord(StringRef Str, size_t Loc) {
  1543. while (Loc < Str.size() && IsPartOfWord(Str[Loc]))
  1544. ++Loc;
  1545. return Loc;
  1546. }
  1547. /// Searches the buffer for the first prefix in the prefix regular expression.
  1548. ///
  1549. /// This searches the buffer using the provided regular expression, however it
  1550. /// enforces constraints beyond that:
  1551. /// 1) The found prefix must not be a suffix of something that looks like
  1552. /// a valid prefix.
  1553. /// 2) The found prefix must be followed by a valid check type suffix using \c
  1554. /// FindCheckType above.
  1555. ///
  1556. /// \returns a pair of StringRefs into the Buffer, which combines:
  1557. /// - the first match of the regular expression to satisfy these two is
  1558. /// returned,
  1559. /// otherwise an empty StringRef is returned to indicate failure.
  1560. /// - buffer rewound to the location right after parsed suffix, for parsing
  1561. /// to continue from
  1562. ///
  1563. /// If this routine returns a valid prefix, it will also shrink \p Buffer to
  1564. /// start at the beginning of the returned prefix, increment \p LineNumber for
  1565. /// each new line consumed from \p Buffer, and set \p CheckTy to the type of
  1566. /// check found by examining the suffix.
  1567. ///
  1568. /// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy
  1569. /// is unspecified.
  1570. static std::pair<StringRef, StringRef>
  1571. FindFirstMatchingPrefix(const FileCheckRequest &Req, Regex &PrefixRE,
  1572. StringRef &Buffer, unsigned &LineNumber,
  1573. Check::FileCheckType &CheckTy) {
  1574. SmallVector<StringRef, 2> Matches;
  1575. while (!Buffer.empty()) {
  1576. // Find the first (longest) match using the RE.
  1577. if (!PrefixRE.match(Buffer, &Matches))
  1578. // No match at all, bail.
  1579. return {StringRef(), StringRef()};
  1580. StringRef Prefix = Matches[0];
  1581. Matches.clear();
  1582. assert(Prefix.data() >= Buffer.data() &&
  1583. Prefix.data() < Buffer.data() + Buffer.size() &&
  1584. "Prefix doesn't start inside of buffer!");
  1585. size_t Loc = Prefix.data() - Buffer.data();
  1586. StringRef Skipped = Buffer.substr(0, Loc);
  1587. Buffer = Buffer.drop_front(Loc);
  1588. LineNumber += Skipped.count('\n');
  1589. // Check that the matched prefix isn't a suffix of some other check-like
  1590. // word.
  1591. // FIXME: This is a very ad-hoc check. it would be better handled in some
  1592. // other way. Among other things it seems hard to distinguish between
  1593. // intentional and unintentional uses of this feature.
  1594. if (Skipped.empty() || !IsPartOfWord(Skipped.back())) {
  1595. // Now extract the type.
  1596. StringRef AfterSuffix;
  1597. std::tie(CheckTy, AfterSuffix) = FindCheckType(Req, Buffer, Prefix);
  1598. // If we've found a valid check type for this prefix, we're done.
  1599. if (CheckTy != Check::CheckNone)
  1600. return {Prefix, AfterSuffix};
  1601. }
  1602. // If we didn't successfully find a prefix, we need to skip this invalid
  1603. // prefix and continue scanning. We directly skip the prefix that was
  1604. // matched and any additional parts of that check-like word.
  1605. Buffer = Buffer.drop_front(SkipWord(Buffer, Prefix.size()));
  1606. }
  1607. // We ran out of buffer while skipping partial matches so give up.
  1608. return {StringRef(), StringRef()};
  1609. }
  1610. void FileCheckPatternContext::createLineVariable() {
  1611. assert(!LineVariable && "@LINE pseudo numeric variable already created");
  1612. StringRef LineName = "@LINE";
  1613. LineVariable = makeNumericVariable(
  1614. LineName, ExpressionFormat(ExpressionFormat::Kind::Unsigned));
  1615. GlobalNumericVariableTable[LineName] = LineVariable;
  1616. }
  1617. FileCheck::FileCheck(FileCheckRequest Req)
  1618. : Req(Req), PatternContext(std::make_unique<FileCheckPatternContext>()),
  1619. CheckStrings(std::make_unique<std::vector<FileCheckString>>()) {}
  1620. FileCheck::~FileCheck() = default;
  1621. bool FileCheck::readCheckFile(
  1622. SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
  1623. std::pair<unsigned, unsigned> *ImpPatBufferIDRange) {
  1624. if (ImpPatBufferIDRange)
  1625. ImpPatBufferIDRange->first = ImpPatBufferIDRange->second = 0;
  1626. Error DefineError =
  1627. PatternContext->defineCmdlineVariables(Req.GlobalDefines, SM);
  1628. if (DefineError) {
  1629. logAllUnhandledErrors(std::move(DefineError), errs());
  1630. return true;
  1631. }
  1632. PatternContext->createLineVariable();
  1633. std::vector<Pattern> ImplicitNegativeChecks;
  1634. for (StringRef PatternString : Req.ImplicitCheckNot) {
  1635. // Create a buffer with fake command line content in order to display the
  1636. // command line option responsible for the specific implicit CHECK-NOT.
  1637. std::string Prefix = "-implicit-check-not='";
  1638. std::string Suffix = "'";
  1639. std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
  1640. (Prefix + PatternString + Suffix).str(), "command line");
  1641. StringRef PatternInBuffer =
  1642. CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
  1643. unsigned BufferID = SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
  1644. if (ImpPatBufferIDRange) {
  1645. if (ImpPatBufferIDRange->first == ImpPatBufferIDRange->second) {
  1646. ImpPatBufferIDRange->first = BufferID;
  1647. ImpPatBufferIDRange->second = BufferID + 1;
  1648. } else {
  1649. assert(BufferID == ImpPatBufferIDRange->second &&
  1650. "expected consecutive source buffer IDs");
  1651. ++ImpPatBufferIDRange->second;
  1652. }
  1653. }
  1654. ImplicitNegativeChecks.push_back(
  1655. Pattern(Check::CheckNot, PatternContext.get()));
  1656. ImplicitNegativeChecks.back().parsePattern(PatternInBuffer,
  1657. "IMPLICIT-CHECK", SM, Req);
  1658. }
  1659. std::vector<Pattern> DagNotMatches = ImplicitNegativeChecks;
  1660. // LineNumber keeps track of the line on which CheckPrefix instances are
  1661. // found.
  1662. unsigned LineNumber = 1;
  1663. std::set<StringRef> PrefixesNotFound(Req.CheckPrefixes.begin(),
  1664. Req.CheckPrefixes.end());
  1665. const size_t DistinctPrefixes = PrefixesNotFound.size();
  1666. while (true) {
  1667. Check::FileCheckType CheckTy;
  1668. // See if a prefix occurs in the memory buffer.
  1669. StringRef UsedPrefix;
  1670. StringRef AfterSuffix;
  1671. std::tie(UsedPrefix, AfterSuffix) =
  1672. FindFirstMatchingPrefix(Req, PrefixRE, Buffer, LineNumber, CheckTy);
  1673. if (UsedPrefix.empty())
  1674. break;
  1675. if (CheckTy != Check::CheckComment)
  1676. PrefixesNotFound.erase(UsedPrefix);
  1677. assert(UsedPrefix.data() == Buffer.data() &&
  1678. "Failed to move Buffer's start forward, or pointed prefix outside "
  1679. "of the buffer!");
  1680. assert(AfterSuffix.data() >= Buffer.data() &&
  1681. AfterSuffix.data() < Buffer.data() + Buffer.size() &&
  1682. "Parsing after suffix doesn't start inside of buffer!");
  1683. // Location to use for error messages.
  1684. const char *UsedPrefixStart = UsedPrefix.data();
  1685. // Skip the buffer to the end of parsed suffix (or just prefix, if no good
  1686. // suffix was processed).
  1687. Buffer = AfterSuffix.empty() ? Buffer.drop_front(UsedPrefix.size())
  1688. : AfterSuffix;
  1689. // Complain about useful-looking but unsupported suffixes.
  1690. if (CheckTy == Check::CheckBadNot) {
  1691. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
  1692. "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
  1693. return true;
  1694. }
  1695. // Complain about invalid count specification.
  1696. if (CheckTy == Check::CheckBadCount) {
  1697. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
  1698. "invalid count in -COUNT specification on prefix '" +
  1699. UsedPrefix + "'");
  1700. return true;
  1701. }
  1702. // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
  1703. // leading whitespace.
  1704. if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
  1705. Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
  1706. // Scan ahead to the end of line.
  1707. size_t EOL = Buffer.find_first_of("\n\r");
  1708. // Remember the location of the start of the pattern, for diagnostics.
  1709. SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
  1710. // Extract the pattern from the buffer.
  1711. StringRef PatternBuffer = Buffer.substr(0, EOL);
  1712. Buffer = Buffer.substr(EOL);
  1713. // If this is a comment, we're done.
  1714. if (CheckTy == Check::CheckComment)
  1715. continue;
  1716. // Parse the pattern.
  1717. Pattern P(CheckTy, PatternContext.get(), LineNumber);
  1718. if (P.parsePattern(PatternBuffer, UsedPrefix, SM, Req))
  1719. return true;
  1720. // Verify that CHECK-LABEL lines do not define or use variables
  1721. if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
  1722. SM.PrintMessage(
  1723. SMLoc::getFromPointer(UsedPrefixStart), SourceMgr::DK_Error,
  1724. "found '" + UsedPrefix + "-LABEL:'"
  1725. " with variable definition or use");
  1726. return true;
  1727. }
  1728. // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
  1729. if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame ||
  1730. CheckTy == Check::CheckEmpty) &&
  1731. CheckStrings->empty()) {
  1732. StringRef Type = CheckTy == Check::CheckNext
  1733. ? "NEXT"
  1734. : CheckTy == Check::CheckEmpty ? "EMPTY" : "SAME";
  1735. SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
  1736. SourceMgr::DK_Error,
  1737. "found '" + UsedPrefix + "-" + Type +
  1738. "' without previous '" + UsedPrefix + ": line");
  1739. return true;
  1740. }
  1741. // Handle CHECK-DAG/-NOT.
  1742. if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
  1743. DagNotMatches.push_back(P);
  1744. continue;
  1745. }
  1746. // Okay, add the string we captured to the output vector and move on.
  1747. CheckStrings->emplace_back(P, UsedPrefix, PatternLoc);
  1748. std::swap(DagNotMatches, CheckStrings->back().DagNotStrings);
  1749. DagNotMatches = ImplicitNegativeChecks;
  1750. }
  1751. // When there are no used prefixes we report an error except in the case that
  1752. // no prefix is specified explicitly but -implicit-check-not is specified.
  1753. const bool NoPrefixesFound = PrefixesNotFound.size() == DistinctPrefixes;
  1754. const bool SomePrefixesUnexpectedlyNotUsed =
  1755. !Req.AllowUnusedPrefixes && !PrefixesNotFound.empty();
  1756. if ((NoPrefixesFound || SomePrefixesUnexpectedlyNotUsed) &&
  1757. (ImplicitNegativeChecks.empty() || !Req.IsDefaultCheckPrefix)) {
  1758. errs() << "error: no check strings found with prefix"
  1759. << (PrefixesNotFound.size() > 1 ? "es " : " ");
  1760. bool First = true;
  1761. for (StringRef MissingPrefix : PrefixesNotFound) {
  1762. if (!First)
  1763. errs() << ", ";
  1764. errs() << "\'" << MissingPrefix << ":'";
  1765. First = false;
  1766. }
  1767. errs() << '\n';
  1768. return true;
  1769. }
  1770. // Add an EOF pattern for any trailing --implicit-check-not/CHECK-DAG/-NOTs,
  1771. // and use the first prefix as a filler for the error message.
  1772. if (!DagNotMatches.empty()) {
  1773. CheckStrings->emplace_back(
  1774. Pattern(Check::CheckEOF, PatternContext.get(), LineNumber + 1),
  1775. *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data()));
  1776. std::swap(DagNotMatches, CheckStrings->back().DagNotStrings);
  1777. }
  1778. return false;
  1779. }
  1780. /// Returns either (1) \c ErrorSuccess if there was no error or (2)
  1781. /// \c ErrorReported if an error was reported, such as an unexpected match.
  1782. static Error printMatch(bool ExpectedMatch, const SourceMgr &SM,
  1783. StringRef Prefix, SMLoc Loc, const Pattern &Pat,
  1784. int MatchedCount, StringRef Buffer,
  1785. Pattern::MatchResult MatchResult,
  1786. const FileCheckRequest &Req,
  1787. std::vector<FileCheckDiag> *Diags) {
  1788. // Suppress some verbosity if there's no error.
  1789. bool HasError = !ExpectedMatch || MatchResult.TheError;
  1790. bool PrintDiag = true;
  1791. if (!HasError) {
  1792. if (!Req.Verbose)
  1793. return ErrorReported::reportedOrSuccess(HasError);
  1794. if (!Req.VerboseVerbose && Pat.getCheckTy() == Check::CheckEOF)
  1795. return ErrorReported::reportedOrSuccess(HasError);
  1796. // Due to their verbosity, we don't print verbose diagnostics here if we're
  1797. // gathering them for Diags to be rendered elsewhere, but we always print
  1798. // other diagnostics.
  1799. PrintDiag = !Diags;
  1800. }
  1801. // Add "found" diagnostic, substitutions, and variable definitions to Diags.
  1802. FileCheckDiag::MatchType MatchTy = ExpectedMatch
  1803. ? FileCheckDiag::MatchFoundAndExpected
  1804. : FileCheckDiag::MatchFoundButExcluded;
  1805. SMRange MatchRange = ProcessMatchResult(MatchTy, SM, Loc, Pat.getCheckTy(),
  1806. Buffer, MatchResult.TheMatch->Pos,
  1807. MatchResult.TheMatch->Len, Diags);
  1808. if (Diags) {
  1809. Pat.printSubstitutions(SM, Buffer, MatchRange, MatchTy, Diags);
  1810. Pat.printVariableDefs(SM, MatchTy, Diags);
  1811. }
  1812. if (!PrintDiag) {
  1813. assert(!HasError && "expected to report more diagnostics for error");
  1814. return ErrorReported::reportedOrSuccess(HasError);
  1815. }
  1816. // Print the match.
  1817. std::string Message = formatv("{0}: {1} string found in input",
  1818. Pat.getCheckTy().getDescription(Prefix),
  1819. (ExpectedMatch ? "expected" : "excluded"))
  1820. .str();
  1821. if (Pat.getCount() > 1)
  1822. Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
  1823. SM.PrintMessage(
  1824. Loc, ExpectedMatch ? SourceMgr::DK_Remark : SourceMgr::DK_Error, Message);
  1825. SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, "found here",
  1826. {MatchRange});
  1827. // Print additional information, which can be useful even if there are errors.
  1828. Pat.printSubstitutions(SM, Buffer, MatchRange, MatchTy, nullptr);
  1829. Pat.printVariableDefs(SM, MatchTy, nullptr);
  1830. // Print errors and add them to Diags. We report these errors after the match
  1831. // itself because we found them after the match. If we had found them before
  1832. // the match, we'd be in printNoMatch.
  1833. handleAllErrors(std::move(MatchResult.TheError),
  1834. [&](const ErrorDiagnostic &E) {
  1835. E.log(errs());
  1836. if (Diags) {
  1837. Diags->emplace_back(SM, Pat.getCheckTy(), Loc,
  1838. FileCheckDiag::MatchFoundErrorNote,
  1839. E.getRange(), E.getMessage().str());
  1840. }
  1841. });
  1842. return ErrorReported::reportedOrSuccess(HasError);
  1843. }
  1844. /// Returns either (1) \c ErrorSuccess if there was no error, or (2)
  1845. /// \c ErrorReported if an error was reported, such as an expected match not
  1846. /// found.
  1847. static Error printNoMatch(bool ExpectedMatch, const SourceMgr &SM,
  1848. StringRef Prefix, SMLoc Loc, const Pattern &Pat,
  1849. int MatchedCount, StringRef Buffer, Error MatchError,
  1850. bool VerboseVerbose,
  1851. std::vector<FileCheckDiag> *Diags) {
  1852. // Print any pattern errors, and record them to be added to Diags later.
  1853. bool HasError = ExpectedMatch;
  1854. bool HasPatternError = false;
  1855. FileCheckDiag::MatchType MatchTy = ExpectedMatch
  1856. ? FileCheckDiag::MatchNoneButExpected
  1857. : FileCheckDiag::MatchNoneAndExcluded;
  1858. SmallVector<std::string, 4> ErrorMsgs;
  1859. handleAllErrors(
  1860. std::move(MatchError),
  1861. [&](const ErrorDiagnostic &E) {
  1862. HasError = HasPatternError = true;
  1863. MatchTy = FileCheckDiag::MatchNoneForInvalidPattern;
  1864. E.log(errs());
  1865. if (Diags)
  1866. ErrorMsgs.push_back(E.getMessage().str());
  1867. },
  1868. // NotFoundError is why printNoMatch was invoked.
  1869. [](const NotFoundError &E) {});
  1870. // Suppress some verbosity if there's no error.
  1871. bool PrintDiag = true;
  1872. if (!HasError) {
  1873. if (!VerboseVerbose)
  1874. return ErrorReported::reportedOrSuccess(HasError);
  1875. // Due to their verbosity, we don't print verbose diagnostics here if we're
  1876. // gathering them for Diags to be rendered elsewhere, but we always print
  1877. // other diagnostics.
  1878. PrintDiag = !Diags;
  1879. }
  1880. // Add "not found" diagnostic, substitutions, and pattern errors to Diags.
  1881. //
  1882. // We handle Diags a little differently than the errors we print directly:
  1883. // we add the "not found" diagnostic to Diags even if there are pattern
  1884. // errors. The reason is that we need to attach pattern errors as notes
  1885. // somewhere in the input, and the input search range from the "not found"
  1886. // diagnostic is all we have to anchor them.
  1887. SMRange SearchRange = ProcessMatchResult(MatchTy, SM, Loc, Pat.getCheckTy(),
  1888. Buffer, 0, Buffer.size(), Diags);
  1889. if (Diags) {
  1890. SMRange NoteRange = SMRange(SearchRange.Start, SearchRange.Start);
  1891. for (StringRef ErrorMsg : ErrorMsgs)
  1892. Diags->emplace_back(SM, Pat.getCheckTy(), Loc, MatchTy, NoteRange,
  1893. ErrorMsg);
  1894. Pat.printSubstitutions(SM, Buffer, SearchRange, MatchTy, Diags);
  1895. }
  1896. if (!PrintDiag) {
  1897. assert(!HasError && "expected to report more diagnostics for error");
  1898. return ErrorReported::reportedOrSuccess(HasError);
  1899. }
  1900. // Print "not found" diagnostic, except that's implied if we already printed a
  1901. // pattern error.
  1902. if (!HasPatternError) {
  1903. std::string Message = formatv("{0}: {1} string not found in input",
  1904. Pat.getCheckTy().getDescription(Prefix),
  1905. (ExpectedMatch ? "expected" : "excluded"))
  1906. .str();
  1907. if (Pat.getCount() > 1)
  1908. Message +=
  1909. formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
  1910. SM.PrintMessage(Loc,
  1911. ExpectedMatch ? SourceMgr::DK_Error : SourceMgr::DK_Remark,
  1912. Message);
  1913. SM.PrintMessage(SearchRange.Start, SourceMgr::DK_Note,
  1914. "scanning from here");
  1915. }
  1916. // Print additional information, which can be useful even after a pattern
  1917. // error.
  1918. Pat.printSubstitutions(SM, Buffer, SearchRange, MatchTy, nullptr);
  1919. if (ExpectedMatch)
  1920. Pat.printFuzzyMatch(SM, Buffer, Diags);
  1921. return ErrorReported::reportedOrSuccess(HasError);
  1922. }
  1923. /// Returns either (1) \c ErrorSuccess if there was no error, or (2)
  1924. /// \c ErrorReported if an error was reported.
  1925. static Error reportMatchResult(bool ExpectedMatch, const SourceMgr &SM,
  1926. StringRef Prefix, SMLoc Loc, const Pattern &Pat,
  1927. int MatchedCount, StringRef Buffer,
  1928. Pattern::MatchResult MatchResult,
  1929. const FileCheckRequest &Req,
  1930. std::vector<FileCheckDiag> *Diags) {
  1931. if (MatchResult.TheMatch)
  1932. return printMatch(ExpectedMatch, SM, Prefix, Loc, Pat, MatchedCount, Buffer,
  1933. std::move(MatchResult), Req, Diags);
  1934. return printNoMatch(ExpectedMatch, SM, Prefix, Loc, Pat, MatchedCount, Buffer,
  1935. std::move(MatchResult.TheError), Req.VerboseVerbose,
  1936. Diags);
  1937. }
  1938. /// Counts the number of newlines in the specified range.
  1939. static unsigned CountNumNewlinesBetween(StringRef Range,
  1940. const char *&FirstNewLine) {
  1941. unsigned NumNewLines = 0;
  1942. while (true) {
  1943. // Scan for newline.
  1944. Range = Range.substr(Range.find_first_of("\n\r"));
  1945. if (Range.empty())
  1946. return NumNewLines;
  1947. ++NumNewLines;
  1948. // Handle \n\r and \r\n as a single newline.
  1949. if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') &&
  1950. (Range[0] != Range[1]))
  1951. Range = Range.substr(1);
  1952. Range = Range.substr(1);
  1953. if (NumNewLines == 1)
  1954. FirstNewLine = Range.begin();
  1955. }
  1956. }
  1957. size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
  1958. bool IsLabelScanMode, size_t &MatchLen,
  1959. FileCheckRequest &Req,
  1960. std::vector<FileCheckDiag> *Diags) const {
  1961. size_t LastPos = 0;
  1962. std::vector<const Pattern *> NotStrings;
  1963. // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
  1964. // bounds; we have not processed variable definitions within the bounded block
  1965. // yet so cannot handle any final CHECK-DAG yet; this is handled when going
  1966. // over the block again (including the last CHECK-LABEL) in normal mode.
  1967. if (!IsLabelScanMode) {
  1968. // Match "dag strings" (with mixed "not strings" if any).
  1969. LastPos = CheckDag(SM, Buffer, NotStrings, Req, Diags);
  1970. if (LastPos == StringRef::npos)
  1971. return StringRef::npos;
  1972. }
  1973. // Match itself from the last position after matching CHECK-DAG.
  1974. size_t LastMatchEnd = LastPos;
  1975. size_t FirstMatchPos = 0;
  1976. // Go match the pattern Count times. Majority of patterns only match with
  1977. // count 1 though.
  1978. assert(Pat.getCount() != 0 && "pattern count can not be zero");
  1979. for (int i = 1; i <= Pat.getCount(); i++) {
  1980. StringRef MatchBuffer = Buffer.substr(LastMatchEnd);
  1981. // get a match at current start point
  1982. Pattern::MatchResult MatchResult = Pat.match(MatchBuffer, SM);
  1983. // report
  1984. if (Error Err = reportMatchResult(/*ExpectedMatch=*/true, SM, Prefix, Loc,
  1985. Pat, i, MatchBuffer,
  1986. std::move(MatchResult), Req, Diags)) {
  1987. cantFail(handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
  1988. return StringRef::npos;
  1989. }
  1990. size_t MatchPos = MatchResult.TheMatch->Pos;
  1991. if (i == 1)
  1992. FirstMatchPos = LastPos + MatchPos;
  1993. // move start point after the match
  1994. LastMatchEnd += MatchPos + MatchResult.TheMatch->Len;
  1995. }
  1996. // Full match len counts from first match pos.
  1997. MatchLen = LastMatchEnd - FirstMatchPos;
  1998. // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
  1999. // or CHECK-NOT
  2000. if (!IsLabelScanMode) {
  2001. size_t MatchPos = FirstMatchPos - LastPos;
  2002. StringRef MatchBuffer = Buffer.substr(LastPos);
  2003. StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
  2004. // If this check is a "CHECK-NEXT", verify that the previous match was on
  2005. // the previous line (i.e. that there is one newline between them).
  2006. if (CheckNext(SM, SkippedRegion)) {
  2007. ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
  2008. Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
  2009. Diags, Req.Verbose);
  2010. return StringRef::npos;
  2011. }
  2012. // If this check is a "CHECK-SAME", verify that the previous match was on
  2013. // the same line (i.e. that there is no newline between them).
  2014. if (CheckSame(SM, SkippedRegion)) {
  2015. ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
  2016. Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
  2017. Diags, Req.Verbose);
  2018. return StringRef::npos;
  2019. }
  2020. // If this match had "not strings", verify that they don't exist in the
  2021. // skipped region.
  2022. if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
  2023. return StringRef::npos;
  2024. }
  2025. return FirstMatchPos;
  2026. }
  2027. bool FileCheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
  2028. if (Pat.getCheckTy() != Check::CheckNext &&
  2029. Pat.getCheckTy() != Check::CheckEmpty)
  2030. return false;
  2031. Twine CheckName =
  2032. Prefix +
  2033. Twine(Pat.getCheckTy() == Check::CheckEmpty ? "-EMPTY" : "-NEXT");
  2034. // Count the number of newlines between the previous match and this one.
  2035. const char *FirstNewLine = nullptr;
  2036. unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
  2037. if (NumNewLines == 0) {
  2038. SM.PrintMessage(Loc, SourceMgr::DK_Error,
  2039. CheckName + ": is on the same line as previous match");
  2040. SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
  2041. "'next' match was here");
  2042. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
  2043. "previous match ended here");
  2044. return true;
  2045. }
  2046. if (NumNewLines != 1) {
  2047. SM.PrintMessage(Loc, SourceMgr::DK_Error,
  2048. CheckName +
  2049. ": is not on the line after the previous match");
  2050. SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
  2051. "'next' match was here");
  2052. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
  2053. "previous match ended here");
  2054. SM.PrintMessage(SMLoc::getFromPointer(FirstNewLine), SourceMgr::DK_Note,
  2055. "non-matching line after previous match is here");
  2056. return true;
  2057. }
  2058. return false;
  2059. }
  2060. bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
  2061. if (Pat.getCheckTy() != Check::CheckSame)
  2062. return false;
  2063. // Count the number of newlines between the previous match and this one.
  2064. const char *FirstNewLine = nullptr;
  2065. unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
  2066. if (NumNewLines != 0) {
  2067. SM.PrintMessage(Loc, SourceMgr::DK_Error,
  2068. Prefix +
  2069. "-SAME: is not on the same line as the previous match");
  2070. SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
  2071. "'next' match was here");
  2072. SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
  2073. "previous match ended here");
  2074. return true;
  2075. }
  2076. return false;
  2077. }
  2078. bool FileCheckString::CheckNot(const SourceMgr &SM, StringRef Buffer,
  2079. const std::vector<const Pattern *> &NotStrings,
  2080. const FileCheckRequest &Req,
  2081. std::vector<FileCheckDiag> *Diags) const {
  2082. bool DirectiveFail = false;
  2083. for (const Pattern *Pat : NotStrings) {
  2084. assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!");
  2085. Pattern::MatchResult MatchResult = Pat->match(Buffer, SM);
  2086. if (Error Err = reportMatchResult(/*ExpectedMatch=*/false, SM, Prefix,
  2087. Pat->getLoc(), *Pat, 1, Buffer,
  2088. std::move(MatchResult), Req, Diags)) {
  2089. cantFail(handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
  2090. DirectiveFail = true;
  2091. continue;
  2092. }
  2093. }
  2094. return DirectiveFail;
  2095. }
  2096. size_t FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
  2097. std::vector<const Pattern *> &NotStrings,
  2098. const FileCheckRequest &Req,
  2099. std::vector<FileCheckDiag> *Diags) const {
  2100. if (DagNotStrings.empty())
  2101. return 0;
  2102. // The start of the search range.
  2103. size_t StartPos = 0;
  2104. struct MatchRange {
  2105. size_t Pos;
  2106. size_t End;
  2107. };
  2108. // A sorted list of ranges for non-overlapping CHECK-DAG matches. Match
  2109. // ranges are erased from this list once they are no longer in the search
  2110. // range.
  2111. std::list<MatchRange> MatchRanges;
  2112. // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG
  2113. // group, so we don't use a range-based for loop here.
  2114. for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end();
  2115. PatItr != PatEnd; ++PatItr) {
  2116. const Pattern &Pat = *PatItr;
  2117. assert((Pat.getCheckTy() == Check::CheckDAG ||
  2118. Pat.getCheckTy() == Check::CheckNot) &&
  2119. "Invalid CHECK-DAG or CHECK-NOT!");
  2120. if (Pat.getCheckTy() == Check::CheckNot) {
  2121. NotStrings.push_back(&Pat);
  2122. continue;
  2123. }
  2124. assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
  2125. // CHECK-DAG always matches from the start.
  2126. size_t MatchLen = 0, MatchPos = StartPos;
  2127. // Search for a match that doesn't overlap a previous match in this
  2128. // CHECK-DAG group.
  2129. for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) {
  2130. StringRef MatchBuffer = Buffer.substr(MatchPos);
  2131. Pattern::MatchResult MatchResult = Pat.match(MatchBuffer, SM);
  2132. // With a group of CHECK-DAGs, a single mismatching means the match on
  2133. // that group of CHECK-DAGs fails immediately.
  2134. if (MatchResult.TheError || Req.VerboseVerbose) {
  2135. if (Error Err = reportMatchResult(/*ExpectedMatch=*/true, SM, Prefix,
  2136. Pat.getLoc(), Pat, 1, MatchBuffer,
  2137. std::move(MatchResult), Req, Diags)) {
  2138. cantFail(
  2139. handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
  2140. return StringRef::npos;
  2141. }
  2142. }
  2143. MatchLen = MatchResult.TheMatch->Len;
  2144. // Re-calc it as the offset relative to the start of the original
  2145. // string.
  2146. MatchPos += MatchResult.TheMatch->Pos;
  2147. MatchRange M{MatchPos, MatchPos + MatchLen};
  2148. if (Req.AllowDeprecatedDagOverlap) {
  2149. // We don't need to track all matches in this mode, so we just maintain
  2150. // one match range that encompasses the current CHECK-DAG group's
  2151. // matches.
  2152. if (MatchRanges.empty())
  2153. MatchRanges.insert(MatchRanges.end(), M);
  2154. else {
  2155. auto Block = MatchRanges.begin();
  2156. Block->Pos = std::min(Block->Pos, M.Pos);
  2157. Block->End = std::max(Block->End, M.End);
  2158. }
  2159. break;
  2160. }
  2161. // Iterate previous matches until overlapping match or insertion point.
  2162. bool Overlap = false;
  2163. for (; MI != ME; ++MI) {
  2164. if (M.Pos < MI->End) {
  2165. // !Overlap => New match has no overlap and is before this old match.
  2166. // Overlap => New match overlaps this old match.
  2167. Overlap = MI->Pos < M.End;
  2168. break;
  2169. }
  2170. }
  2171. if (!Overlap) {
  2172. // Insert non-overlapping match into list.
  2173. MatchRanges.insert(MI, M);
  2174. break;
  2175. }
  2176. if (Req.VerboseVerbose) {
  2177. // Due to their verbosity, we don't print verbose diagnostics here if
  2178. // we're gathering them for a different rendering, but we always print
  2179. // other diagnostics.
  2180. if (!Diags) {
  2181. SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos);
  2182. SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End);
  2183. SMRange OldRange(OldStart, OldEnd);
  2184. SM.PrintMessage(OldStart, SourceMgr::DK_Note,
  2185. "match discarded, overlaps earlier DAG match here",
  2186. {OldRange});
  2187. } else {
  2188. SMLoc CheckLoc = Diags->rbegin()->CheckLoc;
  2189. for (auto I = Diags->rbegin(), E = Diags->rend();
  2190. I != E && I->CheckLoc == CheckLoc; ++I)
  2191. I->MatchTy = FileCheckDiag::MatchFoundButDiscarded;
  2192. }
  2193. }
  2194. MatchPos = MI->End;
  2195. }
  2196. if (!Req.VerboseVerbose)
  2197. cantFail(printMatch(
  2198. /*ExpectedMatch=*/true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer,
  2199. Pattern::MatchResult(MatchPos, MatchLen, Error::success()), Req,
  2200. Diags));
  2201. // Handle the end of a CHECK-DAG group.
  2202. if (std::next(PatItr) == PatEnd ||
  2203. std::next(PatItr)->getCheckTy() == Check::CheckNot) {
  2204. if (!NotStrings.empty()) {
  2205. // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to
  2206. // CHECK-DAG, verify that there are no 'not' strings occurred in that
  2207. // region.
  2208. StringRef SkippedRegion =
  2209. Buffer.slice(StartPos, MatchRanges.begin()->Pos);
  2210. if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
  2211. return StringRef::npos;
  2212. // Clear "not strings".
  2213. NotStrings.clear();
  2214. }
  2215. // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the
  2216. // end of this CHECK-DAG group's match range.
  2217. StartPos = MatchRanges.rbegin()->End;
  2218. // Don't waste time checking for (impossible) overlaps before that.
  2219. MatchRanges.clear();
  2220. }
  2221. }
  2222. return StartPos;
  2223. }
  2224. static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes,
  2225. ArrayRef<StringRef> SuppliedPrefixes) {
  2226. for (StringRef Prefix : SuppliedPrefixes) {
  2227. if (Prefix.empty()) {
  2228. errs() << "error: supplied " << Kind << " prefix must not be the empty "
  2229. << "string\n";
  2230. return false;
  2231. }
  2232. static const Regex Validator("^[a-zA-Z0-9_-]*$");
  2233. if (!Validator.match(Prefix)) {
  2234. errs() << "error: supplied " << Kind << " prefix must start with a "
  2235. << "letter and contain only alphanumeric characters, hyphens, and "
  2236. << "underscores: '" << Prefix << "'\n";
  2237. return false;
  2238. }
  2239. if (!UniquePrefixes.insert(Prefix).second) {
  2240. errs() << "error: supplied " << Kind << " prefix must be unique among "
  2241. << "check and comment prefixes: '" << Prefix << "'\n";
  2242. return false;
  2243. }
  2244. }
  2245. return true;
  2246. }
  2247. static const char *DefaultCheckPrefixes[] = {"CHECK"};
  2248. static const char *DefaultCommentPrefixes[] = {"COM", "RUN"};
  2249. bool FileCheck::ValidateCheckPrefixes() {
  2250. StringSet<> UniquePrefixes;
  2251. // Add default prefixes to catch user-supplied duplicates of them below.
  2252. if (Req.CheckPrefixes.empty()) {
  2253. for (const char *Prefix : DefaultCheckPrefixes)
  2254. UniquePrefixes.insert(Prefix);
  2255. }
  2256. if (Req.CommentPrefixes.empty()) {
  2257. for (const char *Prefix : DefaultCommentPrefixes)
  2258. UniquePrefixes.insert(Prefix);
  2259. }
  2260. // Do not validate the default prefixes, or diagnostics about duplicates might
  2261. // incorrectly indicate that they were supplied by the user.
  2262. if (!ValidatePrefixes("check", UniquePrefixes, Req.CheckPrefixes))
  2263. return false;
  2264. if (!ValidatePrefixes("comment", UniquePrefixes, Req.CommentPrefixes))
  2265. return false;
  2266. return true;
  2267. }
  2268. Regex FileCheck::buildCheckPrefixRegex() {
  2269. if (Req.CheckPrefixes.empty()) {
  2270. for (const char *Prefix : DefaultCheckPrefixes)
  2271. Req.CheckPrefixes.push_back(Prefix);
  2272. Req.IsDefaultCheckPrefix = true;
  2273. }
  2274. if (Req.CommentPrefixes.empty()) {
  2275. for (const char *Prefix : DefaultCommentPrefixes)
  2276. Req.CommentPrefixes.push_back(Prefix);
  2277. }
  2278. // We already validated the contents of CheckPrefixes and CommentPrefixes so
  2279. // just concatenate them as alternatives.
  2280. SmallString<32> PrefixRegexStr;
  2281. for (size_t I = 0, E = Req.CheckPrefixes.size(); I != E; ++I) {
  2282. if (I != 0)
  2283. PrefixRegexStr.push_back('|');
  2284. PrefixRegexStr.append(Req.CheckPrefixes[I]);
  2285. }
  2286. for (StringRef Prefix : Req.CommentPrefixes) {
  2287. PrefixRegexStr.push_back('|');
  2288. PrefixRegexStr.append(Prefix);
  2289. }
  2290. return Regex(PrefixRegexStr);
  2291. }
  2292. Error FileCheckPatternContext::defineCmdlineVariables(
  2293. ArrayRef<StringRef> CmdlineDefines, SourceMgr &SM) {
  2294. assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() &&
  2295. "Overriding defined variable with command-line variable definitions");
  2296. if (CmdlineDefines.empty())
  2297. return Error::success();
  2298. // Create a string representing the vector of command-line definitions. Each
  2299. // definition is on its own line and prefixed with a definition number to
  2300. // clarify which definition a given diagnostic corresponds to.
  2301. unsigned I = 0;
  2302. Error Errs = Error::success();
  2303. std::string CmdlineDefsDiag;
  2304. SmallVector<std::pair<size_t, size_t>, 4> CmdlineDefsIndices;
  2305. for (StringRef CmdlineDef : CmdlineDefines) {
  2306. std::string DefPrefix = ("Global define #" + Twine(++I) + ": ").str();
  2307. size_t EqIdx = CmdlineDef.find('=');
  2308. if (EqIdx == StringRef::npos) {
  2309. CmdlineDefsIndices.push_back(std::make_pair(CmdlineDefsDiag.size(), 0));
  2310. continue;
  2311. }
  2312. // Numeric variable definition.
  2313. if (CmdlineDef[0] == '#') {
  2314. // Append a copy of the command-line definition adapted to use the same
  2315. // format as in the input file to be able to reuse
  2316. // parseNumericSubstitutionBlock.
  2317. CmdlineDefsDiag += (DefPrefix + CmdlineDef + " (parsed as: [[").str();
  2318. std::string SubstitutionStr = std::string(CmdlineDef);
  2319. SubstitutionStr[EqIdx] = ':';
  2320. CmdlineDefsIndices.push_back(
  2321. std::make_pair(CmdlineDefsDiag.size(), SubstitutionStr.size()));
  2322. CmdlineDefsDiag += (SubstitutionStr + Twine("]])\n")).str();
  2323. } else {
  2324. CmdlineDefsDiag += DefPrefix;
  2325. CmdlineDefsIndices.push_back(
  2326. std::make_pair(CmdlineDefsDiag.size(), CmdlineDef.size()));
  2327. CmdlineDefsDiag += (CmdlineDef + "\n").str();
  2328. }
  2329. }
  2330. // Create a buffer with fake command line content in order to display
  2331. // parsing diagnostic with location information and point to the
  2332. // global definition with invalid syntax.
  2333. std::unique_ptr<MemoryBuffer> CmdLineDefsDiagBuffer =
  2334. MemoryBuffer::getMemBufferCopy(CmdlineDefsDiag, "Global defines");
  2335. StringRef CmdlineDefsDiagRef = CmdLineDefsDiagBuffer->getBuffer();
  2336. SM.AddNewSourceBuffer(std::move(CmdLineDefsDiagBuffer), SMLoc());
  2337. for (std::pair<size_t, size_t> CmdlineDefIndices : CmdlineDefsIndices) {
  2338. StringRef CmdlineDef = CmdlineDefsDiagRef.substr(CmdlineDefIndices.first,
  2339. CmdlineDefIndices.second);
  2340. if (CmdlineDef.empty()) {
  2341. Errs = joinErrors(
  2342. std::move(Errs),
  2343. ErrorDiagnostic::get(SM, CmdlineDef,
  2344. "missing equal sign in global definition"));
  2345. continue;
  2346. }
  2347. // Numeric variable definition.
  2348. if (CmdlineDef[0] == '#') {
  2349. // Now parse the definition both to check that the syntax is correct and
  2350. // to create the necessary class instance.
  2351. StringRef CmdlineDefExpr = CmdlineDef.substr(1);
  2352. Optional<NumericVariable *> DefinedNumericVariable;
  2353. Expected<std::unique_ptr<Expression>> ExpressionResult =
  2354. Pattern::parseNumericSubstitutionBlock(
  2355. CmdlineDefExpr, DefinedNumericVariable, false, None, this, SM);
  2356. if (!ExpressionResult) {
  2357. Errs = joinErrors(std::move(Errs), ExpressionResult.takeError());
  2358. continue;
  2359. }
  2360. std::unique_ptr<Expression> Expression = std::move(*ExpressionResult);
  2361. // Now evaluate the expression whose value this variable should be set
  2362. // to, since the expression of a command-line variable definition should
  2363. // only use variables defined earlier on the command-line. If not, this
  2364. // is an error and we report it.
  2365. Expected<ExpressionValue> Value = Expression->getAST()->eval();
  2366. if (!Value) {
  2367. Errs = joinErrors(std::move(Errs), Value.takeError());
  2368. continue;
  2369. }
  2370. assert(DefinedNumericVariable && "No variable defined");
  2371. (*DefinedNumericVariable)->setValue(*Value);
  2372. // Record this variable definition.
  2373. GlobalNumericVariableTable[(*DefinedNumericVariable)->getName()] =
  2374. *DefinedNumericVariable;
  2375. } else {
  2376. // String variable definition.
  2377. std::pair<StringRef, StringRef> CmdlineNameVal = CmdlineDef.split('=');
  2378. StringRef CmdlineName = CmdlineNameVal.first;
  2379. StringRef OrigCmdlineName = CmdlineName;
  2380. Expected<Pattern::VariableProperties> ParseVarResult =
  2381. Pattern::parseVariable(CmdlineName, SM);
  2382. if (!ParseVarResult) {
  2383. Errs = joinErrors(std::move(Errs), ParseVarResult.takeError());
  2384. continue;
  2385. }
  2386. // Check that CmdlineName does not denote a pseudo variable is only
  2387. // composed of the parsed numeric variable. This catches cases like
  2388. // "FOO+2" in a "FOO+2=10" definition.
  2389. if (ParseVarResult->IsPseudo || !CmdlineName.empty()) {
  2390. Errs = joinErrors(std::move(Errs),
  2391. ErrorDiagnostic::get(
  2392. SM, OrigCmdlineName,
  2393. "invalid name in string variable definition '" +
  2394. OrigCmdlineName + "'"));
  2395. continue;
  2396. }
  2397. StringRef Name = ParseVarResult->Name;
  2398. // Detect collisions between string and numeric variables when the former
  2399. // is created later than the latter.
  2400. if (GlobalNumericVariableTable.find(Name) !=
  2401. GlobalNumericVariableTable.end()) {
  2402. Errs = joinErrors(std::move(Errs),
  2403. ErrorDiagnostic::get(SM, Name,
  2404. "numeric variable with name '" +
  2405. Name + "' already exists"));
  2406. continue;
  2407. }
  2408. GlobalVariableTable.insert(CmdlineNameVal);
  2409. // Mark the string variable as defined to detect collisions between
  2410. // string and numeric variables in defineCmdlineVariables when the latter
  2411. // is created later than the former. We cannot reuse GlobalVariableTable
  2412. // for this by populating it with an empty string since we would then
  2413. // lose the ability to detect the use of an undefined variable in
  2414. // match().
  2415. DefinedVariableTable[Name] = true;
  2416. }
  2417. }
  2418. return Errs;
  2419. }
  2420. void FileCheckPatternContext::clearLocalVars() {
  2421. SmallVector<StringRef, 16> LocalPatternVars, LocalNumericVars;
  2422. for (const StringMapEntry<StringRef> &Var : GlobalVariableTable)
  2423. if (Var.first()[0] != '$')
  2424. LocalPatternVars.push_back(Var.first());
  2425. // Numeric substitution reads the value of a variable directly, not via
  2426. // GlobalNumericVariableTable. Therefore, we clear local variables by
  2427. // clearing their value which will lead to a numeric substitution failure. We
  2428. // also mark the variable for removal from GlobalNumericVariableTable since
  2429. // this is what defineCmdlineVariables checks to decide that no global
  2430. // variable has been defined.
  2431. for (const auto &Var : GlobalNumericVariableTable)
  2432. if (Var.first()[0] != '$') {
  2433. Var.getValue()->clearValue();
  2434. LocalNumericVars.push_back(Var.first());
  2435. }
  2436. for (const auto &Var : LocalPatternVars)
  2437. GlobalVariableTable.erase(Var);
  2438. for (const auto &Var : LocalNumericVars)
  2439. GlobalNumericVariableTable.erase(Var);
  2440. }
  2441. bool FileCheck::checkInput(SourceMgr &SM, StringRef Buffer,
  2442. std::vector<FileCheckDiag> *Diags) {
  2443. bool ChecksFailed = false;
  2444. unsigned i = 0, j = 0, e = CheckStrings->size();
  2445. while (true) {
  2446. StringRef CheckRegion;
  2447. if (j == e) {
  2448. CheckRegion = Buffer;
  2449. } else {
  2450. const FileCheckString &CheckLabelStr = (*CheckStrings)[j];
  2451. if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) {
  2452. ++j;
  2453. continue;
  2454. }
  2455. // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
  2456. size_t MatchLabelLen = 0;
  2457. size_t MatchLabelPos =
  2458. CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, Req, Diags);
  2459. if (MatchLabelPos == StringRef::npos)
  2460. // Immediately bail if CHECK-LABEL fails, nothing else we can do.
  2461. return false;
  2462. CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
  2463. Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
  2464. ++j;
  2465. }
  2466. // Do not clear the first region as it's the one before the first
  2467. // CHECK-LABEL and it would clear variables defined on the command-line
  2468. // before they get used.
  2469. if (i != 0 && Req.EnableVarScope)
  2470. PatternContext->clearLocalVars();
  2471. for (; i != j; ++i) {
  2472. const FileCheckString &CheckStr = (*CheckStrings)[i];
  2473. // Check each string within the scanned region, including a second check
  2474. // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
  2475. size_t MatchLen = 0;
  2476. size_t MatchPos =
  2477. CheckStr.Check(SM, CheckRegion, false, MatchLen, Req, Diags);
  2478. if (MatchPos == StringRef::npos) {
  2479. ChecksFailed = true;
  2480. i = j;
  2481. break;
  2482. }
  2483. CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
  2484. }
  2485. if (j == e)
  2486. break;
  2487. }
  2488. // Success if no checks failed.
  2489. return !ChecksFailed;
  2490. }