blob: 6d84a2a4b91f82ea054e58e8a16103311e09d437 [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) {
Dmitri Gribenkoabcf0dc2012-09-13 20:36:01 +0000418 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
419 return;
420
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000421 ParagraphComment *Paragraph = Command->getParagraph();
422 if (Paragraph->isWhitespace()) {
423 SourceLocation DiagLoc;
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000424 if (Command->getNumArgs() > 0)
425 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000426 if (!DiagLoc.isValid())
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000427 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000428 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000429 << Command->getCommandName(Traits)
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000430 << Command->getSourceRange();
431 }
432}
433
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000434void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000435 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000436 return;
437 if (isFunctionDecl()) {
438 if (ThisDeclInfo->ResultType->isVoidType()) {
439 unsigned DiagKind;
440 switch (ThisDeclInfo->ThisDecl->getKind()) {
441 default:
Dmitri Gribenko88815f32012-08-06 16:29:26 +0000442 if (ThisDeclInfo->IsObjCMethod)
443 DiagKind = 3;
444 else
445 DiagKind = 0;
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000446 break;
447 case Decl::CXXConstructor:
448 DiagKind = 1;
449 break;
450 case Decl::CXXDestructor:
451 DiagKind = 2;
452 break;
453 }
454 Diag(Command->getLocation(),
455 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000456 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000457 << DiagKind
458 << Command->getSourceRange();
459 }
460 return;
461 }
462 Diag(Command->getLocation(),
463 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000464 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000465 << Command->getSourceRange();
466}
467
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000468void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000469 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000470 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000471 if (Info->IsBriefCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000472 if (!BriefCommand) {
473 BriefCommand = Command;
474 return;
475 }
476 PrevCommand = BriefCommand;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000477 } else if (Info->IsReturnsCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000478 if (!ReturnsCommand) {
479 ReturnsCommand = Command;
480 return;
481 }
482 PrevCommand = ReturnsCommand;
483 } else {
484 // We don't want to check this command for duplicates.
485 return;
486 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000487 StringRef CommandName = Command->getCommandName(Traits);
488 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000489 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000490 << CommandName
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000491 << Command->getSourceRange();
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000492 if (CommandName == PrevCommandName)
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000493 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000494 << PrevCommandName
495 << PrevCommand->getSourceRange();
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000496 else
497 Diag(PrevCommand->getLocation(),
498 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000499 << PrevCommandName
500 << CommandName;
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000501}
502
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000503void Sema::resolveParamCommandIndexes(const FullComment *FC) {
504 if (!isFunctionDecl()) {
505 // We already warned that \\param commands are not attached to a function
506 // decl.
507 return;
508 }
509
510 llvm::SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
511
512 // Comment AST nodes that correspond to \c ParamVars for which we have
513 // found a \\param command or NULL if no documentation was found so far.
514 llvm::SmallVector<ParamCommandComment *, 8> ParamVarDocs;
515
516 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
517 ParamVarDocs.resize(ParamVars.size(), NULL);
518
519 // First pass over all \\param commands: resolve all parameter names.
520 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
521 I != E; ++I) {
522 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
523 if (!PCC || !PCC->hasParamName())
524 continue;
525 StringRef ParamName = PCC->getParamName();
526
527 // Check that referenced parameter name is in the function decl.
528 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
529 ParamVars);
530 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
531 UnresolvedParamCommands.push_back(PCC);
532 continue;
533 }
534 PCC->setParamIndex(ResolvedParamIndex);
535 if (ParamVarDocs[ResolvedParamIndex]) {
536 SourceRange ArgRange = PCC->getParamNameRange();
537 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
538 << ParamName << ArgRange;
539 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
540 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
541 << PrevCommand->getParamNameRange();
542 }
543 ParamVarDocs[ResolvedParamIndex] = PCC;
544 }
545
546 // Find parameter declarations that have no corresponding \\param.
547 llvm::SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
548 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
549 if (!ParamVarDocs[i])
550 OrphanedParamDecls.push_back(ParamVars[i]);
551 }
552
553 // Second pass over unresolved \\param commands: do typo correction.
554 // Suggest corrections from a set of parameter declarations that have no
555 // corresponding \\param.
556 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
557 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
558
559 SourceRange ArgRange = PCC->getParamNameRange();
560 StringRef ParamName = PCC->getParamName();
561 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
562 << ParamName << ArgRange;
563
564 // All parameters documented -- can't suggest a correction.
565 if (OrphanedParamDecls.size() == 0)
566 continue;
567
568 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
569 if (OrphanedParamDecls.size() == 1) {
570 // If one parameter is not documented then that parameter is the only
571 // possible suggestion.
572 CorrectedParamIndex = 0;
573 } else {
574 // Do typo correction.
575 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
576 OrphanedParamDecls);
577 }
578 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
579 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
580 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
581 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
582 << CorrectedII->getName()
583 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
584 }
585 }
586}
587
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000588bool Sema::isFunctionDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000589 if (!ThisDeclInfo)
590 return false;
591 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000592 inspectThisDecl();
Dmitri Gribenkoaf19a6a2012-08-02 21:45:39 +0000593 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000594}
595
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000596bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000597 if (!ThisDeclInfo)
598 return false;
599 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000600 inspectThisDecl();
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000601 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000602}
603
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000604ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000605 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000606 inspectThisDecl();
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000607 return ThisDeclInfo->ParamVars;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000608}
609
610void Sema::inspectThisDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000611 ThisDeclInfo->fill();
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000612}
613
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000614unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000615 ArrayRef<const ParmVarDecl *> ParamVars) {
616 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000617 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
618 if (II && II->getName() == Name)
619 return i;
620 }
621 return ParamCommandComment::InvalidParamIndex;
622}
623
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000624namespace {
625class SimpleTypoCorrector {
626 StringRef Typo;
627 const unsigned MaxEditDistance;
628
629 const NamedDecl *BestDecl;
630 unsigned BestEditDistance;
631 unsigned BestIndex;
632 unsigned NextIndex;
633
634public:
635 SimpleTypoCorrector(StringRef Typo) :
636 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
637 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
638 BestIndex(0), NextIndex(0)
639 { }
640
641 void addDecl(const NamedDecl *ND);
642
643 const NamedDecl *getBestDecl() const {
644 if (BestEditDistance > MaxEditDistance)
645 return NULL;
646
647 return BestDecl;
648 }
649
650 unsigned getBestDeclIndex() const {
651 assert(getBestDecl());
652 return BestIndex;
653 }
654};
655
656void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
657 unsigned CurrIndex = NextIndex++;
658
659 const IdentifierInfo *II = ND->getIdentifier();
660 if (!II)
661 return;
662
663 StringRef Name = II->getName();
664 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
665 if (MinPossibleEditDistance > 0 &&
666 Typo.size() / MinPossibleEditDistance < 3)
667 return;
668
669 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
670 if (EditDistance < BestEditDistance) {
671 BestEditDistance = EditDistance;
672 BestDecl = ND;
673 BestIndex = CurrIndex;
674 }
675}
676} // unnamed namespace
677
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000678unsigned Sema::correctTypoInParmVarReference(
679 StringRef Typo,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000680 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000681 SimpleTypoCorrector Corrector(Typo);
682 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
683 Corrector.addDecl(ParamVars[i]);
684 if (Corrector.getBestDecl())
685 return Corrector.getBestDeclIndex();
686 else
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000687 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000688}
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000689
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000690namespace {
691bool ResolveTParamReferenceHelper(
692 StringRef Name,
693 const TemplateParameterList *TemplateParameters,
694 SmallVectorImpl<unsigned> *Position) {
695 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
696 const NamedDecl *Param = TemplateParameters->getParam(i);
697 const IdentifierInfo *II = Param->getIdentifier();
698 if (II && II->getName() == Name) {
699 Position->push_back(i);
700 return true;
701 }
702
703 if (const TemplateTemplateParmDecl *TTP =
704 dyn_cast<TemplateTemplateParmDecl>(Param)) {
705 Position->push_back(i);
706 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
707 Position))
708 return true;
709 Position->pop_back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000710 }
711 }
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000712 return false;
713}
714} // unnamed namespace
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000715
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000716bool Sema::resolveTParamReference(
717 StringRef Name,
718 const TemplateParameterList *TemplateParameters,
719 SmallVectorImpl<unsigned> *Position) {
720 Position->clear();
721 if (!TemplateParameters)
722 return false;
723
724 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
725}
726
727namespace {
728void CorrectTypoInTParamReferenceHelper(
729 const TemplateParameterList *TemplateParameters,
730 SimpleTypoCorrector &Corrector) {
731 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
732 const NamedDecl *Param = TemplateParameters->getParam(i);
733 Corrector.addDecl(Param);
734
735 if (const TemplateTemplateParmDecl *TTP =
736 dyn_cast<TemplateTemplateParmDecl>(Param))
737 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
738 Corrector);
739 }
740}
741} // unnamed namespace
742
743StringRef Sema::correctTypoInTParamReference(
744 StringRef Typo,
745 const TemplateParameterList *TemplateParameters) {
746 SimpleTypoCorrector Corrector(Typo);
747 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
748 if (const NamedDecl *ND = Corrector.getBestDecl()) {
749 const IdentifierInfo *II = ND->getIdentifier();
750 assert(II && "SimpleTypoCorrector should not return this decl");
751 return II->getName();
752 }
753 return StringRef();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000754}
755
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000756InlineCommandComment::RenderKind
757Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000758 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000759
760 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
761 .Case("b", InlineCommandComment::RenderBold)
762 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
763 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
764 .Default(InlineCommandComment::RenderNormal);
765}
766
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000767} // end namespace comments
768} // end namespace clang
769