blob: c242eb0f6067b70bb584e0acc184f22b62a7973f [file] [log] [blame]
Fariborz Jahanian43c8df82013-03-05 19:52:24 +00001//===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===//
Dmitri Gribenkoec925312012-07-06 00:28:32 +00002//
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 Kramerea70eb32012-12-01 15:09:41 +000011#include "clang/AST/Attr.h"
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000012#include "clang/AST/CommentCommandTraits.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000013#include "clang/AST/CommentDiagnostic.h"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000014#include "clang/AST/Decl.h"
Dmitri Gribenko34df2202012-07-31 22:37:06 +000015#include "clang/AST/DeclTemplate.h"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000016#include "clang/Basic/SourceManager.h"
Dmitri Gribenko6743e042012-09-29 11:40:46 +000017#include "clang/Lex/Preprocessor.h"
Dmitri Gribenko6743e042012-09-29 11:40:46 +000018#include "llvm/ADT/SmallString.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "llvm/ADT/StringSwitch.h"
Dmitri Gribenkoec925312012-07-06 00:28:32 +000020
21namespace clang {
22namespace comments {
23
Dmitri Gribenko3ca956f2012-08-31 02:21:44 +000024namespace {
25#include "clang/AST/CommentHTMLTagsProperties.inc"
26} // unnamed namespace
27
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000028Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
Dmitri Gribenko6743e042012-09-29 11:40:46 +000029 DiagnosticsEngine &Diags, CommandTraits &Traits,
30 const Preprocessor *PP) :
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000031 Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits),
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +000032 PP(PP), ThisDeclInfo(NULL), BriefCommand(NULL), ReturnsCommand(NULL),
33 HeaderfileCommand(NULL) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000034}
35
36void Sema::setDecl(const Decl *D) {
Dmitri Gribenko527ab212012-08-01 23:08:09 +000037 if (!D)
38 return;
39
40 ThisDeclInfo = new (Allocator) DeclInfo;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +000041 ThisDeclInfo->CommentDecl = D;
Dmitri Gribenkoe6213dd2012-08-01 23:21:57 +000042 ThisDeclInfo->IsFilled = false;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000043}
44
45ParagraphComment *Sema::actOnParagraphComment(
46 ArrayRef<InlineContentComment *> Content) {
47 return new (Allocator) ParagraphComment(Content);
48}
49
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000050BlockCommandComment *Sema::actOnBlockCommandStart(
51 SourceLocation LocBegin,
52 SourceLocation LocEnd,
53 unsigned CommandID,
54 CommandMarkerKind CommandMarker) {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +000055 BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd,
56 CommandID,
57 CommandMarker);
58 checkContainerDecl(BC);
59 return BC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000060}
61
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +000062void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
63 ArrayRef<BlockCommandComment::Argument> Args) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +000064 Command->setArgs(Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +000065}
66
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +000067void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
68 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +000069 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000070 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +000071 checkBlockCommandDuplicate(Command);
Dmitri Gribenko64305832012-08-03 21:15:32 +000072 checkReturnsCommand(Command);
Dmitri Gribenko1da88862012-09-22 21:47:50 +000073 checkDeprecatedCommand(Command);
Dmitri Gribenkoec925312012-07-06 00:28:32 +000074}
75
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000076ParamCommandComment *Sema::actOnParamCommandStart(
77 SourceLocation LocBegin,
78 SourceLocation LocEnd,
79 unsigned CommandID,
80 CommandMarkerKind CommandMarker) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000081 ParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000082 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
83 CommandMarker);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000084
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +000085 if (!isFunctionDecl())
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000086 Diag(Command->getLocation(),
87 diag::warn_doc_param_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000088 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000089 << Command->getCommandNameRange(Traits);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000090
91 return Command;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000092}
93
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +000094void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
95 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahanian56fe4062013-03-05 22:46:07 +000096 if (!Info->IsFunctionDeclarationCommand)
97 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +000098
99 unsigned DiagSelect;
100 switch (Comment->getCommandID()) {
101 case CommandTraits::KCI_function:
102 DiagSelect = !isAnyFunctionDecl() ? 1 : 0;
103 break;
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000104 case CommandTraits::KCI_functiongroup:
105 DiagSelect = !isAnyFunctionDecl() ? 2 : 0;
106 break;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000107 case CommandTraits::KCI_method:
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000108 DiagSelect = !isObjCMethodDecl() ? 3 : 0;
109 break;
110 case CommandTraits::KCI_methodgroup:
111 DiagSelect = !isObjCMethodDecl() ? 4 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000112 break;
113 case CommandTraits::KCI_callback:
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000114 DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000115 break;
116 default:
117 DiagSelect = 0;
118 break;
119 }
Fariborz Jahanian41bb7132013-03-06 17:36:51 +0000120 if (DiagSelect)
121 Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
122 << Comment->getCommandMarker()
123 << (DiagSelect-1) << (DiagSelect-1)
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000124 << Comment->getSourceRange();
125}
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000126
127void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
128 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000129 if (!Info->IsRecordLikeDeclarationCommand)
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000130 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000131 unsigned DiagSelect;
132 switch (Comment->getCommandID()) {
133 case CommandTraits::KCI_class:
134 DiagSelect = !isClassOrStructDecl() ? 1 : 0;
Fariborz Jahanian04eb8ab2013-05-20 23:40:39 +0000135 // Allow @class command on @interface declarations.
136 // FIXME. Currently, \class and @class are indistinguishable. So,
137 // \class is also allowed on an @interface declaration
138 if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
139 DiagSelect = 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000140 break;
141 case CommandTraits::KCI_interface:
142 DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
143 break;
144 case CommandTraits::KCI_protocol:
145 DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
146 break;
147 case CommandTraits::KCI_struct:
148 DiagSelect = !isClassOrStructDecl() ? 4 : 0;
149 break;
150 case CommandTraits::KCI_union:
151 DiagSelect = !isUnionDecl() ? 5 : 0;
152 break;
153 default:
154 DiagSelect = 0;
155 break;
156 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000157 if (DiagSelect)
158 Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
159 << Comment->getCommandMarker()
160 << (DiagSelect-1) << (DiagSelect-1)
161 << Comment->getSourceRange();
162}
163
164void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
165 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000166 if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000167 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000168 unsigned DiagSelect;
169 switch (Comment->getCommandID()) {
170 case CommandTraits::KCI_classdesign:
171 DiagSelect = 1;
172 break;
173 case CommandTraits::KCI_coclass:
174 DiagSelect = 2;
175 break;
176 case CommandTraits::KCI_dependency:
177 DiagSelect = 3;
178 break;
179 case CommandTraits::KCI_helper:
180 DiagSelect = 4;
181 break;
182 case CommandTraits::KCI_helperclass:
183 DiagSelect = 5;
184 break;
185 case CommandTraits::KCI_helps:
186 DiagSelect = 6;
187 break;
188 case CommandTraits::KCI_instancesize:
189 DiagSelect = 7;
190 break;
191 case CommandTraits::KCI_ownership:
192 DiagSelect = 8;
193 break;
194 case CommandTraits::KCI_performance:
195 DiagSelect = 9;
196 break;
197 case CommandTraits::KCI_security:
198 DiagSelect = 10;
199 break;
200 case CommandTraits::KCI_superclass:
201 DiagSelect = 11;
202 break;
203 default:
204 DiagSelect = 0;
205 break;
206 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000207 if (DiagSelect)
208 Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
209 << Comment->getCommandMarker()
210 << (DiagSelect-1)
211 << Comment->getSourceRange();
212}
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000213
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000214void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
215 SourceLocation ArgLocBegin,
216 SourceLocation ArgLocEnd,
217 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000218 ParamCommandComment::PassDirection Direction;
219 std::string ArgLower = Arg.lower();
220 // TODO: optimize: lower Name first (need an API in SmallString for that),
221 // after that StringSwitch.
222 if (ArgLower == "[in]")
223 Direction = ParamCommandComment::In;
224 else if (ArgLower == "[out]")
225 Direction = ParamCommandComment::Out;
226 else if (ArgLower == "[in,out]" || ArgLower == "[out,in]")
227 Direction = ParamCommandComment::InOut;
228 else {
229 // Remove spaces.
230 std::string::iterator O = ArgLower.begin();
231 for (std::string::iterator I = ArgLower.begin(), E = ArgLower.end();
232 I != E; ++I) {
233 const char C = *I;
234 if (C != ' ' && C != '\n' && C != '\r' &&
235 C != '\t' && C != '\v' && C != '\f')
236 *O++ = C;
237 }
238 ArgLower.resize(O - ArgLower.begin());
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000239
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000240 bool RemovingWhitespaceHelped = false;
241 if (ArgLower == "[in]") {
242 Direction = ParamCommandComment::In;
243 RemovingWhitespaceHelped = true;
244 } else if (ArgLower == "[out]") {
245 Direction = ParamCommandComment::Out;
246 RemovingWhitespaceHelped = true;
247 } else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") {
248 Direction = ParamCommandComment::InOut;
249 RemovingWhitespaceHelped = true;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000250 } else {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000251 Direction = ParamCommandComment::In;
252 RemovingWhitespaceHelped = false;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000253 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000254
255 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
256 if (RemovingWhitespaceHelped)
257 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
258 << ArgRange
259 << FixItHint::CreateReplacement(
260 ArgRange,
261 ParamCommandComment::getDirectionAsString(Direction));
262 else
263 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction)
264 << ArgRange;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000265 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000266 Command->setDirection(Direction, /* Explicit = */ true);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000267}
268
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000269void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
270 SourceLocation ArgLocBegin,
271 SourceLocation ArgLocEnd,
272 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000273 // Parser will not feed us more arguments than needed.
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000274 assert(Command->getNumArgs() == 0);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000275
276 if (!Command->isDirectionExplicit()) {
277 // User didn't provide a direction argument.
278 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
279 }
280 typedef BlockCommandComment::Argument Argument;
281 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
282 ArgLocEnd),
283 Arg);
284 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000285}
286
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000287void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
288 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000289 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000290 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000291}
292
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000293TParamCommandComment *Sema::actOnTParamCommandStart(
294 SourceLocation LocBegin,
295 SourceLocation LocEnd,
296 unsigned CommandID,
297 CommandMarkerKind CommandMarker) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000298 TParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000299 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
300 CommandMarker);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000301
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000302 if (!isTemplateOrSpecialization())
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000303 Diag(Command->getLocation(),
304 diag::warn_doc_tparam_not_attached_to_a_template_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000305 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000306 << Command->getCommandNameRange(Traits);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000307
308 return Command;
309}
310
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000311void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
312 SourceLocation ArgLocBegin,
313 SourceLocation ArgLocEnd,
314 StringRef Arg) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000315 // Parser will not feed us more arguments than needed.
316 assert(Command->getNumArgs() == 0);
317
318 typedef BlockCommandComment::Argument Argument;
319 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
320 ArgLocEnd),
321 Arg);
322 Command->setArgs(llvm::makeArrayRef(A, 1));
323
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000324 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000325 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000326 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000327 }
328
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000329 const TemplateParameterList *TemplateParameters =
330 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000331 SmallVector<unsigned, 2> Position;
332 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
333 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
334 llvm::StringMap<TParamCommandComment *>::iterator PrevCommandIt =
335 TemplateParameterDocs.find(Arg);
336 if (PrevCommandIt != TemplateParameterDocs.end()) {
337 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
338 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
339 << Arg << ArgRange;
340 TParamCommandComment *PrevCommand = PrevCommandIt->second;
341 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
342 << PrevCommand->getParamNameRange();
343 }
344 TemplateParameterDocs[Arg] = Command;
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000345 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000346 }
347
348 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
349 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
350 << Arg << ArgRange;
351
352 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000353 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000354
355 StringRef CorrectedName;
356 if (TemplateParameters->size() == 1) {
357 const NamedDecl *Param = TemplateParameters->getParam(0);
358 const IdentifierInfo *II = Param->getIdentifier();
359 if (II)
360 CorrectedName = II->getName();
361 } else {
362 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
363 }
364
365 if (!CorrectedName.empty()) {
366 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
367 << CorrectedName
368 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
369 }
370
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000371 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000372}
373
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000374void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
375 ParagraphComment *Paragraph) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000376 Command->setParagraph(Paragraph);
377 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000378}
379
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000380InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
381 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000382 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000383 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000384 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000385 return new (Allocator) InlineCommandComment(
386 CommandLocBegin,
387 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000388 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000389 getInlineCommandRenderKind(CommandName),
390 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000391}
392
393InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
394 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000395 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000396 SourceLocation ArgLocBegin,
397 SourceLocation ArgLocEnd,
398 StringRef Arg) {
399 typedef InlineCommandComment::Argument Argument;
400 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
401 ArgLocEnd),
402 Arg);
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000403 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000404
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000405 return new (Allocator) InlineCommandComment(
406 CommandLocBegin,
407 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000408 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000409 getInlineCommandRenderKind(CommandName),
410 llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000411}
412
413InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
414 SourceLocation LocEnd,
Dmitri Gribenko9304d862012-09-11 19:22:03 +0000415 StringRef CommandName) {
416 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
417 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
418}
419
420InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
421 SourceLocation LocEnd,
422 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000423 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000424 return new (Allocator) InlineCommandComment(
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000425 LocBegin, LocEnd, CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000426 InlineCommandComment::RenderNormal,
427 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000428}
429
430TextComment *Sema::actOnText(SourceLocation LocBegin,
431 SourceLocation LocEnd,
432 StringRef Text) {
433 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
434}
435
436VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000437 unsigned CommandID) {
438 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000439 return new (Allocator) VerbatimBlockComment(
440 Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000441 Loc.getLocWithOffset(1 + CommandName.size()),
442 CommandID);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000443}
444
445VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
446 StringRef Text) {
447 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
448}
449
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000450void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000451 VerbatimBlockComment *Block,
452 SourceLocation CloseNameLocBegin,
453 StringRef CloseName,
454 ArrayRef<VerbatimBlockLineComment *> Lines) {
455 Block->setCloseName(CloseName, CloseNameLocBegin);
456 Block->setLines(Lines);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000457}
458
459VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000460 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000461 SourceLocation TextBegin,
462 StringRef Text) {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000463 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000464 LocBegin,
465 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000466 CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000467 TextBegin,
468 Text);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000469 checkFunctionDeclVerbatimLine(VL);
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000470 checkContainerDeclVerbatimLine(VL);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000471 return VL;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000472}
473
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000474HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
475 StringRef TagName) {
476 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000477}
478
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000479void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000480 HTMLStartTagComment *Tag,
481 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000482 SourceLocation GreaterLoc,
483 bool IsSelfClosing) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000484 Tag->setAttrs(Attrs);
485 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000486 if (IsSelfClosing)
487 Tag->setSelfClosing();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000488 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000489 HTMLOpenTags.push_back(Tag);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000490}
491
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000492HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
493 SourceLocation LocEnd,
494 StringRef TagName) {
495 HTMLEndTagComment *HET =
496 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
497 if (isHTMLEndTagForbidden(TagName)) {
498 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
499 << TagName << HET->getSourceRange();
500 return HET;
Dmitri Gribenko9460fbf2012-07-12 23:37:09 +0000501 }
502
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000503 bool FoundOpen = false;
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000504 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000505 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
506 I != E; ++I) {
507 if ((*I)->getTagName() == TagName) {
508 FoundOpen = true;
509 break;
510 }
511 }
512 if (!FoundOpen) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000513 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
514 << HET->getSourceRange();
515 return HET;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000516 }
517
518 while (!HTMLOpenTags.empty()) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000519 const HTMLStartTagComment *HST = HTMLOpenTags.back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000520 HTMLOpenTags.pop_back();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000521 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000522 if (LastNotClosedTagName == TagName)
523 break;
524
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000525 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000526 continue;
527
528 bool OpenLineInvalid;
529 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000530 HST->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000531 &OpenLineInvalid);
532 bool CloseLineInvalid;
533 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000534 HET->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000535 &CloseLineInvalid);
536
537 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000538 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
539 << HST->getTagName() << HET->getTagName()
540 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000541 else {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000542 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
543 << HST->getTagName() << HET->getTagName()
544 << HST->getSourceRange();
545 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
546 << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000547 }
548 }
549
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000550 return HET;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000551}
552
553FullComment *Sema::actOnFullComment(
554 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian42e31322012-10-11 23:52:50 +0000555 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000556 resolveParamCommandIndexes(FC);
557 return FC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000558}
559
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000560void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkob37d5e82012-09-13 20:36:01 +0000561 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
562 return;
563
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000564 ParagraphComment *Paragraph = Command->getParagraph();
565 if (Paragraph->isWhitespace()) {
566 SourceLocation DiagLoc;
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000567 if (Command->getNumArgs() > 0)
568 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000569 if (!DiagLoc.isValid())
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000570 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000571 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000572 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000573 << Command->getCommandName(Traits)
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000574 << Command->getSourceRange();
575 }
576}
577
Dmitri Gribenko64305832012-08-03 21:15:32 +0000578void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000579 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000580 return;
581 if (isFunctionDecl()) {
582 if (ThisDeclInfo->ResultType->isVoidType()) {
583 unsigned DiagKind;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000584 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000585 default:
Dmitri Gribenko558babc2012-08-06 16:29:26 +0000586 if (ThisDeclInfo->IsObjCMethod)
587 DiagKind = 3;
588 else
589 DiagKind = 0;
Dmitri Gribenko64305832012-08-03 21:15:32 +0000590 break;
591 case Decl::CXXConstructor:
592 DiagKind = 1;
593 break;
594 case Decl::CXXDestructor:
595 DiagKind = 2;
596 break;
597 }
598 Diag(Command->getLocation(),
599 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000600 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000601 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000602 << DiagKind
603 << Command->getSourceRange();
604 }
605 return;
606 }
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000607 else if (isObjCPropertyDecl())
608 return;
609
Dmitri Gribenko64305832012-08-03 21:15:32 +0000610 Diag(Command->getLocation(),
611 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000612 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000613 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000614 << Command->getSourceRange();
615}
616
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000617void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000618 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000619 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000620 if (Info->IsBriefCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000621 if (!BriefCommand) {
622 BriefCommand = Command;
623 return;
624 }
625 PrevCommand = BriefCommand;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000626 } else if (Info->IsReturnsCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000627 if (!ReturnsCommand) {
628 ReturnsCommand = Command;
629 return;
630 }
631 PrevCommand = ReturnsCommand;
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +0000632 } else if (Info->IsHeaderfileCommand) {
633 if (!HeaderfileCommand) {
634 HeaderfileCommand = Command;
635 return;
636 }
637 PrevCommand = HeaderfileCommand;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000638 } else {
639 // We don't want to check this command for duplicates.
640 return;
641 }
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000642 StringRef CommandName = Command->getCommandName(Traits);
643 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000644 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000645 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000646 << CommandName
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000647 << Command->getSourceRange();
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000648 if (CommandName == PrevCommandName)
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000649 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000650 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000651 << PrevCommandName
652 << PrevCommand->getSourceRange();
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000653 else
654 Diag(PrevCommand->getLocation(),
655 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000656 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000657 << PrevCommandName
658 << CommandName;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000659}
660
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000661void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
662 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
663 return;
664
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000665 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000666 if (!D)
667 return;
668
669 if (D->hasAttr<DeprecatedAttr>() ||
670 D->hasAttr<AvailabilityAttr>() ||
671 D->hasAttr<UnavailableAttr>())
672 return;
673
674 Diag(Command->getLocation(),
675 diag::warn_doc_deprecated_not_sync)
676 << Command->getSourceRange();
677
678 // Try to emit a fixit with a deprecation attribute.
679 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
680 // Don't emit a Fix-It for non-member function definitions. GCC does not
681 // accept attributes on them.
682 const DeclContext *Ctx = FD->getDeclContext();
683 if ((!Ctx || !Ctx->isRecord()) &&
684 FD->doesThisDeclarationHaveABody())
685 return;
686
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000687 StringRef AttributeSpelling = "__attribute__((deprecated))";
688 if (PP) {
689 TokenValue Tokens[] = {
690 tok::kw___attribute, tok::l_paren, tok::l_paren,
691 PP->getIdentifierInfo("deprecated"),
692 tok::r_paren, tok::r_paren
693 };
694 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
695 Tokens);
696 if (!MacroName.empty())
697 AttributeSpelling = MacroName;
698 }
699
700 SmallString<64> TextToInsert(" ");
701 TextToInsert += AttributeSpelling;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000702 Diag(FD->getLocEnd(),
703 diag::note_add_deprecation_attr)
704 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000705 TextToInsert);
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000706 }
707}
708
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000709void Sema::resolveParamCommandIndexes(const FullComment *FC) {
710 if (!isFunctionDecl()) {
711 // We already warned that \\param commands are not attached to a function
712 // decl.
713 return;
714 }
715
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000716 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000717
718 // Comment AST nodes that correspond to \c ParamVars for which we have
719 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000720 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000721
722 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
723 ParamVarDocs.resize(ParamVars.size(), NULL);
724
725 // First pass over all \\param commands: resolve all parameter names.
726 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
727 I != E; ++I) {
728 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
729 if (!PCC || !PCC->hasParamName())
730 continue;
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000731 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000732
733 // Check that referenced parameter name is in the function decl.
734 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
735 ParamVars);
736 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
737 UnresolvedParamCommands.push_back(PCC);
738 continue;
739 }
740 PCC->setParamIndex(ResolvedParamIndex);
741 if (ParamVarDocs[ResolvedParamIndex]) {
742 SourceRange ArgRange = PCC->getParamNameRange();
743 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
744 << ParamName << ArgRange;
745 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
746 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
747 << PrevCommand->getParamNameRange();
748 }
749 ParamVarDocs[ResolvedParamIndex] = PCC;
750 }
751
752 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000753 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000754 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
755 if (!ParamVarDocs[i])
756 OrphanedParamDecls.push_back(ParamVars[i]);
757 }
758
759 // Second pass over unresolved \\param commands: do typo correction.
760 // Suggest corrections from a set of parameter declarations that have no
761 // corresponding \\param.
762 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
763 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
764
765 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000766 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000767 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
768 << ParamName << ArgRange;
769
770 // All parameters documented -- can't suggest a correction.
771 if (OrphanedParamDecls.size() == 0)
772 continue;
773
774 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
775 if (OrphanedParamDecls.size() == 1) {
776 // If one parameter is not documented then that parameter is the only
777 // possible suggestion.
778 CorrectedParamIndex = 0;
779 } else {
780 // Do typo correction.
781 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
782 OrphanedParamDecls);
783 }
784 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
785 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
786 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
787 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
788 << CorrectedII->getName()
789 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
790 }
791 }
792}
793
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000794bool Sema::isFunctionDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000795 if (!ThisDeclInfo)
796 return false;
797 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000798 inspectThisDecl();
Dmitri Gribenko37a7faf2012-08-02 21:45:39 +0000799 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000800}
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000801
Fariborz Jahaniana668bf52013-03-05 23:20:29 +0000802bool Sema::isAnyFunctionDecl() {
803 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
804 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
805}
806
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000807bool Sema::isObjCMethodDecl() {
808 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
809 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
810}
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000811
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000812/// isFunctionPointerVarDecl - returns 'true' if declaration is a pointer to
813/// function decl.
814bool Sema::isFunctionPointerVarDecl() {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000815 if (!ThisDeclInfo)
816 return false;
817 if (!ThisDeclInfo->IsFilled)
818 inspectThisDecl();
819 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
820 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
821 QualType QT = VD->getType();
822 return QT->isFunctionPointerType();
823 }
824 }
825 return false;
826}
827
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000828bool Sema::isObjCPropertyDecl() {
829 if (!ThisDeclInfo)
830 return false;
831 if (!ThisDeclInfo->IsFilled)
832 inspectThisDecl();
833 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
834}
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000835
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000836bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000837 if (!ThisDeclInfo)
838 return false;
839 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000840 inspectThisDecl();
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000841 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000842}
843
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000844bool Sema::isRecordLikeDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000845 if (!ThisDeclInfo)
846 return false;
847 if (!ThisDeclInfo->IsFilled)
848 inspectThisDecl();
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000849 return isUnionDecl() || isClassOrStructDecl()
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000850 || isObjCInterfaceDecl() || isObjCProtocolDecl();
851}
852
853bool Sema::isUnionDecl() {
854 if (!ThisDeclInfo)
855 return false;
856 if (!ThisDeclInfo->IsFilled)
857 inspectThisDecl();
858 if (const RecordDecl *RD =
859 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
860 return RD->isUnion();
861 return false;
862}
863
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000864bool Sema::isClassOrStructDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000865 if (!ThisDeclInfo)
866 return false;
867 if (!ThisDeclInfo->IsFilled)
868 inspectThisDecl();
869 return ThisDeclInfo->CurrentDecl &&
870 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
871 !isUnionDecl();
872}
873
874bool Sema::isObjCInterfaceDecl() {
875 if (!ThisDeclInfo)
876 return false;
877 if (!ThisDeclInfo->IsFilled)
878 inspectThisDecl();
879 return ThisDeclInfo->CurrentDecl &&
880 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
881}
882
883bool Sema::isObjCProtocolDecl() {
884 if (!ThisDeclInfo)
885 return false;
886 if (!ThisDeclInfo->IsFilled)
887 inspectThisDecl();
888 return ThisDeclInfo->CurrentDecl &&
889 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
890}
891
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000892ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000893 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000894 inspectThisDecl();
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000895 return ThisDeclInfo->ParamVars;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000896}
897
898void Sema::inspectThisDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000899 ThisDeclInfo->fill();
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000900}
901
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000902unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000903 ArrayRef<const ParmVarDecl *> ParamVars) {
904 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000905 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
906 if (II && II->getName() == Name)
907 return i;
908 }
909 return ParamCommandComment::InvalidParamIndex;
910}
911
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000912namespace {
913class SimpleTypoCorrector {
914 StringRef Typo;
915 const unsigned MaxEditDistance;
916
917 const NamedDecl *BestDecl;
918 unsigned BestEditDistance;
919 unsigned BestIndex;
920 unsigned NextIndex;
921
922public:
923 SimpleTypoCorrector(StringRef Typo) :
924 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
925 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
926 BestIndex(0), NextIndex(0)
927 { }
928
929 void addDecl(const NamedDecl *ND);
930
931 const NamedDecl *getBestDecl() const {
932 if (BestEditDistance > MaxEditDistance)
933 return NULL;
934
935 return BestDecl;
936 }
937
938 unsigned getBestDeclIndex() const {
939 assert(getBestDecl());
940 return BestIndex;
941 }
942};
943
944void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
945 unsigned CurrIndex = NextIndex++;
946
947 const IdentifierInfo *II = ND->getIdentifier();
948 if (!II)
949 return;
950
951 StringRef Name = II->getName();
952 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
953 if (MinPossibleEditDistance > 0 &&
954 Typo.size() / MinPossibleEditDistance < 3)
955 return;
956
957 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
958 if (EditDistance < BestEditDistance) {
959 BestEditDistance = EditDistance;
960 BestDecl = ND;
961 BestIndex = CurrIndex;
962 }
963}
964} // unnamed namespace
965
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000966unsigned Sema::correctTypoInParmVarReference(
967 StringRef Typo,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000968 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000969 SimpleTypoCorrector Corrector(Typo);
970 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
971 Corrector.addDecl(ParamVars[i]);
972 if (Corrector.getBestDecl())
973 return Corrector.getBestDeclIndex();
974 else
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000975 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000976}
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000977
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000978namespace {
979bool ResolveTParamReferenceHelper(
980 StringRef Name,
981 const TemplateParameterList *TemplateParameters,
982 SmallVectorImpl<unsigned> *Position) {
983 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
984 const NamedDecl *Param = TemplateParameters->getParam(i);
985 const IdentifierInfo *II = Param->getIdentifier();
986 if (II && II->getName() == Name) {
987 Position->push_back(i);
988 return true;
989 }
990
991 if (const TemplateTemplateParmDecl *TTP =
992 dyn_cast<TemplateTemplateParmDecl>(Param)) {
993 Position->push_back(i);
994 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
995 Position))
996 return true;
997 Position->pop_back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000998 }
999 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001000 return false;
1001}
1002} // unnamed namespace
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001003
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001004bool Sema::resolveTParamReference(
1005 StringRef Name,
1006 const TemplateParameterList *TemplateParameters,
1007 SmallVectorImpl<unsigned> *Position) {
1008 Position->clear();
1009 if (!TemplateParameters)
1010 return false;
1011
1012 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1013}
1014
1015namespace {
1016void CorrectTypoInTParamReferenceHelper(
1017 const TemplateParameterList *TemplateParameters,
1018 SimpleTypoCorrector &Corrector) {
1019 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1020 const NamedDecl *Param = TemplateParameters->getParam(i);
1021 Corrector.addDecl(Param);
1022
1023 if (const TemplateTemplateParmDecl *TTP =
1024 dyn_cast<TemplateTemplateParmDecl>(Param))
1025 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1026 Corrector);
1027 }
1028}
1029} // unnamed namespace
1030
1031StringRef Sema::correctTypoInTParamReference(
1032 StringRef Typo,
1033 const TemplateParameterList *TemplateParameters) {
1034 SimpleTypoCorrector Corrector(Typo);
1035 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1036 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1037 const IdentifierInfo *II = ND->getIdentifier();
1038 assert(II && "SimpleTypoCorrector should not return this decl");
1039 return II->getName();
1040 }
1041 return StringRef();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001042}
1043
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001044InlineCommandComment::RenderKind
1045Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +00001046 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001047
1048 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1049 .Case("b", InlineCommandComment::RenderBold)
1050 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1051 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1052 .Default(InlineCommandComment::RenderNormal);
1053}
1054
Dmitri Gribenkoec925312012-07-06 00:28:32 +00001055} // end namespace comments
1056} // end namespace clang
1057