blob: ffb6e8678845aa0bdc141a86e5b3510c24be62cd [file] [log] [blame]
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001//===--- 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
10#include "clang/AST/CommentSema.h"
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000011#include "clang/AST/CommentDiagnostic.h"
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000012#include "clang/AST/CommentCommandTraits.h"
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000013#include "clang/AST/Decl.h"
Dmitri Gribenko96b09862012-07-31 22:37:06 +000014#include "clang/AST/DeclTemplate.h"
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000015#include "clang/Basic/SourceManager.h"
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000016#include "llvm/ADT/StringSwitch.h"
17
18namespace clang {
19namespace comments {
20
Dmitri Gribenkoc24a76e2012-08-31 02:21:44 +000021namespace {
22#include "clang/AST/CommentHTMLTagsProperties.inc"
23} // unnamed namespace
24
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000025Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000026 DiagnosticsEngine &Diags, const CommandTraits &Traits) :
27 Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits),
Dmitri Gribenko9443c572012-08-06 17:08:27 +000028 ThisDeclInfo(NULL), BriefCommand(NULL), ReturnsCommand(NULL) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000029}
30
31void Sema::setDecl(const Decl *D) {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +000032 if (!D)
33 return;
34
35 ThisDeclInfo = new (Allocator) DeclInfo;
36 ThisDeclInfo->ThisDecl = D;
Dmitri Gribenko651f8ce2012-08-01 23:21:57 +000037 ThisDeclInfo->IsFilled = false;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000038}
39
40ParagraphComment *Sema::actOnParagraphComment(
41 ArrayRef<InlineContentComment *> Content) {
42 return new (Allocator) ParagraphComment(Content);
43}
44
45BlockCommandComment *Sema::actOnBlockCommandStart(SourceLocation LocBegin,
46 SourceLocation LocEnd,
47 StringRef Name) {
48 return new (Allocator) BlockCommandComment(LocBegin, LocEnd, Name);
49}
50
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +000051void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
52 ArrayRef<BlockCommandComment::Argument> Args) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000053 Command->setArgs(Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000054}
55
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +000056void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
57 ParagraphComment *Paragraph) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000058 Command->setParagraph(Paragraph);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000059 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko9443c572012-08-06 17:08:27 +000060 checkBlockCommandDuplicate(Command);
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +000061 checkReturnsCommand(Command);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000062}
63
64ParamCommandComment *Sema::actOnParamCommandStart(SourceLocation LocBegin,
65 SourceLocation LocEnd,
66 StringRef Name) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000067 ParamCommandComment *Command =
68 new (Allocator) ParamCommandComment(LocBegin, LocEnd, Name);
69
Dmitri Gribenko8487c522012-07-23 17:40:30 +000070 if (!isFunctionDecl())
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000071 Diag(Command->getLocation(),
72 diag::warn_doc_param_not_attached_to_a_function_decl)
73 << Command->getCommandNameRange();
74
75 return Command;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000076}
77
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +000078void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
79 SourceLocation ArgLocBegin,
80 SourceLocation ArgLocEnd,
81 StringRef Arg) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000082 ParamCommandComment::PassDirection Direction;
83 std::string ArgLower = Arg.lower();
84 // TODO: optimize: lower Name first (need an API in SmallString for that),
85 // after that StringSwitch.
86 if (ArgLower == "[in]")
87 Direction = ParamCommandComment::In;
88 else if (ArgLower == "[out]")
89 Direction = ParamCommandComment::Out;
90 else if (ArgLower == "[in,out]" || ArgLower == "[out,in]")
91 Direction = ParamCommandComment::InOut;
92 else {
93 // Remove spaces.
94 std::string::iterator O = ArgLower.begin();
95 for (std::string::iterator I = ArgLower.begin(), E = ArgLower.end();
96 I != E; ++I) {
97 const char C = *I;
98 if (C != ' ' && C != '\n' && C != '\r' &&
99 C != '\t' && C != '\v' && C != '\f')
100 *O++ = C;
101 }
102 ArgLower.resize(O - ArgLower.begin());
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000103
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000104 bool RemovingWhitespaceHelped = false;
105 if (ArgLower == "[in]") {
106 Direction = ParamCommandComment::In;
107 RemovingWhitespaceHelped = true;
108 } else if (ArgLower == "[out]") {
109 Direction = ParamCommandComment::Out;
110 RemovingWhitespaceHelped = true;
111 } else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") {
112 Direction = ParamCommandComment::InOut;
113 RemovingWhitespaceHelped = true;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000114 } else {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000115 Direction = ParamCommandComment::In;
116 RemovingWhitespaceHelped = false;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000117 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000118
119 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
120 if (RemovingWhitespaceHelped)
121 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
122 << ArgRange
123 << FixItHint::CreateReplacement(
124 ArgRange,
125 ParamCommandComment::getDirectionAsString(Direction));
126 else
127 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction)
128 << ArgRange;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000129 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000130 Command->setDirection(Direction, /* Explicit = */ true);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000131}
132
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000133void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
134 SourceLocation ArgLocBegin,
135 SourceLocation ArgLocEnd,
136 StringRef Arg) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000137 // Parser will not feed us more arguments than needed.
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000138 assert(Command->getNumArgs() == 0);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000139
140 if (!Command->isDirectionExplicit()) {
141 // User didn't provide a direction argument.
142 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
143 }
144 typedef BlockCommandComment::Argument Argument;
145 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
146 ArgLocEnd),
147 Arg);
148 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000149}
150
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000151void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
152 ParagraphComment *Paragraph) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000153 Command->setParagraph(Paragraph);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000154 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000155}
156
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000157TParamCommandComment *Sema::actOnTParamCommandStart(SourceLocation LocBegin,
158 SourceLocation LocEnd,
159 StringRef Name) {
160 TParamCommandComment *Command =
161 new (Allocator) TParamCommandComment(LocBegin, LocEnd, Name);
162
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000163 if (!isTemplateOrSpecialization())
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000164 Diag(Command->getLocation(),
165 diag::warn_doc_tparam_not_attached_to_a_template_decl)
166 << Command->getCommandNameRange();
167
168 return Command;
169}
170
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000171void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
172 SourceLocation ArgLocBegin,
173 SourceLocation ArgLocEnd,
174 StringRef Arg) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000175 // Parser will not feed us more arguments than needed.
176 assert(Command->getNumArgs() == 0);
177
178 typedef BlockCommandComment::Argument Argument;
179 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
180 ArgLocEnd),
181 Arg);
182 Command->setArgs(llvm::makeArrayRef(A, 1));
183
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000184 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000185 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000186 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000187 }
188
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000189 const TemplateParameterList *TemplateParameters =
190 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000191 SmallVector<unsigned, 2> Position;
192 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
193 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
194 llvm::StringMap<TParamCommandComment *>::iterator PrevCommandIt =
195 TemplateParameterDocs.find(Arg);
196 if (PrevCommandIt != TemplateParameterDocs.end()) {
197 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
198 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
199 << Arg << ArgRange;
200 TParamCommandComment *PrevCommand = PrevCommandIt->second;
201 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
202 << PrevCommand->getParamNameRange();
203 }
204 TemplateParameterDocs[Arg] = Command;
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000205 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000206 }
207
208 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
209 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
210 << Arg << ArgRange;
211
212 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000213 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000214
215 StringRef CorrectedName;
216 if (TemplateParameters->size() == 1) {
217 const NamedDecl *Param = TemplateParameters->getParam(0);
218 const IdentifierInfo *II = Param->getIdentifier();
219 if (II)
220 CorrectedName = II->getName();
221 } else {
222 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
223 }
224
225 if (!CorrectedName.empty()) {
226 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
227 << CorrectedName
228 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
229 }
230
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000231 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000232}
233
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000234void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
235 ParagraphComment *Paragraph) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000236 Command->setParagraph(Paragraph);
237 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000238}
239
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000240InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
241 SourceLocation CommandLocEnd,
242 StringRef CommandName) {
243 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000244 return new (Allocator) InlineCommandComment(
245 CommandLocBegin,
246 CommandLocEnd,
247 CommandName,
248 getInlineCommandRenderKind(CommandName),
249 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000250}
251
252InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
253 SourceLocation CommandLocEnd,
254 StringRef CommandName,
255 SourceLocation ArgLocBegin,
256 SourceLocation ArgLocEnd,
257 StringRef Arg) {
258 typedef InlineCommandComment::Argument Argument;
259 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
260 ArgLocEnd),
261 Arg);
262
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000263 return new (Allocator) InlineCommandComment(
264 CommandLocBegin,
265 CommandLocEnd,
266 CommandName,
267 getInlineCommandRenderKind(CommandName),
268 llvm::makeArrayRef(A, 1));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000269}
270
271InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
272 SourceLocation LocEnd,
273 StringRef Name) {
274 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000275 return new (Allocator) InlineCommandComment(
276 LocBegin, LocEnd, Name,
277 InlineCommandComment::RenderNormal,
278 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000279}
280
281TextComment *Sema::actOnText(SourceLocation LocBegin,
282 SourceLocation LocEnd,
283 StringRef Text) {
284 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
285}
286
287VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
288 StringRef Name) {
289 return new (Allocator) VerbatimBlockComment(
290 Loc,
291 Loc.getLocWithOffset(1 + Name.size()),
292 Name);
293}
294
295VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
296 StringRef Text) {
297 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
298}
299
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000300void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000301 VerbatimBlockComment *Block,
302 SourceLocation CloseNameLocBegin,
303 StringRef CloseName,
304 ArrayRef<VerbatimBlockLineComment *> Lines) {
305 Block->setCloseName(CloseName, CloseNameLocBegin);
306 Block->setLines(Lines);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000307}
308
309VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
310 StringRef Name,
311 SourceLocation TextBegin,
312 StringRef Text) {
313 return new (Allocator) VerbatimLineComment(
314 LocBegin,
315 TextBegin.getLocWithOffset(Text.size()),
316 Name,
317 TextBegin,
318 Text);
319}
320
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000321HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
322 StringRef TagName) {
323 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000324}
325
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000326void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000327 HTMLStartTagComment *Tag,
328 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000329 SourceLocation GreaterLoc,
330 bool IsSelfClosing) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000331 Tag->setAttrs(Attrs);
332 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000333 if (IsSelfClosing)
334 Tag->setSelfClosing();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000335 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000336 HTMLOpenTags.push_back(Tag);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000337}
338
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000339HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
340 SourceLocation LocEnd,
341 StringRef TagName) {
342 HTMLEndTagComment *HET =
343 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
344 if (isHTMLEndTagForbidden(TagName)) {
345 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
346 << TagName << HET->getSourceRange();
347 return HET;
Dmitri Gribenko3d986982012-07-12 23:37:09 +0000348 }
349
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000350 bool FoundOpen = false;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000351 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000352 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
353 I != E; ++I) {
354 if ((*I)->getTagName() == TagName) {
355 FoundOpen = true;
356 break;
357 }
358 }
359 if (!FoundOpen) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000360 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
361 << HET->getSourceRange();
362 return HET;
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000363 }
364
365 while (!HTMLOpenTags.empty()) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000366 const HTMLStartTagComment *HST = HTMLOpenTags.back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000367 HTMLOpenTags.pop_back();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000368 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000369 if (LastNotClosedTagName == TagName)
370 break;
371
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000372 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000373 continue;
374
375 bool OpenLineInvalid;
376 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000377 HST->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000378 &OpenLineInvalid);
379 bool CloseLineInvalid;
380 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000381 HET->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000382 &CloseLineInvalid);
383
384 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000385 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
386 << HST->getTagName() << HET->getTagName()
387 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000388 else {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000389 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
390 << HST->getTagName() << HET->getTagName()
391 << HST->getSourceRange();
392 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
393 << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000394 }
395 }
396
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000397 return HET;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000398}
399
400FullComment *Sema::actOnFullComment(
401 ArrayRef<BlockContentComment *> Blocks) {
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000402 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
403 resolveParamCommandIndexes(FC);
404 return FC;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000405}
406
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000407void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
408 ParagraphComment *Paragraph = Command->getParagraph();
409 if (Paragraph->isWhitespace()) {
410 SourceLocation DiagLoc;
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000411 if (Command->getNumArgs() > 0)
412 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000413 if (!DiagLoc.isValid())
414 DiagLoc = Command->getCommandNameRange().getEnd();
415 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
416 << Command->getCommandName()
417 << Command->getSourceRange();
418 }
419}
420
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000421void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000422 if (!Traits.isReturnsCommand(Command->getCommandName()))
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000423 return;
424 if (isFunctionDecl()) {
425 if (ThisDeclInfo->ResultType->isVoidType()) {
426 unsigned DiagKind;
427 switch (ThisDeclInfo->ThisDecl->getKind()) {
428 default:
Dmitri Gribenko88815f32012-08-06 16:29:26 +0000429 if (ThisDeclInfo->IsObjCMethod)
430 DiagKind = 3;
431 else
432 DiagKind = 0;
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000433 break;
434 case Decl::CXXConstructor:
435 DiagKind = 1;
436 break;
437 case Decl::CXXDestructor:
438 DiagKind = 2;
439 break;
440 }
441 Diag(Command->getLocation(),
442 diag::warn_doc_returns_attached_to_a_void_function)
443 << Command->getCommandName()
444 << DiagKind
445 << Command->getSourceRange();
446 }
447 return;
448 }
449 Diag(Command->getLocation(),
450 diag::warn_doc_returns_not_attached_to_a_function_decl)
451 << Command->getCommandName()
452 << Command->getSourceRange();
453}
454
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000455void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
456 StringRef Name = Command->getCommandName();
457 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000458 if (Traits.isBriefCommand(Name)) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000459 if (!BriefCommand) {
460 BriefCommand = Command;
461 return;
462 }
463 PrevCommand = BriefCommand;
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000464 } else if (Traits.isReturnsCommand(Name)) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000465 if (!ReturnsCommand) {
466 ReturnsCommand = Command;
467 return;
468 }
469 PrevCommand = ReturnsCommand;
470 } else {
471 // We don't want to check this command for duplicates.
472 return;
473 }
474 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
475 << Name
476 << Command->getSourceRange();
477 if (Name == PrevCommand->getCommandName())
478 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
479 << PrevCommand->getCommandName()
480 << Command->getSourceRange();
481 else
482 Diag(PrevCommand->getLocation(),
483 diag::note_doc_block_command_previous_alias)
484 << PrevCommand->getCommandName()
485 << Name;
486}
487
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000488void Sema::resolveParamCommandIndexes(const FullComment *FC) {
489 if (!isFunctionDecl()) {
490 // We already warned that \\param commands are not attached to a function
491 // decl.
492 return;
493 }
494
495 llvm::SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
496
497 // Comment AST nodes that correspond to \c ParamVars for which we have
498 // found a \\param command or NULL if no documentation was found so far.
499 llvm::SmallVector<ParamCommandComment *, 8> ParamVarDocs;
500
501 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
502 ParamVarDocs.resize(ParamVars.size(), NULL);
503
504 // First pass over all \\param commands: resolve all parameter names.
505 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
506 I != E; ++I) {
507 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
508 if (!PCC || !PCC->hasParamName())
509 continue;
510 StringRef ParamName = PCC->getParamName();
511
512 // Check that referenced parameter name is in the function decl.
513 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
514 ParamVars);
515 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
516 UnresolvedParamCommands.push_back(PCC);
517 continue;
518 }
519 PCC->setParamIndex(ResolvedParamIndex);
520 if (ParamVarDocs[ResolvedParamIndex]) {
521 SourceRange ArgRange = PCC->getParamNameRange();
522 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
523 << ParamName << ArgRange;
524 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
525 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
526 << PrevCommand->getParamNameRange();
527 }
528 ParamVarDocs[ResolvedParamIndex] = PCC;
529 }
530
531 // Find parameter declarations that have no corresponding \\param.
532 llvm::SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
533 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
534 if (!ParamVarDocs[i])
535 OrphanedParamDecls.push_back(ParamVars[i]);
536 }
537
538 // Second pass over unresolved \\param commands: do typo correction.
539 // Suggest corrections from a set of parameter declarations that have no
540 // corresponding \\param.
541 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
542 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
543
544 SourceRange ArgRange = PCC->getParamNameRange();
545 StringRef ParamName = PCC->getParamName();
546 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
547 << ParamName << ArgRange;
548
549 // All parameters documented -- can't suggest a correction.
550 if (OrphanedParamDecls.size() == 0)
551 continue;
552
553 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
554 if (OrphanedParamDecls.size() == 1) {
555 // If one parameter is not documented then that parameter is the only
556 // possible suggestion.
557 CorrectedParamIndex = 0;
558 } else {
559 // Do typo correction.
560 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
561 OrphanedParamDecls);
562 }
563 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
564 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
565 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
566 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
567 << CorrectedII->getName()
568 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
569 }
570 }
571}
572
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000573bool Sema::isFunctionDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000574 if (!ThisDeclInfo)
575 return false;
576 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000577 inspectThisDecl();
Dmitri Gribenkoaf19a6a2012-08-02 21:45:39 +0000578 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000579}
580
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000581bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000582 if (!ThisDeclInfo)
583 return false;
584 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000585 inspectThisDecl();
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000586 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000587}
588
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000589ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000590 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000591 inspectThisDecl();
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000592 return ThisDeclInfo->ParamVars;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000593}
594
595void Sema::inspectThisDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000596 ThisDeclInfo->fill();
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000597}
598
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000599unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000600 ArrayRef<const ParmVarDecl *> ParamVars) {
601 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000602 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
603 if (II && II->getName() == Name)
604 return i;
605 }
606 return ParamCommandComment::InvalidParamIndex;
607}
608
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000609namespace {
610class SimpleTypoCorrector {
611 StringRef Typo;
612 const unsigned MaxEditDistance;
613
614 const NamedDecl *BestDecl;
615 unsigned BestEditDistance;
616 unsigned BestIndex;
617 unsigned NextIndex;
618
619public:
620 SimpleTypoCorrector(StringRef Typo) :
621 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
622 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
623 BestIndex(0), NextIndex(0)
624 { }
625
626 void addDecl(const NamedDecl *ND);
627
628 const NamedDecl *getBestDecl() const {
629 if (BestEditDistance > MaxEditDistance)
630 return NULL;
631
632 return BestDecl;
633 }
634
635 unsigned getBestDeclIndex() const {
636 assert(getBestDecl());
637 return BestIndex;
638 }
639};
640
641void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
642 unsigned CurrIndex = NextIndex++;
643
644 const IdentifierInfo *II = ND->getIdentifier();
645 if (!II)
646 return;
647
648 StringRef Name = II->getName();
649 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
650 if (MinPossibleEditDistance > 0 &&
651 Typo.size() / MinPossibleEditDistance < 3)
652 return;
653
654 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
655 if (EditDistance < BestEditDistance) {
656 BestEditDistance = EditDistance;
657 BestDecl = ND;
658 BestIndex = CurrIndex;
659 }
660}
661} // unnamed namespace
662
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000663unsigned Sema::correctTypoInParmVarReference(
664 StringRef Typo,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000665 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000666 SimpleTypoCorrector Corrector(Typo);
667 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
668 Corrector.addDecl(ParamVars[i]);
669 if (Corrector.getBestDecl())
670 return Corrector.getBestDeclIndex();
671 else
672 return ParamCommandComment::InvalidParamIndex;;
673}
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000674
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000675namespace {
676bool ResolveTParamReferenceHelper(
677 StringRef Name,
678 const TemplateParameterList *TemplateParameters,
679 SmallVectorImpl<unsigned> *Position) {
680 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
681 const NamedDecl *Param = TemplateParameters->getParam(i);
682 const IdentifierInfo *II = Param->getIdentifier();
683 if (II && II->getName() == Name) {
684 Position->push_back(i);
685 return true;
686 }
687
688 if (const TemplateTemplateParmDecl *TTP =
689 dyn_cast<TemplateTemplateParmDecl>(Param)) {
690 Position->push_back(i);
691 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
692 Position))
693 return true;
694 Position->pop_back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000695 }
696 }
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000697 return false;
698}
699} // unnamed namespace
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000700
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000701bool Sema::resolveTParamReference(
702 StringRef Name,
703 const TemplateParameterList *TemplateParameters,
704 SmallVectorImpl<unsigned> *Position) {
705 Position->clear();
706 if (!TemplateParameters)
707 return false;
708
709 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
710}
711
712namespace {
713void CorrectTypoInTParamReferenceHelper(
714 const TemplateParameterList *TemplateParameters,
715 SimpleTypoCorrector &Corrector) {
716 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
717 const NamedDecl *Param = TemplateParameters->getParam(i);
718 Corrector.addDecl(Param);
719
720 if (const TemplateTemplateParmDecl *TTP =
721 dyn_cast<TemplateTemplateParmDecl>(Param))
722 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
723 Corrector);
724 }
725}
726} // unnamed namespace
727
728StringRef Sema::correctTypoInTParamReference(
729 StringRef Typo,
730 const TemplateParameterList *TemplateParameters) {
731 SimpleTypoCorrector Corrector(Typo);
732 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
733 if (const NamedDecl *ND = Corrector.getBestDecl()) {
734 const IdentifierInfo *II = ND->getIdentifier();
735 assert(II && "SimpleTypoCorrector should not return this decl");
736 return II->getName();
737 }
738 return StringRef();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000739}
740
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000741InlineCommandComment::RenderKind
742Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000743 assert(Traits.isInlineCommand(Name));
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000744
745 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
746 .Case("b", InlineCommandComment::RenderBold)
747 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
748 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
749 .Default(InlineCommandComment::RenderNormal);
750}
751
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000752} // end namespace comments
753} // end namespace clang
754