blob: 3e4b28356b9bcd7156f3dc4c0fc8e80c2257ad52 [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)
Fariborz Jahanian66f6c242013-03-04 18:53:41 +000081 << AtCommand
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000082 << Command->getCommandNameRange(Traits);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000083
84 return Command;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000085}
86
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +000087void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
88 SourceLocation ArgLocBegin,
89 SourceLocation ArgLocEnd,
90 StringRef Arg) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000091 ParamCommandComment::PassDirection Direction;
92 std::string ArgLower = Arg.lower();
93 // TODO: optimize: lower Name first (need an API in SmallString for that),
94 // after that StringSwitch.
95 if (ArgLower == "[in]")
96 Direction = ParamCommandComment::In;
97 else if (ArgLower == "[out]")
98 Direction = ParamCommandComment::Out;
99 else if (ArgLower == "[in,out]" || ArgLower == "[out,in]")
100 Direction = ParamCommandComment::InOut;
101 else {
102 // Remove spaces.
103 std::string::iterator O = ArgLower.begin();
104 for (std::string::iterator I = ArgLower.begin(), E = ArgLower.end();
105 I != E; ++I) {
106 const char C = *I;
107 if (C != ' ' && C != '\n' && C != '\r' &&
108 C != '\t' && C != '\v' && C != '\f')
109 *O++ = C;
110 }
111 ArgLower.resize(O - ArgLower.begin());
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000112
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000113 bool RemovingWhitespaceHelped = false;
114 if (ArgLower == "[in]") {
115 Direction = ParamCommandComment::In;
116 RemovingWhitespaceHelped = true;
117 } else if (ArgLower == "[out]") {
118 Direction = ParamCommandComment::Out;
119 RemovingWhitespaceHelped = true;
120 } else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") {
121 Direction = ParamCommandComment::InOut;
122 RemovingWhitespaceHelped = true;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000123 } else {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000124 Direction = ParamCommandComment::In;
125 RemovingWhitespaceHelped = false;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000126 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000127
128 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
129 if (RemovingWhitespaceHelped)
130 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
131 << ArgRange
132 << FixItHint::CreateReplacement(
133 ArgRange,
134 ParamCommandComment::getDirectionAsString(Direction));
135 else
136 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction)
137 << ArgRange;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000138 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000139 Command->setDirection(Direction, /* Explicit = */ true);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000140}
141
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000142void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
143 SourceLocation ArgLocBegin,
144 SourceLocation ArgLocEnd,
145 StringRef Arg) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000146 // Parser will not feed us more arguments than needed.
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000147 assert(Command->getNumArgs() == 0);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000148
149 if (!Command->isDirectionExplicit()) {
150 // User didn't provide a direction argument.
151 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
152 }
153 typedef BlockCommandComment::Argument Argument;
154 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
155 ArgLocEnd),
156 Arg);
157 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000158}
159
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000160void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
161 ParagraphComment *Paragraph) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000162 Command->setParagraph(Paragraph);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000163 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000164}
165
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000166TParamCommandComment *Sema::actOnTParamCommandStart(SourceLocation LocBegin,
167 SourceLocation LocEnd,
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000168 unsigned CommandID,
169 bool AtCommand) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000170 TParamCommandComment *Command =
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000171 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID, AtCommand);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000172
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000173 if (!isTemplateOrSpecialization())
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000174 Diag(Command->getLocation(),
175 diag::warn_doc_tparam_not_attached_to_a_template_decl)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000176 << Command->getCommandNameRange(Traits);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000177
178 return Command;
179}
180
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000181void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
182 SourceLocation ArgLocBegin,
183 SourceLocation ArgLocEnd,
184 StringRef Arg) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000185 // Parser will not feed us more arguments than needed.
186 assert(Command->getNumArgs() == 0);
187
188 typedef BlockCommandComment::Argument Argument;
189 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
190 ArgLocEnd),
191 Arg);
192 Command->setArgs(llvm::makeArrayRef(A, 1));
193
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000194 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000195 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000196 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000197 }
198
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000199 const TemplateParameterList *TemplateParameters =
200 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000201 SmallVector<unsigned, 2> Position;
202 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
203 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
204 llvm::StringMap<TParamCommandComment *>::iterator PrevCommandIt =
205 TemplateParameterDocs.find(Arg);
206 if (PrevCommandIt != TemplateParameterDocs.end()) {
207 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
208 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
209 << Arg << ArgRange;
210 TParamCommandComment *PrevCommand = PrevCommandIt->second;
211 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
212 << PrevCommand->getParamNameRange();
213 }
214 TemplateParameterDocs[Arg] = Command;
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000215 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000216 }
217
218 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
219 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
220 << Arg << ArgRange;
221
222 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000223 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000224
225 StringRef CorrectedName;
226 if (TemplateParameters->size() == 1) {
227 const NamedDecl *Param = TemplateParameters->getParam(0);
228 const IdentifierInfo *II = Param->getIdentifier();
229 if (II)
230 CorrectedName = II->getName();
231 } else {
232 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
233 }
234
235 if (!CorrectedName.empty()) {
236 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
237 << CorrectedName
238 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
239 }
240
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000241 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000242}
243
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000244void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
245 ParagraphComment *Paragraph) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000246 Command->setParagraph(Paragraph);
247 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000248}
249
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000250InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
251 SourceLocation CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000252 unsigned CommandID) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000253 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000254 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000255 return new (Allocator) InlineCommandComment(
256 CommandLocBegin,
257 CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000258 CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000259 getInlineCommandRenderKind(CommandName),
260 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000261}
262
263InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
264 SourceLocation CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000265 unsigned CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000266 SourceLocation ArgLocBegin,
267 SourceLocation ArgLocEnd,
268 StringRef Arg) {
269 typedef InlineCommandComment::Argument Argument;
270 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
271 ArgLocEnd),
272 Arg);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000273 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000274
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000275 return new (Allocator) InlineCommandComment(
276 CommandLocBegin,
277 CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000278 CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000279 getInlineCommandRenderKind(CommandName),
280 llvm::makeArrayRef(A, 1));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000281}
282
283InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
284 SourceLocation LocEnd,
Dmitri Gribenkob0b8a962012-09-11 19:22:03 +0000285 StringRef CommandName) {
286 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
287 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
288}
289
290InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
291 SourceLocation LocEnd,
292 unsigned CommandID) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000293 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000294 return new (Allocator) InlineCommandComment(
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000295 LocBegin, LocEnd, CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000296 InlineCommandComment::RenderNormal,
297 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000298}
299
300TextComment *Sema::actOnText(SourceLocation LocBegin,
301 SourceLocation LocEnd,
302 StringRef Text) {
303 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
304}
305
306VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000307 unsigned CommandID) {
308 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000309 return new (Allocator) VerbatimBlockComment(
310 Loc,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000311 Loc.getLocWithOffset(1 + CommandName.size()),
312 CommandID);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000313}
314
315VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
316 StringRef Text) {
317 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
318}
319
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000320void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000321 VerbatimBlockComment *Block,
322 SourceLocation CloseNameLocBegin,
323 StringRef CloseName,
324 ArrayRef<VerbatimBlockLineComment *> Lines) {
325 Block->setCloseName(CloseName, CloseNameLocBegin);
326 Block->setLines(Lines);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000327}
328
329VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000330 unsigned CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000331 SourceLocation TextBegin,
332 StringRef Text) {
333 return new (Allocator) VerbatimLineComment(
334 LocBegin,
335 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000336 CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000337 TextBegin,
338 Text);
339}
340
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000341HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
342 StringRef TagName) {
343 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000344}
345
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000346void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000347 HTMLStartTagComment *Tag,
348 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000349 SourceLocation GreaterLoc,
350 bool IsSelfClosing) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000351 Tag->setAttrs(Attrs);
352 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000353 if (IsSelfClosing)
354 Tag->setSelfClosing();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000355 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000356 HTMLOpenTags.push_back(Tag);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000357}
358
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000359HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
360 SourceLocation LocEnd,
361 StringRef TagName) {
362 HTMLEndTagComment *HET =
363 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
364 if (isHTMLEndTagForbidden(TagName)) {
365 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
366 << TagName << HET->getSourceRange();
367 return HET;
Dmitri Gribenko3d986982012-07-12 23:37:09 +0000368 }
369
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000370 bool FoundOpen = false;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000371 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000372 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
373 I != E; ++I) {
374 if ((*I)->getTagName() == TagName) {
375 FoundOpen = true;
376 break;
377 }
378 }
379 if (!FoundOpen) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000380 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
381 << HET->getSourceRange();
382 return HET;
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000383 }
384
385 while (!HTMLOpenTags.empty()) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000386 const HTMLStartTagComment *HST = HTMLOpenTags.back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000387 HTMLOpenTags.pop_back();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000388 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000389 if (LastNotClosedTagName == TagName)
390 break;
391
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000392 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000393 continue;
394
395 bool OpenLineInvalid;
396 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000397 HST->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000398 &OpenLineInvalid);
399 bool CloseLineInvalid;
400 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000401 HET->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000402 &CloseLineInvalid);
403
404 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000405 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
406 << HST->getTagName() << HET->getTagName()
407 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000408 else {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000409 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
410 << HST->getTagName() << HET->getTagName()
411 << HST->getSourceRange();
412 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
413 << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000414 }
415 }
416
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000417 return HET;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000418}
419
420FullComment *Sema::actOnFullComment(
421 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian749ace62012-10-11 23:52:50 +0000422 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000423 resolveParamCommandIndexes(FC);
424 return FC;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000425}
426
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000427void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkoabcf0dc2012-09-13 20:36:01 +0000428 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
429 return;
430
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000431 ParagraphComment *Paragraph = Command->getParagraph();
432 if (Paragraph->isWhitespace()) {
433 SourceLocation DiagLoc;
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000434 if (Command->getNumArgs() > 0)
435 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000436 if (!DiagLoc.isValid())
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000437 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000438 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000439 << Command->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000440 << Command->getCommandName(Traits)
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000441 << Command->getSourceRange();
442 }
443}
444
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000445void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000446 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000447 return;
448 if (isFunctionDecl()) {
449 if (ThisDeclInfo->ResultType->isVoidType()) {
450 unsigned DiagKind;
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000451 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000452 default:
Dmitri Gribenko88815f32012-08-06 16:29:26 +0000453 if (ThisDeclInfo->IsObjCMethod)
454 DiagKind = 3;
455 else
456 DiagKind = 0;
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000457 break;
458 case Decl::CXXConstructor:
459 DiagKind = 1;
460 break;
461 case Decl::CXXDestructor:
462 DiagKind = 2;
463 break;
464 }
465 Diag(Command->getLocation(),
466 diag::warn_doc_returns_attached_to_a_void_function)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000467 << Command->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000468 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000469 << DiagKind
470 << Command->getSourceRange();
471 }
472 return;
473 }
Fariborz Jahanian664e8602013-02-27 00:46:06 +0000474 else if (isObjCPropertyDecl())
475 return;
476
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000477 Diag(Command->getLocation(),
478 diag::warn_doc_returns_not_attached_to_a_function_decl)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000479 << Command->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000480 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000481 << Command->getSourceRange();
482}
483
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000484void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000485 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000486 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000487 if (Info->IsBriefCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000488 if (!BriefCommand) {
489 BriefCommand = Command;
490 return;
491 }
492 PrevCommand = BriefCommand;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000493 } else if (Info->IsReturnsCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000494 if (!ReturnsCommand) {
495 ReturnsCommand = Command;
496 return;
497 }
498 PrevCommand = ReturnsCommand;
Fariborz Jahanianf843a582013-01-31 23:12:39 +0000499 } else if (Info->IsHeaderfileCommand) {
500 if (!HeaderfileCommand) {
501 HeaderfileCommand = Command;
502 return;
503 }
504 PrevCommand = HeaderfileCommand;
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000505 } else {
506 // We don't want to check this command for duplicates.
507 return;
508 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000509 StringRef CommandName = Command->getCommandName(Traits);
510 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000511 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000512 << Command->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000513 << CommandName
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000514 << Command->getSourceRange();
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000515 if (CommandName == PrevCommandName)
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000516 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000517 << PrevCommand->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000518 << PrevCommandName
519 << PrevCommand->getSourceRange();
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000520 else
521 Diag(PrevCommand->getLocation(),
522 diag::note_doc_block_command_previous_alias)
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000523 << PrevCommand->getAtCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000524 << PrevCommandName
525 << CommandName;
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000526}
527
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000528void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
529 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
530 return;
531
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000532 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000533 if (!D)
534 return;
535
536 if (D->hasAttr<DeprecatedAttr>() ||
537 D->hasAttr<AvailabilityAttr>() ||
538 D->hasAttr<UnavailableAttr>())
539 return;
540
541 Diag(Command->getLocation(),
542 diag::warn_doc_deprecated_not_sync)
543 << Command->getSourceRange();
544
545 // Try to emit a fixit with a deprecation attribute.
546 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
547 // Don't emit a Fix-It for non-member function definitions. GCC does not
548 // accept attributes on them.
549 const DeclContext *Ctx = FD->getDeclContext();
550 if ((!Ctx || !Ctx->isRecord()) &&
551 FD->doesThisDeclarationHaveABody())
552 return;
553
Dmitri Gribenko19523542012-09-29 11:40:46 +0000554 StringRef AttributeSpelling = "__attribute__((deprecated))";
555 if (PP) {
556 TokenValue Tokens[] = {
557 tok::kw___attribute, tok::l_paren, tok::l_paren,
558 PP->getIdentifierInfo("deprecated"),
559 tok::r_paren, tok::r_paren
560 };
561 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
562 Tokens);
563 if (!MacroName.empty())
564 AttributeSpelling = MacroName;
565 }
566
567 SmallString<64> TextToInsert(" ");
568 TextToInsert += AttributeSpelling;
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000569 Diag(FD->getLocEnd(),
570 diag::note_add_deprecation_attr)
571 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko19523542012-09-29 11:40:46 +0000572 TextToInsert);
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000573 }
574}
575
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000576void Sema::resolveParamCommandIndexes(const FullComment *FC) {
577 if (!isFunctionDecl()) {
578 // We already warned that \\param commands are not attached to a function
579 // decl.
580 return;
581 }
582
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000583 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000584
585 // Comment AST nodes that correspond to \c ParamVars for which we have
586 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000587 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000588
589 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
590 ParamVarDocs.resize(ParamVars.size(), NULL);
591
592 // First pass over all \\param commands: resolve all parameter names.
593 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
594 I != E; ++I) {
595 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
596 if (!PCC || !PCC->hasParamName())
597 continue;
Fariborz Jahanian262e60c2012-10-18 21:42:42 +0000598 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000599
600 // Check that referenced parameter name is in the function decl.
601 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
602 ParamVars);
603 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
604 UnresolvedParamCommands.push_back(PCC);
605 continue;
606 }
607 PCC->setParamIndex(ResolvedParamIndex);
608 if (ParamVarDocs[ResolvedParamIndex]) {
609 SourceRange ArgRange = PCC->getParamNameRange();
610 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
611 << ParamName << ArgRange;
612 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
613 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
614 << PrevCommand->getParamNameRange();
615 }
616 ParamVarDocs[ResolvedParamIndex] = PCC;
617 }
618
619 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000620 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000621 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
622 if (!ParamVarDocs[i])
623 OrphanedParamDecls.push_back(ParamVars[i]);
624 }
625
626 // Second pass over unresolved \\param commands: do typo correction.
627 // Suggest corrections from a set of parameter declarations that have no
628 // corresponding \\param.
629 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
630 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
631
632 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian262e60c2012-10-18 21:42:42 +0000633 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000634 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
635 << ParamName << ArgRange;
636
637 // All parameters documented -- can't suggest a correction.
638 if (OrphanedParamDecls.size() == 0)
639 continue;
640
641 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
642 if (OrphanedParamDecls.size() == 1) {
643 // If one parameter is not documented then that parameter is the only
644 // possible suggestion.
645 CorrectedParamIndex = 0;
646 } else {
647 // Do typo correction.
648 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
649 OrphanedParamDecls);
650 }
651 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
652 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
653 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
654 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
655 << CorrectedII->getName()
656 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
657 }
658 }
659}
660
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000661bool Sema::isFunctionDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000662 if (!ThisDeclInfo)
663 return false;
664 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000665 inspectThisDecl();
Dmitri Gribenkoaf19a6a2012-08-02 21:45:39 +0000666 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000667}
Fariborz Jahanian664e8602013-02-27 00:46:06 +0000668
669bool Sema::isObjCPropertyDecl() {
670 if (!ThisDeclInfo)
671 return false;
672 if (!ThisDeclInfo->IsFilled)
673 inspectThisDecl();
674 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
675}
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000676
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000677bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000678 if (!ThisDeclInfo)
679 return false;
680 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000681 inspectThisDecl();
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000682 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000683}
684
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000685ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000686 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000687 inspectThisDecl();
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000688 return ThisDeclInfo->ParamVars;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000689}
690
691void Sema::inspectThisDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000692 ThisDeclInfo->fill();
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000693}
694
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000695unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000696 ArrayRef<const ParmVarDecl *> ParamVars) {
697 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000698 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
699 if (II && II->getName() == Name)
700 return i;
701 }
702 return ParamCommandComment::InvalidParamIndex;
703}
704
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000705namespace {
706class SimpleTypoCorrector {
707 StringRef Typo;
708 const unsigned MaxEditDistance;
709
710 const NamedDecl *BestDecl;
711 unsigned BestEditDistance;
712 unsigned BestIndex;
713 unsigned NextIndex;
714
715public:
716 SimpleTypoCorrector(StringRef Typo) :
717 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
718 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
719 BestIndex(0), NextIndex(0)
720 { }
721
722 void addDecl(const NamedDecl *ND);
723
724 const NamedDecl *getBestDecl() const {
725 if (BestEditDistance > MaxEditDistance)
726 return NULL;
727
728 return BestDecl;
729 }
730
731 unsigned getBestDeclIndex() const {
732 assert(getBestDecl());
733 return BestIndex;
734 }
735};
736
737void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
738 unsigned CurrIndex = NextIndex++;
739
740 const IdentifierInfo *II = ND->getIdentifier();
741 if (!II)
742 return;
743
744 StringRef Name = II->getName();
745 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
746 if (MinPossibleEditDistance > 0 &&
747 Typo.size() / MinPossibleEditDistance < 3)
748 return;
749
750 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
751 if (EditDistance < BestEditDistance) {
752 BestEditDistance = EditDistance;
753 BestDecl = ND;
754 BestIndex = CurrIndex;
755 }
756}
757} // unnamed namespace
758
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000759unsigned Sema::correctTypoInParmVarReference(
760 StringRef Typo,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000761 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000762 SimpleTypoCorrector Corrector(Typo);
763 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
764 Corrector.addDecl(ParamVars[i]);
765 if (Corrector.getBestDecl())
766 return Corrector.getBestDeclIndex();
767 else
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000768 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000769}
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000770
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000771namespace {
772bool ResolveTParamReferenceHelper(
773 StringRef Name,
774 const TemplateParameterList *TemplateParameters,
775 SmallVectorImpl<unsigned> *Position) {
776 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
777 const NamedDecl *Param = TemplateParameters->getParam(i);
778 const IdentifierInfo *II = Param->getIdentifier();
779 if (II && II->getName() == Name) {
780 Position->push_back(i);
781 return true;
782 }
783
784 if (const TemplateTemplateParmDecl *TTP =
785 dyn_cast<TemplateTemplateParmDecl>(Param)) {
786 Position->push_back(i);
787 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
788 Position))
789 return true;
790 Position->pop_back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000791 }
792 }
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000793 return false;
794}
795} // unnamed namespace
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000796
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000797bool Sema::resolveTParamReference(
798 StringRef Name,
799 const TemplateParameterList *TemplateParameters,
800 SmallVectorImpl<unsigned> *Position) {
801 Position->clear();
802 if (!TemplateParameters)
803 return false;
804
805 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
806}
807
808namespace {
809void CorrectTypoInTParamReferenceHelper(
810 const TemplateParameterList *TemplateParameters,
811 SimpleTypoCorrector &Corrector) {
812 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
813 const NamedDecl *Param = TemplateParameters->getParam(i);
814 Corrector.addDecl(Param);
815
816 if (const TemplateTemplateParmDecl *TTP =
817 dyn_cast<TemplateTemplateParmDecl>(Param))
818 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
819 Corrector);
820 }
821}
822} // unnamed namespace
823
824StringRef Sema::correctTypoInTParamReference(
825 StringRef Typo,
826 const TemplateParameterList *TemplateParameters) {
827 SimpleTypoCorrector Corrector(Typo);
828 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
829 if (const NamedDecl *ND = Corrector.getBestDecl()) {
830 const IdentifierInfo *II = ND->getIdentifier();
831 assert(II && "SimpleTypoCorrector should not return this decl");
832 return II->getName();
833 }
834 return StringRef();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000835}
836
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000837InlineCommandComment::RenderKind
838Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000839 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000840
841 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
842 .Case("b", InlineCommandComment::RenderBold)
843 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
844 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
845 .Default(InlineCommandComment::RenderNormal);
846}
847
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000848} // end namespace comments
849} // end namespace clang
850