blob: 477717f735ef4475cecd1c716b0687df95df9342 [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 Gribenkoe4330a32012-09-10 20:32:42 +000026 DiagnosticsEngine &Diags, CommandTraits &Traits) :
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000027 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,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000047 unsigned CommandID) {
48 return new (Allocator) BlockCommandComment(LocBegin, LocEnd, CommandID);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000049}
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,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000066 unsigned CommandID) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000067 ParamCommandComment *Command =
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000068 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000069
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)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000073 << Command->getCommandNameRange(Traits);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000074
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,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000159 unsigned CommandID) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000160 TParamCommandComment *Command =
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000161 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000162
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)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000166 << Command->getCommandNameRange(Traits);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000167
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,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000242 unsigned CommandID) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000243 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000244 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000245 return new (Allocator) InlineCommandComment(
246 CommandLocBegin,
247 CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000248 CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000249 getInlineCommandRenderKind(CommandName),
250 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000251}
252
253InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
254 SourceLocation CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000255 unsigned CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000256 SourceLocation ArgLocBegin,
257 SourceLocation ArgLocEnd,
258 StringRef Arg) {
259 typedef InlineCommandComment::Argument Argument;
260 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
261 ArgLocEnd),
262 Arg);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000263 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000264
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000265 return new (Allocator) InlineCommandComment(
266 CommandLocBegin,
267 CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000268 CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000269 getInlineCommandRenderKind(CommandName),
270 llvm::makeArrayRef(A, 1));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000271}
272
273InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
274 SourceLocation LocEnd,
Dmitri Gribenkob0b8a962012-09-11 19:22:03 +0000275 StringRef CommandName) {
276 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
277 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
278}
279
280InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
281 SourceLocation LocEnd,
282 unsigned CommandID) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000283 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000284 return new (Allocator) InlineCommandComment(
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000285 LocBegin, LocEnd, CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000286 InlineCommandComment::RenderNormal,
287 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000288}
289
290TextComment *Sema::actOnText(SourceLocation LocBegin,
291 SourceLocation LocEnd,
292 StringRef Text) {
293 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
294}
295
296VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000297 unsigned CommandID) {
298 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000299 return new (Allocator) VerbatimBlockComment(
300 Loc,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000301 Loc.getLocWithOffset(1 + CommandName.size()),
302 CommandID);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000303}
304
305VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
306 StringRef Text) {
307 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
308}
309
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000310void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000311 VerbatimBlockComment *Block,
312 SourceLocation CloseNameLocBegin,
313 StringRef CloseName,
314 ArrayRef<VerbatimBlockLineComment *> Lines) {
315 Block->setCloseName(CloseName, CloseNameLocBegin);
316 Block->setLines(Lines);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000317}
318
319VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000320 unsigned CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000321 SourceLocation TextBegin,
322 StringRef Text) {
323 return new (Allocator) VerbatimLineComment(
324 LocBegin,
325 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000326 CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000327 TextBegin,
328 Text);
329}
330
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000331HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
332 StringRef TagName) {
333 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000334}
335
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000336void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000337 HTMLStartTagComment *Tag,
338 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000339 SourceLocation GreaterLoc,
340 bool IsSelfClosing) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000341 Tag->setAttrs(Attrs);
342 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000343 if (IsSelfClosing)
344 Tag->setSelfClosing();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000345 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000346 HTMLOpenTags.push_back(Tag);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000347}
348
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000349HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
350 SourceLocation LocEnd,
351 StringRef TagName) {
352 HTMLEndTagComment *HET =
353 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
354 if (isHTMLEndTagForbidden(TagName)) {
355 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
356 << TagName << HET->getSourceRange();
357 return HET;
Dmitri Gribenko3d986982012-07-12 23:37:09 +0000358 }
359
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000360 bool FoundOpen = false;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000361 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000362 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
363 I != E; ++I) {
364 if ((*I)->getTagName() == TagName) {
365 FoundOpen = true;
366 break;
367 }
368 }
369 if (!FoundOpen) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000370 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
371 << HET->getSourceRange();
372 return HET;
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000373 }
374
375 while (!HTMLOpenTags.empty()) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000376 const HTMLStartTagComment *HST = HTMLOpenTags.back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000377 HTMLOpenTags.pop_back();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000378 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000379 if (LastNotClosedTagName == TagName)
380 break;
381
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000382 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000383 continue;
384
385 bool OpenLineInvalid;
386 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000387 HST->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000388 &OpenLineInvalid);
389 bool CloseLineInvalid;
390 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000391 HET->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000392 &CloseLineInvalid);
393
394 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000395 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
396 << HST->getTagName() << HET->getTagName()
397 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000398 else {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000399 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
400 << HST->getTagName() << HET->getTagName()
401 << HST->getSourceRange();
402 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
403 << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000404 }
405 }
406
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000407 return HET;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000408}
409
410FullComment *Sema::actOnFullComment(
411 ArrayRef<BlockContentComment *> Blocks) {
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000412 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
413 resolveParamCommandIndexes(FC);
414 return FC;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000415}
416
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000417void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
418 ParagraphComment *Paragraph = Command->getParagraph();
419 if (Paragraph->isWhitespace()) {
420 SourceLocation DiagLoc;
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000421 if (Command->getNumArgs() > 0)
422 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000423 if (!DiagLoc.isValid())
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000424 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000425 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000426 << Command->getCommandName(Traits)
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000427 << Command->getSourceRange();
428 }
429}
430
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000431void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000432 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000433 return;
434 if (isFunctionDecl()) {
435 if (ThisDeclInfo->ResultType->isVoidType()) {
436 unsigned DiagKind;
437 switch (ThisDeclInfo->ThisDecl->getKind()) {
438 default:
Dmitri Gribenko88815f32012-08-06 16:29:26 +0000439 if (ThisDeclInfo->IsObjCMethod)
440 DiagKind = 3;
441 else
442 DiagKind = 0;
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000443 break;
444 case Decl::CXXConstructor:
445 DiagKind = 1;
446 break;
447 case Decl::CXXDestructor:
448 DiagKind = 2;
449 break;
450 }
451 Diag(Command->getLocation(),
452 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000453 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000454 << DiagKind
455 << Command->getSourceRange();
456 }
457 return;
458 }
459 Diag(Command->getLocation(),
460 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000461 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000462 << Command->getSourceRange();
463}
464
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000465void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000466 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000467 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000468 if (Info->IsBriefCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000469 if (!BriefCommand) {
470 BriefCommand = Command;
471 return;
472 }
473 PrevCommand = BriefCommand;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000474 } else if (Info->IsReturnsCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000475 if (!ReturnsCommand) {
476 ReturnsCommand = Command;
477 return;
478 }
479 PrevCommand = ReturnsCommand;
480 } else {
481 // We don't want to check this command for duplicates.
482 return;
483 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000484 StringRef CommandName = Command->getCommandName(Traits);
485 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000486 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000487 << CommandName
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000488 << Command->getSourceRange();
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000489 if (CommandName == PrevCommandName)
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000490 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000491 << PrevCommandName
492 << PrevCommand->getSourceRange();
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000493 else
494 Diag(PrevCommand->getLocation(),
495 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000496 << PrevCommandName
497 << CommandName;
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000498}
499
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000500void Sema::resolveParamCommandIndexes(const FullComment *FC) {
501 if (!isFunctionDecl()) {
502 // We already warned that \\param commands are not attached to a function
503 // decl.
504 return;
505 }
506
507 llvm::SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
508
509 // Comment AST nodes that correspond to \c ParamVars for which we have
510 // found a \\param command or NULL if no documentation was found so far.
511 llvm::SmallVector<ParamCommandComment *, 8> ParamVarDocs;
512
513 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
514 ParamVarDocs.resize(ParamVars.size(), NULL);
515
516 // First pass over all \\param commands: resolve all parameter names.
517 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
518 I != E; ++I) {
519 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
520 if (!PCC || !PCC->hasParamName())
521 continue;
522 StringRef ParamName = PCC->getParamName();
523
524 // Check that referenced parameter name is in the function decl.
525 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
526 ParamVars);
527 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
528 UnresolvedParamCommands.push_back(PCC);
529 continue;
530 }
531 PCC->setParamIndex(ResolvedParamIndex);
532 if (ParamVarDocs[ResolvedParamIndex]) {
533 SourceRange ArgRange = PCC->getParamNameRange();
534 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
535 << ParamName << ArgRange;
536 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
537 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
538 << PrevCommand->getParamNameRange();
539 }
540 ParamVarDocs[ResolvedParamIndex] = PCC;
541 }
542
543 // Find parameter declarations that have no corresponding \\param.
544 llvm::SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
545 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
546 if (!ParamVarDocs[i])
547 OrphanedParamDecls.push_back(ParamVars[i]);
548 }
549
550 // Second pass over unresolved \\param commands: do typo correction.
551 // Suggest corrections from a set of parameter declarations that have no
552 // corresponding \\param.
553 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
554 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
555
556 SourceRange ArgRange = PCC->getParamNameRange();
557 StringRef ParamName = PCC->getParamName();
558 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
559 << ParamName << ArgRange;
560
561 // All parameters documented -- can't suggest a correction.
562 if (OrphanedParamDecls.size() == 0)
563 continue;
564
565 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
566 if (OrphanedParamDecls.size() == 1) {
567 // If one parameter is not documented then that parameter is the only
568 // possible suggestion.
569 CorrectedParamIndex = 0;
570 } else {
571 // Do typo correction.
572 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
573 OrphanedParamDecls);
574 }
575 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
576 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
577 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
578 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
579 << CorrectedII->getName()
580 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
581 }
582 }
583}
584
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000585bool Sema::isFunctionDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000586 if (!ThisDeclInfo)
587 return false;
588 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000589 inspectThisDecl();
Dmitri Gribenkoaf19a6a2012-08-02 21:45:39 +0000590 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000591}
592
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000593bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000594 if (!ThisDeclInfo)
595 return false;
596 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000597 inspectThisDecl();
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000598 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000599}
600
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000601ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000602 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000603 inspectThisDecl();
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000604 return ThisDeclInfo->ParamVars;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000605}
606
607void Sema::inspectThisDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000608 ThisDeclInfo->fill();
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000609}
610
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000611unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000612 ArrayRef<const ParmVarDecl *> ParamVars) {
613 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000614 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
615 if (II && II->getName() == Name)
616 return i;
617 }
618 return ParamCommandComment::InvalidParamIndex;
619}
620
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000621namespace {
622class SimpleTypoCorrector {
623 StringRef Typo;
624 const unsigned MaxEditDistance;
625
626 const NamedDecl *BestDecl;
627 unsigned BestEditDistance;
628 unsigned BestIndex;
629 unsigned NextIndex;
630
631public:
632 SimpleTypoCorrector(StringRef Typo) :
633 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
634 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
635 BestIndex(0), NextIndex(0)
636 { }
637
638 void addDecl(const NamedDecl *ND);
639
640 const NamedDecl *getBestDecl() const {
641 if (BestEditDistance > MaxEditDistance)
642 return NULL;
643
644 return BestDecl;
645 }
646
647 unsigned getBestDeclIndex() const {
648 assert(getBestDecl());
649 return BestIndex;
650 }
651};
652
653void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
654 unsigned CurrIndex = NextIndex++;
655
656 const IdentifierInfo *II = ND->getIdentifier();
657 if (!II)
658 return;
659
660 StringRef Name = II->getName();
661 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
662 if (MinPossibleEditDistance > 0 &&
663 Typo.size() / MinPossibleEditDistance < 3)
664 return;
665
666 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
667 if (EditDistance < BestEditDistance) {
668 BestEditDistance = EditDistance;
669 BestDecl = ND;
670 BestIndex = CurrIndex;
671 }
672}
673} // unnamed namespace
674
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000675unsigned Sema::correctTypoInParmVarReference(
676 StringRef Typo,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000677 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000678 SimpleTypoCorrector Corrector(Typo);
679 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
680 Corrector.addDecl(ParamVars[i]);
681 if (Corrector.getBestDecl())
682 return Corrector.getBestDeclIndex();
683 else
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000684 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000685}
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000686
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000687namespace {
688bool ResolveTParamReferenceHelper(
689 StringRef Name,
690 const TemplateParameterList *TemplateParameters,
691 SmallVectorImpl<unsigned> *Position) {
692 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
693 const NamedDecl *Param = TemplateParameters->getParam(i);
694 const IdentifierInfo *II = Param->getIdentifier();
695 if (II && II->getName() == Name) {
696 Position->push_back(i);
697 return true;
698 }
699
700 if (const TemplateTemplateParmDecl *TTP =
701 dyn_cast<TemplateTemplateParmDecl>(Param)) {
702 Position->push_back(i);
703 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
704 Position))
705 return true;
706 Position->pop_back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000707 }
708 }
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000709 return false;
710}
711} // unnamed namespace
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000712
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000713bool Sema::resolveTParamReference(
714 StringRef Name,
715 const TemplateParameterList *TemplateParameters,
716 SmallVectorImpl<unsigned> *Position) {
717 Position->clear();
718 if (!TemplateParameters)
719 return false;
720
721 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
722}
723
724namespace {
725void CorrectTypoInTParamReferenceHelper(
726 const TemplateParameterList *TemplateParameters,
727 SimpleTypoCorrector &Corrector) {
728 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
729 const NamedDecl *Param = TemplateParameters->getParam(i);
730 Corrector.addDecl(Param);
731
732 if (const TemplateTemplateParmDecl *TTP =
733 dyn_cast<TemplateTemplateParmDecl>(Param))
734 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
735 Corrector);
736 }
737}
738} // unnamed namespace
739
740StringRef Sema::correctTypoInTParamReference(
741 StringRef Typo,
742 const TemplateParameterList *TemplateParameters) {
743 SimpleTypoCorrector Corrector(Typo);
744 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
745 if (const NamedDecl *ND = Corrector.getBestDecl()) {
746 const IdentifierInfo *II = ND->getIdentifier();
747 assert(II && "SimpleTypoCorrector should not return this decl");
748 return II->getName();
749 }
750 return StringRef();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000751}
752
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000753InlineCommandComment::RenderKind
754Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000755 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000756
757 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
758 .Case("b", InlineCommandComment::RenderBold)
759 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
760 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
761 .Default(InlineCommandComment::RenderNormal);
762}
763
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000764} // end namespace comments
765} // end namespace clang
766