VerifyDiagnosticConsumer.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. //===- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ---------===//
  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. // This is a concrete diagnostic client, which buffers the diagnostic messages.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Frontend/VerifyDiagnosticConsumer.h"
  13. #include "clang/Basic/CharInfo.h"
  14. #include "clang/Basic/Diagnostic.h"
  15. #include "clang/Basic/DiagnosticOptions.h"
  16. #include "clang/Basic/FileManager.h"
  17. #include "clang/Basic/LLVM.h"
  18. #include "clang/Basic/SourceLocation.h"
  19. #include "clang/Basic/SourceManager.h"
  20. #include "clang/Basic/TokenKinds.h"
  21. #include "clang/Frontend/FrontendDiagnostic.h"
  22. #include "clang/Frontend/TextDiagnosticBuffer.h"
  23. #include "clang/Lex/HeaderSearch.h"
  24. #include "clang/Lex/Lexer.h"
  25. #include "clang/Lex/PPCallbacks.h"
  26. #include "clang/Lex/Preprocessor.h"
  27. #include "clang/Lex/Token.h"
  28. #include "llvm/ADT/STLExtras.h"
  29. #include "llvm/ADT/SmallPtrSet.h"
  30. #include "llvm/ADT/SmallString.h"
  31. #include "llvm/ADT/StringRef.h"
  32. #include "llvm/ADT/Twine.h"
  33. #include "llvm/Support/ErrorHandling.h"
  34. #include "llvm/Support/Regex.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. #include <algorithm>
  37. #include <cassert>
  38. #include <cstddef>
  39. #include <cstring>
  40. #include <iterator>
  41. #include <memory>
  42. #include <string>
  43. #include <utility>
  44. #include <vector>
  45. using namespace clang;
  46. using Directive = VerifyDiagnosticConsumer::Directive;
  47. using DirectiveList = VerifyDiagnosticConsumer::DirectiveList;
  48. using ExpectedData = VerifyDiagnosticConsumer::ExpectedData;
  49. #ifndef NDEBUG
  50. namespace {
  51. class VerifyFileTracker : public PPCallbacks {
  52. VerifyDiagnosticConsumer &Verify;
  53. SourceManager &SM;
  54. public:
  55. VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM)
  56. : Verify(Verify), SM(SM) {}
  57. /// Hook into the preprocessor and update the list of parsed
  58. /// files when the preprocessor indicates a new file is entered.
  59. void FileChanged(SourceLocation Loc, FileChangeReason Reason,
  60. SrcMgr::CharacteristicKind FileType,
  61. FileID PrevFID) override {
  62. Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc),
  63. VerifyDiagnosticConsumer::IsParsed);
  64. }
  65. };
  66. } // namespace
  67. #endif
  68. //===----------------------------------------------------------------------===//
  69. // Checking diagnostics implementation.
  70. //===----------------------------------------------------------------------===//
  71. using DiagList = TextDiagnosticBuffer::DiagList;
  72. using const_diag_iterator = TextDiagnosticBuffer::const_iterator;
  73. namespace {
  74. /// StandardDirective - Directive with string matching.
  75. class StandardDirective : public Directive {
  76. public:
  77. StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
  78. bool MatchAnyLine, StringRef Text, unsigned Min,
  79. unsigned Max)
  80. : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max) {}
  81. bool isValid(std::string &Error) override {
  82. // all strings are considered valid; even empty ones
  83. return true;
  84. }
  85. bool match(StringRef S) override {
  86. return S.find(Text) != StringRef::npos;
  87. }
  88. };
  89. /// RegexDirective - Directive with regular-expression matching.
  90. class RegexDirective : public Directive {
  91. public:
  92. RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
  93. bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max,
  94. StringRef RegexStr)
  95. : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max),
  96. Regex(RegexStr) {}
  97. bool isValid(std::string &Error) override {
  98. return Regex.isValid(Error);
  99. }
  100. bool match(StringRef S) override {
  101. return Regex.match(S);
  102. }
  103. private:
  104. llvm::Regex Regex;
  105. };
  106. class ParseHelper
  107. {
  108. public:
  109. ParseHelper(StringRef S)
  110. : Begin(S.begin()), End(S.end()), C(Begin), P(Begin) {}
  111. // Return true if string literal is next.
  112. bool Next(StringRef S) {
  113. P = C;
  114. PEnd = C + S.size();
  115. if (PEnd > End)
  116. return false;
  117. return memcmp(P, S.data(), S.size()) == 0;
  118. }
  119. // Return true if number is next.
  120. // Output N only if number is next.
  121. bool Next(unsigned &N) {
  122. unsigned TMP = 0;
  123. P = C;
  124. PEnd = P;
  125. for (; PEnd < End && *PEnd >= '0' && *PEnd <= '9'; ++PEnd) {
  126. TMP *= 10;
  127. TMP += *PEnd - '0';
  128. }
  129. if (PEnd == C)
  130. return false;
  131. N = TMP;
  132. return true;
  133. }
  134. // Return true if a marker is next.
  135. // A marker is the longest match for /#[A-Za-z0-9_-]+/.
  136. bool NextMarker() {
  137. P = C;
  138. if (P == End || *P != '#')
  139. return false;
  140. PEnd = P;
  141. ++PEnd;
  142. while ((isAlphanumeric(*PEnd) || *PEnd == '-' || *PEnd == '_') &&
  143. PEnd < End)
  144. ++PEnd;
  145. return PEnd > P + 1;
  146. }
  147. // Return true if string literal S is matched in content.
  148. // When true, P marks begin-position of the match, and calling Advance sets C
  149. // to end-position of the match.
  150. // If S is the empty string, then search for any letter instead (makes sense
  151. // with FinishDirectiveToken=true).
  152. // If EnsureStartOfWord, then skip matches that don't start a new word.
  153. // If FinishDirectiveToken, then assume the match is the start of a comment
  154. // directive for -verify, and extend the match to include the entire first
  155. // token of that directive.
  156. bool Search(StringRef S, bool EnsureStartOfWord = false,
  157. bool FinishDirectiveToken = false) {
  158. do {
  159. if (!S.empty()) {
  160. P = std::search(C, End, S.begin(), S.end());
  161. PEnd = P + S.size();
  162. }
  163. else {
  164. P = C;
  165. while (P != End && !isLetter(*P))
  166. ++P;
  167. PEnd = P + 1;
  168. }
  169. if (P == End)
  170. break;
  171. // If not start of word but required, skip and search again.
  172. if (EnsureStartOfWord
  173. // Check if string literal starts a new word.
  174. && !(P == Begin || isWhitespace(P[-1])
  175. // Or it could be preceded by the start of a comment.
  176. || (P > (Begin + 1) && (P[-1] == '/' || P[-1] == '*')
  177. && P[-2] == '/')))
  178. continue;
  179. if (FinishDirectiveToken) {
  180. while (PEnd != End && (isAlphanumeric(*PEnd)
  181. || *PEnd == '-' || *PEnd == '_'))
  182. ++PEnd;
  183. // Put back trailing digits and hyphens to be parsed later as a count
  184. // or count range. Because -verify prefixes must start with letters,
  185. // we know the actual directive we found starts with a letter, so
  186. // we won't put back the entire directive word and thus record an empty
  187. // string.
  188. assert(isLetter(*P) && "-verify prefix must start with a letter");
  189. while (isDigit(PEnd[-1]) || PEnd[-1] == '-')
  190. --PEnd;
  191. }
  192. return true;
  193. } while (Advance());
  194. return false;
  195. }
  196. // Return true if a CloseBrace that closes the OpenBrace at the current nest
  197. // level is found. When true, P marks begin-position of CloseBrace.
  198. bool SearchClosingBrace(StringRef OpenBrace, StringRef CloseBrace) {
  199. unsigned Depth = 1;
  200. P = C;
  201. while (P < End) {
  202. StringRef S(P, End - P);
  203. if (S.startswith(OpenBrace)) {
  204. ++Depth;
  205. P += OpenBrace.size();
  206. } else if (S.startswith(CloseBrace)) {
  207. --Depth;
  208. if (Depth == 0) {
  209. PEnd = P + CloseBrace.size();
  210. return true;
  211. }
  212. P += CloseBrace.size();
  213. } else {
  214. ++P;
  215. }
  216. }
  217. return false;
  218. }
  219. // Advance 1-past previous next/search.
  220. // Behavior is undefined if previous next/search failed.
  221. bool Advance() {
  222. C = PEnd;
  223. return C < End;
  224. }
  225. // Return the text matched by the previous next/search.
  226. // Behavior is undefined if previous next/search failed.
  227. StringRef Match() { return StringRef(P, PEnd - P); }
  228. // Skip zero or more whitespace.
  229. void SkipWhitespace() {
  230. for (; C < End && isWhitespace(*C); ++C)
  231. ;
  232. }
  233. // Return true if EOF reached.
  234. bool Done() {
  235. return !(C < End);
  236. }
  237. // Beginning of expected content.
  238. const char * const Begin;
  239. // End of expected content (1-past).
  240. const char * const End;
  241. // Position of next char in content.
  242. const char *C;
  243. // Previous next/search subject start.
  244. const char *P;
  245. private:
  246. // Previous next/search subject end (1-past).
  247. const char *PEnd = nullptr;
  248. };
  249. // The information necessary to create a directive.
  250. struct UnattachedDirective {
  251. DirectiveList *DL = nullptr;
  252. bool RegexKind = false;
  253. SourceLocation DirectivePos, ContentBegin;
  254. std::string Text;
  255. unsigned Min = 1, Max = 1;
  256. };
  257. // Attach the specified directive to the line of code indicated by
  258. // \p ExpectedLoc.
  259. void attachDirective(DiagnosticsEngine &Diags, const UnattachedDirective &UD,
  260. SourceLocation ExpectedLoc, bool MatchAnyLine = false) {
  261. // Construct new directive.
  262. std::unique_ptr<Directive> D =
  263. Directive::create(UD.RegexKind, UD.DirectivePos, ExpectedLoc,
  264. MatchAnyLine, UD.Text, UD.Min, UD.Max);
  265. std::string Error;
  266. if (!D->isValid(Error)) {
  267. Diags.Report(UD.ContentBegin, diag::err_verify_invalid_content)
  268. << (UD.RegexKind ? "regex" : "string") << Error;
  269. }
  270. UD.DL->push_back(std::move(D));
  271. }
  272. } // anonymous
  273. // Tracker for markers in the input files. A marker is a comment of the form
  274. //
  275. // n = 123; // #123
  276. //
  277. // ... that can be referred to by a later expected-* directive:
  278. //
  279. // // expected-error@#123 {{undeclared identifier 'n'}}
  280. //
  281. // Marker declarations must be at the start of a comment or preceded by
  282. // whitespace to distinguish them from uses of markers in directives.
  283. class VerifyDiagnosticConsumer::MarkerTracker {
  284. DiagnosticsEngine &Diags;
  285. struct Marker {
  286. SourceLocation DefLoc;
  287. SourceLocation RedefLoc;
  288. SourceLocation UseLoc;
  289. };
  290. llvm::StringMap<Marker> Markers;
  291. // Directives that couldn't be created yet because they name an unknown
  292. // marker.
  293. llvm::StringMap<llvm::SmallVector<UnattachedDirective, 2>> DeferredDirectives;
  294. public:
  295. MarkerTracker(DiagnosticsEngine &Diags) : Diags(Diags) {}
  296. // Register a marker.
  297. void addMarker(StringRef MarkerName, SourceLocation Pos) {
  298. auto InsertResult = Markers.insert(
  299. {MarkerName, Marker{Pos, SourceLocation(), SourceLocation()}});
  300. Marker &M = InsertResult.first->second;
  301. if (!InsertResult.second) {
  302. // Marker was redefined.
  303. M.RedefLoc = Pos;
  304. } else {
  305. // First definition: build any deferred directives.
  306. auto Deferred = DeferredDirectives.find(MarkerName);
  307. if (Deferred != DeferredDirectives.end()) {
  308. for (auto &UD : Deferred->second) {
  309. if (M.UseLoc.isInvalid())
  310. M.UseLoc = UD.DirectivePos;
  311. attachDirective(Diags, UD, Pos);
  312. }
  313. DeferredDirectives.erase(Deferred);
  314. }
  315. }
  316. }
  317. // Register a directive at the specified marker.
  318. void addDirective(StringRef MarkerName, const UnattachedDirective &UD) {
  319. auto MarkerIt = Markers.find(MarkerName);
  320. if (MarkerIt != Markers.end()) {
  321. Marker &M = MarkerIt->second;
  322. if (M.UseLoc.isInvalid())
  323. M.UseLoc = UD.DirectivePos;
  324. return attachDirective(Diags, UD, M.DefLoc);
  325. }
  326. DeferredDirectives[MarkerName].push_back(UD);
  327. }
  328. // Ensure we have no remaining deferred directives, and no
  329. // multiply-defined-and-used markers.
  330. void finalize() {
  331. for (auto &MarkerInfo : Markers) {
  332. StringRef Name = MarkerInfo.first();
  333. Marker &M = MarkerInfo.second;
  334. if (M.RedefLoc.isValid() && M.UseLoc.isValid()) {
  335. Diags.Report(M.UseLoc, diag::err_verify_ambiguous_marker) << Name;
  336. Diags.Report(M.DefLoc, diag::note_verify_ambiguous_marker) << Name;
  337. Diags.Report(M.RedefLoc, diag::note_verify_ambiguous_marker) << Name;
  338. }
  339. }
  340. for (auto &DeferredPair : DeferredDirectives) {
  341. Diags.Report(DeferredPair.second.front().DirectivePos,
  342. diag::err_verify_no_such_marker)
  343. << DeferredPair.first();
  344. }
  345. }
  346. };
  347. /// ParseDirective - Go through the comment and see if it indicates expected
  348. /// diagnostics. If so, then put them in the appropriate directive list.
  349. ///
  350. /// Returns true if any valid directives were found.
  351. static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
  352. Preprocessor *PP, SourceLocation Pos,
  353. VerifyDiagnosticConsumer::DirectiveStatus &Status,
  354. VerifyDiagnosticConsumer::MarkerTracker &Markers) {
  355. DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics();
  356. // First, scan the comment looking for markers.
  357. for (ParseHelper PH(S); !PH.Done();) {
  358. if (!PH.Search("#", true))
  359. break;
  360. PH.C = PH.P;
  361. if (!PH.NextMarker()) {
  362. PH.Next("#");
  363. PH.Advance();
  364. continue;
  365. }
  366. PH.Advance();
  367. Markers.addMarker(PH.Match(), Pos);
  368. }
  369. // A single comment may contain multiple directives.
  370. bool FoundDirective = false;
  371. for (ParseHelper PH(S); !PH.Done();) {
  372. // Search for the initial directive token.
  373. // If one prefix, save time by searching only for its directives.
  374. // Otherwise, search for any potential directive token and check it later.
  375. const auto &Prefixes = Diags.getDiagnosticOptions().VerifyPrefixes;
  376. if (!(Prefixes.size() == 1 ? PH.Search(*Prefixes.begin(), true, true)
  377. : PH.Search("", true, true)))
  378. break;
  379. StringRef DToken = PH.Match();
  380. PH.Advance();
  381. // Default directive kind.
  382. UnattachedDirective D;
  383. const char *KindStr = "string";
  384. // Parse the initial directive token in reverse so we can easily determine
  385. // its exact actual prefix. If we were to parse it from the front instead,
  386. // it would be harder to determine where the prefix ends because there
  387. // might be multiple matching -verify prefixes because some might prefix
  388. // others.
  389. // Regex in initial directive token: -re
  390. if (DToken.endswith("-re")) {
  391. D.RegexKind = true;
  392. KindStr = "regex";
  393. DToken = DToken.substr(0, DToken.size()-3);
  394. }
  395. // Type in initial directive token: -{error|warning|note|no-diagnostics}
  396. bool NoDiag = false;
  397. StringRef DType;
  398. if (DToken.endswith(DType="-error"))
  399. D.DL = ED ? &ED->Errors : nullptr;
  400. else if (DToken.endswith(DType="-warning"))
  401. D.DL = ED ? &ED->Warnings : nullptr;
  402. else if (DToken.endswith(DType="-remark"))
  403. D.DL = ED ? &ED->Remarks : nullptr;
  404. else if (DToken.endswith(DType="-note"))
  405. D.DL = ED ? &ED->Notes : nullptr;
  406. else if (DToken.endswith(DType="-no-diagnostics")) {
  407. NoDiag = true;
  408. if (D.RegexKind)
  409. continue;
  410. }
  411. else
  412. continue;
  413. DToken = DToken.substr(0, DToken.size()-DType.size());
  414. // What's left in DToken is the actual prefix. That might not be a -verify
  415. // prefix even if there is only one -verify prefix (for example, the full
  416. // DToken is foo-bar-warning, but foo is the only -verify prefix).
  417. if (!std::binary_search(Prefixes.begin(), Prefixes.end(), DToken))
  418. continue;
  419. if (NoDiag) {
  420. if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
  421. Diags.Report(Pos, diag::err_verify_invalid_no_diags)
  422. << /*IsExpectedNoDiagnostics=*/true;
  423. else
  424. Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
  425. continue;
  426. }
  427. if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
  428. Diags.Report(Pos, diag::err_verify_invalid_no_diags)
  429. << /*IsExpectedNoDiagnostics=*/false;
  430. continue;
  431. }
  432. Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
  433. // If a directive has been found but we're not interested
  434. // in storing the directive information, return now.
  435. if (!D.DL)
  436. return true;
  437. // Next optional token: @
  438. SourceLocation ExpectedLoc;
  439. StringRef Marker;
  440. bool MatchAnyLine = false;
  441. if (!PH.Next("@")) {
  442. ExpectedLoc = Pos;
  443. } else {
  444. PH.Advance();
  445. unsigned Line = 0;
  446. bool FoundPlus = PH.Next("+");
  447. if (FoundPlus || PH.Next("-")) {
  448. // Relative to current line.
  449. PH.Advance();
  450. bool Invalid = false;
  451. unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
  452. if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) {
  453. if (FoundPlus) ExpectedLine += Line;
  454. else ExpectedLine -= Line;
  455. ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
  456. }
  457. } else if (PH.Next(Line)) {
  458. // Absolute line number.
  459. if (Line > 0)
  460. ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
  461. } else if (PH.NextMarker()) {
  462. Marker = PH.Match();
  463. } else if (PP && PH.Search(":")) {
  464. // Specific source file.
  465. StringRef Filename(PH.C, PH.P-PH.C);
  466. PH.Advance();
  467. // Lookup file via Preprocessor, like a #include.
  468. const DirectoryLookup *CurDir;
  469. Optional<FileEntryRef> File =
  470. PP->LookupFile(Pos, Filename, false, nullptr, nullptr, CurDir,
  471. nullptr, nullptr, nullptr, nullptr, nullptr);
  472. if (!File) {
  473. Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
  474. diag::err_verify_missing_file) << Filename << KindStr;
  475. continue;
  476. }
  477. const FileEntry *FE = &File->getFileEntry();
  478. if (SM.translateFile(FE).isInvalid())
  479. SM.createFileID(FE, Pos, SrcMgr::C_User);
  480. if (PH.Next(Line) && Line > 0)
  481. ExpectedLoc = SM.translateFileLineCol(FE, Line, 1);
  482. else if (PH.Next("*")) {
  483. MatchAnyLine = true;
  484. ExpectedLoc = SM.translateFileLineCol(FE, 1, 1);
  485. }
  486. } else if (PH.Next("*")) {
  487. MatchAnyLine = true;
  488. ExpectedLoc = SourceLocation();
  489. }
  490. if (ExpectedLoc.isInvalid() && !MatchAnyLine && Marker.empty()) {
  491. Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
  492. diag::err_verify_missing_line) << KindStr;
  493. continue;
  494. }
  495. PH.Advance();
  496. }
  497. // Skip optional whitespace.
  498. PH.SkipWhitespace();
  499. // Next optional token: positive integer or a '+'.
  500. if (PH.Next(D.Min)) {
  501. PH.Advance();
  502. // A positive integer can be followed by a '+' meaning min
  503. // or more, or by a '-' meaning a range from min to max.
  504. if (PH.Next("+")) {
  505. D.Max = Directive::MaxCount;
  506. PH.Advance();
  507. } else if (PH.Next("-")) {
  508. PH.Advance();
  509. if (!PH.Next(D.Max) || D.Max < D.Min) {
  510. Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
  511. diag::err_verify_invalid_range) << KindStr;
  512. continue;
  513. }
  514. PH.Advance();
  515. } else {
  516. D.Max = D.Min;
  517. }
  518. } else if (PH.Next("+")) {
  519. // '+' on its own means "1 or more".
  520. D.Max = Directive::MaxCount;
  521. PH.Advance();
  522. }
  523. // Skip optional whitespace.
  524. PH.SkipWhitespace();
  525. // Next token: {{
  526. if (!PH.Next("{{")) {
  527. Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
  528. diag::err_verify_missing_start) << KindStr;
  529. continue;
  530. }
  531. PH.Advance();
  532. const char* const ContentBegin = PH.C; // mark content begin
  533. // Search for token: }}
  534. if (!PH.SearchClosingBrace("{{", "}}")) {
  535. Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin),
  536. diag::err_verify_missing_end) << KindStr;
  537. continue;
  538. }
  539. const char* const ContentEnd = PH.P; // mark content end
  540. PH.Advance();
  541. D.DirectivePos = Pos;
  542. D.ContentBegin = Pos.getLocWithOffset(ContentBegin - PH.Begin);
  543. // Build directive text; convert \n to newlines.
  544. StringRef NewlineStr = "\\n";
  545. StringRef Content(ContentBegin, ContentEnd-ContentBegin);
  546. size_t CPos = 0;
  547. size_t FPos;
  548. while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
  549. D.Text += Content.substr(CPos, FPos-CPos);
  550. D.Text += '\n';
  551. CPos = FPos + NewlineStr.size();
  552. }
  553. if (D.Text.empty())
  554. D.Text.assign(ContentBegin, ContentEnd);
  555. // Check that regex directives contain at least one regex.
  556. if (D.RegexKind && D.Text.find("{{") == StringRef::npos) {
  557. Diags.Report(D.ContentBegin, diag::err_verify_missing_regex) << D.Text;
  558. return false;
  559. }
  560. if (Marker.empty())
  561. attachDirective(Diags, D, ExpectedLoc, MatchAnyLine);
  562. else
  563. Markers.addDirective(Marker, D);
  564. FoundDirective = true;
  565. }
  566. return FoundDirective;
  567. }
  568. VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &Diags_)
  569. : Diags(Diags_), PrimaryClient(Diags.getClient()),
  570. PrimaryClientOwner(Diags.takeClient()),
  571. Buffer(new TextDiagnosticBuffer()), Markers(new MarkerTracker(Diags)),
  572. Status(HasNoDirectives) {
  573. if (Diags.hasSourceManager())
  574. setSourceManager(Diags.getSourceManager());
  575. }
  576. VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
  577. assert(!ActiveSourceFiles && "Incomplete parsing of source files!");
  578. assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!");
  579. SrcManager = nullptr;
  580. CheckDiagnostics();
  581. assert(!Diags.ownsClient() &&
  582. "The VerifyDiagnosticConsumer takes over ownership of the client!");
  583. }
  584. // DiagnosticConsumer interface.
  585. void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
  586. const Preprocessor *PP) {
  587. // Attach comment handler on first invocation.
  588. if (++ActiveSourceFiles == 1) {
  589. if (PP) {
  590. CurrentPreprocessor = PP;
  591. this->LangOpts = &LangOpts;
  592. setSourceManager(PP->getSourceManager());
  593. const_cast<Preprocessor *>(PP)->addCommentHandler(this);
  594. #ifndef NDEBUG
  595. // Debug build tracks parsed files.
  596. const_cast<Preprocessor *>(PP)->addPPCallbacks(
  597. std::make_unique<VerifyFileTracker>(*this, *SrcManager));
  598. #endif
  599. }
  600. }
  601. assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!");
  602. PrimaryClient->BeginSourceFile(LangOpts, PP);
  603. }
  604. void VerifyDiagnosticConsumer::EndSourceFile() {
  605. assert(ActiveSourceFiles && "No active source files!");
  606. PrimaryClient->EndSourceFile();
  607. // Detach comment handler once last active source file completed.
  608. if (--ActiveSourceFiles == 0) {
  609. if (CurrentPreprocessor)
  610. const_cast<Preprocessor *>(CurrentPreprocessor)->
  611. removeCommentHandler(this);
  612. // Diagnose any used-but-not-defined markers.
  613. Markers->finalize();
  614. // Check diagnostics once last file completed.
  615. CheckDiagnostics();
  616. CurrentPreprocessor = nullptr;
  617. LangOpts = nullptr;
  618. }
  619. }
  620. void VerifyDiagnosticConsumer::HandleDiagnostic(
  621. DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
  622. if (Info.hasSourceManager()) {
  623. // If this diagnostic is for a different source manager, ignore it.
  624. if (SrcManager && &Info.getSourceManager() != SrcManager)
  625. return;
  626. setSourceManager(Info.getSourceManager());
  627. }
  628. #ifndef NDEBUG
  629. // Debug build tracks unparsed files for possible
  630. // unparsed expected-* directives.
  631. if (SrcManager) {
  632. SourceLocation Loc = Info.getLocation();
  633. if (Loc.isValid()) {
  634. ParsedStatus PS = IsUnparsed;
  635. Loc = SrcManager->getExpansionLoc(Loc);
  636. FileID FID = SrcManager->getFileID(Loc);
  637. const FileEntry *FE = SrcManager->getFileEntryForID(FID);
  638. if (FE && CurrentPreprocessor && SrcManager->isLoadedFileID(FID)) {
  639. // If the file is a modules header file it shall not be parsed
  640. // for expected-* directives.
  641. HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo();
  642. if (HS.findModuleForHeader(FE))
  643. PS = IsUnparsedNoDirectives;
  644. }
  645. UpdateParsedFileStatus(*SrcManager, FID, PS);
  646. }
  647. }
  648. #endif
  649. // Send the diagnostic to the buffer, we will check it once we reach the end
  650. // of the source file (or are destructed).
  651. Buffer->HandleDiagnostic(DiagLevel, Info);
  652. }
  653. /// HandleComment - Hook into the preprocessor and extract comments containing
  654. /// expected errors and warnings.
  655. bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP,
  656. SourceRange Comment) {
  657. SourceManager &SM = PP.getSourceManager();
  658. // If this comment is for a different source manager, ignore it.
  659. if (SrcManager && &SM != SrcManager)
  660. return false;
  661. SourceLocation CommentBegin = Comment.getBegin();
  662. const char *CommentRaw = SM.getCharacterData(CommentBegin);
  663. StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw);
  664. if (C.empty())
  665. return false;
  666. // Fold any "\<EOL>" sequences
  667. size_t loc = C.find('\\');
  668. if (loc == StringRef::npos) {
  669. ParseDirective(C, &ED, SM, &PP, CommentBegin, Status, *Markers);
  670. return false;
  671. }
  672. std::string C2;
  673. C2.reserve(C.size());
  674. for (size_t last = 0;; loc = C.find('\\', last)) {
  675. if (loc == StringRef::npos || loc == C.size()) {
  676. C2 += C.substr(last);
  677. break;
  678. }
  679. C2 += C.substr(last, loc-last);
  680. last = loc + 1;
  681. if (C[last] == '\n' || C[last] == '\r') {
  682. ++last;
  683. // Escape \r\n or \n\r, but not \n\n.
  684. if (last < C.size())
  685. if (C[last] == '\n' || C[last] == '\r')
  686. if (C[last] != C[last-1])
  687. ++last;
  688. } else {
  689. // This was just a normal backslash.
  690. C2 += '\\';
  691. }
  692. }
  693. if (!C2.empty())
  694. ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status, *Markers);
  695. return false;
  696. }
  697. #ifndef NDEBUG
  698. /// Lex the specified source file to determine whether it contains
  699. /// any expected-* directives. As a Lexer is used rather than a full-blown
  700. /// Preprocessor, directives inside skipped #if blocks will still be found.
  701. ///
  702. /// \return true if any directives were found.
  703. static bool findDirectives(SourceManager &SM, FileID FID,
  704. const LangOptions &LangOpts) {
  705. // Create a raw lexer to pull all the comments out of FID.
  706. if (FID.isInvalid())
  707. return false;
  708. // Create a lexer to lex all the tokens of the main file in raw mode.
  709. const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
  710. Lexer RawLex(FID, FromFile, SM, LangOpts);
  711. // Return comments as tokens, this is how we find expected diagnostics.
  712. RawLex.SetCommentRetentionState(true);
  713. Token Tok;
  714. Tok.setKind(tok::comment);
  715. VerifyDiagnosticConsumer::DirectiveStatus Status =
  716. VerifyDiagnosticConsumer::HasNoDirectives;
  717. while (Tok.isNot(tok::eof)) {
  718. RawLex.LexFromRawLexer(Tok);
  719. if (!Tok.is(tok::comment)) continue;
  720. std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
  721. if (Comment.empty()) continue;
  722. // We don't care about tracking markers for this phase.
  723. VerifyDiagnosticConsumer::MarkerTracker Markers(SM.getDiagnostics());
  724. // Find first directive.
  725. if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(),
  726. Status, Markers))
  727. return true;
  728. }
  729. return false;
  730. }
  731. #endif // !NDEBUG
  732. /// Takes a list of diagnostics that have been generated but not matched
  733. /// by an expected-* directive and produces a diagnostic to the user from this.
  734. static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
  735. const_diag_iterator diag_begin,
  736. const_diag_iterator diag_end,
  737. const char *Kind) {
  738. if (diag_begin == diag_end) return 0;
  739. SmallString<256> Fmt;
  740. llvm::raw_svector_ostream OS(Fmt);
  741. for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
  742. if (I->first.isInvalid() || !SourceMgr)
  743. OS << "\n (frontend)";
  744. else {
  745. OS << "\n ";
  746. if (const FileEntry *File = SourceMgr->getFileEntryForID(
  747. SourceMgr->getFileID(I->first)))
  748. OS << " File " << File->getName();
  749. OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
  750. }
  751. OS << ": " << I->second;
  752. }
  753. Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
  754. << Kind << /*Unexpected=*/true << OS.str();
  755. return std::distance(diag_begin, diag_end);
  756. }
  757. /// Takes a list of diagnostics that were expected to have been generated
  758. /// but were not and produces a diagnostic to the user from this.
  759. static unsigned PrintExpected(DiagnosticsEngine &Diags,
  760. SourceManager &SourceMgr,
  761. std::vector<Directive *> &DL, const char *Kind) {
  762. if (DL.empty())
  763. return 0;
  764. SmallString<256> Fmt;
  765. llvm::raw_svector_ostream OS(Fmt);
  766. for (const auto *D : DL) {
  767. if (D->DiagnosticLoc.isInvalid())
  768. OS << "\n File *";
  769. else
  770. OS << "\n File " << SourceMgr.getFilename(D->DiagnosticLoc);
  771. if (D->MatchAnyLine)
  772. OS << " Line *";
  773. else
  774. OS << " Line " << SourceMgr.getPresumedLineNumber(D->DiagnosticLoc);
  775. if (D->DirectiveLoc != D->DiagnosticLoc)
  776. OS << " (directive at "
  777. << SourceMgr.getFilename(D->DirectiveLoc) << ':'
  778. << SourceMgr.getPresumedLineNumber(D->DirectiveLoc) << ')';
  779. OS << ": " << D->Text;
  780. }
  781. Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
  782. << Kind << /*Unexpected=*/false << OS.str();
  783. return DL.size();
  784. }
  785. /// Determine whether two source locations come from the same file.
  786. static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc,
  787. SourceLocation DiagnosticLoc) {
  788. while (DiagnosticLoc.isMacroID())
  789. DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc);
  790. if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc))
  791. return true;
  792. const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc));
  793. if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc))
  794. return true;
  795. return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc)));
  796. }
  797. /// CheckLists - Compare expected to seen diagnostic lists and return the
  798. /// the difference between them.
  799. static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
  800. const char *Label,
  801. DirectiveList &Left,
  802. const_diag_iterator d2_begin,
  803. const_diag_iterator d2_end,
  804. bool IgnoreUnexpected) {
  805. std::vector<Directive *> LeftOnly;
  806. DiagList Right(d2_begin, d2_end);
  807. for (auto &Owner : Left) {
  808. Directive &D = *Owner;
  809. unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
  810. for (unsigned i = 0; i < D.Max; ++i) {
  811. DiagList::iterator II, IE;
  812. for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
  813. if (!D.MatchAnyLine) {
  814. unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
  815. if (LineNo1 != LineNo2)
  816. continue;
  817. }
  818. if (!D.DiagnosticLoc.isInvalid() &&
  819. !IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first))
  820. continue;
  821. const std::string &RightText = II->second;
  822. if (D.match(RightText))
  823. break;
  824. }
  825. if (II == IE) {
  826. // Not found.
  827. if (i >= D.Min) break;
  828. LeftOnly.push_back(&D);
  829. } else {
  830. // Found. The same cannot be found twice.
  831. Right.erase(II);
  832. }
  833. }
  834. }
  835. // Now all that's left in Right are those that were not matched.
  836. unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label);
  837. if (!IgnoreUnexpected)
  838. num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label);
  839. return num;
  840. }
  841. /// CheckResults - This compares the expected results to those that
  842. /// were actually reported. It emits any discrepencies. Return "true" if there
  843. /// were problems. Return "false" otherwise.
  844. static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
  845. const TextDiagnosticBuffer &Buffer,
  846. ExpectedData &ED) {
  847. // We want to capture the delta between what was expected and what was
  848. // seen.
  849. //
  850. // Expected \ Seen - set expected but not seen
  851. // Seen \ Expected - set seen but not expected
  852. unsigned NumProblems = 0;
  853. const DiagnosticLevelMask DiagMask =
  854. Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected();
  855. // See if there are error mismatches.
  856. NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
  857. Buffer.err_begin(), Buffer.err_end(),
  858. bool(DiagnosticLevelMask::Error & DiagMask));
  859. // See if there are warning mismatches.
  860. NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
  861. Buffer.warn_begin(), Buffer.warn_end(),
  862. bool(DiagnosticLevelMask::Warning & DiagMask));
  863. // See if there are remark mismatches.
  864. NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks,
  865. Buffer.remark_begin(), Buffer.remark_end(),
  866. bool(DiagnosticLevelMask::Remark & DiagMask));
  867. // See if there are note mismatches.
  868. NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
  869. Buffer.note_begin(), Buffer.note_end(),
  870. bool(DiagnosticLevelMask::Note & DiagMask));
  871. return NumProblems;
  872. }
  873. void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM,
  874. FileID FID,
  875. ParsedStatus PS) {
  876. // Check SourceManager hasn't changed.
  877. setSourceManager(SM);
  878. #ifndef NDEBUG
  879. if (FID.isInvalid())
  880. return;
  881. const FileEntry *FE = SM.getFileEntryForID(FID);
  882. if (PS == IsParsed) {
  883. // Move the FileID from the unparsed set to the parsed set.
  884. UnparsedFiles.erase(FID);
  885. ParsedFiles.insert(std::make_pair(FID, FE));
  886. } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) {
  887. // Add the FileID to the unparsed set if we haven't seen it before.
  888. // Check for directives.
  889. bool FoundDirectives;
  890. if (PS == IsUnparsedNoDirectives)
  891. FoundDirectives = false;
  892. else
  893. FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts);
  894. // Add the FileID to the unparsed set.
  895. UnparsedFiles.insert(std::make_pair(FID,
  896. UnparsedFileStatus(FE, FoundDirectives)));
  897. }
  898. #endif
  899. }
  900. void VerifyDiagnosticConsumer::CheckDiagnostics() {
  901. // Ensure any diagnostics go to the primary client.
  902. DiagnosticConsumer *CurClient = Diags.getClient();
  903. std::unique_ptr<DiagnosticConsumer> Owner = Diags.takeClient();
  904. Diags.setClient(PrimaryClient, false);
  905. #ifndef NDEBUG
  906. // In a debug build, scan through any files that may have been missed
  907. // during parsing and issue a fatal error if directives are contained
  908. // within these files. If a fatal error occurs, this suggests that
  909. // this file is being parsed separately from the main file, in which
  910. // case consider moving the directives to the correct place, if this
  911. // is applicable.
  912. if (!UnparsedFiles.empty()) {
  913. // Generate a cache of parsed FileEntry pointers for alias lookups.
  914. llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache;
  915. for (const auto &I : ParsedFiles)
  916. if (const FileEntry *FE = I.second)
  917. ParsedFileCache.insert(FE);
  918. // Iterate through list of unparsed files.
  919. for (const auto &I : UnparsedFiles) {
  920. const UnparsedFileStatus &Status = I.second;
  921. const FileEntry *FE = Status.getFile();
  922. // Skip files that have been parsed via an alias.
  923. if (FE && ParsedFileCache.count(FE))
  924. continue;
  925. // Report a fatal error if this file contained directives.
  926. if (Status.foundDirectives()) {
  927. llvm::report_fatal_error(Twine("-verify directives found after rather"
  928. " than during normal parsing of ",
  929. StringRef(FE ? FE->getName() : "(unknown)")));
  930. }
  931. }
  932. // UnparsedFiles has been processed now, so clear it.
  933. UnparsedFiles.clear();
  934. }
  935. #endif // !NDEBUG
  936. if (SrcManager) {
  937. // Produce an error if no expected-* directives could be found in the
  938. // source file(s) processed.
  939. if (Status == HasNoDirectives) {
  940. Diags.Report(diag::err_verify_no_directives).setForceEmit();
  941. ++NumErrors;
  942. Status = HasNoDirectivesReported;
  943. }
  944. // Check that the expected diagnostics occurred.
  945. NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED);
  946. } else {
  947. const DiagnosticLevelMask DiagMask =
  948. ~Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected();
  949. if (bool(DiagnosticLevelMask::Error & DiagMask))
  950. NumErrors += PrintUnexpected(Diags, nullptr, Buffer->err_begin(),
  951. Buffer->err_end(), "error");
  952. if (bool(DiagnosticLevelMask::Warning & DiagMask))
  953. NumErrors += PrintUnexpected(Diags, nullptr, Buffer->warn_begin(),
  954. Buffer->warn_end(), "warn");
  955. if (bool(DiagnosticLevelMask::Remark & DiagMask))
  956. NumErrors += PrintUnexpected(Diags, nullptr, Buffer->remark_begin(),
  957. Buffer->remark_end(), "remark");
  958. if (bool(DiagnosticLevelMask::Note & DiagMask))
  959. NumErrors += PrintUnexpected(Diags, nullptr, Buffer->note_begin(),
  960. Buffer->note_end(), "note");
  961. }
  962. Diags.setClient(CurClient, Owner.release() != nullptr);
  963. // Reset the buffer, we have processed all the diagnostics in it.
  964. Buffer.reset(new TextDiagnosticBuffer());
  965. ED.Reset();
  966. }
  967. std::unique_ptr<Directive> Directive::create(bool RegexKind,
  968. SourceLocation DirectiveLoc,
  969. SourceLocation DiagnosticLoc,
  970. bool MatchAnyLine, StringRef Text,
  971. unsigned Min, unsigned Max) {
  972. if (!RegexKind)
  973. return std::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc,
  974. MatchAnyLine, Text, Min, Max);
  975. // Parse the directive into a regular expression.
  976. std::string RegexStr;
  977. StringRef S = Text;
  978. while (!S.empty()) {
  979. if (S.startswith("{{")) {
  980. S = S.drop_front(2);
  981. size_t RegexMatchLength = S.find("}}");
  982. assert(RegexMatchLength != StringRef::npos);
  983. // Append the regex, enclosed in parentheses.
  984. RegexStr += "(";
  985. RegexStr.append(S.data(), RegexMatchLength);
  986. RegexStr += ")";
  987. S = S.drop_front(RegexMatchLength + 2);
  988. } else {
  989. size_t VerbatimMatchLength = S.find("{{");
  990. if (VerbatimMatchLength == StringRef::npos)
  991. VerbatimMatchLength = S.size();
  992. // Escape and append the fixed string.
  993. RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength));
  994. S = S.drop_front(VerbatimMatchLength);
  995. }
  996. }
  997. return std::make_unique<RegexDirective>(
  998. DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max, RegexStr);
  999. }