CommentSema.cpp 37 KB

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