blob: e57dac71044d245aba0193bf14a9d78f682fbfd4 [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,
275 StringRef Name) {
276 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000277 unsigned CommandID = Traits.registerUnknownCommand(Name)->getID();
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000278 return new (Allocator) InlineCommandComment(
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000279 LocBegin, LocEnd, CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000280 InlineCommandComment::RenderNormal,
281 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000282}
283
284TextComment *Sema::actOnText(SourceLocation LocBegin,
285 SourceLocation LocEnd,
286 StringRef Text) {
287 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
288}
289
290VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000291 unsigned CommandID) {
292 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000293 return new (Allocator) VerbatimBlockComment(
294 Loc,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000295 Loc.getLocWithOffset(1 + CommandName.size()),
296 CommandID);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000297}
298
299VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
300 StringRef Text) {
301 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
302}
303
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000304void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000305 VerbatimBlockComment *Block,
306 SourceLocation CloseNameLocBegin,
307 StringRef CloseName,
308 ArrayRef<VerbatimBlockLineComment *> Lines) {
309 Block->setCloseName(CloseName, CloseNameLocBegin);
310 Block->setLines(Lines);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000311}
312
313VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000314 unsigned CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000315 SourceLocation TextBegin,
316 StringRef Text) {
317 return new (Allocator) VerbatimLineComment(
318 LocBegin,
319 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000320 CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000321 TextBegin,
322 Text);
323}
324
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000325HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
326 StringRef TagName) {
327 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000328}
329
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000330void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000331 HTMLStartTagComment *Tag,
332 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000333 SourceLocation GreaterLoc,
334 bool IsSelfClosing) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000335 Tag->setAttrs(Attrs);
336 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000337 if (IsSelfClosing)
338 Tag->setSelfClosing();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000339 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000340 HTMLOpenTags.push_back(Tag);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000341}
342
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000343HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
344 SourceLocation LocEnd,
345 StringRef TagName) {
346 HTMLEndTagComment *HET =
347 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
348 if (isHTMLEndTagForbidden(TagName)) {
349 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
350 << TagName << HET->getSourceRange();
351 return HET;
Dmitri Gribenko3d986982012-07-12 23:37:09 +0000352 }
353
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000354 bool FoundOpen = false;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000355 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000356 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
357 I != E; ++I) {
358 if ((*I)->getTagName() == TagName) {
359 FoundOpen = true;
360 break;
361 }
362 }
363 if (!FoundOpen) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000364 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
365 << HET->getSourceRange();
366 return HET;
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000367 }
368
369 while (!HTMLOpenTags.empty()) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000370 const HTMLStartTagComment *HST = HTMLOpenTags.back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000371 HTMLOpenTags.pop_back();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000372 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000373 if (LastNotClosedTagName == TagName)
374 break;
375
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000376 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000377 continue;
378
379 bool OpenLineInvalid;
380 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000381 HST->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000382 &OpenLineInvalid);
383 bool CloseLineInvalid;
384 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000385 HET->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000386 &CloseLineInvalid);
387
388 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
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() << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000392 else {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000393 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
394 << HST->getTagName() << HET->getTagName()
395 << HST->getSourceRange();
396 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
397 << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000398 }
399 }
400
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000401 return HET;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000402}
403
404FullComment *Sema::actOnFullComment(
405 ArrayRef<BlockContentComment *> Blocks) {
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000406 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
407 resolveParamCommandIndexes(FC);
408 return FC;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000409}
410
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000411void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
412 ParagraphComment *Paragraph = Command->getParagraph();
413 if (Paragraph->isWhitespace()) {
414 SourceLocation DiagLoc;
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000415 if (Command->getNumArgs() > 0)
416 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000417 if (!DiagLoc.isValid())
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000418 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000419 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000420 << Command->getCommandName(Traits)
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000421 << Command->getSourceRange();
422 }
423}
424
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000425void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000426 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000427 return;
428 if (isFunctionDecl()) {
429 if (ThisDeclInfo->ResultType->isVoidType()) {
430 unsigned DiagKind;
431 switch (ThisDeclInfo->ThisDecl->getKind()) {
432 default:
Dmitri Gribenko88815f32012-08-06 16:29:26 +0000433 if (ThisDeclInfo->IsObjCMethod)
434 DiagKind = 3;
435 else
436 DiagKind = 0;
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000437 break;
438 case Decl::CXXConstructor:
439 DiagKind = 1;
440 break;
441 case Decl::CXXDestructor:
442 DiagKind = 2;
443 break;
444 }
445 Diag(Command->getLocation(),
446 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000447 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000448 << DiagKind
449 << Command->getSourceRange();
450 }
451 return;
452 }
453 Diag(Command->getLocation(),
454 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000455 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000456 << Command->getSourceRange();
457}
458
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000459void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000460 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000461 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000462 if (Info->IsBriefCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000463 if (!BriefCommand) {
464 BriefCommand = Command;
465 return;
466 }
467 PrevCommand = BriefCommand;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000468 } else if (Info->IsReturnsCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000469 if (!ReturnsCommand) {
470 ReturnsCommand = Command;
471 return;
472 }
473 PrevCommand = ReturnsCommand;
474 } else {
475 // We don't want to check this command for duplicates.
476 return;
477 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000478 StringRef CommandName = Command->getCommandName(Traits);
479 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000480 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000481 << CommandName
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000482 << Command->getSourceRange();
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000483 if (CommandName == PrevCommandName)
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000484 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000485 << PrevCommandName
486 << PrevCommand->getSourceRange();
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000487 else
488 Diag(PrevCommand->getLocation(),
489 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000490 << PrevCommandName
491 << CommandName;
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000492}
493
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000494void Sema::resolveParamCommandIndexes(const FullComment *FC) {
495 if (!isFunctionDecl()) {
496 // We already warned that \\param commands are not attached to a function
497 // decl.
498 return;
499 }
500
501 llvm::SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
502
503 // Comment AST nodes that correspond to \c ParamVars for which we have
504 // found a \\param command or NULL if no documentation was found so far.
505 llvm::SmallVector<ParamCommandComment *, 8> ParamVarDocs;
506
507 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
508 ParamVarDocs.resize(ParamVars.size(), NULL);
509
510 // First pass over all \\param commands: resolve all parameter names.
511 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
512 I != E; ++I) {
513 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
514 if (!PCC || !PCC->hasParamName())
515 continue;
516 StringRef ParamName = PCC->getParamName();
517
518 // Check that referenced parameter name is in the function decl.
519 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
520 ParamVars);
521 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
522 UnresolvedParamCommands.push_back(PCC);
523 continue;
524 }
525 PCC->setParamIndex(ResolvedParamIndex);
526 if (ParamVarDocs[ResolvedParamIndex]) {
527 SourceRange ArgRange = PCC->getParamNameRange();
528 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
529 << ParamName << ArgRange;
530 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
531 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
532 << PrevCommand->getParamNameRange();
533 }
534 ParamVarDocs[ResolvedParamIndex] = PCC;
535 }
536
537 // Find parameter declarations that have no corresponding \\param.
538 llvm::SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
539 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
540 if (!ParamVarDocs[i])
541 OrphanedParamDecls.push_back(ParamVars[i]);
542 }
543
544 // Second pass over unresolved \\param commands: do typo correction.
545 // Suggest corrections from a set of parameter declarations that have no
546 // corresponding \\param.
547 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
548 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
549
550 SourceRange ArgRange = PCC->getParamNameRange();
551 StringRef ParamName = PCC->getParamName();
552 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
553 << ParamName << ArgRange;
554
555 // All parameters documented -- can't suggest a correction.
556 if (OrphanedParamDecls.size() == 0)
557 continue;
558
559 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
560 if (OrphanedParamDecls.size() == 1) {
561 // If one parameter is not documented then that parameter is the only
562 // possible suggestion.
563 CorrectedParamIndex = 0;
564 } else {
565 // Do typo correction.
566 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
567 OrphanedParamDecls);
568 }
569 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
570 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
571 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
572 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
573 << CorrectedII->getName()
574 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
575 }
576 }
577}
578
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000579bool Sema::isFunctionDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000580 if (!ThisDeclInfo)
581 return false;
582 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000583 inspectThisDecl();
Dmitri Gribenkoaf19a6a2012-08-02 21:45:39 +0000584 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000585}
586
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000587bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000588 if (!ThisDeclInfo)
589 return false;
590 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000591 inspectThisDecl();
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000592 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000593}
594
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000595ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000596 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000597 inspectThisDecl();
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000598 return ThisDeclInfo->ParamVars;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000599}
600
601void Sema::inspectThisDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000602 ThisDeclInfo->fill();
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000603}
604
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000605unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000606 ArrayRef<const ParmVarDecl *> ParamVars) {
607 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000608 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
609 if (II && II->getName() == Name)
610 return i;
611 }
612 return ParamCommandComment::InvalidParamIndex;
613}
614
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000615namespace {
616class SimpleTypoCorrector {
617 StringRef Typo;
618 const unsigned MaxEditDistance;
619
620 const NamedDecl *BestDecl;
621 unsigned BestEditDistance;
622 unsigned BestIndex;
623 unsigned NextIndex;
624
625public:
626 SimpleTypoCorrector(StringRef Typo) :
627 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
628 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
629 BestIndex(0), NextIndex(0)
630 { }
631
632 void addDecl(const NamedDecl *ND);
633
634 const NamedDecl *getBestDecl() const {
635 if (BestEditDistance > MaxEditDistance)
636 return NULL;
637
638 return BestDecl;
639 }
640
641 unsigned getBestDeclIndex() const {
642 assert(getBestDecl());
643 return BestIndex;
644 }
645};
646
647void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
648 unsigned CurrIndex = NextIndex++;
649
650 const IdentifierInfo *II = ND->getIdentifier();
651 if (!II)
652 return;
653
654 StringRef Name = II->getName();
655 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
656 if (MinPossibleEditDistance > 0 &&
657 Typo.size() / MinPossibleEditDistance < 3)
658 return;
659
660 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
661 if (EditDistance < BestEditDistance) {
662 BestEditDistance = EditDistance;
663 BestDecl = ND;
664 BestIndex = CurrIndex;
665 }
666}
667} // unnamed namespace
668
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000669unsigned Sema::correctTypoInParmVarReference(
670 StringRef Typo,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000671 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000672 SimpleTypoCorrector Corrector(Typo);
673 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
674 Corrector.addDecl(ParamVars[i]);
675 if (Corrector.getBestDecl())
676 return Corrector.getBestDeclIndex();
677 else
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000678 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000679}
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000680
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000681namespace {
682bool ResolveTParamReferenceHelper(
683 StringRef Name,
684 const TemplateParameterList *TemplateParameters,
685 SmallVectorImpl<unsigned> *Position) {
686 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
687 const NamedDecl *Param = TemplateParameters->getParam(i);
688 const IdentifierInfo *II = Param->getIdentifier();
689 if (II && II->getName() == Name) {
690 Position->push_back(i);
691 return true;
692 }
693
694 if (const TemplateTemplateParmDecl *TTP =
695 dyn_cast<TemplateTemplateParmDecl>(Param)) {
696 Position->push_back(i);
697 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
698 Position))
699 return true;
700 Position->pop_back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000701 }
702 }
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000703 return false;
704}
705} // unnamed namespace
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000706
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000707bool Sema::resolveTParamReference(
708 StringRef Name,
709 const TemplateParameterList *TemplateParameters,
710 SmallVectorImpl<unsigned> *Position) {
711 Position->clear();
712 if (!TemplateParameters)
713 return false;
714
715 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
716}
717
718namespace {
719void CorrectTypoInTParamReferenceHelper(
720 const TemplateParameterList *TemplateParameters,
721 SimpleTypoCorrector &Corrector) {
722 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
723 const NamedDecl *Param = TemplateParameters->getParam(i);
724 Corrector.addDecl(Param);
725
726 if (const TemplateTemplateParmDecl *TTP =
727 dyn_cast<TemplateTemplateParmDecl>(Param))
728 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
729 Corrector);
730 }
731}
732} // unnamed namespace
733
734StringRef Sema::correctTypoInTParamReference(
735 StringRef Typo,
736 const TemplateParameterList *TemplateParameters) {
737 SimpleTypoCorrector Corrector(Typo);
738 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
739 if (const NamedDecl *ND = Corrector.getBestDecl()) {
740 const IdentifierInfo *II = ND->getIdentifier();
741 assert(II && "SimpleTypoCorrector should not return this decl");
742 return II->getName();
743 }
744 return StringRef();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000745}
746
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000747InlineCommandComment::RenderKind
748Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000749 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000750
751 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
752 .Case("b", InlineCommandComment::RenderBold)
753 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
754 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
755 .Default(InlineCommandComment::RenderNormal);
756}
757
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000758} // end namespace comments
759} // end namespace clang
760