PlistDiagnostics.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. //===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- C++ -*-===//
  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 file defines the PlistDiagnostics object.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Analysis/PathDiagnostic.h"
  13. #include "clang/Basic/FileManager.h"
  14. #include "clang/Basic/PlistSupport.h"
  15. #include "clang/Basic/SourceManager.h"
  16. #include "clang/Basic/Version.h"
  17. #include "clang/CrossTU/CrossTranslationUnit.h"
  18. #include "clang/Frontend/ASTUnit.h"
  19. #include "clang/Lex/Preprocessor.h"
  20. #include "clang/Lex/TokenConcatenation.h"
  21. #include "clang/Rewrite/Core/HTMLRewrite.h"
  22. #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
  23. #include "clang/StaticAnalyzer/Core/IssueHash.h"
  24. #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
  25. #include "llvm/ADT/SmallPtrSet.h"
  26. #include "llvm/ADT/SmallVector.h"
  27. #include "llvm/ADT/Statistic.h"
  28. #include "llvm/Support/Casting.h"
  29. using namespace clang;
  30. using namespace ento;
  31. using namespace markup;
  32. //===----------------------------------------------------------------------===//
  33. // Declarations of helper classes and functions for emitting bug reports in
  34. // plist format.
  35. //===----------------------------------------------------------------------===//
  36. namespace {
  37. class PlistDiagnostics : public PathDiagnosticConsumer {
  38. const std::string OutputFile;
  39. const Preprocessor &PP;
  40. const cross_tu::CrossTranslationUnitContext &CTU;
  41. AnalyzerOptions &AnOpts;
  42. const bool SupportsCrossFileDiagnostics;
  43. public:
  44. PlistDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string &prefix,
  45. const Preprocessor &PP,
  46. const cross_tu::CrossTranslationUnitContext &CTU,
  47. bool supportsMultipleFiles);
  48. ~PlistDiagnostics() override {}
  49. void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
  50. FilesMade *filesMade) override;
  51. StringRef getName() const override {
  52. return "PlistDiagnostics";
  53. }
  54. PathGenerationScheme getGenerationScheme() const override {
  55. return Extensive;
  56. }
  57. bool supportsLogicalOpControlFlow() const override { return true; }
  58. bool supportsCrossFileDiagnostics() const override {
  59. return SupportsCrossFileDiagnostics;
  60. }
  61. };
  62. } // end anonymous namespace
  63. namespace {
  64. /// A helper class for emitting a single report.
  65. class PlistPrinter {
  66. const FIDMap& FM;
  67. AnalyzerOptions &AnOpts;
  68. const Preprocessor &PP;
  69. const cross_tu::CrossTranslationUnitContext &CTU;
  70. llvm::SmallVector<const PathDiagnosticMacroPiece *, 0> MacroPieces;
  71. public:
  72. PlistPrinter(const FIDMap& FM, AnalyzerOptions &AnOpts,
  73. const Preprocessor &PP,
  74. const cross_tu::CrossTranslationUnitContext &CTU)
  75. : FM(FM), AnOpts(AnOpts), PP(PP), CTU(CTU) {
  76. }
  77. void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P) {
  78. ReportPiece(o, P, /*indent*/ 4, /*depth*/ 0, /*includeControlFlow*/ true);
  79. // Don't emit a warning about an unused private field.
  80. (void)AnOpts;
  81. }
  82. /// Print the expansions of the collected macro pieces.
  83. ///
  84. /// Each time ReportDiag is called on a PathDiagnosticMacroPiece (or, if one
  85. /// is found through a call piece, etc), it's subpieces are reported, and the
  86. /// piece itself is collected. Call this function after the entire bugpath
  87. /// was reported.
  88. void ReportMacroExpansions(raw_ostream &o, unsigned indent);
  89. private:
  90. void ReportPiece(raw_ostream &o, const PathDiagnosticPiece &P,
  91. unsigned indent, unsigned depth, bool includeControlFlow,
  92. bool isKeyEvent = false) {
  93. switch (P.getKind()) {
  94. case PathDiagnosticPiece::ControlFlow:
  95. if (includeControlFlow)
  96. ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), indent);
  97. break;
  98. case PathDiagnosticPiece::Call:
  99. ReportCall(o, cast<PathDiagnosticCallPiece>(P), indent,
  100. depth);
  101. break;
  102. case PathDiagnosticPiece::Event:
  103. ReportEvent(o, cast<PathDiagnosticEventPiece>(P), indent, depth,
  104. isKeyEvent);
  105. break;
  106. case PathDiagnosticPiece::Macro:
  107. ReportMacroSubPieces(o, cast<PathDiagnosticMacroPiece>(P), indent,
  108. depth);
  109. break;
  110. case PathDiagnosticPiece::Note:
  111. ReportNote(o, cast<PathDiagnosticNotePiece>(P), indent);
  112. break;
  113. case PathDiagnosticPiece::PopUp:
  114. ReportPopUp(o, cast<PathDiagnosticPopUpPiece>(P), indent);
  115. break;
  116. }
  117. }
  118. void EmitRanges(raw_ostream &o, const ArrayRef<SourceRange> Ranges,
  119. unsigned indent);
  120. void EmitMessage(raw_ostream &o, StringRef Message, unsigned indent);
  121. void EmitFixits(raw_ostream &o, ArrayRef<FixItHint> fixits, unsigned indent);
  122. void ReportControlFlow(raw_ostream &o,
  123. const PathDiagnosticControlFlowPiece& P,
  124. unsigned indent);
  125. void ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
  126. unsigned indent, unsigned depth, bool isKeyEvent = false);
  127. void ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
  128. unsigned indent, unsigned depth);
  129. void ReportMacroSubPieces(raw_ostream &o, const PathDiagnosticMacroPiece& P,
  130. unsigned indent, unsigned depth);
  131. void ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
  132. unsigned indent);
  133. void ReportPopUp(raw_ostream &o, const PathDiagnosticPopUpPiece &P,
  134. unsigned indent);
  135. };
  136. } // end of anonymous namespace
  137. namespace {
  138. struct ExpansionInfo {
  139. std::string MacroName;
  140. std::string Expansion;
  141. ExpansionInfo(std::string N, std::string E)
  142. : MacroName(std::move(N)), Expansion(std::move(E)) {}
  143. };
  144. } // end of anonymous namespace
  145. static void printBugPath(llvm::raw_ostream &o, const FIDMap& FM,
  146. AnalyzerOptions &AnOpts, const Preprocessor &PP,
  147. const cross_tu::CrossTranslationUnitContext &CTU,
  148. const PathPieces &Path);
  149. /// Print coverage information to output stream {@code o}.
  150. /// May modify the used list of files {@code Fids} by inserting new ones.
  151. static void printCoverage(const PathDiagnostic *D,
  152. unsigned InputIndentLevel,
  153. SmallVectorImpl<FileID> &Fids,
  154. FIDMap &FM,
  155. llvm::raw_fd_ostream &o);
  156. static ExpansionInfo
  157. getExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP,
  158. const cross_tu::CrossTranslationUnitContext &CTU);
  159. //===----------------------------------------------------------------------===//
  160. // Methods of PlistPrinter.
  161. //===----------------------------------------------------------------------===//
  162. void PlistPrinter::EmitRanges(raw_ostream &o,
  163. const ArrayRef<SourceRange> Ranges,
  164. unsigned indent) {
  165. if (Ranges.empty())
  166. return;
  167. Indent(o, indent) << "<key>ranges</key>\n";
  168. Indent(o, indent) << "<array>\n";
  169. ++indent;
  170. const SourceManager &SM = PP.getSourceManager();
  171. const LangOptions &LangOpts = PP.getLangOpts();
  172. for (auto &R : Ranges)
  173. EmitRange(o, SM,
  174. Lexer::getAsCharRange(SM.getExpansionRange(R), SM, LangOpts),
  175. FM, indent + 1);
  176. --indent;
  177. Indent(o, indent) << "</array>\n";
  178. }
  179. void PlistPrinter::EmitMessage(raw_ostream &o, StringRef Message,
  180. unsigned indent) {
  181. // Output the text.
  182. assert(!Message.empty());
  183. Indent(o, indent) << "<key>extended_message</key>\n";
  184. Indent(o, indent);
  185. EmitString(o, Message) << '\n';
  186. // Output the short text.
  187. // FIXME: Really use a short string.
  188. Indent(o, indent) << "<key>message</key>\n";
  189. Indent(o, indent);
  190. EmitString(o, Message) << '\n';
  191. }
  192. void PlistPrinter::EmitFixits(raw_ostream &o, ArrayRef<FixItHint> fixits,
  193. unsigned indent) {
  194. if (fixits.size() == 0)
  195. return;
  196. const SourceManager &SM = PP.getSourceManager();
  197. const LangOptions &LangOpts = PP.getLangOpts();
  198. Indent(o, indent) << "<key>fixits</key>\n";
  199. Indent(o, indent) << "<array>\n";
  200. for (const auto &fixit : fixits) {
  201. assert(!fixit.isNull());
  202. // FIXME: Add support for InsertFromRange and BeforePreviousInsertion.
  203. assert(!fixit.InsertFromRange.isValid() && "Not implemented yet!");
  204. assert(!fixit.BeforePreviousInsertions && "Not implemented yet!");
  205. Indent(o, indent) << " <dict>\n";
  206. Indent(o, indent) << " <key>remove_range</key>\n";
  207. EmitRange(o, SM, Lexer::getAsCharRange(fixit.RemoveRange, SM, LangOpts),
  208. FM, indent + 2);
  209. Indent(o, indent) << " <key>insert_string</key>";
  210. EmitString(o, fixit.CodeToInsert);
  211. o << "\n";
  212. Indent(o, indent) << " </dict>\n";
  213. }
  214. Indent(o, indent) << "</array>\n";
  215. }
  216. void PlistPrinter::ReportControlFlow(raw_ostream &o,
  217. const PathDiagnosticControlFlowPiece& P,
  218. unsigned indent) {
  219. const SourceManager &SM = PP.getSourceManager();
  220. const LangOptions &LangOpts = PP.getLangOpts();
  221. Indent(o, indent) << "<dict>\n";
  222. ++indent;
  223. Indent(o, indent) << "<key>kind</key><string>control</string>\n";
  224. // Emit edges.
  225. Indent(o, indent) << "<key>edges</key>\n";
  226. ++indent;
  227. Indent(o, indent) << "<array>\n";
  228. ++indent;
  229. for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
  230. I!=E; ++I) {
  231. Indent(o, indent) << "<dict>\n";
  232. ++indent;
  233. // Make the ranges of the start and end point self-consistent with adjacent edges
  234. // by forcing to use only the beginning of the range. This simplifies the layout
  235. // logic for clients.
  236. Indent(o, indent) << "<key>start</key>\n";
  237. SourceRange StartEdge(
  238. SM.getExpansionLoc(I->getStart().asRange().getBegin()));
  239. EmitRange(o, SM, Lexer::getAsCharRange(StartEdge, SM, LangOpts), FM,
  240. indent + 1);
  241. Indent(o, indent) << "<key>end</key>\n";
  242. SourceRange EndEdge(SM.getExpansionLoc(I->getEnd().asRange().getBegin()));
  243. EmitRange(o, SM, Lexer::getAsCharRange(EndEdge, SM, LangOpts), FM,
  244. indent + 1);
  245. --indent;
  246. Indent(o, indent) << "</dict>\n";
  247. }
  248. --indent;
  249. Indent(o, indent) << "</array>\n";
  250. --indent;
  251. // Output any helper text.
  252. const auto &s = P.getString();
  253. if (!s.empty()) {
  254. Indent(o, indent) << "<key>alternate</key>";
  255. EmitString(o, s) << '\n';
  256. }
  257. assert(P.getFixits().size() == 0 &&
  258. "Fixits on constrol flow pieces are not implemented yet!");
  259. --indent;
  260. Indent(o, indent) << "</dict>\n";
  261. }
  262. void PlistPrinter::ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
  263. unsigned indent, unsigned depth,
  264. bool isKeyEvent) {
  265. const SourceManager &SM = PP.getSourceManager();
  266. Indent(o, indent) << "<dict>\n";
  267. ++indent;
  268. Indent(o, indent) << "<key>kind</key><string>event</string>\n";
  269. if (isKeyEvent) {
  270. Indent(o, indent) << "<key>key_event</key><true/>\n";
  271. }
  272. // Output the location.
  273. FullSourceLoc L = P.getLocation().asLocation();
  274. Indent(o, indent) << "<key>location</key>\n";
  275. EmitLocation(o, SM, L, FM, indent);
  276. // Output the ranges (if any).
  277. ArrayRef<SourceRange> Ranges = P.getRanges();
  278. EmitRanges(o, Ranges, indent);
  279. // Output the call depth.
  280. Indent(o, indent) << "<key>depth</key>";
  281. EmitInteger(o, depth) << '\n';
  282. // Output the text.
  283. EmitMessage(o, P.getString(), indent);
  284. // Output the fixits.
  285. EmitFixits(o, P.getFixits(), indent);
  286. // Finish up.
  287. --indent;
  288. Indent(o, indent); o << "</dict>\n";
  289. }
  290. void PlistPrinter::ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
  291. unsigned indent,
  292. unsigned depth) {
  293. if (auto callEnter = P.getCallEnterEvent())
  294. ReportPiece(o, *callEnter, indent, depth, /*includeControlFlow*/ true,
  295. P.isLastInMainSourceFile());
  296. ++depth;
  297. if (auto callEnterWithinCaller = P.getCallEnterWithinCallerEvent())
  298. ReportPiece(o, *callEnterWithinCaller, indent, depth,
  299. /*includeControlFlow*/ true);
  300. for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I)
  301. ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ true);
  302. --depth;
  303. if (auto callExit = P.getCallExitEvent())
  304. ReportPiece(o, *callExit, indent, depth, /*includeControlFlow*/ true);
  305. assert(P.getFixits().size() == 0 &&
  306. "Fixits on call pieces are not implemented yet!");
  307. }
  308. void PlistPrinter::ReportMacroSubPieces(raw_ostream &o,
  309. const PathDiagnosticMacroPiece& P,
  310. unsigned indent, unsigned depth) {
  311. MacroPieces.push_back(&P);
  312. for (PathPieces::const_iterator I = P.subPieces.begin(),
  313. E = P.subPieces.end();
  314. I != E; ++I) {
  315. ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ false);
  316. }
  317. assert(P.getFixits().size() == 0 &&
  318. "Fixits on constrol flow pieces are not implemented yet!");
  319. }
  320. void PlistPrinter::ReportMacroExpansions(raw_ostream &o, unsigned indent) {
  321. for (const PathDiagnosticMacroPiece *P : MacroPieces) {
  322. const SourceManager &SM = PP.getSourceManager();
  323. ExpansionInfo EI = getExpandedMacro(P->getLocation().asLocation(), PP, CTU);
  324. Indent(o, indent) << "<dict>\n";
  325. ++indent;
  326. // Output the location.
  327. FullSourceLoc L = P->getLocation().asLocation();
  328. Indent(o, indent) << "<key>location</key>\n";
  329. EmitLocation(o, SM, L, FM, indent);
  330. // Output the ranges (if any).
  331. ArrayRef<SourceRange> Ranges = P->getRanges();
  332. EmitRanges(o, Ranges, indent);
  333. // Output the macro name.
  334. Indent(o, indent) << "<key>name</key>";
  335. EmitString(o, EI.MacroName) << '\n';
  336. // Output what it expands into.
  337. Indent(o, indent) << "<key>expansion</key>";
  338. EmitString(o, EI.Expansion) << '\n';
  339. // Finish up.
  340. --indent;
  341. Indent(o, indent);
  342. o << "</dict>\n";
  343. }
  344. }
  345. void PlistPrinter::ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
  346. unsigned indent) {
  347. const SourceManager &SM = PP.getSourceManager();
  348. Indent(o, indent) << "<dict>\n";
  349. ++indent;
  350. // Output the location.
  351. FullSourceLoc L = P.getLocation().asLocation();
  352. Indent(o, indent) << "<key>location</key>\n";
  353. EmitLocation(o, SM, L, FM, indent);
  354. // Output the ranges (if any).
  355. ArrayRef<SourceRange> Ranges = P.getRanges();
  356. EmitRanges(o, Ranges, indent);
  357. // Output the text.
  358. EmitMessage(o, P.getString(), indent);
  359. // Output the fixits.
  360. EmitFixits(o, P.getFixits(), indent);
  361. // Finish up.
  362. --indent;
  363. Indent(o, indent); o << "</dict>\n";
  364. }
  365. void PlistPrinter::ReportPopUp(raw_ostream &o,
  366. const PathDiagnosticPopUpPiece &P,
  367. unsigned indent) {
  368. const SourceManager &SM = PP.getSourceManager();
  369. Indent(o, indent) << "<dict>\n";
  370. ++indent;
  371. Indent(o, indent) << "<key>kind</key><string>pop-up</string>\n";
  372. // Output the location.
  373. FullSourceLoc L = P.getLocation().asLocation();
  374. Indent(o, indent) << "<key>location</key>\n";
  375. EmitLocation(o, SM, L, FM, indent);
  376. // Output the ranges (if any).
  377. ArrayRef<SourceRange> Ranges = P.getRanges();
  378. EmitRanges(o, Ranges, indent);
  379. // Output the text.
  380. EmitMessage(o, P.getString(), indent);
  381. assert(P.getFixits().size() == 0 &&
  382. "Fixits on pop-up pieces are not implemented yet!");
  383. // Finish up.
  384. --indent;
  385. Indent(o, indent) << "</dict>\n";
  386. }
  387. //===----------------------------------------------------------------------===//
  388. // Static function definitions.
  389. //===----------------------------------------------------------------------===//
  390. /// Print coverage information to output stream {@code o}.
  391. /// May modify the used list of files {@code Fids} by inserting new ones.
  392. static void printCoverage(const PathDiagnostic *D,
  393. unsigned InputIndentLevel,
  394. SmallVectorImpl<FileID> &Fids,
  395. FIDMap &FM,
  396. llvm::raw_fd_ostream &o) {
  397. unsigned IndentLevel = InputIndentLevel;
  398. Indent(o, IndentLevel) << "<key>ExecutedLines</key>\n";
  399. Indent(o, IndentLevel) << "<dict>\n";
  400. IndentLevel++;
  401. // Mapping from file IDs to executed lines.
  402. const FilesToLineNumsMap &ExecutedLines = D->getExecutedLines();
  403. for (auto I = ExecutedLines.begin(), E = ExecutedLines.end(); I != E; ++I) {
  404. unsigned FileKey = AddFID(FM, Fids, I->first);
  405. Indent(o, IndentLevel) << "<key>" << FileKey << "</key>\n";
  406. Indent(o, IndentLevel) << "<array>\n";
  407. IndentLevel++;
  408. for (unsigned LineNo : I->second) {
  409. Indent(o, IndentLevel);
  410. EmitInteger(o, LineNo) << "\n";
  411. }
  412. IndentLevel--;
  413. Indent(o, IndentLevel) << "</array>\n";
  414. }
  415. IndentLevel--;
  416. Indent(o, IndentLevel) << "</dict>\n";
  417. assert(IndentLevel == InputIndentLevel);
  418. }
  419. static void printBugPath(llvm::raw_ostream &o, const FIDMap& FM,
  420. AnalyzerOptions &AnOpts, const Preprocessor &PP,
  421. const cross_tu::CrossTranslationUnitContext &CTU,
  422. const PathPieces &Path) {
  423. PlistPrinter Printer(FM, AnOpts, PP, CTU);
  424. assert(std::is_partitioned(Path.begin(), Path.end(),
  425. [](const PathDiagnosticPieceRef &E) {
  426. return E->getKind() == PathDiagnosticPiece::Note;
  427. }) &&
  428. "PathDiagnostic is not partitioned so that notes precede the rest");
  429. PathPieces::const_iterator FirstNonNote = std::partition_point(
  430. Path.begin(), Path.end(), [](const PathDiagnosticPieceRef &E) {
  431. return E->getKind() == PathDiagnosticPiece::Note;
  432. });
  433. PathPieces::const_iterator I = Path.begin();
  434. if (FirstNonNote != Path.begin()) {
  435. o << " <key>notes</key>\n"
  436. " <array>\n";
  437. for (; I != FirstNonNote; ++I)
  438. Printer.ReportDiag(o, **I);
  439. o << " </array>\n";
  440. }
  441. o << " <key>path</key>\n";
  442. o << " <array>\n";
  443. for (PathPieces::const_iterator E = Path.end(); I != E; ++I)
  444. Printer.ReportDiag(o, **I);
  445. o << " </array>\n";
  446. if (!AnOpts.ShouldDisplayMacroExpansions)
  447. return;
  448. o << " <key>macro_expansions</key>\n"
  449. " <array>\n";
  450. Printer.ReportMacroExpansions(o, /* indent */ 4);
  451. o << " </array>\n";
  452. }
  453. //===----------------------------------------------------------------------===//
  454. // Methods of PlistDiagnostics.
  455. //===----------------------------------------------------------------------===//
  456. PlistDiagnostics::PlistDiagnostics(
  457. AnalyzerOptions &AnalyzerOpts, const std::string &output,
  458. const Preprocessor &PP, const cross_tu::CrossTranslationUnitContext &CTU,
  459. bool supportsMultipleFiles)
  460. : OutputFile(output), PP(PP), CTU(CTU), AnOpts(AnalyzerOpts),
  461. SupportsCrossFileDiagnostics(supportsMultipleFiles) {
  462. // FIXME: Will be used by a later planned change.
  463. (void)this->CTU;
  464. }
  465. void ento::createPlistDiagnosticConsumer(
  466. AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
  467. const std::string &s, const Preprocessor &PP,
  468. const cross_tu::CrossTranslationUnitContext &CTU) {
  469. C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
  470. /*supportsMultipleFiles*/ false));
  471. }
  472. void ento::createPlistMultiFileDiagnosticConsumer(
  473. AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
  474. const std::string &s, const Preprocessor &PP,
  475. const cross_tu::CrossTranslationUnitContext &CTU) {
  476. C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
  477. /*supportsMultipleFiles*/ true));
  478. }
  479. void PlistDiagnostics::FlushDiagnosticsImpl(
  480. std::vector<const PathDiagnostic *> &Diags,
  481. FilesMade *filesMade) {
  482. // Build up a set of FIDs that we use by scanning the locations and
  483. // ranges of the diagnostics.
  484. FIDMap FM;
  485. SmallVector<FileID, 10> Fids;
  486. const SourceManager& SM = PP.getSourceManager();
  487. const LangOptions &LangOpts = PP.getLangOpts();
  488. auto AddPieceFID = [&FM, &Fids, &SM](const PathDiagnosticPiece &Piece) {
  489. AddFID(FM, Fids, SM, Piece.getLocation().asLocation());
  490. ArrayRef<SourceRange> Ranges = Piece.getRanges();
  491. for (const SourceRange &Range : Ranges) {
  492. AddFID(FM, Fids, SM, Range.getBegin());
  493. AddFID(FM, Fids, SM, Range.getEnd());
  494. }
  495. };
  496. for (const PathDiagnostic *D : Diags) {
  497. SmallVector<const PathPieces *, 5> WorkList;
  498. WorkList.push_back(&D->path);
  499. while (!WorkList.empty()) {
  500. const PathPieces &Path = *WorkList.pop_back_val();
  501. for (const auto &Iter : Path) {
  502. const PathDiagnosticPiece &Piece = *Iter;
  503. AddPieceFID(Piece);
  504. if (const PathDiagnosticCallPiece *Call =
  505. dyn_cast<PathDiagnosticCallPiece>(&Piece)) {
  506. if (auto CallEnterWithin = Call->getCallEnterWithinCallerEvent())
  507. AddPieceFID(*CallEnterWithin);
  508. if (auto CallEnterEvent = Call->getCallEnterEvent())
  509. AddPieceFID(*CallEnterEvent);
  510. WorkList.push_back(&Call->path);
  511. } else if (const PathDiagnosticMacroPiece *Macro =
  512. dyn_cast<PathDiagnosticMacroPiece>(&Piece)) {
  513. WorkList.push_back(&Macro->subPieces);
  514. }
  515. }
  516. }
  517. }
  518. // Open the file.
  519. std::error_code EC;
  520. llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::OF_Text);
  521. if (EC) {
  522. llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
  523. return;
  524. }
  525. EmitPlistHeader(o);
  526. // Write the root object: a <dict> containing...
  527. // - "clang_version", the string representation of clang version
  528. // - "files", an <array> mapping from FIDs to file names
  529. // - "diagnostics", an <array> containing the path diagnostics
  530. o << "<dict>\n" <<
  531. " <key>clang_version</key>\n";
  532. EmitString(o, getClangFullVersion()) << '\n';
  533. o << " <key>diagnostics</key>\n"
  534. " <array>\n";
  535. for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
  536. DE = Diags.end(); DI!=DE; ++DI) {
  537. o << " <dict>\n";
  538. const PathDiagnostic *D = *DI;
  539. printBugPath(o, FM, AnOpts, PP, CTU, D->path);
  540. // Output the bug type and bug category.
  541. o << " <key>description</key>";
  542. EmitString(o, D->getShortDescription()) << '\n';
  543. o << " <key>category</key>";
  544. EmitString(o, D->getCategory()) << '\n';
  545. o << " <key>type</key>";
  546. EmitString(o, D->getBugType()) << '\n';
  547. o << " <key>check_name</key>";
  548. EmitString(o, D->getCheckerName()) << '\n';
  549. o << " <!-- This hash is experimental and going to change! -->\n";
  550. o << " <key>issue_hash_content_of_line_in_context</key>";
  551. PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
  552. FullSourceLoc L(SM.getExpansionLoc(UPDLoc.isValid()
  553. ? UPDLoc.asLocation()
  554. : D->getLocation().asLocation()),
  555. SM);
  556. const Decl *DeclWithIssue = D->getDeclWithIssue();
  557. EmitString(o, GetIssueHash(SM, L, D->getCheckerName(), D->getBugType(),
  558. DeclWithIssue, LangOpts))
  559. << '\n';
  560. // Output information about the semantic context where
  561. // the issue occurred.
  562. if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
  563. // FIXME: handle blocks, which have no name.
  564. if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
  565. StringRef declKind;
  566. switch (ND->getKind()) {
  567. case Decl::CXXRecord:
  568. declKind = "C++ class";
  569. break;
  570. case Decl::CXXMethod:
  571. declKind = "C++ method";
  572. break;
  573. case Decl::ObjCMethod:
  574. declKind = "Objective-C method";
  575. break;
  576. case Decl::Function:
  577. declKind = "function";
  578. break;
  579. default:
  580. break;
  581. }
  582. if (!declKind.empty()) {
  583. const std::string &declName = ND->getDeclName().getAsString();
  584. o << " <key>issue_context_kind</key>";
  585. EmitString(o, declKind) << '\n';
  586. o << " <key>issue_context</key>";
  587. EmitString(o, declName) << '\n';
  588. }
  589. // Output the bug hash for issue unique-ing. Currently, it's just an
  590. // offset from the beginning of the function.
  591. if (const Stmt *Body = DeclWithIssue->getBody()) {
  592. // If the bug uniqueing location exists, use it for the hash.
  593. // For example, this ensures that two leaks reported on the same line
  594. // will have different issue_hashes and that the hash will identify
  595. // the leak location even after code is added between the allocation
  596. // site and the end of scope (leak report location).
  597. if (UPDLoc.isValid()) {
  598. FullSourceLoc UFunL(
  599. SM.getExpansionLoc(
  600. D->getUniqueingDecl()->getBody()->getBeginLoc()),
  601. SM);
  602. o << " <key>issue_hash_function_offset</key><string>"
  603. << L.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
  604. << "</string>\n";
  605. // Otherwise, use the location on which the bug is reported.
  606. } else {
  607. FullSourceLoc FunL(SM.getExpansionLoc(Body->getBeginLoc()), SM);
  608. o << " <key>issue_hash_function_offset</key><string>"
  609. << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
  610. << "</string>\n";
  611. }
  612. }
  613. }
  614. }
  615. // Output the location of the bug.
  616. o << " <key>location</key>\n";
  617. EmitLocation(o, SM, D->getLocation().asLocation(), FM, 2);
  618. // Output the diagnostic to the sub-diagnostic client, if any.
  619. if (!filesMade->empty()) {
  620. StringRef lastName;
  621. PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
  622. if (files) {
  623. for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
  624. CE = files->end(); CI != CE; ++CI) {
  625. StringRef newName = CI->first;
  626. if (newName != lastName) {
  627. if (!lastName.empty()) {
  628. o << " </array>\n";
  629. }
  630. lastName = newName;
  631. o << " <key>" << lastName << "_files</key>\n";
  632. o << " <array>\n";
  633. }
  634. o << " <string>" << CI->second << "</string>\n";
  635. }
  636. o << " </array>\n";
  637. }
  638. }
  639. printCoverage(D, /*IndentLevel=*/2, Fids, FM, o);
  640. // Close up the entry.
  641. o << " </dict>\n";
  642. }
  643. o << " </array>\n";
  644. o << " <key>files</key>\n"
  645. " <array>\n";
  646. for (FileID FID : Fids)
  647. EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n';
  648. o << " </array>\n";
  649. if (llvm::AreStatisticsEnabled() && AnOpts.ShouldSerializeStats) {
  650. o << " <key>statistics</key>\n";
  651. std::string stats;
  652. llvm::raw_string_ostream os(stats);
  653. llvm::PrintStatisticsJSON(os);
  654. os.flush();
  655. EmitString(o, html::EscapeText(stats)) << '\n';
  656. }
  657. // Finish.
  658. o << "</dict>\n</plist>\n";
  659. }
  660. //===----------------------------------------------------------------------===//
  661. // Declarations of helper functions and data structures for expanding macros.
  662. //===----------------------------------------------------------------------===//
  663. namespace {
  664. using ExpArgTokens = llvm::SmallVector<Token, 2>;
  665. /// Maps unexpanded macro arguments to expanded arguments. A macro argument may
  666. /// need to expanded further when it is nested inside another macro.
  667. class MacroArgMap : public std::map<const IdentifierInfo *, ExpArgTokens> {
  668. public:
  669. void expandFromPrevMacro(const MacroArgMap &Super);
  670. };
  671. struct MacroNameAndArgs {
  672. std::string Name;
  673. const MacroInfo *MI = nullptr;
  674. MacroArgMap Args;
  675. MacroNameAndArgs(std::string N, const MacroInfo *MI, MacroArgMap M)
  676. : Name(std::move(N)), MI(MI), Args(std::move(M)) {}
  677. };
  678. class TokenPrinter {
  679. llvm::raw_ostream &OS;
  680. const Preprocessor &PP;
  681. Token PrevTok, PrevPrevTok;
  682. TokenConcatenation ConcatInfo;
  683. public:
  684. TokenPrinter(llvm::raw_ostream &OS, const Preprocessor &PP)
  685. : OS(OS), PP(PP), ConcatInfo(PP) {
  686. PrevTok.setKind(tok::unknown);
  687. PrevPrevTok.setKind(tok::unknown);
  688. }
  689. void printToken(const Token &Tok);
  690. };
  691. } // end of anonymous namespace
  692. /// The implementation method of getMacroExpansion: It prints the expansion of
  693. /// a macro to \p Printer, and returns with the name of the macro.
  694. ///
  695. /// Since macros can be nested in one another, this function may call itself
  696. /// recursively.
  697. ///
  698. /// Unfortunately, macro arguments have to expanded manually. To understand why,
  699. /// observe the following example:
  700. ///
  701. /// #define PRINT(x) print(x)
  702. /// #define DO_SOMETHING(str) PRINT(str)
  703. ///
  704. /// DO_SOMETHING("Cute panda cubs.");
  705. ///
  706. /// As we expand the last line, we'll immediately replace PRINT(str) with
  707. /// print(x). The information that both 'str' and 'x' refers to the same string
  708. /// is an information we have to forward, hence the argument \p PrevArgs.
  709. ///
  710. /// To avoid infinite recursion we maintain the already processed tokens in
  711. /// a set. This is carried as a parameter through the recursive calls. The set
  712. /// is extended with the currently processed token and after processing it, the
  713. /// token is removed. If the token is already in the set, then recursion stops:
  714. ///
  715. /// #define f(y) x
  716. /// #define x f(x)
  717. static std::string getMacroNameAndPrintExpansion(
  718. TokenPrinter &Printer,
  719. SourceLocation MacroLoc,
  720. const Preprocessor &PP,
  721. const MacroArgMap &PrevArgs,
  722. llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens);
  723. /// Retrieves the name of the macro and what it's arguments expand into
  724. /// at \p ExpanLoc.
  725. ///
  726. /// For example, for the following macro expansion:
  727. ///
  728. /// #define SET_TO_NULL(x) x = 0
  729. /// #define NOT_SUSPICIOUS(a) \
  730. /// { \
  731. /// int b = 0; \
  732. /// } \
  733. /// SET_TO_NULL(a)
  734. ///
  735. /// int *ptr = new int(4);
  736. /// NOT_SUSPICIOUS(&ptr);
  737. /// *ptr = 5;
  738. ///
  739. /// When \p ExpanLoc references the last line, the macro name "NOT_SUSPICIOUS"
  740. /// and the MacroArgMap map { (a, &ptr) } will be returned.
  741. ///
  742. /// When \p ExpanLoc references "SET_TO_NULL(a)" within the definition of
  743. /// "NOT_SUSPICOUS", the macro name "SET_TO_NULL" and the MacroArgMap map
  744. /// { (x, a) } will be returned.
  745. static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
  746. const Preprocessor &PP);
  747. /// Retrieves the ')' token that matches '(' \p It points to.
  748. static MacroInfo::tokens_iterator getMatchingRParen(
  749. MacroInfo::tokens_iterator It,
  750. MacroInfo::tokens_iterator End);
  751. /// Retrieves the macro info for \p II refers to at \p Loc. This is important
  752. /// because macros can be redefined or undefined.
  753. static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
  754. const SourceManager &SM,
  755. const IdentifierInfo *II,
  756. SourceLocation Loc);
  757. //===----------------------------------------------------------------------===//
  758. // Definitions of helper functions and methods for expanding macros.
  759. //===----------------------------------------------------------------------===//
  760. static ExpansionInfo
  761. getExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP,
  762. const cross_tu::CrossTranslationUnitContext &CTU) {
  763. const Preprocessor *PPToUse = &PP;
  764. if (auto LocAndUnit = CTU.getImportedFromSourceLocation(MacroLoc)) {
  765. MacroLoc = LocAndUnit->first;
  766. PPToUse = &LocAndUnit->second->getPreprocessor();
  767. }
  768. llvm::SmallString<200> ExpansionBuf;
  769. llvm::raw_svector_ostream OS(ExpansionBuf);
  770. TokenPrinter Printer(OS, *PPToUse);
  771. llvm::SmallPtrSet<IdentifierInfo*, 8> AlreadyProcessedTokens;
  772. std::string MacroName = getMacroNameAndPrintExpansion(
  773. Printer, MacroLoc, *PPToUse, MacroArgMap{}, AlreadyProcessedTokens);
  774. return { MacroName, OS.str() };
  775. }
  776. static std::string getMacroNameAndPrintExpansion(
  777. TokenPrinter &Printer,
  778. SourceLocation MacroLoc,
  779. const Preprocessor &PP,
  780. const MacroArgMap &PrevArgs,
  781. llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens) {
  782. const SourceManager &SM = PP.getSourceManager();
  783. MacroNameAndArgs Info = getMacroNameAndArgs(SM.getExpansionLoc(MacroLoc), PP);
  784. IdentifierInfo* IDInfo = PP.getIdentifierInfo(Info.Name);
  785. // TODO: If the macro definition contains another symbol then this function is
  786. // called recursively. In case this symbol is the one being defined, it will
  787. // be an infinite recursion which is stopped by this "if" statement. However,
  788. // in this case we don't get the full expansion text in the Plist file. See
  789. // the test file where "value" is expanded to "garbage_" instead of
  790. // "garbage_value".
  791. if (AlreadyProcessedTokens.find(IDInfo) != AlreadyProcessedTokens.end())
  792. return Info.Name;
  793. AlreadyProcessedTokens.insert(IDInfo);
  794. if (!Info.MI)
  795. return Info.Name;
  796. // Manually expand its arguments from the previous macro.
  797. Info.Args.expandFromPrevMacro(PrevArgs);
  798. // Iterate over the macro's tokens and stringify them.
  799. for (auto It = Info.MI->tokens_begin(), E = Info.MI->tokens_end(); It != E;
  800. ++It) {
  801. Token T = *It;
  802. // If this token is not an identifier, we only need to print it.
  803. if (T.isNot(tok::identifier)) {
  804. Printer.printToken(T);
  805. continue;
  806. }
  807. const auto *II = T.getIdentifierInfo();
  808. assert(II &&
  809. "This token is an identifier but has no IdentifierInfo!");
  810. // If this token is a macro that should be expanded inside the current
  811. // macro.
  812. if (getMacroInfoForLocation(PP, SM, II, T.getLocation())) {
  813. getMacroNameAndPrintExpansion(Printer, T.getLocation(), PP, Info.Args,
  814. AlreadyProcessedTokens);
  815. // If this is a function-like macro, skip its arguments, as
  816. // getExpandedMacro() already printed them. If this is the case, let's
  817. // first jump to the '(' token.
  818. auto N = std::next(It);
  819. if (N != E && N->is(tok::l_paren))
  820. It = getMatchingRParen(++It, E);
  821. continue;
  822. }
  823. // If this token is the current macro's argument, we should expand it.
  824. auto ArgMapIt = Info.Args.find(II);
  825. if (ArgMapIt != Info.Args.end()) {
  826. for (MacroInfo::tokens_iterator ArgIt = ArgMapIt->second.begin(),
  827. ArgEnd = ArgMapIt->second.end();
  828. ArgIt != ArgEnd; ++ArgIt) {
  829. // These tokens may still be macros, if that is the case, handle it the
  830. // same way we did above.
  831. const auto *ArgII = ArgIt->getIdentifierInfo();
  832. if (!ArgII) {
  833. Printer.printToken(*ArgIt);
  834. continue;
  835. }
  836. const auto *MI = PP.getMacroInfo(ArgII);
  837. if (!MI) {
  838. Printer.printToken(*ArgIt);
  839. continue;
  840. }
  841. getMacroNameAndPrintExpansion(Printer, ArgIt->getLocation(), PP,
  842. Info.Args, AlreadyProcessedTokens);
  843. // Peek the next token if it is a tok::l_paren. This way we can decide
  844. // if this is the application or just a reference to a function maxro
  845. // symbol:
  846. //
  847. // #define apply(f) ...
  848. // #define func(x) ...
  849. // apply(func)
  850. // apply(func(42))
  851. auto N = std::next(ArgIt);
  852. if (N != ArgEnd && N->is(tok::l_paren))
  853. ArgIt = getMatchingRParen(++ArgIt, ArgEnd);
  854. }
  855. continue;
  856. }
  857. // If control reached here, then this token isn't a macro identifier, nor an
  858. // unexpanded macro argument that we need to handle, print it.
  859. Printer.printToken(T);
  860. }
  861. AlreadyProcessedTokens.erase(IDInfo);
  862. return Info.Name;
  863. }
  864. static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
  865. const Preprocessor &PP) {
  866. const SourceManager &SM = PP.getSourceManager();
  867. const LangOptions &LangOpts = PP.getLangOpts();
  868. // First, we create a Lexer to lex *at the expansion location* the tokens
  869. // referring to the macro's name and its arguments.
  870. std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ExpanLoc);
  871. const llvm::MemoryBuffer *MB = SM.getBuffer(LocInfo.first);
  872. const char *MacroNameTokenPos = MB->getBufferStart() + LocInfo.second;
  873. Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
  874. MB->getBufferStart(), MacroNameTokenPos, MB->getBufferEnd());
  875. // Acquire the macro's name.
  876. Token TheTok;
  877. RawLexer.LexFromRawLexer(TheTok);
  878. std::string MacroName = PP.getSpelling(TheTok);
  879. const auto *II = PP.getIdentifierInfo(MacroName);
  880. assert(II && "Failed to acquire the IndetifierInfo for the macro!");
  881. const MacroInfo *MI = getMacroInfoForLocation(PP, SM, II, ExpanLoc);
  882. // assert(MI && "The macro must've been defined at it's expansion location!");
  883. //
  884. // We should always be able to obtain the MacroInfo in a given TU, but if
  885. // we're running the analyzer with CTU, the Preprocessor won't contain the
  886. // directive history (or anything for that matter) from another TU.
  887. // TODO: assert when we're not running with CTU.
  888. if (!MI)
  889. return { MacroName, MI, {} };
  890. // Acquire the macro's arguments.
  891. //
  892. // The rough idea here is to lex from the first left parentheses to the last
  893. // right parentheses, and map the macro's unexpanded arguments to what they
  894. // will be expanded to. An expanded macro argument may contain several tokens
  895. // (like '3 + 4'), so we'll lex until we find a tok::comma or tok::r_paren, at
  896. // which point we start lexing the next argument or finish.
  897. ArrayRef<const IdentifierInfo *> MacroArgs = MI->params();
  898. if (MacroArgs.empty())
  899. return { MacroName, MI, {} };
  900. RawLexer.LexFromRawLexer(TheTok);
  901. // When this is a token which expands to another macro function then its
  902. // parentheses are not at its expansion locaiton. For example:
  903. //
  904. // #define foo(x) int bar() { return x; }
  905. // #define apply_zero(f) f(0)
  906. // apply_zero(foo)
  907. // ^
  908. // This is not a tok::l_paren, but foo is a function.
  909. if (TheTok.isNot(tok::l_paren))
  910. return { MacroName, MI, {} };
  911. MacroArgMap Args;
  912. // When the macro's argument is a function call, like
  913. // CALL_FN(someFunctionName(param1, param2))
  914. // we will find tok::l_paren, tok::r_paren, and tok::comma that do not divide
  915. // actual macro arguments, or do not represent the macro argument's closing
  916. // parentheses, so we'll count how many parentheses aren't closed yet.
  917. // If ParanthesesDepth
  918. // * = 0, then there are no more arguments to lex.
  919. // * = 1, then if we find a tok::comma, we can start lexing the next arg.
  920. // * > 1, then tok::comma is a part of the current arg.
  921. int ParenthesesDepth = 1;
  922. // If we encounter __VA_ARGS__, we will lex until the closing tok::r_paren,
  923. // even if we lex a tok::comma and ParanthesesDepth == 1.
  924. const IdentifierInfo *__VA_ARGS__II = PP.getIdentifierInfo("__VA_ARGS__");
  925. for (const IdentifierInfo *UnexpArgII : MacroArgs) {
  926. MacroArgMap::mapped_type ExpandedArgTokens;
  927. // One could also simply not supply a single argument to __VA_ARGS__ -- this
  928. // results in a preprocessor warning, but is not an error:
  929. // #define VARIADIC(ptr, ...) \
  930. // someVariadicTemplateFunction(__VA_ARGS__)
  931. //
  932. // int *ptr;
  933. // VARIADIC(ptr); // Note that there are no commas, this isn't just an
  934. // // empty parameter -- there are no parameters for '...'.
  935. // In any other case, ParenthesesDepth mustn't be 0 here.
  936. if (ParenthesesDepth != 0) {
  937. // Lex the first token of the next macro parameter.
  938. RawLexer.LexFromRawLexer(TheTok);
  939. while (!(ParenthesesDepth == 1 &&
  940. (UnexpArgII == __VA_ARGS__II ? false : TheTok.is(tok::comma)))) {
  941. assert(TheTok.isNot(tok::eof) &&
  942. "EOF encountered while looking for expanded macro args!");
  943. if (TheTok.is(tok::l_paren))
  944. ++ParenthesesDepth;
  945. if (TheTok.is(tok::r_paren))
  946. --ParenthesesDepth;
  947. if (ParenthesesDepth == 0)
  948. break;
  949. if (TheTok.is(tok::raw_identifier))
  950. PP.LookUpIdentifierInfo(TheTok);
  951. ExpandedArgTokens.push_back(TheTok);
  952. RawLexer.LexFromRawLexer(TheTok);
  953. }
  954. } else {
  955. assert(UnexpArgII == __VA_ARGS__II);
  956. }
  957. Args.emplace(UnexpArgII, std::move(ExpandedArgTokens));
  958. }
  959. assert(TheTok.is(tok::r_paren) &&
  960. "Expanded macro argument acquisition failed! After the end of the loop"
  961. " this token should be ')'!");
  962. return { MacroName, MI, Args };
  963. }
  964. static MacroInfo::tokens_iterator getMatchingRParen(
  965. MacroInfo::tokens_iterator It,
  966. MacroInfo::tokens_iterator End) {
  967. assert(It->is(tok::l_paren) && "This token should be '('!");
  968. // Skip until we find the closing ')'.
  969. int ParenthesesDepth = 1;
  970. while (ParenthesesDepth != 0) {
  971. ++It;
  972. assert(It->isNot(tok::eof) &&
  973. "Encountered EOF while attempting to skip macro arguments!");
  974. assert(It != End &&
  975. "End of the macro definition reached before finding ')'!");
  976. if (It->is(tok::l_paren))
  977. ++ParenthesesDepth;
  978. if (It->is(tok::r_paren))
  979. --ParenthesesDepth;
  980. }
  981. return It;
  982. }
  983. static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
  984. const SourceManager &SM,
  985. const IdentifierInfo *II,
  986. SourceLocation Loc) {
  987. const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II);
  988. if (!MD)
  989. return nullptr;
  990. return MD->findDirectiveAtLoc(Loc, SM).getMacroInfo();
  991. }
  992. void MacroArgMap::expandFromPrevMacro(const MacroArgMap &Super) {
  993. for (value_type &Pair : *this) {
  994. ExpArgTokens &CurrExpArgTokens = Pair.second;
  995. // For each token in the expanded macro argument.
  996. auto It = CurrExpArgTokens.begin();
  997. while (It != CurrExpArgTokens.end()) {
  998. if (It->isNot(tok::identifier)) {
  999. ++It;
  1000. continue;
  1001. }
  1002. const auto *II = It->getIdentifierInfo();
  1003. assert(II);
  1004. // Is this an argument that "Super" expands further?
  1005. if (!Super.count(II)) {
  1006. ++It;
  1007. continue;
  1008. }
  1009. const ExpArgTokens &SuperExpArgTokens = Super.at(II);
  1010. It = CurrExpArgTokens.insert(
  1011. It, SuperExpArgTokens.begin(), SuperExpArgTokens.end());
  1012. std::advance(It, SuperExpArgTokens.size());
  1013. It = CurrExpArgTokens.erase(It);
  1014. }
  1015. }
  1016. }
  1017. void TokenPrinter::printToken(const Token &Tok) {
  1018. // If this is the first token to be printed, don't print space.
  1019. if (PrevTok.isNot(tok::unknown)) {
  1020. // If the tokens were already space separated, or if they must be to avoid
  1021. // them being implicitly pasted, add a space between them.
  1022. if(Tok.hasLeadingSpace() || ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok,
  1023. Tok)) {
  1024. // AvoidConcat doesn't check for ##, don't print a space around it.
  1025. if (PrevTok.isNot(tok::hashhash) && Tok.isNot(tok::hashhash)) {
  1026. OS << ' ';
  1027. }
  1028. }
  1029. }
  1030. if (!Tok.isOneOf(tok::hash, tok::hashhash)) {
  1031. if (PrevTok.is(tok::hash))
  1032. OS << '\"' << PP.getSpelling(Tok) << '\"';
  1033. else
  1034. OS << PP.getSpelling(Tok);
  1035. }
  1036. PrevPrevTok = PrevTok;
  1037. PrevTok = Tok;
  1038. }