blob: e7bd5c8cad2bbdfc332e98a90cc82bfaa5fb1f4e [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"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000011#include "clang/AST/Attr.h"
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000012#include "clang/AST/CommentCommandTraits.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000013#include "clang/AST/CommentDiagnostic.h"
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000014#include "clang/AST/Decl.h"
Dmitri Gribenko96b09862012-07-31 22:37:06 +000015#include "clang/AST/DeclTemplate.h"
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000016#include "clang/Basic/SourceManager.h"
Dmitri Gribenko19523542012-09-29 11:40:46 +000017#include "clang/Lex/Preprocessor.h"
Dmitri Gribenko19523542012-09-29 11:40:46 +000018#include "llvm/ADT/SmallString.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "llvm/ADT/StringSwitch.h"
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000020
21namespace clang {
22namespace comments {
23
Dmitri Gribenkoc24a76e2012-08-31 02:21:44 +000024namespace {
25#include "clang/AST/CommentHTMLTagsProperties.inc"
26} // unnamed namespace
27
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000028Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
Dmitri Gribenko19523542012-09-29 11:40:46 +000029 DiagnosticsEngine &Diags, CommandTraits &Traits,
30 const Preprocessor *PP) :
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000031 Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits),
Fariborz Jahanianf843a582013-01-31 23:12:39 +000032 PP(PP), ThisDeclInfo(NULL), BriefCommand(NULL), ReturnsCommand(NULL),
33 HeaderfileCommand(NULL) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000034}
35
36void Sema::setDecl(const Decl *D) {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +000037 if (!D)
38 return;
39
40 ThisDeclInfo = new (Allocator) DeclInfo;
Fariborz Jahanianbf967be2012-10-10 18:34:52 +000041 ThisDeclInfo->CommentDecl = D;
Dmitri Gribenko651f8ce2012-08-01 23:21:57 +000042 ThisDeclInfo->IsFilled = false;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000043}
44
45ParagraphComment *Sema::actOnParagraphComment(
46 ArrayRef<InlineContentComment *> Content) {
47 return new (Allocator) ParagraphComment(Content);
48}
49
50BlockCommandComment *Sema::actOnBlockCommandStart(SourceLocation LocBegin,
51 SourceLocation LocEnd,
Fariborz Jahanian8536fa12013-03-02 02:39:57 +000052 unsigned CommandID,
53 bool AtCommand) {
54 return new (Allocator) BlockCommandComment(LocBegin, LocEnd, CommandID, AtCommand);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000055}
56
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +000057void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
58 ArrayRef<BlockCommandComment::Argument> Args) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000059 Command->setArgs(Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000060}
61
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +000062void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
63 ParagraphComment *Paragraph) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000064 Command->setParagraph(Paragraph);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000065 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko9443c572012-08-06 17:08:27 +000066 checkBlockCommandDuplicate(Command);
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +000067 checkReturnsCommand(Command);
Dmitri Gribenko0bd98382012-09-22 21:47:50 +000068 checkDeprecatedCommand(Command);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000069}
70
71ParamCommandComment *Sema::actOnParamCommandStart(SourceLocation LocBegin,
72 SourceLocation LocEnd,
Fariborz Jahanian8536fa12013-03-02 02:39:57 +000073 unsigned CommandID,
74 bool AtCommand) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000075 ParamCommandComment *Command =
Fariborz Jahanian8536fa12013-03-02 02:39:57 +000076 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID, AtCommand);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000077
Dmitri Gribenko8487c522012-07-23 17:40:30 +000078 if (!isFunctionDecl())
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000079 Diag(Command->getLocation(),
80 diag::warn_doc_param_not_attached_to_a_function_decl)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000081 << Command->getCommandNameRange(Traits);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000082
83 return Command;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000084}
85
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +000086void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
87 SourceLocation ArgLocBegin,
88 SourceLocation ArgLocEnd,
89 StringRef Arg) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000090 ParamCommandComment::PassDirection Direction;
91 std::string ArgLower = Arg.lower();
92 // TODO: optimize: lower Name first (need an API in SmallString for that),
93 // after that StringSwitch.
94 if (ArgLower == "[in]")
95 Direction = ParamCommandComment::In;
96 else if (ArgLower == "[out]")
97 Direction = ParamCommandComment::Out;
98 else if (ArgLower == "[in,out]" || ArgLower == "[out,in]")
99 Direction = ParamCommandComment::InOut;
100 else {
101 // Remove spaces.
102 std::string::iterator O = ArgLower.begin();
103 for (std::string::iterator I = ArgLower.begin(), E = ArgLower.end();
104 I != E; ++I) {
105 const char C = *I;
106 if (C != ' ' && C != '\n' && C != '\r' &&
107 C != '\t' && C != '\v' && C != '\f')
108 *O++ = C;
109 }
110 ArgLower.resize(O - ArgLower.begin());
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000111
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000112 bool RemovingWhitespaceHelped = false;
113 if (ArgLower == "[in]") {
114 Direction = ParamCommandComment::In;
115 RemovingWhitespaceHelped = true;
116 } else if (ArgLower == "[out]") {
117 Direction = ParamCommandComment::Out;
118 RemovingWhitespaceHelped = true;
119 } else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") {
120 Direction = ParamCommandComment::InOut;
121 RemovingWhitespaceHelped = true;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000122 } else {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000123 Direction = ParamCommandComment::In;
124 RemovingWhitespaceHelped = false;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000125 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000126
127 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
128 if (RemovingWhitespaceHelped)
129 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
130 << ArgRange
131 << FixItHint::CreateReplacement(
132 ArgRange,
133 ParamCommandComment::getDirectionAsString(Direction));
134 else
135 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction)
136 << ArgRange;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000137 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000138 Command->setDirection(Direction, /* Explicit = */ true);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000139}
140
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000141void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
142 SourceLocation ArgLocBegin,
143 SourceLocation ArgLocEnd,
144 StringRef Arg) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000145 // Parser will not feed us more arguments than needed.
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000146 assert(Command->getNumArgs() == 0);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000147
148 if (!Command->isDirectionExplicit()) {
149 // User didn't provide a direction argument.
150 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
151 }
152 typedef BlockCommandComment::Argument Argument;
153 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
154 ArgLocEnd),
155 Arg);
156 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000157}
158
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000159void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
160 ParagraphComment *Paragraph) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000161 Command->setParagraph(Paragraph);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000162 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000163}
164
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000165TParamCommandComment *Sema::actOnTParamCommandStart(SourceLocation LocBegin,
166 SourceLocation LocEnd,
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000167 unsigned CommandID,
168 bool AtCommand) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000169 TParamCommandComment *Command =
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000170 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID, AtCommand);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000171
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000172 if (!isTemplateOrSpecialization())
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000173 Diag(Command->getLocation(),
174 diag::warn_doc_tparam_not_attached_to_a_template_decl)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000175 << Command->getCommandNameRange(Traits);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000176
177 return Command;
178}
179
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000180void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
181 SourceLocation ArgLocBegin,
182 SourceLocation ArgLocEnd,
183 StringRef Arg) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000184 // Parser will not feed us more arguments than needed.
185 assert(Command->getNumArgs() == 0);
186
187 typedef BlockCommandComment::Argument Argument;
188 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
189 ArgLocEnd),
190 Arg);
191 Command->setArgs(llvm::makeArrayRef(A, 1));
192
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000193 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000194 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000195 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000196 }
197
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000198 const TemplateParameterList *TemplateParameters =
199 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000200 SmallVector<unsigned, 2> Position;
201 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
202 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
203 llvm::StringMap<TParamCommandComment *>::iterator PrevCommandIt =
204 TemplateParameterDocs.find(Arg);
205 if (PrevCommandIt != TemplateParameterDocs.end()) {
206 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
207 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
208 << Arg << ArgRange;
209 TParamCommandComment *PrevCommand = PrevCommandIt->second;
210 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
211 << PrevCommand->getParamNameRange();
212 }
213 TemplateParameterDocs[Arg] = Command;
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000214 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000215 }
216
217 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
218 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
219 << Arg << ArgRange;
220
221 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000222 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000223
224 StringRef CorrectedName;
225 if (TemplateParameters->size() == 1) {
226 const NamedDecl *Param = TemplateParameters->getParam(0);
227 const IdentifierInfo *II = Param->getIdentifier();
228 if (II)
229 CorrectedName = II->getName();
230 } else {
231 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
232 }
233
234 if (!CorrectedName.empty()) {
235 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
236 << CorrectedName
237 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
238 }
239
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000240 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000241}
242
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000243void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
244 ParagraphComment *Paragraph) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000245 Command->setParagraph(Paragraph);
246 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000247}
248
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000249InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
250 SourceLocation CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000251 unsigned CommandID) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000252 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000253 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000254 return new (Allocator) InlineCommandComment(
255 CommandLocBegin,
256 CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000257 CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000258 getInlineCommandRenderKind(CommandName),
259 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000260}
261
262InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
263 SourceLocation CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000264 unsigned CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000265 SourceLocation ArgLocBegin,
266 SourceLocation ArgLocEnd,
267 StringRef Arg) {
268 typedef InlineCommandComment::Argument Argument;
269 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
270 ArgLocEnd),
271 Arg);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000272 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000273
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000274 return new (Allocator) InlineCommandComment(
275 CommandLocBegin,
276 CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000277 CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000278 getInlineCommandRenderKind(CommandName),
279 llvm::makeArrayRef(A, 1));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000280}
281
282InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
283 SourceLocation LocEnd,
Dmitri Gribenkob0b8a962012-09-11 19:22:03 +0000284 StringRef CommandName) {
285 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
286 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
287}
288
289InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
290 SourceLocation LocEnd,
291 unsigned CommandID) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000292 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000293 return new (Allocator) InlineCommandComment(
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000294 LocBegin, LocEnd, CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000295 InlineCommandComment::RenderNormal,
296 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000297}
298
299TextComment *Sema::actOnText(SourceLocation LocBegin,
300 SourceLocation LocEnd,
301 StringRef Text) {
302 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
303}
304
305VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000306 unsigned CommandID) {
307 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000308 return new (Allocator) VerbatimBlockComment(
309 Loc,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000310 Loc.getLocWithOffset(1 + CommandName.size()),
311 CommandID);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000312}
313
314VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
315 StringRef Text) {
316 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
317}
318
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000319void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000320 VerbatimBlockComment *Block,
321 SourceLocation CloseNameLocBegin,
322 StringRef CloseName,
323 ArrayRef<VerbatimBlockLineComment *> Lines) {
324 Block->setCloseName(CloseName, CloseNameLocBegin);
325 Block->setLines(Lines);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000326}
327
328VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000329 unsigned CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000330 SourceLocation TextBegin,
331 StringRef Text) {
332 return new (Allocator) VerbatimLineComment(
333 LocBegin,
334 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000335 CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000336 TextBegin,
337 Text);
338}
339
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000340HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
341 StringRef TagName) {
342 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000343}
344
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000345void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000346 HTMLStartTagComment *Tag,
347 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000348 SourceLocation GreaterLoc,
349 bool IsSelfClosing) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000350 Tag->setAttrs(Attrs);
351 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000352 if (IsSelfClosing)
353 Tag->setSelfClosing();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000354 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000355 HTMLOpenTags.push_back(Tag);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000356}
357
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000358HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
359 SourceLocation LocEnd,
360 StringRef TagName) {
361 HTMLEndTagComment *HET =
362 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
363 if (isHTMLEndTagForbidden(TagName)) {
364 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
365 << TagName << HET->getSourceRange();
366 return HET;
Dmitri Gribenko3d986982012-07-12 23:37:09 +0000367 }
368
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000369 bool FoundOpen = false;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000370 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000371 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
372 I != E; ++I) {
373 if ((*I)->getTagName() == TagName) {
374 FoundOpen = true;
375 break;
376 }
377 }
378 if (!FoundOpen) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000379 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
380 << HET->getSourceRange();
381 return HET;
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000382 }
383
384 while (!HTMLOpenTags.empty()) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000385 const HTMLStartTagComment *HST = HTMLOpenTags.back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000386 HTMLOpenTags.pop_back();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000387 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000388 if (LastNotClosedTagName == TagName)
389 break;
390
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000391 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000392 continue;
393
394 bool OpenLineInvalid;
395 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000396 HST->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000397 &OpenLineInvalid);
398 bool CloseLineInvalid;
399 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000400 HET->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000401 &CloseLineInvalid);
402
403 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000404 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
405 << HST->getTagName() << HET->getTagName()
406 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000407 else {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000408 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
409 << HST->getTagName() << HET->getTagName()
410 << HST->getSourceRange();
411 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
412 << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000413 }
414 }
415
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000416 return HET;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000417}
418
419FullComment *Sema::actOnFullComment(
420 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian749ace62012-10-11 23:52:50 +0000421 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000422 resolveParamCommandIndexes(FC);
423 return FC;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000424}
425
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000426void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkoabcf0dc2012-09-13 20:36:01 +0000427 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
428 return;
429
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000430 ParagraphComment *Paragraph = Command->getParagraph();
431 if (Paragraph->isWhitespace()) {
432 SourceLocation DiagLoc;
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000433 if (Command->getNumArgs() > 0)
434 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000435 if (!DiagLoc.isValid())
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000436 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000437 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000438 << Command->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000439 << Command->getCommandName(Traits)
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000440 << Command->getSourceRange();
441 }
442}
443
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000444void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000445 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000446 return;
447 if (isFunctionDecl()) {
448 if (ThisDeclInfo->ResultType->isVoidType()) {
449 unsigned DiagKind;
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000450 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000451 default:
Dmitri Gribenko88815f32012-08-06 16:29:26 +0000452 if (ThisDeclInfo->IsObjCMethod)
453 DiagKind = 3;
454 else
455 DiagKind = 0;
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000456 break;
457 case Decl::CXXConstructor:
458 DiagKind = 1;
459 break;
460 case Decl::CXXDestructor:
461 DiagKind = 2;
462 break;
463 }
464 Diag(Command->getLocation(),
465 diag::warn_doc_returns_attached_to_a_void_function)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000466 << Command->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000467 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000468 << DiagKind
469 << Command->getSourceRange();
470 }
471 return;
472 }
Fariborz Jahanian664e8602013-02-27 00:46:06 +0000473 else if (isObjCPropertyDecl())
474 return;
475
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000476 Diag(Command->getLocation(),
477 diag::warn_doc_returns_not_attached_to_a_function_decl)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000478 << Command->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000479 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000480 << Command->getSourceRange();
481}
482
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000483void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000484 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000485 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000486 if (Info->IsBriefCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000487 if (!BriefCommand) {
488 BriefCommand = Command;
489 return;
490 }
491 PrevCommand = BriefCommand;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000492 } else if (Info->IsReturnsCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000493 if (!ReturnsCommand) {
494 ReturnsCommand = Command;
495 return;
496 }
497 PrevCommand = ReturnsCommand;
Fariborz Jahanianf843a582013-01-31 23:12:39 +0000498 } else if (Info->IsHeaderfileCommand) {
499 if (!HeaderfileCommand) {
500 HeaderfileCommand = Command;
501 return;
502 }
503 PrevCommand = HeaderfileCommand;
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000504 } else {
505 // We don't want to check this command for duplicates.
506 return;
507 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000508 StringRef CommandName = Command->getCommandName(Traits);
509 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000510 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000511 << Command->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000512 << CommandName
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000513 << Command->getSourceRange();
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000514 if (CommandName == PrevCommandName)
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000515 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000516 << PrevCommand->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000517 << PrevCommandName
518 << PrevCommand->getSourceRange();
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000519 else
520 Diag(PrevCommand->getLocation(),
521 diag::note_doc_block_command_previous_alias)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000522 << PrevCommand->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000523 << PrevCommandName
524 << CommandName;
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000525}
526
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000527void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
528 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
529 return;
530
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000531 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000532 if (!D)
533 return;
534
535 if (D->hasAttr<DeprecatedAttr>() ||
536 D->hasAttr<AvailabilityAttr>() ||
537 D->hasAttr<UnavailableAttr>())
538 return;
539
540 Diag(Command->getLocation(),
541 diag::warn_doc_deprecated_not_sync)
542 << Command->getSourceRange();
543
544 // Try to emit a fixit with a deprecation attribute.
545 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
546 // Don't emit a Fix-It for non-member function definitions. GCC does not
547 // accept attributes on them.
548 const DeclContext *Ctx = FD->getDeclContext();
549 if ((!Ctx || !Ctx->isRecord()) &&
550 FD->doesThisDeclarationHaveABody())
551 return;
552
Dmitri Gribenko19523542012-09-29 11:40:46 +0000553 StringRef AttributeSpelling = "__attribute__((deprecated))";
554 if (PP) {
555 TokenValue Tokens[] = {
556 tok::kw___attribute, tok::l_paren, tok::l_paren,
557 PP->getIdentifierInfo("deprecated"),
558 tok::r_paren, tok::r_paren
559 };
560 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
561 Tokens);
562 if (!MacroName.empty())
563 AttributeSpelling = MacroName;
564 }
565
566 SmallString<64> TextToInsert(" ");
567 TextToInsert += AttributeSpelling;
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000568 Diag(FD->getLocEnd(),
569 diag::note_add_deprecation_attr)
570 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko19523542012-09-29 11:40:46 +0000571 TextToInsert);
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000572 }
573}
574
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000575void Sema::resolveParamCommandIndexes(const FullComment *FC) {
576 if (!isFunctionDecl()) {
577 // We already warned that \\param commands are not attached to a function
578 // decl.
579 return;
580 }
581
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000582 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000583
584 // Comment AST nodes that correspond to \c ParamVars for which we have
585 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000586 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000587
588 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
589 ParamVarDocs.resize(ParamVars.size(), NULL);
590
591 // First pass over all \\param commands: resolve all parameter names.
592 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
593 I != E; ++I) {
594 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
595 if (!PCC || !PCC->hasParamName())
596 continue;
Fariborz Jahanian262e60c2012-10-18 21:42:42 +0000597 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000598
599 // Check that referenced parameter name is in the function decl.
600 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
601 ParamVars);
602 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
603 UnresolvedParamCommands.push_back(PCC);
604 continue;
605 }
606 PCC->setParamIndex(ResolvedParamIndex);
607 if (ParamVarDocs[ResolvedParamIndex]) {
608 SourceRange ArgRange = PCC->getParamNameRange();
609 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
610 << ParamName << ArgRange;
611 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
612 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
613 << PrevCommand->getParamNameRange();
614 }
615 ParamVarDocs[ResolvedParamIndex] = PCC;
616 }
617
618 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000619 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000620 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
621 if (!ParamVarDocs[i])
622 OrphanedParamDecls.push_back(ParamVars[i]);
623 }
624
625 // Second pass over unresolved \\param commands: do typo correction.
626 // Suggest corrections from a set of parameter declarations that have no
627 // corresponding \\param.
628 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
629 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
630
631 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian262e60c2012-10-18 21:42:42 +0000632 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000633 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
634 << ParamName << ArgRange;
635
636 // All parameters documented -- can't suggest a correction.
637 if (OrphanedParamDecls.size() == 0)
638 continue;
639
640 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
641 if (OrphanedParamDecls.size() == 1) {
642 // If one parameter is not documented then that parameter is the only
643 // possible suggestion.
644 CorrectedParamIndex = 0;
645 } else {
646 // Do typo correction.
647 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
648 OrphanedParamDecls);
649 }
650 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
651 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
652 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
653 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
654 << CorrectedII->getName()
655 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
656 }
657 }
658}
659
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000660bool Sema::isFunctionDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000661 if (!ThisDeclInfo)
662 return false;
663 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000664 inspectThisDecl();
Dmitri Gribenkoaf19a6a2012-08-02 21:45:39 +0000665 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000666}
Fariborz Jahanian664e8602013-02-27 00:46:06 +0000667
668bool Sema::isObjCPropertyDecl() {
669 if (!ThisDeclInfo)
670 return false;
671 if (!ThisDeclInfo->IsFilled)
672 inspectThisDecl();
673 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
674}
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000675
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000676bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000677 if (!ThisDeclInfo)
678 return false;
679 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000680 inspectThisDecl();
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000681 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000682}
683
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000684ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000685 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000686 inspectThisDecl();
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000687 return ThisDeclInfo->ParamVars;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000688}
689
690void Sema::inspectThisDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000691 ThisDeclInfo->fill();
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000692}
693
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000694unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000695 ArrayRef<const ParmVarDecl *> ParamVars) {
696 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000697 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
698 if (II && II->getName() == Name)
699 return i;
700 }
701 return ParamCommandComment::InvalidParamIndex;
702}
703
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000704namespace {
705class SimpleTypoCorrector {
706 StringRef Typo;
707 const unsigned MaxEditDistance;
708
709 const NamedDecl *BestDecl;
710 unsigned BestEditDistance;
711 unsigned BestIndex;
712 unsigned NextIndex;
713
714public:
715 SimpleTypoCorrector(StringRef Typo) :
716 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
717 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
718 BestIndex(0), NextIndex(0)
719 { }
720
721 void addDecl(const NamedDecl *ND);
722
723 const NamedDecl *getBestDecl() const {
724 if (BestEditDistance > MaxEditDistance)
725 return NULL;
726
727 return BestDecl;
728 }
729
730 unsigned getBestDeclIndex() const {
731 assert(getBestDecl());
732 return BestIndex;
733 }
734};
735
736void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
737 unsigned CurrIndex = NextIndex++;
738
739 const IdentifierInfo *II = ND->getIdentifier();
740 if (!II)
741 return;
742
743 StringRef Name = II->getName();
744 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
745 if (MinPossibleEditDistance > 0 &&
746 Typo.size() / MinPossibleEditDistance < 3)
747 return;
748
749 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
750 if (EditDistance < BestEditDistance) {
751 BestEditDistance = EditDistance;
752 BestDecl = ND;
753 BestIndex = CurrIndex;
754 }
755}
756} // unnamed namespace
757
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000758unsigned Sema::correctTypoInParmVarReference(
759 StringRef Typo,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000760 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000761 SimpleTypoCorrector Corrector(Typo);
762 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
763 Corrector.addDecl(ParamVars[i]);
764 if (Corrector.getBestDecl())
765 return Corrector.getBestDeclIndex();
766 else
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000767 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000768}
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000769
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000770namespace {
771bool ResolveTParamReferenceHelper(
772 StringRef Name,
773 const TemplateParameterList *TemplateParameters,
774 SmallVectorImpl<unsigned> *Position) {
775 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
776 const NamedDecl *Param = TemplateParameters->getParam(i);
777 const IdentifierInfo *II = Param->getIdentifier();
778 if (II && II->getName() == Name) {
779 Position->push_back(i);
780 return true;
781 }
782
783 if (const TemplateTemplateParmDecl *TTP =
784 dyn_cast<TemplateTemplateParmDecl>(Param)) {
785 Position->push_back(i);
786 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
787 Position))
788 return true;
789 Position->pop_back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000790 }
791 }
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000792 return false;
793}
794} // unnamed namespace
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000795
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000796bool Sema::resolveTParamReference(
797 StringRef Name,
798 const TemplateParameterList *TemplateParameters,
799 SmallVectorImpl<unsigned> *Position) {
800 Position->clear();
801 if (!TemplateParameters)
802 return false;
803
804 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
805}
806
807namespace {
808void CorrectTypoInTParamReferenceHelper(
809 const TemplateParameterList *TemplateParameters,
810 SimpleTypoCorrector &Corrector) {
811 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
812 const NamedDecl *Param = TemplateParameters->getParam(i);
813 Corrector.addDecl(Param);
814
815 if (const TemplateTemplateParmDecl *TTP =
816 dyn_cast<TemplateTemplateParmDecl>(Param))
817 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
818 Corrector);
819 }
820}
821} // unnamed namespace
822
823StringRef Sema::correctTypoInTParamReference(
824 StringRef Typo,
825 const TemplateParameterList *TemplateParameters) {
826 SimpleTypoCorrector Corrector(Typo);
827 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
828 if (const NamedDecl *ND = Corrector.getBestDecl()) {
829 const IdentifierInfo *II = ND->getIdentifier();
830 assert(II && "SimpleTypoCorrector should not return this decl");
831 return II->getName();
832 }
833 return StringRef();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000834}
835
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000836InlineCommandComment::RenderKind
837Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000838 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000839
840 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
841 .Case("b", InlineCommandComment::RenderBold)
842 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
843 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
844 .Default(InlineCommandComment::RenderNormal);
845}
846
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000847} // end namespace comments
848} // end namespace clang
849