CommentParser.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. //===- unittests/AST/CommentParser.cpp ------ Comment parser tests --------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/AST/CommentParser.h"
  10. #include "clang/AST/Comment.h"
  11. #include "clang/AST/CommentCommandTraits.h"
  12. #include "clang/AST/CommentLexer.h"
  13. #include "clang/AST/CommentSema.h"
  14. #include "clang/Basic/CommentOptions.h"
  15. #include "clang/Basic/Diagnostic.h"
  16. #include "clang/Basic/DiagnosticOptions.h"
  17. #include "clang/Basic/FileManager.h"
  18. #include "clang/Basic/SourceManager.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/Support/Allocator.h"
  21. #include "gtest/gtest.h"
  22. #include <vector>
  23. using namespace llvm;
  24. using namespace clang;
  25. namespace clang {
  26. namespace comments {
  27. namespace {
  28. const bool DEBUG = true;
  29. class CommentParserTest : public ::testing::Test {
  30. protected:
  31. CommentParserTest()
  32. : FileMgr(FileMgrOpts),
  33. DiagID(new DiagnosticIDs()),
  34. Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
  35. SourceMgr(Diags, FileMgr),
  36. Traits(Allocator, CommentOptions()) {
  37. }
  38. FileSystemOptions FileMgrOpts;
  39. FileManager FileMgr;
  40. IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
  41. DiagnosticsEngine Diags;
  42. SourceManager SourceMgr;
  43. llvm::BumpPtrAllocator Allocator;
  44. CommandTraits Traits;
  45. FullComment *parseString(const char *Source);
  46. };
  47. FullComment *CommentParserTest::parseString(const char *Source) {
  48. std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Source);
  49. FileID File = SourceMgr.createFileID(std::move(Buf));
  50. SourceLocation Begin = SourceMgr.getLocForStartOfFile(File);
  51. Lexer L(Allocator, Diags, Traits, Begin, Source, Source + strlen(Source));
  52. Sema S(Allocator, SourceMgr, Diags, Traits, /*PP=*/ nullptr);
  53. Parser P(L, S, Allocator, SourceMgr, Diags, Traits);
  54. FullComment *FC = P.parseFullComment();
  55. if (DEBUG) {
  56. llvm::errs() << "=== Source:\n" << Source << "\n=== AST:\n";
  57. FC->dump(llvm::errs(), &Traits, &SourceMgr);
  58. }
  59. Token Tok;
  60. L.lex(Tok);
  61. if (Tok.is(tok::eof))
  62. return FC;
  63. else
  64. return nullptr;
  65. }
  66. ::testing::AssertionResult HasChildCount(const Comment *C, size_t Count) {
  67. if (!C)
  68. return ::testing::AssertionFailure() << "Comment is NULL";
  69. if (Count != C->child_count())
  70. return ::testing::AssertionFailure()
  71. << "Count = " << Count
  72. << ", child_count = " << C->child_count();
  73. return ::testing::AssertionSuccess();
  74. }
  75. template <typename T>
  76. ::testing::AssertionResult GetChildAt(const Comment *C,
  77. size_t Idx,
  78. T *&Child) {
  79. if (!C)
  80. return ::testing::AssertionFailure() << "Comment is NULL";
  81. if (Idx >= C->child_count())
  82. return ::testing::AssertionFailure()
  83. << "Idx out of range. Idx = " << Idx
  84. << ", child_count = " << C->child_count();
  85. Comment::child_iterator I = C->child_begin() + Idx;
  86. Comment *CommentChild = *I;
  87. if (!CommentChild)
  88. return ::testing::AssertionFailure() << "Child is NULL";
  89. Child = dyn_cast<T>(CommentChild);
  90. if (!Child)
  91. return ::testing::AssertionFailure()
  92. << "Child is not of requested type, but a "
  93. << CommentChild->getCommentKindName();
  94. return ::testing::AssertionSuccess();
  95. }
  96. ::testing::AssertionResult HasTextAt(const Comment *C,
  97. size_t Idx,
  98. StringRef Text) {
  99. TextComment *TC;
  100. ::testing::AssertionResult AR = GetChildAt(C, Idx, TC);
  101. if (!AR)
  102. return AR;
  103. StringRef ActualText = TC->getText();
  104. if (ActualText != Text)
  105. return ::testing::AssertionFailure()
  106. << "TextComment has text \"" << ActualText.str() << "\", "
  107. "expected \"" << Text.str() << "\"";
  108. if (TC->hasTrailingNewline())
  109. return ::testing::AssertionFailure()
  110. << "TextComment has a trailing newline";
  111. return ::testing::AssertionSuccess();
  112. }
  113. ::testing::AssertionResult HasTextWithNewlineAt(const Comment *C,
  114. size_t Idx,
  115. StringRef Text) {
  116. TextComment *TC;
  117. ::testing::AssertionResult AR = GetChildAt(C, Idx, TC);
  118. if (!AR)
  119. return AR;
  120. StringRef ActualText = TC->getText();
  121. if (ActualText != Text)
  122. return ::testing::AssertionFailure()
  123. << "TextComment has text \"" << ActualText.str() << "\", "
  124. "expected \"" << Text.str() << "\"";
  125. if (!TC->hasTrailingNewline())
  126. return ::testing::AssertionFailure()
  127. << "TextComment has no trailing newline";
  128. return ::testing::AssertionSuccess();
  129. }
  130. ::testing::AssertionResult HasBlockCommandAt(const Comment *C,
  131. const CommandTraits &Traits,
  132. size_t Idx,
  133. BlockCommandComment *&BCC,
  134. StringRef Name,
  135. ParagraphComment *&Paragraph) {
  136. ::testing::AssertionResult AR = GetChildAt(C, Idx, BCC);
  137. if (!AR)
  138. return AR;
  139. StringRef ActualName = BCC->getCommandName(Traits);
  140. if (ActualName != Name)
  141. return ::testing::AssertionFailure()
  142. << "BlockCommandComment has name \"" << ActualName.str() << "\", "
  143. "expected \"" << Name.str() << "\"";
  144. Paragraph = BCC->getParagraph();
  145. return ::testing::AssertionSuccess();
  146. }
  147. ::testing::AssertionResult HasParamCommandAt(
  148. const Comment *C,
  149. const CommandTraits &Traits,
  150. size_t Idx,
  151. ParamCommandComment *&PCC,
  152. StringRef CommandName,
  153. ParamCommandComment::PassDirection Direction,
  154. bool IsDirectionExplicit,
  155. StringRef ParamName,
  156. ParagraphComment *&Paragraph) {
  157. ::testing::AssertionResult AR = GetChildAt(C, Idx, PCC);
  158. if (!AR)
  159. return AR;
  160. StringRef ActualCommandName = PCC->getCommandName(Traits);
  161. if (ActualCommandName != CommandName)
  162. return ::testing::AssertionFailure()
  163. << "ParamCommandComment has name \"" << ActualCommandName.str() << "\", "
  164. "expected \"" << CommandName.str() << "\"";
  165. if (PCC->getDirection() != Direction)
  166. return ::testing::AssertionFailure()
  167. << "ParamCommandComment has direction " << PCC->getDirection() << ", "
  168. "expected " << Direction;
  169. if (PCC->isDirectionExplicit() != IsDirectionExplicit)
  170. return ::testing::AssertionFailure()
  171. << "ParamCommandComment has "
  172. << (PCC->isDirectionExplicit() ? "explicit" : "implicit")
  173. << " direction, "
  174. "expected " << (IsDirectionExplicit ? "explicit" : "implicit");
  175. if (!ParamName.empty() && !PCC->hasParamName())
  176. return ::testing::AssertionFailure()
  177. << "ParamCommandComment has no parameter name";
  178. StringRef ActualParamName = PCC->hasParamName() ? PCC->getParamNameAsWritten() : "";
  179. if (ActualParamName != ParamName)
  180. return ::testing::AssertionFailure()
  181. << "ParamCommandComment has parameter name \"" << ActualParamName.str()
  182. << "\", "
  183. "expected \"" << ParamName.str() << "\"";
  184. Paragraph = PCC->getParagraph();
  185. return ::testing::AssertionSuccess();
  186. }
  187. ::testing::AssertionResult HasTParamCommandAt(
  188. const Comment *C,
  189. const CommandTraits &Traits,
  190. size_t Idx,
  191. TParamCommandComment *&TPCC,
  192. StringRef CommandName,
  193. StringRef ParamName,
  194. ParagraphComment *&Paragraph) {
  195. ::testing::AssertionResult AR = GetChildAt(C, Idx, TPCC);
  196. if (!AR)
  197. return AR;
  198. StringRef ActualCommandName = TPCC->getCommandName(Traits);
  199. if (ActualCommandName != CommandName)
  200. return ::testing::AssertionFailure()
  201. << "TParamCommandComment has name \"" << ActualCommandName.str() << "\", "
  202. "expected \"" << CommandName.str() << "\"";
  203. if (!ParamName.empty() && !TPCC->hasParamName())
  204. return ::testing::AssertionFailure()
  205. << "TParamCommandComment has no parameter name";
  206. StringRef ActualParamName = TPCC->hasParamName() ? TPCC->getParamNameAsWritten() : "";
  207. if (ActualParamName != ParamName)
  208. return ::testing::AssertionFailure()
  209. << "TParamCommandComment has parameter name \"" << ActualParamName.str()
  210. << "\", "
  211. "expected \"" << ParamName.str() << "\"";
  212. Paragraph = TPCC->getParagraph();
  213. return ::testing::AssertionSuccess();
  214. }
  215. ::testing::AssertionResult HasInlineCommandAt(const Comment *C,
  216. const CommandTraits &Traits,
  217. size_t Idx,
  218. InlineCommandComment *&ICC,
  219. StringRef Name) {
  220. ::testing::AssertionResult AR = GetChildAt(C, Idx, ICC);
  221. if (!AR)
  222. return AR;
  223. StringRef ActualName = ICC->getCommandName(Traits);
  224. if (ActualName != Name)
  225. return ::testing::AssertionFailure()
  226. << "InlineCommandComment has name \"" << ActualName.str() << "\", "
  227. "expected \"" << Name.str() << "\"";
  228. return ::testing::AssertionSuccess();
  229. }
  230. struct NoArgs {};
  231. ::testing::AssertionResult HasInlineCommandAt(const Comment *C,
  232. const CommandTraits &Traits,
  233. size_t Idx,
  234. InlineCommandComment *&ICC,
  235. StringRef Name,
  236. NoArgs) {
  237. ::testing::AssertionResult AR = HasInlineCommandAt(C, Traits, Idx, ICC, Name);
  238. if (!AR)
  239. return AR;
  240. if (ICC->getNumArgs() != 0)
  241. return ::testing::AssertionFailure()
  242. << "InlineCommandComment has " << ICC->getNumArgs() << " arg(s), "
  243. "expected 0";
  244. return ::testing::AssertionSuccess();
  245. }
  246. ::testing::AssertionResult HasInlineCommandAt(const Comment *C,
  247. const CommandTraits &Traits,
  248. size_t Idx,
  249. InlineCommandComment *&ICC,
  250. StringRef Name,
  251. StringRef Arg) {
  252. ::testing::AssertionResult AR = HasInlineCommandAt(C, Traits, Idx, ICC, Name);
  253. if (!AR)
  254. return AR;
  255. if (ICC->getNumArgs() != 1)
  256. return ::testing::AssertionFailure()
  257. << "InlineCommandComment has " << ICC->getNumArgs() << " arg(s), "
  258. "expected 1";
  259. StringRef ActualArg = ICC->getArgText(0);
  260. if (ActualArg != Arg)
  261. return ::testing::AssertionFailure()
  262. << "InlineCommandComment has argument \"" << ActualArg.str() << "\", "
  263. "expected \"" << Arg.str() << "\"";
  264. return ::testing::AssertionSuccess();
  265. }
  266. ::testing::AssertionResult HasHTMLStartTagAt(const Comment *C,
  267. size_t Idx,
  268. HTMLStartTagComment *&HST,
  269. StringRef TagName) {
  270. ::testing::AssertionResult AR = GetChildAt(C, Idx, HST);
  271. if (!AR)
  272. return AR;
  273. StringRef ActualTagName = HST->getTagName();
  274. if (ActualTagName != TagName)
  275. return ::testing::AssertionFailure()
  276. << "HTMLStartTagComment has name \"" << ActualTagName.str() << "\", "
  277. "expected \"" << TagName.str() << "\"";
  278. return ::testing::AssertionSuccess();
  279. }
  280. struct SelfClosing {};
  281. ::testing::AssertionResult HasHTMLStartTagAt(const Comment *C,
  282. size_t Idx,
  283. HTMLStartTagComment *&HST,
  284. StringRef TagName,
  285. SelfClosing) {
  286. ::testing::AssertionResult AR = HasHTMLStartTagAt(C, Idx, HST, TagName);
  287. if (!AR)
  288. return AR;
  289. if (!HST->isSelfClosing())
  290. return ::testing::AssertionFailure()
  291. << "HTMLStartTagComment is not self-closing";
  292. return ::testing::AssertionSuccess();
  293. }
  294. struct NoAttrs {};
  295. ::testing::AssertionResult HasHTMLStartTagAt(const Comment *C,
  296. size_t Idx,
  297. HTMLStartTagComment *&HST,
  298. StringRef TagName,
  299. NoAttrs) {
  300. ::testing::AssertionResult AR = HasHTMLStartTagAt(C, Idx, HST, TagName);
  301. if (!AR)
  302. return AR;
  303. if (HST->isSelfClosing())
  304. return ::testing::AssertionFailure()
  305. << "HTMLStartTagComment is self-closing";
  306. if (HST->getNumAttrs() != 0)
  307. return ::testing::AssertionFailure()
  308. << "HTMLStartTagComment has " << HST->getNumAttrs() << " attr(s), "
  309. "expected 0";
  310. return ::testing::AssertionSuccess();
  311. }
  312. ::testing::AssertionResult HasHTMLStartTagAt(const Comment *C,
  313. size_t Idx,
  314. HTMLStartTagComment *&HST,
  315. StringRef TagName,
  316. StringRef AttrName,
  317. StringRef AttrValue) {
  318. ::testing::AssertionResult AR = HasHTMLStartTagAt(C, Idx, HST, TagName);
  319. if (!AR)
  320. return AR;
  321. if (HST->isSelfClosing())
  322. return ::testing::AssertionFailure()
  323. << "HTMLStartTagComment is self-closing";
  324. if (HST->getNumAttrs() != 1)
  325. return ::testing::AssertionFailure()
  326. << "HTMLStartTagComment has " << HST->getNumAttrs() << " attr(s), "
  327. "expected 1";
  328. StringRef ActualName = HST->getAttr(0).Name;
  329. if (ActualName != AttrName)
  330. return ::testing::AssertionFailure()
  331. << "HTMLStartTagComment has attr \"" << ActualName.str() << "\", "
  332. "expected \"" << AttrName.str() << "\"";
  333. StringRef ActualValue = HST->getAttr(0).Value;
  334. if (ActualValue != AttrValue)
  335. return ::testing::AssertionFailure()
  336. << "HTMLStartTagComment has attr value \"" << ActualValue.str() << "\", "
  337. "expected \"" << AttrValue.str() << "\"";
  338. return ::testing::AssertionSuccess();
  339. }
  340. ::testing::AssertionResult HasHTMLEndTagAt(const Comment *C,
  341. size_t Idx,
  342. HTMLEndTagComment *&HET,
  343. StringRef TagName) {
  344. ::testing::AssertionResult AR = GetChildAt(C, Idx, HET);
  345. if (!AR)
  346. return AR;
  347. StringRef ActualTagName = HET->getTagName();
  348. if (ActualTagName != TagName)
  349. return ::testing::AssertionFailure()
  350. << "HTMLEndTagComment has name \"" << ActualTagName.str() << "\", "
  351. "expected \"" << TagName.str() << "\"";
  352. return ::testing::AssertionSuccess();
  353. }
  354. ::testing::AssertionResult HasParagraphCommentAt(const Comment *C,
  355. size_t Idx,
  356. StringRef Text) {
  357. ParagraphComment *PC;
  358. {
  359. ::testing::AssertionResult AR = GetChildAt(C, Idx, PC);
  360. if (!AR)
  361. return AR;
  362. }
  363. {
  364. ::testing::AssertionResult AR = HasChildCount(PC, 1);
  365. if (!AR)
  366. return AR;
  367. }
  368. {
  369. ::testing::AssertionResult AR = HasTextAt(PC, 0, Text);
  370. if (!AR)
  371. return AR;
  372. }
  373. return ::testing::AssertionSuccess();
  374. }
  375. ::testing::AssertionResult HasVerbatimBlockAt(const Comment *C,
  376. const CommandTraits &Traits,
  377. size_t Idx,
  378. VerbatimBlockComment *&VBC,
  379. StringRef Name,
  380. StringRef CloseName) {
  381. ::testing::AssertionResult AR = GetChildAt(C, Idx, VBC);
  382. if (!AR)
  383. return AR;
  384. StringRef ActualName = VBC->getCommandName(Traits);
  385. if (ActualName != Name)
  386. return ::testing::AssertionFailure()
  387. << "VerbatimBlockComment has name \"" << ActualName.str() << "\", "
  388. "expected \"" << Name.str() << "\"";
  389. StringRef ActualCloseName = VBC->getCloseName();
  390. if (ActualCloseName != CloseName)
  391. return ::testing::AssertionFailure()
  392. << "VerbatimBlockComment has closing command name \""
  393. << ActualCloseName.str() << "\", "
  394. "expected \"" << CloseName.str() << "\"";
  395. return ::testing::AssertionSuccess();
  396. }
  397. struct NoLines {};
  398. struct Lines {};
  399. ::testing::AssertionResult HasVerbatimBlockAt(const Comment *C,
  400. const CommandTraits &Traits,
  401. size_t Idx,
  402. VerbatimBlockComment *&VBC,
  403. StringRef Name,
  404. StringRef CloseName,
  405. NoLines) {
  406. ::testing::AssertionResult AR = HasVerbatimBlockAt(C, Traits, Idx, VBC, Name,
  407. CloseName);
  408. if (!AR)
  409. return AR;
  410. if (VBC->getNumLines() != 0)
  411. return ::testing::AssertionFailure()
  412. << "VerbatimBlockComment has " << VBC->getNumLines() << " lines(s), "
  413. "expected 0";
  414. return ::testing::AssertionSuccess();
  415. }
  416. ::testing::AssertionResult HasVerbatimBlockAt(const Comment *C,
  417. const CommandTraits &Traits,
  418. size_t Idx,
  419. VerbatimBlockComment *&VBC,
  420. StringRef Name,
  421. StringRef CloseName,
  422. Lines,
  423. StringRef Line0) {
  424. ::testing::AssertionResult AR = HasVerbatimBlockAt(C, Traits, Idx, VBC, Name,
  425. CloseName);
  426. if (!AR)
  427. return AR;
  428. if (VBC->getNumLines() != 1)
  429. return ::testing::AssertionFailure()
  430. << "VerbatimBlockComment has " << VBC->getNumLines() << " lines(s), "
  431. "expected 1";
  432. StringRef ActualLine0 = VBC->getText(0);
  433. if (ActualLine0 != Line0)
  434. return ::testing::AssertionFailure()
  435. << "VerbatimBlockComment has lines[0] \"" << ActualLine0.str() << "\", "
  436. "expected \"" << Line0.str() << "\"";
  437. return ::testing::AssertionSuccess();
  438. }
  439. ::testing::AssertionResult HasVerbatimBlockAt(const Comment *C,
  440. const CommandTraits &Traits,
  441. size_t Idx,
  442. VerbatimBlockComment *&VBC,
  443. StringRef Name,
  444. StringRef CloseName,
  445. Lines,
  446. StringRef Line0,
  447. StringRef Line1) {
  448. ::testing::AssertionResult AR = HasVerbatimBlockAt(C, Traits, Idx, VBC, Name,
  449. CloseName);
  450. if (!AR)
  451. return AR;
  452. if (VBC->getNumLines() != 2)
  453. return ::testing::AssertionFailure()
  454. << "VerbatimBlockComment has " << VBC->getNumLines() << " lines(s), "
  455. "expected 2";
  456. StringRef ActualLine0 = VBC->getText(0);
  457. if (ActualLine0 != Line0)
  458. return ::testing::AssertionFailure()
  459. << "VerbatimBlockComment has lines[0] \"" << ActualLine0.str() << "\", "
  460. "expected \"" << Line0.str() << "\"";
  461. StringRef ActualLine1 = VBC->getText(1);
  462. if (ActualLine1 != Line1)
  463. return ::testing::AssertionFailure()
  464. << "VerbatimBlockComment has lines[1] \"" << ActualLine1.str() << "\", "
  465. "expected \"" << Line1.str() << "\"";
  466. return ::testing::AssertionSuccess();
  467. }
  468. ::testing::AssertionResult HasVerbatimLineAt(const Comment *C,
  469. const CommandTraits &Traits,
  470. size_t Idx,
  471. VerbatimLineComment *&VLC,
  472. StringRef Name,
  473. StringRef Text) {
  474. ::testing::AssertionResult AR = GetChildAt(C, Idx, VLC);
  475. if (!AR)
  476. return AR;
  477. StringRef ActualName = VLC->getCommandName(Traits);
  478. if (ActualName != Name)
  479. return ::testing::AssertionFailure()
  480. << "VerbatimLineComment has name \"" << ActualName.str() << "\", "
  481. "expected \"" << Name.str() << "\"";
  482. StringRef ActualText = VLC->getText();
  483. if (ActualText != Text)
  484. return ::testing::AssertionFailure()
  485. << "VerbatimLineComment has text \"" << ActualText.str() << "\", "
  486. "expected \"" << Text.str() << "\"";
  487. return ::testing::AssertionSuccess();
  488. }
  489. TEST_F(CommentParserTest, Basic1) {
  490. const char *Source = "//";
  491. FullComment *FC = parseString(Source);
  492. ASSERT_TRUE(HasChildCount(FC, 0));
  493. }
  494. TEST_F(CommentParserTest, Basic2) {
  495. const char *Source = "// Meow";
  496. FullComment *FC = parseString(Source);
  497. ASSERT_TRUE(HasChildCount(FC, 1));
  498. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " Meow"));
  499. }
  500. TEST_F(CommentParserTest, Basic3) {
  501. const char *Source =
  502. "// Aaa\n"
  503. "// Bbb";
  504. FullComment *FC = parseString(Source);
  505. ASSERT_TRUE(HasChildCount(FC, 1));
  506. {
  507. ParagraphComment *PC;
  508. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  509. ASSERT_TRUE(HasChildCount(PC, 2));
  510. ASSERT_TRUE(HasTextWithNewlineAt(PC, 0, " Aaa"));
  511. ASSERT_TRUE(HasTextAt(PC, 1, " Bbb"));
  512. }
  513. }
  514. TEST_F(CommentParserTest, ParagraphSplitting1) {
  515. const char *Sources[] = {
  516. "// Aaa\n"
  517. "//\n"
  518. "// Bbb",
  519. "// Aaa\n"
  520. "// \n"
  521. "// Bbb",
  522. "// Aaa\n"
  523. "//\t\n"
  524. "// Bbb",
  525. "// Aaa\n"
  526. "//\n"
  527. "//\n"
  528. "// Bbb",
  529. "/**\n"
  530. " Aaa\n"
  531. "\n"
  532. " Bbb\n"
  533. "*/",
  534. "/**\n"
  535. " Aaa\n"
  536. " \n"
  537. " Bbb\n"
  538. "*/",
  539. "/**\n"
  540. " Aaa\n"
  541. "\t \n"
  542. " Bbb\n"
  543. "*/",
  544. };
  545. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  546. FullComment *FC = parseString(Sources[i]);
  547. ASSERT_TRUE(HasChildCount(FC, 2));
  548. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " Aaa"));
  549. ASSERT_TRUE(HasParagraphCommentAt(FC, 1, " Bbb"));
  550. }
  551. }
  552. TEST_F(CommentParserTest, Paragraph1) {
  553. const char *Source =
  554. "// \\brief Aaa\n"
  555. "//\n"
  556. "// Bbb";
  557. FullComment *FC = parseString(Source);
  558. ASSERT_TRUE(HasChildCount(FC, 3));
  559. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  560. {
  561. BlockCommandComment *BCC;
  562. ParagraphComment *PC;
  563. ASSERT_TRUE(HasBlockCommandAt(FC, Traits, 1, BCC, "brief", PC));
  564. ASSERT_TRUE(HasParagraphCommentAt(BCC, 0, " Aaa"));
  565. }
  566. ASSERT_TRUE(HasParagraphCommentAt(FC, 2, " Bbb"));
  567. }
  568. TEST_F(CommentParserTest, Paragraph2) {
  569. const char *Source = "// \\brief \\author";
  570. FullComment *FC = parseString(Source);
  571. ASSERT_TRUE(HasChildCount(FC, 3));
  572. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  573. {
  574. BlockCommandComment *BCC;
  575. ParagraphComment *PC;
  576. ASSERT_TRUE(HasBlockCommandAt(FC, Traits, 1, BCC, "brief", PC));
  577. ASSERT_TRUE(HasParagraphCommentAt(BCC, 0, " "));
  578. }
  579. {
  580. BlockCommandComment *BCC;
  581. ParagraphComment *PC;
  582. ASSERT_TRUE(HasBlockCommandAt(FC, Traits, 2, BCC, "author", PC));
  583. ASSERT_TRUE(GetChildAt(BCC, 0, PC));
  584. ASSERT_TRUE(HasChildCount(PC, 0));
  585. }
  586. }
  587. TEST_F(CommentParserTest, Paragraph3) {
  588. const char *Source =
  589. "// \\brief Aaa\n"
  590. "// Bbb \\author\n"
  591. "// Ccc";
  592. FullComment *FC = parseString(Source);
  593. ASSERT_TRUE(HasChildCount(FC, 3));
  594. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  595. {
  596. BlockCommandComment *BCC;
  597. ParagraphComment *PC;
  598. ASSERT_TRUE(HasBlockCommandAt(FC, Traits, 1, BCC, "brief", PC));
  599. ASSERT_TRUE(GetChildAt(BCC, 0, PC));
  600. ASSERT_TRUE(HasChildCount(PC, 2));
  601. ASSERT_TRUE(HasTextWithNewlineAt(PC, 0, " Aaa"));
  602. ASSERT_TRUE(HasTextAt(PC, 1, " Bbb "));
  603. }
  604. {
  605. BlockCommandComment *BCC;
  606. ParagraphComment *PC;
  607. ASSERT_TRUE(HasBlockCommandAt(FC, Traits, 2, BCC, "author", PC));
  608. ASSERT_TRUE(HasParagraphCommentAt(BCC, 0, " Ccc"));
  609. }
  610. }
  611. TEST_F(CommentParserTest, ParamCommand1) {
  612. const char *Source = "// \\param aaa";
  613. FullComment *FC = parseString(Source);
  614. ASSERT_TRUE(HasChildCount(FC, 2));
  615. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  616. {
  617. ParamCommandComment *PCC;
  618. ParagraphComment *PC;
  619. ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
  620. ParamCommandComment::In,
  621. /* IsDirectionExplicit = */ false,
  622. "aaa", PC));
  623. ASSERT_TRUE(HasChildCount(PCC, 1));
  624. ASSERT_TRUE(HasChildCount(PC, 0));
  625. }
  626. }
  627. TEST_F(CommentParserTest, ParamCommand2) {
  628. const char *Source = "// \\param\\brief";
  629. FullComment *FC = parseString(Source);
  630. ASSERT_TRUE(HasChildCount(FC, 3));
  631. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  632. {
  633. ParamCommandComment *PCC;
  634. ParagraphComment *PC;
  635. ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
  636. ParamCommandComment::In,
  637. /* IsDirectionExplicit = */ false,
  638. "", PC));
  639. ASSERT_TRUE(HasChildCount(PCC, 1));
  640. ASSERT_TRUE(HasChildCount(PC, 0));
  641. }
  642. {
  643. BlockCommandComment *BCC;
  644. ParagraphComment *PC;
  645. ASSERT_TRUE(HasBlockCommandAt(FC, Traits, 2, BCC, "brief", PC));
  646. ASSERT_TRUE(HasChildCount(PC, 0));
  647. }
  648. }
  649. TEST_F(CommentParserTest, ParamCommand3) {
  650. const char *Sources[] = {
  651. "// \\param aaa Bbb\n",
  652. "// \\param\n"
  653. "// aaa Bbb\n",
  654. "// \\param \n"
  655. "// aaa Bbb\n",
  656. "// \\param aaa\n"
  657. "// Bbb\n"
  658. };
  659. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  660. FullComment *FC = parseString(Sources[i]);
  661. ASSERT_TRUE(HasChildCount(FC, 2));
  662. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  663. {
  664. ParamCommandComment *PCC;
  665. ParagraphComment *PC;
  666. ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
  667. ParamCommandComment::In,
  668. /* IsDirectionExplicit = */ false,
  669. "aaa", PC));
  670. ASSERT_TRUE(HasChildCount(PCC, 1));
  671. ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
  672. }
  673. }
  674. }
  675. TEST_F(CommentParserTest, ParamCommand4) {
  676. const char *Sources[] = {
  677. "// \\param [in] aaa Bbb\n",
  678. "// \\param[in] aaa Bbb\n",
  679. "// \\param\n"
  680. "// [in] aaa Bbb\n",
  681. "// \\param [in]\n"
  682. "// aaa Bbb\n",
  683. "// \\param [in] aaa\n"
  684. "// Bbb\n",
  685. };
  686. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  687. FullComment *FC = parseString(Sources[i]);
  688. ASSERT_TRUE(HasChildCount(FC, 2));
  689. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  690. {
  691. ParamCommandComment *PCC;
  692. ParagraphComment *PC;
  693. ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
  694. ParamCommandComment::In,
  695. /* IsDirectionExplicit = */ true,
  696. "aaa", PC));
  697. ASSERT_TRUE(HasChildCount(PCC, 1));
  698. ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
  699. }
  700. }
  701. }
  702. TEST_F(CommentParserTest, ParamCommand5) {
  703. const char *Sources[] = {
  704. "// \\param [out] aaa Bbb\n",
  705. "// \\param[out] aaa Bbb\n",
  706. "// \\param\n"
  707. "// [out] aaa Bbb\n",
  708. "// \\param [out]\n"
  709. "// aaa Bbb\n",
  710. "// \\param [out] aaa\n"
  711. "// Bbb\n",
  712. };
  713. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  714. FullComment *FC = parseString(Sources[i]);
  715. ASSERT_TRUE(HasChildCount(FC, 2));
  716. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  717. {
  718. ParamCommandComment *PCC;
  719. ParagraphComment *PC;
  720. ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
  721. ParamCommandComment::Out,
  722. /* IsDirectionExplicit = */ true,
  723. "aaa", PC));
  724. ASSERT_TRUE(HasChildCount(PCC, 1));
  725. ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
  726. }
  727. }
  728. }
  729. TEST_F(CommentParserTest, ParamCommand6) {
  730. const char *Sources[] = {
  731. "// \\param [in,out] aaa Bbb\n",
  732. "// \\param[in,out] aaa Bbb\n",
  733. "// \\param [in, out] aaa Bbb\n",
  734. "// \\param [in,\n"
  735. "// out] aaa Bbb\n",
  736. "// \\param [in,out]\n"
  737. "// aaa Bbb\n",
  738. "// \\param [in,out] aaa\n"
  739. "// Bbb\n"
  740. };
  741. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  742. FullComment *FC = parseString(Sources[i]);
  743. ASSERT_TRUE(HasChildCount(FC, 2));
  744. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  745. {
  746. ParamCommandComment *PCC;
  747. ParagraphComment *PC;
  748. ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
  749. ParamCommandComment::InOut,
  750. /* IsDirectionExplicit = */ true,
  751. "aaa", PC));
  752. ASSERT_TRUE(HasChildCount(PCC, 1));
  753. ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
  754. }
  755. }
  756. }
  757. TEST_F(CommentParserTest, ParamCommand7) {
  758. const char *Source =
  759. "// \\param aaa \\% Bbb \\$ ccc\n";
  760. FullComment *FC = parseString(Source);
  761. ASSERT_TRUE(HasChildCount(FC, 2));
  762. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  763. {
  764. ParamCommandComment *PCC;
  765. ParagraphComment *PC;
  766. ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
  767. ParamCommandComment::In,
  768. /* IsDirectionExplicit = */ false,
  769. "aaa", PC));
  770. ASSERT_TRUE(HasChildCount(PCC, 1));
  771. ASSERT_TRUE(HasChildCount(PC, 5));
  772. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  773. ASSERT_TRUE(HasTextAt(PC, 1, "%"));
  774. ASSERT_TRUE(HasTextAt(PC, 2, " Bbb "));
  775. ASSERT_TRUE(HasTextAt(PC, 3, "$"));
  776. ASSERT_TRUE(HasTextAt(PC, 4, " ccc"));
  777. }
  778. }
  779. TEST_F(CommentParserTest, TParamCommand1) {
  780. const char *Sources[] = {
  781. "// \\tparam aaa Bbb\n",
  782. "// \\tparam\n"
  783. "// aaa Bbb\n",
  784. "// \\tparam \n"
  785. "// aaa Bbb\n",
  786. "// \\tparam aaa\n"
  787. "// Bbb\n"
  788. };
  789. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  790. FullComment *FC = parseString(Sources[i]);
  791. ASSERT_TRUE(HasChildCount(FC, 2));
  792. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  793. {
  794. TParamCommandComment *TPCC;
  795. ParagraphComment *PC;
  796. ASSERT_TRUE(HasTParamCommandAt(FC, Traits, 1, TPCC, "tparam",
  797. "aaa", PC));
  798. ASSERT_TRUE(HasChildCount(TPCC, 1));
  799. ASSERT_TRUE(HasParagraphCommentAt(TPCC, 0, " Bbb"));
  800. }
  801. }
  802. }
  803. TEST_F(CommentParserTest, TParamCommand2) {
  804. const char *Source = "// \\tparam\\brief";
  805. FullComment *FC = parseString(Source);
  806. ASSERT_TRUE(HasChildCount(FC, 3));
  807. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  808. {
  809. TParamCommandComment *TPCC;
  810. ParagraphComment *PC;
  811. ASSERT_TRUE(HasTParamCommandAt(FC, Traits, 1, TPCC, "tparam", "", PC));
  812. ASSERT_TRUE(HasChildCount(TPCC, 1));
  813. ASSERT_TRUE(HasChildCount(PC, 0));
  814. }
  815. {
  816. BlockCommandComment *BCC;
  817. ParagraphComment *PC;
  818. ASSERT_TRUE(HasBlockCommandAt(FC, Traits, 2, BCC, "brief", PC));
  819. ASSERT_TRUE(HasChildCount(PC, 0));
  820. }
  821. }
  822. TEST_F(CommentParserTest, InlineCommand1) {
  823. const char *Source = "// \\c";
  824. FullComment *FC = parseString(Source);
  825. ASSERT_TRUE(HasChildCount(FC, 1));
  826. {
  827. ParagraphComment *PC;
  828. InlineCommandComment *ICC;
  829. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  830. ASSERT_TRUE(HasChildCount(PC, 2));
  831. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  832. ASSERT_TRUE(HasInlineCommandAt(PC, Traits, 1, ICC, "c", NoArgs()));
  833. }
  834. }
  835. TEST_F(CommentParserTest, InlineCommand2) {
  836. const char *Source = "// \\c ";
  837. FullComment *FC = parseString(Source);
  838. ASSERT_TRUE(HasChildCount(FC, 1));
  839. {
  840. ParagraphComment *PC;
  841. InlineCommandComment *ICC;
  842. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  843. ASSERT_TRUE(HasChildCount(PC, 3));
  844. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  845. ASSERT_TRUE(HasInlineCommandAt(PC, Traits, 1, ICC, "c", NoArgs()));
  846. ASSERT_TRUE(HasTextAt(PC, 2, " "));
  847. }
  848. }
  849. TEST_F(CommentParserTest, InlineCommand3) {
  850. const char *Source = "// \\c aaa\n";
  851. FullComment *FC = parseString(Source);
  852. ASSERT_TRUE(HasChildCount(FC, 1));
  853. {
  854. ParagraphComment *PC;
  855. InlineCommandComment *ICC;
  856. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  857. ASSERT_TRUE(HasChildCount(PC, 2));
  858. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  859. ASSERT_TRUE(HasInlineCommandAt(PC, Traits, 1, ICC, "c", "aaa"));
  860. }
  861. }
  862. TEST_F(CommentParserTest, InlineCommand4) {
  863. const char *Source = "// \\c aaa bbb";
  864. FullComment *FC = parseString(Source);
  865. ASSERT_TRUE(HasChildCount(FC, 1));
  866. {
  867. ParagraphComment *PC;
  868. InlineCommandComment *ICC;
  869. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  870. ASSERT_TRUE(HasChildCount(PC, 3));
  871. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  872. ASSERT_TRUE(HasInlineCommandAt(PC, Traits, 1, ICC, "c", "aaa"));
  873. ASSERT_TRUE(HasTextAt(PC, 2, " bbb"));
  874. }
  875. }
  876. TEST_F(CommentParserTest, InlineCommand5) {
  877. const char *Source = "// \\unknown aaa\n";
  878. FullComment *FC = parseString(Source);
  879. ASSERT_TRUE(HasChildCount(FC, 1));
  880. {
  881. ParagraphComment *PC;
  882. InlineCommandComment *ICC;
  883. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  884. ASSERT_TRUE(HasChildCount(PC, 3));
  885. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  886. ASSERT_TRUE(HasInlineCommandAt(PC, Traits, 1, ICC, "unknown", NoArgs()));
  887. ASSERT_TRUE(HasTextAt(PC, 2, " aaa"));
  888. }
  889. }
  890. TEST_F(CommentParserTest, HTML1) {
  891. const char *Sources[] = {
  892. "// <a",
  893. "// <a>",
  894. "// <a >"
  895. };
  896. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  897. FullComment *FC = parseString(Sources[i]);
  898. ASSERT_TRUE(HasChildCount(FC, 1));
  899. {
  900. ParagraphComment *PC;
  901. HTMLStartTagComment *HST;
  902. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  903. ASSERT_TRUE(HasChildCount(PC, 2));
  904. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  905. ASSERT_TRUE(HasHTMLStartTagAt(PC, 1, HST, "a", NoAttrs()));
  906. }
  907. }
  908. }
  909. TEST_F(CommentParserTest, HTML2) {
  910. const char *Sources[] = {
  911. "// <br/>",
  912. "// <br />"
  913. };
  914. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  915. FullComment *FC = parseString(Sources[i]);
  916. ASSERT_TRUE(HasChildCount(FC, 1));
  917. {
  918. ParagraphComment *PC;
  919. HTMLStartTagComment *HST;
  920. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  921. ASSERT_TRUE(HasChildCount(PC, 2));
  922. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  923. ASSERT_TRUE(HasHTMLStartTagAt(PC, 1, HST, "br", SelfClosing()));
  924. }
  925. }
  926. }
  927. TEST_F(CommentParserTest, HTML3) {
  928. const char *Sources[] = {
  929. "// <a href",
  930. "// <a href ",
  931. "// <a href>",
  932. "// <a href >",
  933. };
  934. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  935. FullComment *FC = parseString(Sources[i]);
  936. ASSERT_TRUE(HasChildCount(FC, 1));
  937. {
  938. ParagraphComment *PC;
  939. HTMLStartTagComment *HST;
  940. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  941. ASSERT_TRUE(HasChildCount(PC, 2));
  942. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  943. ASSERT_TRUE(HasHTMLStartTagAt(PC, 1, HST, "a", "href", ""));
  944. }
  945. }
  946. }
  947. TEST_F(CommentParserTest, HTML4) {
  948. const char *Sources[] = {
  949. "// <a href=\"bbb\"",
  950. "// <a href=\"bbb\">",
  951. };
  952. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  953. FullComment *FC = parseString(Sources[i]);
  954. ASSERT_TRUE(HasChildCount(FC, 1));
  955. {
  956. ParagraphComment *PC;
  957. HTMLStartTagComment *HST;
  958. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  959. ASSERT_TRUE(HasChildCount(PC, 2));
  960. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  961. ASSERT_TRUE(HasHTMLStartTagAt(PC, 1, HST, "a", "href", "bbb"));
  962. }
  963. }
  964. }
  965. TEST_F(CommentParserTest, HTML5) {
  966. const char *Sources[] = {
  967. "// </a",
  968. "// </a>",
  969. "// </a >"
  970. };
  971. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  972. FullComment *FC = parseString(Sources[i]);
  973. ASSERT_TRUE(HasChildCount(FC, 1));
  974. {
  975. ParagraphComment *PC;
  976. HTMLEndTagComment *HET;
  977. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  978. ASSERT_TRUE(HasChildCount(PC, 2));
  979. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  980. ASSERT_TRUE(HasHTMLEndTagAt(PC, 1, HET, "a"));
  981. }
  982. }
  983. }
  984. TEST_F(CommentParserTest, HTML6) {
  985. const char *Source =
  986. "// <pre>\n"
  987. "// Aaa\n"
  988. "// Bbb\n"
  989. "// </pre>\n";
  990. FullComment *FC = parseString(Source);
  991. ASSERT_TRUE(HasChildCount(FC, 1));
  992. {
  993. ParagraphComment *PC;
  994. HTMLStartTagComment *HST;
  995. HTMLEndTagComment *HET;
  996. ASSERT_TRUE(GetChildAt(FC, 0, PC));
  997. ASSERT_TRUE(HasChildCount(PC, 6));
  998. ASSERT_TRUE(HasTextAt(PC, 0, " "));
  999. ASSERT_TRUE(HasHTMLStartTagAt(PC, 1, HST, "pre", NoAttrs()));
  1000. ASSERT_TRUE(HasTextWithNewlineAt(PC, 2, " Aaa"));
  1001. ASSERT_TRUE(HasTextWithNewlineAt(PC, 3, " Bbb"));
  1002. ASSERT_TRUE(HasTextAt(PC, 4, " "));
  1003. ASSERT_TRUE(HasHTMLEndTagAt(PC, 5, HET, "pre"));
  1004. }
  1005. }
  1006. TEST_F(CommentParserTest, VerbatimBlock1) {
  1007. const char *Source = "// \\verbatim\\endverbatim\n";
  1008. FullComment *FC = parseString(Source);
  1009. ASSERT_TRUE(HasChildCount(FC, 2));
  1010. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  1011. {
  1012. VerbatimBlockComment *VCC;
  1013. ASSERT_TRUE(HasVerbatimBlockAt(FC, Traits, 1, VCC,
  1014. "verbatim", "endverbatim",
  1015. NoLines()));
  1016. }
  1017. }
  1018. TEST_F(CommentParserTest, VerbatimBlock2) {
  1019. const char *Source = "// \\verbatim Aaa \\endverbatim\n";
  1020. FullComment *FC = parseString(Source);
  1021. ASSERT_TRUE(HasChildCount(FC, 2));
  1022. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  1023. {
  1024. VerbatimBlockComment *VBC;
  1025. ASSERT_TRUE(HasVerbatimBlockAt(FC, Traits, 1, VBC,
  1026. "verbatim", "endverbatim",
  1027. Lines(), " Aaa "));
  1028. }
  1029. }
  1030. TEST_F(CommentParserTest, VerbatimBlock3) {
  1031. const char *Source = "// \\verbatim Aaa\n";
  1032. FullComment *FC = parseString(Source);
  1033. ASSERT_TRUE(HasChildCount(FC, 2));
  1034. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  1035. {
  1036. VerbatimBlockComment *VBC;
  1037. ASSERT_TRUE(HasVerbatimBlockAt(FC, Traits, 1, VBC, "verbatim", "",
  1038. Lines(), " Aaa"));
  1039. }
  1040. }
  1041. TEST_F(CommentParserTest, VerbatimBlock4) {
  1042. const char *Source =
  1043. "//\\verbatim\n"
  1044. "//\\endverbatim\n";
  1045. FullComment *FC = parseString(Source);
  1046. ASSERT_TRUE(HasChildCount(FC, 1));
  1047. {
  1048. VerbatimBlockComment *VBC;
  1049. ASSERT_TRUE(HasVerbatimBlockAt(FC, Traits, 0, VBC,
  1050. "verbatim", "endverbatim",
  1051. NoLines()));
  1052. }
  1053. }
  1054. TEST_F(CommentParserTest, VerbatimBlock5) {
  1055. const char *Sources[] = {
  1056. "//\\verbatim\n"
  1057. "// Aaa\n"
  1058. "//\\endverbatim\n",
  1059. "/*\\verbatim\n"
  1060. " * Aaa\n"
  1061. " *\\endverbatim*/"
  1062. };
  1063. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  1064. FullComment *FC = parseString(Sources[i]);
  1065. ASSERT_TRUE(HasChildCount(FC, 1));
  1066. {
  1067. VerbatimBlockComment *VBC;
  1068. ASSERT_TRUE(HasVerbatimBlockAt(FC, Traits, 0, VBC,
  1069. "verbatim", "endverbatim",
  1070. Lines(), " Aaa"));
  1071. }
  1072. }
  1073. }
  1074. TEST_F(CommentParserTest, VerbatimBlock6) {
  1075. const char *Sources[] = {
  1076. "// \\verbatim\n"
  1077. "// Aaa\n"
  1078. "// \\endverbatim\n",
  1079. "/* \\verbatim\n"
  1080. " * Aaa\n"
  1081. " * \\endverbatim*/"
  1082. };
  1083. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  1084. FullComment *FC = parseString(Sources[i]);
  1085. ASSERT_TRUE(HasChildCount(FC, 2));
  1086. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  1087. {
  1088. VerbatimBlockComment *VBC;
  1089. ASSERT_TRUE(HasVerbatimBlockAt(FC, Traits, 1, VBC,
  1090. "verbatim", "endverbatim",
  1091. Lines(), " Aaa"));
  1092. }
  1093. }
  1094. }
  1095. TEST_F(CommentParserTest, VerbatimBlock7) {
  1096. const char *Sources[] = {
  1097. "// \\verbatim\n"
  1098. "// Aaa\n"
  1099. "// Bbb\n"
  1100. "// \\endverbatim\n",
  1101. "/* \\verbatim\n"
  1102. " * Aaa\n"
  1103. " * Bbb\n"
  1104. " * \\endverbatim*/"
  1105. };
  1106. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  1107. FullComment *FC = parseString(Sources[i]);
  1108. ASSERT_TRUE(HasChildCount(FC, 2));
  1109. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  1110. {
  1111. VerbatimBlockComment *VBC;
  1112. ASSERT_TRUE(HasVerbatimBlockAt(FC, Traits, 1, VBC,
  1113. "verbatim", "endverbatim",
  1114. Lines(), " Aaa", " Bbb"));
  1115. }
  1116. }
  1117. }
  1118. TEST_F(CommentParserTest, VerbatimBlock8) {
  1119. const char *Sources[] = {
  1120. "// \\verbatim\n"
  1121. "// Aaa\n"
  1122. "//\n"
  1123. "// Bbb\n"
  1124. "// \\endverbatim\n",
  1125. "/* \\verbatim\n"
  1126. " * Aaa\n"
  1127. " *\n"
  1128. " * Bbb\n"
  1129. " * \\endverbatim*/"
  1130. };
  1131. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  1132. FullComment *FC = parseString(Sources[i]);
  1133. ASSERT_TRUE(HasChildCount(FC, 2));
  1134. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  1135. {
  1136. VerbatimBlockComment *VBC;
  1137. ASSERT_TRUE(HasVerbatimBlockAt(FC, Traits, 1, VBC,
  1138. "verbatim", "endverbatim"));
  1139. ASSERT_EQ(3U, VBC->getNumLines());
  1140. ASSERT_EQ(" Aaa", VBC->getText(0));
  1141. ASSERT_EQ("", VBC->getText(1));
  1142. ASSERT_EQ(" Bbb", VBC->getText(2));
  1143. }
  1144. }
  1145. }
  1146. TEST_F(CommentParserTest, VerbatimLine1) {
  1147. const char *Sources[] = {
  1148. "// \\fn",
  1149. "// \\fn\n"
  1150. };
  1151. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  1152. FullComment *FC = parseString(Sources[i]);
  1153. ASSERT_TRUE(HasChildCount(FC, 2));
  1154. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  1155. {
  1156. VerbatimLineComment *VLC;
  1157. ASSERT_TRUE(HasVerbatimLineAt(FC, Traits, 1, VLC, "fn", ""));
  1158. }
  1159. }
  1160. }
  1161. TEST_F(CommentParserTest, VerbatimLine2) {
  1162. const char *Sources[] = {
  1163. "/// \\fn void *foo(const char *zzz = \"\\$\");\n//",
  1164. "/** \\fn void *foo(const char *zzz = \"\\$\");*/"
  1165. };
  1166. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  1167. FullComment *FC = parseString(Sources[i]);
  1168. ASSERT_TRUE(HasChildCount(FC, 2));
  1169. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  1170. {
  1171. VerbatimLineComment *VLC;
  1172. ASSERT_TRUE(HasVerbatimLineAt(FC, Traits, 1, VLC, "fn",
  1173. " void *foo(const char *zzz = \"\\$\");"));
  1174. }
  1175. }
  1176. }
  1177. TEST_F(CommentParserTest, Deprecated) {
  1178. const char *Sources[] = {
  1179. "/** @deprecated*/",
  1180. "/// @deprecated\n"
  1181. };
  1182. for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
  1183. FullComment *FC = parseString(Sources[i]);
  1184. ASSERT_TRUE(HasChildCount(FC, 2));
  1185. ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
  1186. {
  1187. BlockCommandComment *BCC;
  1188. ParagraphComment *PC;
  1189. ASSERT_TRUE(HasBlockCommandAt(FC, Traits, 1, BCC, "deprecated", PC));
  1190. ASSERT_TRUE(HasChildCount(PC, 0));
  1191. }
  1192. }
  1193. }
  1194. } // unnamed namespace
  1195. } // end namespace comments
  1196. } // end namespace clang