CommentSema.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. //===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===//
  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/CommentSema.h"
  10. #include "clang/AST/Attr.h"
  11. #include "clang/AST/CommentCommandTraits.h"
  12. #include "clang/AST/CommentDiagnostic.h"
  13. #include "clang/AST/Decl.h"
  14. #include "clang/AST/DeclTemplate.h"
  15. #include "clang/Basic/SourceManager.h"
  16. #include "clang/Lex/Preprocessor.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/ADT/StringSwitch.h"
  19. namespace clang {
  20. namespace comments {
  21. namespace {
  22. #include "clang/AST/CommentHTMLTagsProperties.inc"
  23. } // unnamed namespace
  24. Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
  25. DiagnosticsEngine &Diags, CommandTraits &Traits,
  26. const Preprocessor *PP) :
  27. Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits),
  28. PP(PP), ThisDeclInfo(nullptr), BriefCommand(nullptr),
  29. HeaderfileCommand(nullptr) {
  30. }
  31. void Sema::setDecl(const Decl *D) {
  32. if (!D)
  33. return;
  34. ThisDeclInfo = new (Allocator) DeclInfo;
  35. ThisDeclInfo->CommentDecl = D;
  36. ThisDeclInfo->IsFilled = false;
  37. }
  38. ParagraphComment *Sema::actOnParagraphComment(
  39. ArrayRef<InlineContentComment *> Content) {
  40. return new (Allocator) ParagraphComment(Content);
  41. }
  42. BlockCommandComment *Sema::actOnBlockCommandStart(
  43. SourceLocation LocBegin,
  44. SourceLocation LocEnd,
  45. unsigned CommandID,
  46. CommandMarkerKind CommandMarker) {
  47. BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd,
  48. CommandID,
  49. CommandMarker);
  50. checkContainerDecl(BC);
  51. return BC;
  52. }
  53. void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
  54. ArrayRef<BlockCommandComment::Argument> Args) {
  55. Command->setArgs(Args);
  56. }
  57. void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
  58. ParagraphComment *Paragraph) {
  59. Command->setParagraph(Paragraph);
  60. checkBlockCommandEmptyParagraph(Command);
  61. checkBlockCommandDuplicate(Command);
  62. if (ThisDeclInfo) {
  63. // These checks only make sense if the comment is attached to a
  64. // declaration.
  65. checkReturnsCommand(Command);
  66. checkDeprecatedCommand(Command);
  67. }
  68. }
  69. ParamCommandComment *Sema::actOnParamCommandStart(
  70. SourceLocation LocBegin,
  71. SourceLocation LocEnd,
  72. unsigned CommandID,
  73. CommandMarkerKind CommandMarker) {
  74. ParamCommandComment *Command =
  75. new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
  76. CommandMarker);
  77. if (!isFunctionDecl())
  78. Diag(Command->getLocation(),
  79. diag::warn_doc_param_not_attached_to_a_function_decl)
  80. << CommandMarker
  81. << Command->getCommandNameRange(Traits);
  82. return Command;
  83. }
  84. void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
  85. const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
  86. if (!Info->IsFunctionDeclarationCommand)
  87. return;
  88. unsigned DiagSelect;
  89. switch (Comment->getCommandID()) {
  90. case CommandTraits::KCI_function:
  91. DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0;
  92. break;
  93. case CommandTraits::KCI_functiongroup:
  94. DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0;
  95. break;
  96. case CommandTraits::KCI_method:
  97. DiagSelect = !isObjCMethodDecl() ? 3 : 0;
  98. break;
  99. case CommandTraits::KCI_methodgroup:
  100. DiagSelect = !isObjCMethodDecl() ? 4 : 0;
  101. break;
  102. case CommandTraits::KCI_callback:
  103. DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0;
  104. break;
  105. default:
  106. DiagSelect = 0;
  107. break;
  108. }
  109. if (DiagSelect)
  110. Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
  111. << Comment->getCommandMarker()
  112. << (DiagSelect-1) << (DiagSelect-1)
  113. << Comment->getSourceRange();
  114. }
  115. void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
  116. const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
  117. if (!Info->IsRecordLikeDeclarationCommand)
  118. return;
  119. unsigned DiagSelect;
  120. switch (Comment->getCommandID()) {
  121. case CommandTraits::KCI_class:
  122. DiagSelect = (!isClassOrStructDecl() && !isClassTemplateDecl()) ? 1 : 0;
  123. // Allow @class command on @interface declarations.
  124. // FIXME. Currently, \class and @class are indistinguishable. So,
  125. // \class is also allowed on an @interface declaration
  126. if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
  127. DiagSelect = 0;
  128. break;
  129. case CommandTraits::KCI_interface:
  130. DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
  131. break;
  132. case CommandTraits::KCI_protocol:
  133. DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
  134. break;
  135. case CommandTraits::KCI_struct:
  136. DiagSelect = !isClassOrStructDecl() ? 4 : 0;
  137. break;
  138. case CommandTraits::KCI_union:
  139. DiagSelect = !isUnionDecl() ? 5 : 0;
  140. break;
  141. default:
  142. DiagSelect = 0;
  143. break;
  144. }
  145. if (DiagSelect)
  146. Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
  147. << Comment->getCommandMarker()
  148. << (DiagSelect-1) << (DiagSelect-1)
  149. << Comment->getSourceRange();
  150. }
  151. void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
  152. const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
  153. if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
  154. return;
  155. unsigned DiagSelect;
  156. switch (Comment->getCommandID()) {
  157. case CommandTraits::KCI_classdesign:
  158. DiagSelect = 1;
  159. break;
  160. case CommandTraits::KCI_coclass:
  161. DiagSelect = 2;
  162. break;
  163. case CommandTraits::KCI_dependency:
  164. DiagSelect = 3;
  165. break;
  166. case CommandTraits::KCI_helper:
  167. DiagSelect = 4;
  168. break;
  169. case CommandTraits::KCI_helperclass:
  170. DiagSelect = 5;
  171. break;
  172. case CommandTraits::KCI_helps:
  173. DiagSelect = 6;
  174. break;
  175. case CommandTraits::KCI_instancesize:
  176. DiagSelect = 7;
  177. break;
  178. case CommandTraits::KCI_ownership:
  179. DiagSelect = 8;
  180. break;
  181. case CommandTraits::KCI_performance:
  182. DiagSelect = 9;
  183. break;
  184. case CommandTraits::KCI_security:
  185. DiagSelect = 10;
  186. break;
  187. case CommandTraits::KCI_superclass:
  188. DiagSelect = 11;
  189. break;
  190. default:
  191. DiagSelect = 0;
  192. break;
  193. }
  194. if (DiagSelect)
  195. Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
  196. << Comment->getCommandMarker()
  197. << (DiagSelect-1)
  198. << Comment->getSourceRange();
  199. }
  200. /// \brief Turn a string into the corresponding PassDirection or -1 if it's not
  201. /// valid.
  202. static int getParamPassDirection(StringRef Arg) {
  203. return llvm::StringSwitch<int>(Arg)
  204. .Case("[in]", ParamCommandComment::In)
  205. .Case("[out]", ParamCommandComment::Out)
  206. .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
  207. .Default(-1);
  208. }
  209. void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
  210. SourceLocation ArgLocBegin,
  211. SourceLocation ArgLocEnd,
  212. StringRef Arg) {
  213. std::string ArgLower = Arg.lower();
  214. int Direction = getParamPassDirection(ArgLower);
  215. if (Direction == -1) {
  216. // Try again with whitespace removed.
  217. ArgLower.erase(
  218. std::remove_if(ArgLower.begin(), ArgLower.end(), clang::isWhitespace),
  219. ArgLower.end());
  220. Direction = getParamPassDirection(ArgLower);
  221. SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
  222. if (Direction != -1) {
  223. const char *FixedName = ParamCommandComment::getDirectionAsString(
  224. (ParamCommandComment::PassDirection)Direction);
  225. Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
  226. << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
  227. } else {
  228. Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
  229. Direction = ParamCommandComment::In; // Sane fall back.
  230. }
  231. }
  232. Command->setDirection((ParamCommandComment::PassDirection)Direction,
  233. /*Explicit=*/true);
  234. }
  235. void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
  236. SourceLocation ArgLocBegin,
  237. SourceLocation ArgLocEnd,
  238. StringRef Arg) {
  239. // Parser will not feed us more arguments than needed.
  240. assert(Command->getNumArgs() == 0);
  241. if (!Command->isDirectionExplicit()) {
  242. // User didn't provide a direction argument.
  243. Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
  244. }
  245. typedef BlockCommandComment::Argument Argument;
  246. Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
  247. ArgLocEnd),
  248. Arg);
  249. Command->setArgs(llvm::makeArrayRef(A, 1));
  250. }
  251. void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
  252. ParagraphComment *Paragraph) {
  253. Command->setParagraph(Paragraph);
  254. checkBlockCommandEmptyParagraph(Command);
  255. }
  256. TParamCommandComment *Sema::actOnTParamCommandStart(
  257. SourceLocation LocBegin,
  258. SourceLocation LocEnd,
  259. unsigned CommandID,
  260. CommandMarkerKind CommandMarker) {
  261. TParamCommandComment *Command =
  262. new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
  263. CommandMarker);
  264. if (!isTemplateOrSpecialization())
  265. Diag(Command->getLocation(),
  266. diag::warn_doc_tparam_not_attached_to_a_template_decl)
  267. << CommandMarker
  268. << Command->getCommandNameRange(Traits);
  269. return Command;
  270. }
  271. void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
  272. SourceLocation ArgLocBegin,
  273. SourceLocation ArgLocEnd,
  274. StringRef Arg) {
  275. // Parser will not feed us more arguments than needed.
  276. assert(Command->getNumArgs() == 0);
  277. typedef BlockCommandComment::Argument Argument;
  278. Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
  279. ArgLocEnd),
  280. Arg);
  281. Command->setArgs(llvm::makeArrayRef(A, 1));
  282. if (!isTemplateOrSpecialization()) {
  283. // We already warned that this \\tparam is not attached to a template decl.
  284. return;
  285. }
  286. const TemplateParameterList *TemplateParameters =
  287. ThisDeclInfo->TemplateParameters;
  288. SmallVector<unsigned, 2> Position;
  289. if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
  290. Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
  291. TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg];
  292. if (PrevCommand) {
  293. SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
  294. Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
  295. << Arg << ArgRange;
  296. Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
  297. << PrevCommand->getParamNameRange();
  298. }
  299. PrevCommand = Command;
  300. return;
  301. }
  302. SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
  303. Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
  304. << Arg << ArgRange;
  305. if (!TemplateParameters || TemplateParameters->size() == 0)
  306. return;
  307. StringRef CorrectedName;
  308. if (TemplateParameters->size() == 1) {
  309. const NamedDecl *Param = TemplateParameters->getParam(0);
  310. const IdentifierInfo *II = Param->getIdentifier();
  311. if (II)
  312. CorrectedName = II->getName();
  313. } else {
  314. CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
  315. }
  316. if (!CorrectedName.empty()) {
  317. Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
  318. << CorrectedName
  319. << FixItHint::CreateReplacement(ArgRange, CorrectedName);
  320. }
  321. return;
  322. }
  323. void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
  324. ParagraphComment *Paragraph) {
  325. Command->setParagraph(Paragraph);
  326. checkBlockCommandEmptyParagraph(Command);
  327. }
  328. InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
  329. SourceLocation CommandLocEnd,
  330. unsigned CommandID) {
  331. ArrayRef<InlineCommandComment::Argument> Args;
  332. StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
  333. return new (Allocator) InlineCommandComment(
  334. CommandLocBegin,
  335. CommandLocEnd,
  336. CommandID,
  337. getInlineCommandRenderKind(CommandName),
  338. Args);
  339. }
  340. InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
  341. SourceLocation CommandLocEnd,
  342. unsigned CommandID,
  343. SourceLocation ArgLocBegin,
  344. SourceLocation ArgLocEnd,
  345. StringRef Arg) {
  346. typedef InlineCommandComment::Argument Argument;
  347. Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
  348. ArgLocEnd),
  349. Arg);
  350. StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
  351. return new (Allocator) InlineCommandComment(
  352. CommandLocBegin,
  353. CommandLocEnd,
  354. CommandID,
  355. getInlineCommandRenderKind(CommandName),
  356. llvm::makeArrayRef(A, 1));
  357. }
  358. InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
  359. SourceLocation LocEnd,
  360. StringRef CommandName) {
  361. unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
  362. return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
  363. }
  364. InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
  365. SourceLocation LocEnd,
  366. unsigned CommandID) {
  367. ArrayRef<InlineCommandComment::Argument> Args;
  368. return new (Allocator) InlineCommandComment(
  369. LocBegin, LocEnd, CommandID,
  370. InlineCommandComment::RenderNormal,
  371. Args);
  372. }
  373. TextComment *Sema::actOnText(SourceLocation LocBegin,
  374. SourceLocation LocEnd,
  375. StringRef Text) {
  376. return new (Allocator) TextComment(LocBegin, LocEnd, Text);
  377. }
  378. VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
  379. unsigned CommandID) {
  380. StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
  381. return new (Allocator) VerbatimBlockComment(
  382. Loc,
  383. Loc.getLocWithOffset(1 + CommandName.size()),
  384. CommandID);
  385. }
  386. VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
  387. StringRef Text) {
  388. return new (Allocator) VerbatimBlockLineComment(Loc, Text);
  389. }
  390. void Sema::actOnVerbatimBlockFinish(
  391. VerbatimBlockComment *Block,
  392. SourceLocation CloseNameLocBegin,
  393. StringRef CloseName,
  394. ArrayRef<VerbatimBlockLineComment *> Lines) {
  395. Block->setCloseName(CloseName, CloseNameLocBegin);
  396. Block->setLines(Lines);
  397. }
  398. VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
  399. unsigned CommandID,
  400. SourceLocation TextBegin,
  401. StringRef Text) {
  402. VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
  403. LocBegin,
  404. TextBegin.getLocWithOffset(Text.size()),
  405. CommandID,
  406. TextBegin,
  407. Text);
  408. checkFunctionDeclVerbatimLine(VL);
  409. checkContainerDeclVerbatimLine(VL);
  410. return VL;
  411. }
  412. HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
  413. StringRef TagName) {
  414. return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
  415. }
  416. void Sema::actOnHTMLStartTagFinish(
  417. HTMLStartTagComment *Tag,
  418. ArrayRef<HTMLStartTagComment::Attribute> Attrs,
  419. SourceLocation GreaterLoc,
  420. bool IsSelfClosing) {
  421. Tag->setAttrs(Attrs);
  422. Tag->setGreaterLoc(GreaterLoc);
  423. if (IsSelfClosing)
  424. Tag->setSelfClosing();
  425. else if (!isHTMLEndTagForbidden(Tag->getTagName()))
  426. HTMLOpenTags.push_back(Tag);
  427. }
  428. HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
  429. SourceLocation LocEnd,
  430. StringRef TagName) {
  431. HTMLEndTagComment *HET =
  432. new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
  433. if (isHTMLEndTagForbidden(TagName)) {
  434. Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
  435. << TagName << HET->getSourceRange();
  436. HET->setIsMalformed();
  437. return HET;
  438. }
  439. bool FoundOpen = false;
  440. for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
  441. I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
  442. I != E; ++I) {
  443. if ((*I)->getTagName() == TagName) {
  444. FoundOpen = true;
  445. break;
  446. }
  447. }
  448. if (!FoundOpen) {
  449. Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
  450. << HET->getSourceRange();
  451. HET->setIsMalformed();
  452. return HET;
  453. }
  454. while (!HTMLOpenTags.empty()) {
  455. HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
  456. StringRef LastNotClosedTagName = HST->getTagName();
  457. if (LastNotClosedTagName == TagName) {
  458. // If the start tag is malformed, end tag is malformed as well.
  459. if (HST->isMalformed())
  460. HET->setIsMalformed();
  461. break;
  462. }
  463. if (isHTMLEndTagOptional(LastNotClosedTagName))
  464. continue;
  465. bool OpenLineInvalid;
  466. const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
  467. HST->getLocation(),
  468. &OpenLineInvalid);
  469. bool CloseLineInvalid;
  470. const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
  471. HET->getLocation(),
  472. &CloseLineInvalid);
  473. if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) {
  474. Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
  475. << HST->getTagName() << HET->getTagName()
  476. << HST->getSourceRange() << HET->getSourceRange();
  477. HST->setIsMalformed();
  478. } else {
  479. Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
  480. << HST->getTagName() << HET->getTagName()
  481. << HST->getSourceRange();
  482. Diag(HET->getLocation(), diag::note_doc_html_end_tag)
  483. << HET->getSourceRange();
  484. HST->setIsMalformed();
  485. }
  486. }
  487. return HET;
  488. }
  489. FullComment *Sema::actOnFullComment(
  490. ArrayRef<BlockContentComment *> Blocks) {
  491. FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
  492. resolveParamCommandIndexes(FC);
  493. // Complain about HTML tags that are not closed.
  494. while (!HTMLOpenTags.empty()) {
  495. HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
  496. if (isHTMLEndTagOptional(HST->getTagName()))
  497. continue;
  498. Diag(HST->getLocation(), diag::warn_doc_html_missing_end_tag)
  499. << HST->getTagName() << HST->getSourceRange();
  500. HST->setIsMalformed();
  501. }
  502. return FC;
  503. }
  504. void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
  505. if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
  506. return;
  507. ParagraphComment *Paragraph = Command->getParagraph();
  508. if (Paragraph->isWhitespace()) {
  509. SourceLocation DiagLoc;
  510. if (Command->getNumArgs() > 0)
  511. DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
  512. if (!DiagLoc.isValid())
  513. DiagLoc = Command->getCommandNameRange(Traits).getEnd();
  514. Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
  515. << Command->getCommandMarker()
  516. << Command->getCommandName(Traits)
  517. << Command->getSourceRange();
  518. }
  519. }
  520. void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
  521. if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
  522. return;
  523. assert(ThisDeclInfo && "should not call this check on a bare comment");
  524. if (isFunctionDecl()) {
  525. if (ThisDeclInfo->ReturnType->isVoidType()) {
  526. unsigned DiagKind;
  527. switch (ThisDeclInfo->CommentDecl->getKind()) {
  528. default:
  529. if (ThisDeclInfo->IsObjCMethod)
  530. DiagKind = 3;
  531. else
  532. DiagKind = 0;
  533. break;
  534. case Decl::CXXConstructor:
  535. DiagKind = 1;
  536. break;
  537. case Decl::CXXDestructor:
  538. DiagKind = 2;
  539. break;
  540. }
  541. Diag(Command->getLocation(),
  542. diag::warn_doc_returns_attached_to_a_void_function)
  543. << Command->getCommandMarker()
  544. << Command->getCommandName(Traits)
  545. << DiagKind
  546. << Command->getSourceRange();
  547. }
  548. return;
  549. }
  550. else if (isObjCPropertyDecl())
  551. return;
  552. Diag(Command->getLocation(),
  553. diag::warn_doc_returns_not_attached_to_a_function_decl)
  554. << Command->getCommandMarker()
  555. << Command->getCommandName(Traits)
  556. << Command->getSourceRange();
  557. }
  558. void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
  559. const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
  560. const BlockCommandComment *PrevCommand = nullptr;
  561. if (Info->IsBriefCommand) {
  562. if (!BriefCommand) {
  563. BriefCommand = Command;
  564. return;
  565. }
  566. PrevCommand = BriefCommand;
  567. } else if (Info->IsHeaderfileCommand) {
  568. if (!HeaderfileCommand) {
  569. HeaderfileCommand = Command;
  570. return;
  571. }
  572. PrevCommand = HeaderfileCommand;
  573. } else {
  574. // We don't want to check this command for duplicates.
  575. return;
  576. }
  577. StringRef CommandName = Command->getCommandName(Traits);
  578. StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
  579. Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
  580. << Command->getCommandMarker()
  581. << CommandName
  582. << Command->getSourceRange();
  583. if (CommandName == PrevCommandName)
  584. Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
  585. << PrevCommand->getCommandMarker()
  586. << PrevCommandName
  587. << PrevCommand->getSourceRange();
  588. else
  589. Diag(PrevCommand->getLocation(),
  590. diag::note_doc_block_command_previous_alias)
  591. << PrevCommand->getCommandMarker()
  592. << PrevCommandName
  593. << CommandName;
  594. }
  595. void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
  596. if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
  597. return;
  598. assert(ThisDeclInfo && "should not call this check on a bare comment");
  599. const Decl *D = ThisDeclInfo->CommentDecl;
  600. if (!D)
  601. return;
  602. if (D->hasAttr<DeprecatedAttr>() ||
  603. D->hasAttr<AvailabilityAttr>() ||
  604. D->hasAttr<UnavailableAttr>())
  605. return;
  606. Diag(Command->getLocation(),
  607. diag::warn_doc_deprecated_not_sync)
  608. << Command->getSourceRange();
  609. // Try to emit a fixit with a deprecation attribute.
  610. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  611. // Don't emit a Fix-It for non-member function definitions. GCC does not
  612. // accept attributes on them.
  613. const DeclContext *Ctx = FD->getDeclContext();
  614. if ((!Ctx || !Ctx->isRecord()) &&
  615. FD->doesThisDeclarationHaveABody())
  616. return;
  617. StringRef AttributeSpelling = "__attribute__((deprecated))";
  618. if (PP) {
  619. TokenValue Tokens[] = {
  620. tok::kw___attribute, tok::l_paren, tok::l_paren,
  621. PP->getIdentifierInfo("deprecated"),
  622. tok::r_paren, tok::r_paren
  623. };
  624. StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
  625. Tokens);
  626. if (!MacroName.empty())
  627. AttributeSpelling = MacroName;
  628. }
  629. SmallString<64> TextToInsert(" ");
  630. TextToInsert += AttributeSpelling;
  631. Diag(FD->getLocEnd(),
  632. diag::note_add_deprecation_attr)
  633. << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
  634. TextToInsert);
  635. }
  636. }
  637. void Sema::resolveParamCommandIndexes(const FullComment *FC) {
  638. if (!isFunctionDecl()) {
  639. // We already warned that \\param commands are not attached to a function
  640. // decl.
  641. return;
  642. }
  643. SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
  644. // Comment AST nodes that correspond to \c ParamVars for which we have
  645. // found a \\param command or NULL if no documentation was found so far.
  646. SmallVector<ParamCommandComment *, 8> ParamVarDocs;
  647. ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
  648. ParamVarDocs.resize(ParamVars.size(), nullptr);
  649. // First pass over all \\param commands: resolve all parameter names.
  650. for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
  651. I != E; ++I) {
  652. ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
  653. if (!PCC || !PCC->hasParamName())
  654. continue;
  655. StringRef ParamName = PCC->getParamNameAsWritten();
  656. // Check that referenced parameter name is in the function decl.
  657. const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
  658. ParamVars);
  659. if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
  660. PCC->setIsVarArgParam();
  661. continue;
  662. }
  663. if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
  664. UnresolvedParamCommands.push_back(PCC);
  665. continue;
  666. }
  667. PCC->setParamIndex(ResolvedParamIndex);
  668. if (ParamVarDocs[ResolvedParamIndex]) {
  669. SourceRange ArgRange = PCC->getParamNameRange();
  670. Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
  671. << ParamName << ArgRange;
  672. ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
  673. Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
  674. << PrevCommand->getParamNameRange();
  675. }
  676. ParamVarDocs[ResolvedParamIndex] = PCC;
  677. }
  678. // Find parameter declarations that have no corresponding \\param.
  679. SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
  680. for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
  681. if (!ParamVarDocs[i])
  682. OrphanedParamDecls.push_back(ParamVars[i]);
  683. }
  684. // Second pass over unresolved \\param commands: do typo correction.
  685. // Suggest corrections from a set of parameter declarations that have no
  686. // corresponding \\param.
  687. for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
  688. const ParamCommandComment *PCC = UnresolvedParamCommands[i];
  689. SourceRange ArgRange = PCC->getParamNameRange();
  690. StringRef ParamName = PCC->getParamNameAsWritten();
  691. Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
  692. << ParamName << ArgRange;
  693. // All parameters documented -- can't suggest a correction.
  694. if (OrphanedParamDecls.size() == 0)
  695. continue;
  696. unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
  697. if (OrphanedParamDecls.size() == 1) {
  698. // If one parameter is not documented then that parameter is the only
  699. // possible suggestion.
  700. CorrectedParamIndex = 0;
  701. } else {
  702. // Do typo correction.
  703. CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
  704. OrphanedParamDecls);
  705. }
  706. if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
  707. const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
  708. if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
  709. Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
  710. << CorrectedII->getName()
  711. << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
  712. }
  713. }
  714. }
  715. bool Sema::isFunctionDecl() {
  716. if (!ThisDeclInfo)
  717. return false;
  718. if (!ThisDeclInfo->IsFilled)
  719. inspectThisDecl();
  720. return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
  721. }
  722. bool Sema::isAnyFunctionDecl() {
  723. return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
  724. isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
  725. }
  726. bool Sema::isFunctionOrMethodVariadic() {
  727. if (!isAnyFunctionDecl() && !isObjCMethodDecl() && !isFunctionTemplateDecl())
  728. return false;
  729. if (const FunctionDecl *FD =
  730. dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
  731. return FD->isVariadic();
  732. if (const FunctionTemplateDecl *FTD =
  733. dyn_cast<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl))
  734. return FTD->getTemplatedDecl()->isVariadic();
  735. if (const ObjCMethodDecl *MD =
  736. dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
  737. return MD->isVariadic();
  738. return false;
  739. }
  740. bool Sema::isObjCMethodDecl() {
  741. return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
  742. isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
  743. }
  744. bool Sema::isFunctionPointerVarDecl() {
  745. if (!ThisDeclInfo)
  746. return false;
  747. if (!ThisDeclInfo->IsFilled)
  748. inspectThisDecl();
  749. if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
  750. if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
  751. QualType QT = VD->getType();
  752. return QT->isFunctionPointerType();
  753. }
  754. }
  755. return false;
  756. }
  757. bool Sema::isObjCPropertyDecl() {
  758. if (!ThisDeclInfo)
  759. return false;
  760. if (!ThisDeclInfo->IsFilled)
  761. inspectThisDecl();
  762. return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
  763. }
  764. bool Sema::isTemplateOrSpecialization() {
  765. if (!ThisDeclInfo)
  766. return false;
  767. if (!ThisDeclInfo->IsFilled)
  768. inspectThisDecl();
  769. return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
  770. }
  771. bool Sema::isRecordLikeDecl() {
  772. if (!ThisDeclInfo)
  773. return false;
  774. if (!ThisDeclInfo->IsFilled)
  775. inspectThisDecl();
  776. return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
  777. isObjCProtocolDecl();
  778. }
  779. bool Sema::isUnionDecl() {
  780. if (!ThisDeclInfo)
  781. return false;
  782. if (!ThisDeclInfo->IsFilled)
  783. inspectThisDecl();
  784. if (const RecordDecl *RD =
  785. dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
  786. return RD->isUnion();
  787. return false;
  788. }
  789. bool Sema::isClassOrStructDecl() {
  790. if (!ThisDeclInfo)
  791. return false;
  792. if (!ThisDeclInfo->IsFilled)
  793. inspectThisDecl();
  794. return ThisDeclInfo->CurrentDecl &&
  795. isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
  796. !isUnionDecl();
  797. }
  798. bool Sema::isClassTemplateDecl() {
  799. if (!ThisDeclInfo)
  800. return false;
  801. if (!ThisDeclInfo->IsFilled)
  802. inspectThisDecl();
  803. return ThisDeclInfo->CurrentDecl &&
  804. (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
  805. }
  806. bool Sema::isFunctionTemplateDecl() {
  807. if (!ThisDeclInfo)
  808. return false;
  809. if (!ThisDeclInfo->IsFilled)
  810. inspectThisDecl();
  811. return ThisDeclInfo->CurrentDecl &&
  812. (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
  813. }
  814. bool Sema::isObjCInterfaceDecl() {
  815. if (!ThisDeclInfo)
  816. return false;
  817. if (!ThisDeclInfo->IsFilled)
  818. inspectThisDecl();
  819. return ThisDeclInfo->CurrentDecl &&
  820. isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
  821. }
  822. bool Sema::isObjCProtocolDecl() {
  823. if (!ThisDeclInfo)
  824. return false;
  825. if (!ThisDeclInfo->IsFilled)
  826. inspectThisDecl();
  827. return ThisDeclInfo->CurrentDecl &&
  828. isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
  829. }
  830. ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
  831. if (!ThisDeclInfo->IsFilled)
  832. inspectThisDecl();
  833. return ThisDeclInfo->ParamVars;
  834. }
  835. void Sema::inspectThisDecl() {
  836. ThisDeclInfo->fill();
  837. }
  838. unsigned Sema::resolveParmVarReference(StringRef Name,
  839. ArrayRef<const ParmVarDecl *> ParamVars) {
  840. for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
  841. const IdentifierInfo *II = ParamVars[i]->getIdentifier();
  842. if (II && II->getName() == Name)
  843. return i;
  844. }
  845. if (Name == "..." && isFunctionOrMethodVariadic())
  846. return ParamCommandComment::VarArgParamIndex;
  847. return ParamCommandComment::InvalidParamIndex;
  848. }
  849. namespace {
  850. class SimpleTypoCorrector {
  851. StringRef Typo;
  852. const unsigned MaxEditDistance;
  853. const NamedDecl *BestDecl;
  854. unsigned BestEditDistance;
  855. unsigned BestIndex;
  856. unsigned NextIndex;
  857. public:
  858. SimpleTypoCorrector(StringRef Typo) :
  859. Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
  860. BestDecl(nullptr), BestEditDistance(MaxEditDistance + 1),
  861. BestIndex(0), NextIndex(0)
  862. { }
  863. void addDecl(const NamedDecl *ND);
  864. const NamedDecl *getBestDecl() const {
  865. if (BestEditDistance > MaxEditDistance)
  866. return nullptr;
  867. return BestDecl;
  868. }
  869. unsigned getBestDeclIndex() const {
  870. assert(getBestDecl());
  871. return BestIndex;
  872. }
  873. };
  874. void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
  875. unsigned CurrIndex = NextIndex++;
  876. const IdentifierInfo *II = ND->getIdentifier();
  877. if (!II)
  878. return;
  879. StringRef Name = II->getName();
  880. unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
  881. if (MinPossibleEditDistance > 0 &&
  882. Typo.size() / MinPossibleEditDistance < 3)
  883. return;
  884. unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
  885. if (EditDistance < BestEditDistance) {
  886. BestEditDistance = EditDistance;
  887. BestDecl = ND;
  888. BestIndex = CurrIndex;
  889. }
  890. }
  891. } // unnamed namespace
  892. unsigned Sema::correctTypoInParmVarReference(
  893. StringRef Typo,
  894. ArrayRef<const ParmVarDecl *> ParamVars) {
  895. SimpleTypoCorrector Corrector(Typo);
  896. for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
  897. Corrector.addDecl(ParamVars[i]);
  898. if (Corrector.getBestDecl())
  899. return Corrector.getBestDeclIndex();
  900. else
  901. return ParamCommandComment::InvalidParamIndex;
  902. }
  903. namespace {
  904. bool ResolveTParamReferenceHelper(
  905. StringRef Name,
  906. const TemplateParameterList *TemplateParameters,
  907. SmallVectorImpl<unsigned> *Position) {
  908. for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
  909. const NamedDecl *Param = TemplateParameters->getParam(i);
  910. const IdentifierInfo *II = Param->getIdentifier();
  911. if (II && II->getName() == Name) {
  912. Position->push_back(i);
  913. return true;
  914. }
  915. if (const TemplateTemplateParmDecl *TTP =
  916. dyn_cast<TemplateTemplateParmDecl>(Param)) {
  917. Position->push_back(i);
  918. if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
  919. Position))
  920. return true;
  921. Position->pop_back();
  922. }
  923. }
  924. return false;
  925. }
  926. } // unnamed namespace
  927. bool Sema::resolveTParamReference(
  928. StringRef Name,
  929. const TemplateParameterList *TemplateParameters,
  930. SmallVectorImpl<unsigned> *Position) {
  931. Position->clear();
  932. if (!TemplateParameters)
  933. return false;
  934. return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
  935. }
  936. namespace {
  937. void CorrectTypoInTParamReferenceHelper(
  938. const TemplateParameterList *TemplateParameters,
  939. SimpleTypoCorrector &Corrector) {
  940. for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
  941. const NamedDecl *Param = TemplateParameters->getParam(i);
  942. Corrector.addDecl(Param);
  943. if (const TemplateTemplateParmDecl *TTP =
  944. dyn_cast<TemplateTemplateParmDecl>(Param))
  945. CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
  946. Corrector);
  947. }
  948. }
  949. } // unnamed namespace
  950. StringRef Sema::correctTypoInTParamReference(
  951. StringRef Typo,
  952. const TemplateParameterList *TemplateParameters) {
  953. SimpleTypoCorrector Corrector(Typo);
  954. CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
  955. if (const NamedDecl *ND = Corrector.getBestDecl()) {
  956. const IdentifierInfo *II = ND->getIdentifier();
  957. assert(II && "SimpleTypoCorrector should not return this decl");
  958. return II->getName();
  959. }
  960. return StringRef();
  961. }
  962. InlineCommandComment::RenderKind
  963. Sema::getInlineCommandRenderKind(StringRef Name) const {
  964. assert(Traits.getCommandInfo(Name)->IsInlineCommand);
  965. return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
  966. .Case("b", InlineCommandComment::RenderBold)
  967. .Cases("c", "p", InlineCommandComment::RenderMonospaced)
  968. .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
  969. .Default(InlineCommandComment::RenderNormal);
  970. }
  971. } // end namespace comments
  972. } // end namespace clang