blob: 322713880751af123deb7aba57a5d7cfc69b0f64 [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,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000052 unsigned CommandID) {
53 return new (Allocator) BlockCommandComment(LocBegin, LocEnd, CommandID);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000054}
55
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +000056void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
57 ArrayRef<BlockCommandComment::Argument> Args) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000058 Command->setArgs(Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000059}
60
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +000061void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
62 ParagraphComment *Paragraph) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000063 Command->setParagraph(Paragraph);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000064 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko9443c572012-08-06 17:08:27 +000065 checkBlockCommandDuplicate(Command);
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +000066 checkReturnsCommand(Command);
Dmitri Gribenko0bd98382012-09-22 21:47:50 +000067 checkDeprecatedCommand(Command);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000068}
69
70ParamCommandComment *Sema::actOnParamCommandStart(SourceLocation LocBegin,
71 SourceLocation LocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000072 unsigned CommandID) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000073 ParamCommandComment *Command =
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000074 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000075
Dmitri Gribenko8487c522012-07-23 17:40:30 +000076 if (!isFunctionDecl())
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000077 Diag(Command->getLocation(),
78 diag::warn_doc_param_not_attached_to_a_function_decl)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000079 << Command->getCommandNameRange(Traits);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000080
81 return Command;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000082}
83
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +000084void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
85 SourceLocation ArgLocBegin,
86 SourceLocation ArgLocEnd,
87 StringRef Arg) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +000088 ParamCommandComment::PassDirection Direction;
89 std::string ArgLower = Arg.lower();
90 // TODO: optimize: lower Name first (need an API in SmallString for that),
91 // after that StringSwitch.
92 if (ArgLower == "[in]")
93 Direction = ParamCommandComment::In;
94 else if (ArgLower == "[out]")
95 Direction = ParamCommandComment::Out;
96 else if (ArgLower == "[in,out]" || ArgLower == "[out,in]")
97 Direction = ParamCommandComment::InOut;
98 else {
99 // Remove spaces.
100 std::string::iterator O = ArgLower.begin();
101 for (std::string::iterator I = ArgLower.begin(), E = ArgLower.end();
102 I != E; ++I) {
103 const char C = *I;
104 if (C != ' ' && C != '\n' && C != '\r' &&
105 C != '\t' && C != '\v' && C != '\f')
106 *O++ = C;
107 }
108 ArgLower.resize(O - ArgLower.begin());
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000109
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000110 bool RemovingWhitespaceHelped = false;
111 if (ArgLower == "[in]") {
112 Direction = ParamCommandComment::In;
113 RemovingWhitespaceHelped = true;
114 } else if (ArgLower == "[out]") {
115 Direction = ParamCommandComment::Out;
116 RemovingWhitespaceHelped = true;
117 } else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") {
118 Direction = ParamCommandComment::InOut;
119 RemovingWhitespaceHelped = true;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000120 } else {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000121 Direction = ParamCommandComment::In;
122 RemovingWhitespaceHelped = false;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000123 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000124
125 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
126 if (RemovingWhitespaceHelped)
127 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
128 << ArgRange
129 << FixItHint::CreateReplacement(
130 ArgRange,
131 ParamCommandComment::getDirectionAsString(Direction));
132 else
133 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction)
134 << ArgRange;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000135 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000136 Command->setDirection(Direction, /* Explicit = */ true);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000137}
138
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000139void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
140 SourceLocation ArgLocBegin,
141 SourceLocation ArgLocEnd,
142 StringRef Arg) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000143 // Parser will not feed us more arguments than needed.
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000144 assert(Command->getNumArgs() == 0);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000145
146 if (!Command->isDirectionExplicit()) {
147 // User didn't provide a direction argument.
148 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
149 }
150 typedef BlockCommandComment::Argument Argument;
151 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
152 ArgLocEnd),
153 Arg);
154 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000155}
156
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000157void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
158 ParagraphComment *Paragraph) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000159 Command->setParagraph(Paragraph);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000160 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000161}
162
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000163TParamCommandComment *Sema::actOnTParamCommandStart(SourceLocation LocBegin,
164 SourceLocation LocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000165 unsigned CommandID) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000166 TParamCommandComment *Command =
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000167 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000168
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000169 if (!isTemplateOrSpecialization())
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000170 Diag(Command->getLocation(),
171 diag::warn_doc_tparam_not_attached_to_a_template_decl)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000172 << Command->getCommandNameRange(Traits);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000173
174 return Command;
175}
176
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000177void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
178 SourceLocation ArgLocBegin,
179 SourceLocation ArgLocEnd,
180 StringRef Arg) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000181 // Parser will not feed us more arguments than needed.
182 assert(Command->getNumArgs() == 0);
183
184 typedef BlockCommandComment::Argument Argument;
185 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
186 ArgLocEnd),
187 Arg);
188 Command->setArgs(llvm::makeArrayRef(A, 1));
189
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000190 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000191 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000192 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000193 }
194
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000195 const TemplateParameterList *TemplateParameters =
196 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000197 SmallVector<unsigned, 2> Position;
198 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
199 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
200 llvm::StringMap<TParamCommandComment *>::iterator PrevCommandIt =
201 TemplateParameterDocs.find(Arg);
202 if (PrevCommandIt != TemplateParameterDocs.end()) {
203 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
204 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
205 << Arg << ArgRange;
206 TParamCommandComment *PrevCommand = PrevCommandIt->second;
207 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
208 << PrevCommand->getParamNameRange();
209 }
210 TemplateParameterDocs[Arg] = Command;
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000211 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000212 }
213
214 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
215 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
216 << Arg << ArgRange;
217
218 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000219 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000220
221 StringRef CorrectedName;
222 if (TemplateParameters->size() == 1) {
223 const NamedDecl *Param = TemplateParameters->getParam(0);
224 const IdentifierInfo *II = Param->getIdentifier();
225 if (II)
226 CorrectedName = II->getName();
227 } else {
228 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
229 }
230
231 if (!CorrectedName.empty()) {
232 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
233 << CorrectedName
234 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
235 }
236
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000237 return;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000238}
239
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000240void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
241 ParagraphComment *Paragraph) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000242 Command->setParagraph(Paragraph);
243 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000244}
245
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000246InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
247 SourceLocation CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000248 unsigned CommandID) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000249 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000250 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000251 return new (Allocator) InlineCommandComment(
252 CommandLocBegin,
253 CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000254 CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000255 getInlineCommandRenderKind(CommandName),
256 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000257}
258
259InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
260 SourceLocation CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000261 unsigned CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000262 SourceLocation ArgLocBegin,
263 SourceLocation ArgLocEnd,
264 StringRef Arg) {
265 typedef InlineCommandComment::Argument Argument;
266 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
267 ArgLocEnd),
268 Arg);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000269 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000270
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000271 return new (Allocator) InlineCommandComment(
272 CommandLocBegin,
273 CommandLocEnd,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000274 CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000275 getInlineCommandRenderKind(CommandName),
276 llvm::makeArrayRef(A, 1));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000277}
278
279InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
280 SourceLocation LocEnd,
Dmitri Gribenkob0b8a962012-09-11 19:22:03 +0000281 StringRef CommandName) {
282 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
283 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
284}
285
286InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
287 SourceLocation LocEnd,
288 unsigned CommandID) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000289 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000290 return new (Allocator) InlineCommandComment(
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000291 LocBegin, LocEnd, CommandID,
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000292 InlineCommandComment::RenderNormal,
293 Args);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000294}
295
296TextComment *Sema::actOnText(SourceLocation LocBegin,
297 SourceLocation LocEnd,
298 StringRef Text) {
299 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
300}
301
302VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000303 unsigned CommandID) {
304 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000305 return new (Allocator) VerbatimBlockComment(
306 Loc,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000307 Loc.getLocWithOffset(1 + CommandName.size()),
308 CommandID);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000309}
310
311VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
312 StringRef Text) {
313 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
314}
315
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000316void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000317 VerbatimBlockComment *Block,
318 SourceLocation CloseNameLocBegin,
319 StringRef CloseName,
320 ArrayRef<VerbatimBlockLineComment *> Lines) {
321 Block->setCloseName(CloseName, CloseNameLocBegin);
322 Block->setLines(Lines);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000323}
324
325VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000326 unsigned CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000327 SourceLocation TextBegin,
328 StringRef Text) {
329 return new (Allocator) VerbatimLineComment(
330 LocBegin,
331 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000332 CommandID,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000333 TextBegin,
334 Text);
335}
336
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000337HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
338 StringRef TagName) {
339 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000340}
341
Dmitri Gribenko7d9b5112012-08-06 19:03:12 +0000342void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000343 HTMLStartTagComment *Tag,
344 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000345 SourceLocation GreaterLoc,
346 bool IsSelfClosing) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000347 Tag->setAttrs(Attrs);
348 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000349 if (IsSelfClosing)
350 Tag->setSelfClosing();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000351 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000352 HTMLOpenTags.push_back(Tag);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000353}
354
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000355HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
356 SourceLocation LocEnd,
357 StringRef TagName) {
358 HTMLEndTagComment *HET =
359 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
360 if (isHTMLEndTagForbidden(TagName)) {
361 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
362 << TagName << HET->getSourceRange();
363 return HET;
Dmitri Gribenko3d986982012-07-12 23:37:09 +0000364 }
365
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000366 bool FoundOpen = false;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000367 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000368 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
369 I != E; ++I) {
370 if ((*I)->getTagName() == TagName) {
371 FoundOpen = true;
372 break;
373 }
374 }
375 if (!FoundOpen) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000376 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
377 << HET->getSourceRange();
378 return HET;
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000379 }
380
381 while (!HTMLOpenTags.empty()) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000382 const HTMLStartTagComment *HST = HTMLOpenTags.back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000383 HTMLOpenTags.pop_back();
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000384 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000385 if (LastNotClosedTagName == TagName)
386 break;
387
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000388 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000389 continue;
390
391 bool OpenLineInvalid;
392 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000393 HST->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000394 &OpenLineInvalid);
395 bool CloseLineInvalid;
396 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000397 HET->getLocation(),
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000398 &CloseLineInvalid);
399
400 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000401 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
402 << HST->getTagName() << HET->getTagName()
403 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000404 else {
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();
408 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
409 << HET->getSourceRange();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000410 }
411 }
412
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000413 return HET;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000414}
415
416FullComment *Sema::actOnFullComment(
417 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian749ace62012-10-11 23:52:50 +0000418 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000419 resolveParamCommandIndexes(FC);
420 return FC;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000421}
422
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000423void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkoabcf0dc2012-09-13 20:36:01 +0000424 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
425 return;
426
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000427 ParagraphComment *Paragraph = Command->getParagraph();
428 if (Paragraph->isWhitespace()) {
429 SourceLocation DiagLoc;
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000430 if (Command->getNumArgs() > 0)
431 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000432 if (!DiagLoc.isValid())
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000433 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000434 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Fariborz Jahanianc98e9132013-03-01 22:51:30 +0000435 << Command->getHDCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000436 << Command->getCommandName(Traits)
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000437 << Command->getSourceRange();
438 }
439}
440
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000441void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000442 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000443 return;
444 if (isFunctionDecl()) {
445 if (ThisDeclInfo->ResultType->isVoidType()) {
446 unsigned DiagKind;
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000447 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000448 default:
Dmitri Gribenko88815f32012-08-06 16:29:26 +0000449 if (ThisDeclInfo->IsObjCMethod)
450 DiagKind = 3;
451 else
452 DiagKind = 0;
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000453 break;
454 case Decl::CXXConstructor:
455 DiagKind = 1;
456 break;
457 case Decl::CXXDestructor:
458 DiagKind = 2;
459 break;
460 }
461 Diag(Command->getLocation(),
462 diag::warn_doc_returns_attached_to_a_void_function)
Fariborz Jahanianc98e9132013-03-01 22:51:30 +0000463 << Command->getHDCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000464 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000465 << DiagKind
466 << Command->getSourceRange();
467 }
468 return;
469 }
Fariborz Jahanian664e8602013-02-27 00:46:06 +0000470 else if (isObjCPropertyDecl())
471 return;
472
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000473 Diag(Command->getLocation(),
474 diag::warn_doc_returns_not_attached_to_a_function_decl)
Fariborz Jahanianc98e9132013-03-01 22:51:30 +0000475 << Command->getHDCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000476 << Command->getCommandName(Traits)
Dmitri Gribenko89ab7d02012-08-03 21:15:32 +0000477 << Command->getSourceRange();
478}
479
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000480void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000481 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000482 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000483 if (Info->IsBriefCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000484 if (!BriefCommand) {
485 BriefCommand = Command;
486 return;
487 }
488 PrevCommand = BriefCommand;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000489 } else if (Info->IsReturnsCommand) {
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000490 if (!ReturnsCommand) {
491 ReturnsCommand = Command;
492 return;
493 }
494 PrevCommand = ReturnsCommand;
Fariborz Jahanianf843a582013-01-31 23:12:39 +0000495 } else if (Info->IsHeaderfileCommand) {
496 if (!HeaderfileCommand) {
497 HeaderfileCommand = Command;
498 return;
499 }
500 PrevCommand = HeaderfileCommand;
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000501 } else {
502 // We don't want to check this command for duplicates.
503 return;
504 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000505 StringRef CommandName = Command->getCommandName(Traits);
506 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000507 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Fariborz Jahanianc98e9132013-03-01 22:51:30 +0000508 << Command->getHDCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000509 << CommandName
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000510 << Command->getSourceRange();
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000511 if (CommandName == PrevCommandName)
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000512 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Fariborz Jahanianc98e9132013-03-01 22:51:30 +0000513 << PrevCommand->getHDCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000514 << PrevCommandName
515 << PrevCommand->getSourceRange();
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000516 else
517 Diag(PrevCommand->getLocation(),
518 diag::note_doc_block_command_previous_alias)
Fariborz Jahanianc98e9132013-03-01 22:51:30 +0000519 << PrevCommand->getHDCommand()
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000520 << PrevCommandName
521 << CommandName;
Dmitri Gribenko9443c572012-08-06 17:08:27 +0000522}
523
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000524void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
525 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
526 return;
527
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000528 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000529 if (!D)
530 return;
531
532 if (D->hasAttr<DeprecatedAttr>() ||
533 D->hasAttr<AvailabilityAttr>() ||
534 D->hasAttr<UnavailableAttr>())
535 return;
536
537 Diag(Command->getLocation(),
538 diag::warn_doc_deprecated_not_sync)
539 << Command->getSourceRange();
540
541 // Try to emit a fixit with a deprecation attribute.
542 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
543 // Don't emit a Fix-It for non-member function definitions. GCC does not
544 // accept attributes on them.
545 const DeclContext *Ctx = FD->getDeclContext();
546 if ((!Ctx || !Ctx->isRecord()) &&
547 FD->doesThisDeclarationHaveABody())
548 return;
549
Dmitri Gribenko19523542012-09-29 11:40:46 +0000550 StringRef AttributeSpelling = "__attribute__((deprecated))";
551 if (PP) {
552 TokenValue Tokens[] = {
553 tok::kw___attribute, tok::l_paren, tok::l_paren,
554 PP->getIdentifierInfo("deprecated"),
555 tok::r_paren, tok::r_paren
556 };
557 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
558 Tokens);
559 if (!MacroName.empty())
560 AttributeSpelling = MacroName;
561 }
562
563 SmallString<64> TextToInsert(" ");
564 TextToInsert += AttributeSpelling;
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000565 Diag(FD->getLocEnd(),
566 diag::note_add_deprecation_attr)
567 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko19523542012-09-29 11:40:46 +0000568 TextToInsert);
Dmitri Gribenko0bd98382012-09-22 21:47:50 +0000569 }
570}
571
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000572void Sema::resolveParamCommandIndexes(const FullComment *FC) {
573 if (!isFunctionDecl()) {
574 // We already warned that \\param commands are not attached to a function
575 // decl.
576 return;
577 }
578
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000579 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000580
581 // Comment AST nodes that correspond to \c ParamVars for which we have
582 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000583 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000584
585 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
586 ParamVarDocs.resize(ParamVars.size(), NULL);
587
588 // First pass over all \\param commands: resolve all parameter names.
589 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
590 I != E; ++I) {
591 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
592 if (!PCC || !PCC->hasParamName())
593 continue;
Fariborz Jahanian262e60c2012-10-18 21:42:42 +0000594 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000595
596 // Check that referenced parameter name is in the function decl.
597 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
598 ParamVars);
599 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
600 UnresolvedParamCommands.push_back(PCC);
601 continue;
602 }
603 PCC->setParamIndex(ResolvedParamIndex);
604 if (ParamVarDocs[ResolvedParamIndex]) {
605 SourceRange ArgRange = PCC->getParamNameRange();
606 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
607 << ParamName << ArgRange;
608 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
609 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
610 << PrevCommand->getParamNameRange();
611 }
612 ParamVarDocs[ResolvedParamIndex] = PCC;
613 }
614
615 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000616 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000617 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
618 if (!ParamVarDocs[i])
619 OrphanedParamDecls.push_back(ParamVars[i]);
620 }
621
622 // Second pass over unresolved \\param commands: do typo correction.
623 // Suggest corrections from a set of parameter declarations that have no
624 // corresponding \\param.
625 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
626 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
627
628 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian262e60c2012-10-18 21:42:42 +0000629 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko9edd2c82012-08-24 17:45:39 +0000630 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
631 << ParamName << ArgRange;
632
633 // All parameters documented -- can't suggest a correction.
634 if (OrphanedParamDecls.size() == 0)
635 continue;
636
637 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
638 if (OrphanedParamDecls.size() == 1) {
639 // If one parameter is not documented then that parameter is the only
640 // possible suggestion.
641 CorrectedParamIndex = 0;
642 } else {
643 // Do typo correction.
644 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
645 OrphanedParamDecls);
646 }
647 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
648 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
649 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
650 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
651 << CorrectedII->getName()
652 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
653 }
654 }
655}
656
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000657bool Sema::isFunctionDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000658 if (!ThisDeclInfo)
659 return false;
660 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000661 inspectThisDecl();
Dmitri Gribenkoaf19a6a2012-08-02 21:45:39 +0000662 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000663}
Fariborz Jahanian664e8602013-02-27 00:46:06 +0000664
665bool Sema::isObjCPropertyDecl() {
666 if (!ThisDeclInfo)
667 return false;
668 if (!ThisDeclInfo->IsFilled)
669 inspectThisDecl();
670 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
671}
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000672
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000673bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000674 if (!ThisDeclInfo)
675 return false;
676 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000677 inspectThisDecl();
Dmitri Gribenko04bf29e2012-08-06 21:31:15 +0000678 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000679}
680
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000681ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000682 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko00c59f72012-07-24 20:58:46 +0000683 inspectThisDecl();
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000684 return ThisDeclInfo->ParamVars;
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000685}
686
687void Sema::inspectThisDecl() {
Dmitri Gribenko1ca7ecc2012-08-01 23:08:09 +0000688 ThisDeclInfo->fill();
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000689}
690
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000691unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000692 ArrayRef<const ParmVarDecl *> ParamVars) {
693 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000694 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
695 if (II && II->getName() == Name)
696 return i;
697 }
698 return ParamCommandComment::InvalidParamIndex;
699}
700
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000701namespace {
702class SimpleTypoCorrector {
703 StringRef Typo;
704 const unsigned MaxEditDistance;
705
706 const NamedDecl *BestDecl;
707 unsigned BestEditDistance;
708 unsigned BestIndex;
709 unsigned NextIndex;
710
711public:
712 SimpleTypoCorrector(StringRef Typo) :
713 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
714 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
715 BestIndex(0), NextIndex(0)
716 { }
717
718 void addDecl(const NamedDecl *ND);
719
720 const NamedDecl *getBestDecl() const {
721 if (BestEditDistance > MaxEditDistance)
722 return NULL;
723
724 return BestDecl;
725 }
726
727 unsigned getBestDeclIndex() const {
728 assert(getBestDecl());
729 return BestIndex;
730 }
731};
732
733void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
734 unsigned CurrIndex = NextIndex++;
735
736 const IdentifierInfo *II = ND->getIdentifier();
737 if (!II)
738 return;
739
740 StringRef Name = II->getName();
741 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
742 if (MinPossibleEditDistance > 0 &&
743 Typo.size() / MinPossibleEditDistance < 3)
744 return;
745
746 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
747 if (EditDistance < BestEditDistance) {
748 BestEditDistance = EditDistance;
749 BestDecl = ND;
750 BestIndex = CurrIndex;
751 }
752}
753} // unnamed namespace
754
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000755unsigned Sema::correctTypoInParmVarReference(
756 StringRef Typo,
Dmitri Gribenko8487c522012-07-23 17:40:30 +0000757 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000758 SimpleTypoCorrector Corrector(Typo);
759 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
760 Corrector.addDecl(ParamVars[i]);
761 if (Corrector.getBestDecl())
762 return Corrector.getBestDeclIndex();
763 else
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000764 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000765}
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000766
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000767namespace {
768bool ResolveTParamReferenceHelper(
769 StringRef Name,
770 const TemplateParameterList *TemplateParameters,
771 SmallVectorImpl<unsigned> *Position) {
772 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
773 const NamedDecl *Param = TemplateParameters->getParam(i);
774 const IdentifierInfo *II = Param->getIdentifier();
775 if (II && II->getName() == Name) {
776 Position->push_back(i);
777 return true;
778 }
779
780 if (const TemplateTemplateParmDecl *TTP =
781 dyn_cast<TemplateTemplateParmDecl>(Param)) {
782 Position->push_back(i);
783 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
784 Position))
785 return true;
786 Position->pop_back();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000787 }
788 }
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000789 return false;
790}
791} // unnamed namespace
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000792
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000793bool Sema::resolveTParamReference(
794 StringRef Name,
795 const TemplateParameterList *TemplateParameters,
796 SmallVectorImpl<unsigned> *Position) {
797 Position->clear();
798 if (!TemplateParameters)
799 return false;
800
801 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
802}
803
804namespace {
805void CorrectTypoInTParamReferenceHelper(
806 const TemplateParameterList *TemplateParameters,
807 SimpleTypoCorrector &Corrector) {
808 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
809 const NamedDecl *Param = TemplateParameters->getParam(i);
810 Corrector.addDecl(Param);
811
812 if (const TemplateTemplateParmDecl *TTP =
813 dyn_cast<TemplateTemplateParmDecl>(Param))
814 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
815 Corrector);
816 }
817}
818} // unnamed namespace
819
820StringRef Sema::correctTypoInTParamReference(
821 StringRef Typo,
822 const TemplateParameterList *TemplateParameters) {
823 SimpleTypoCorrector Corrector(Typo);
824 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
825 if (const NamedDecl *ND = Corrector.getBestDecl()) {
826 const IdentifierInfo *II = ND->getIdentifier();
827 assert(II && "SimpleTypoCorrector should not return this decl");
828 return II->getName();
829 }
830 return StringRef();
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000831}
832
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000833InlineCommandComment::RenderKind
834Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000835 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000836
837 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
838 .Case("b", InlineCommandComment::RenderBold)
839 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
840 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
841 .Default(InlineCommandComment::RenderNormal);
842}
843
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000844} // end namespace comments
845} // end namespace clang
846