blob: e0138d5f3f276593114a42d8187dd8e829e5e3bc [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;
135 break;
136 case CommandTraits::KCI_interface:
137 DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
138 break;
139 case CommandTraits::KCI_protocol:
140 DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
141 break;
142 case CommandTraits::KCI_struct:
143 DiagSelect = !isClassOrStructDecl() ? 4 : 0;
144 break;
145 case CommandTraits::KCI_union:
146 DiagSelect = !isUnionDecl() ? 5 : 0;
147 break;
148 default:
149 DiagSelect = 0;
150 break;
151 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000152 if (DiagSelect)
153 Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
154 << Comment->getCommandMarker()
155 << (DiagSelect-1) << (DiagSelect-1)
156 << Comment->getSourceRange();
157}
158
159void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
160 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000161 if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000162 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000163 unsigned DiagSelect;
164 switch (Comment->getCommandID()) {
165 case CommandTraits::KCI_classdesign:
166 DiagSelect = 1;
167 break;
168 case CommandTraits::KCI_coclass:
169 DiagSelect = 2;
170 break;
171 case CommandTraits::KCI_dependency:
172 DiagSelect = 3;
173 break;
174 case CommandTraits::KCI_helper:
175 DiagSelect = 4;
176 break;
177 case CommandTraits::KCI_helperclass:
178 DiagSelect = 5;
179 break;
180 case CommandTraits::KCI_helps:
181 DiagSelect = 6;
182 break;
183 case CommandTraits::KCI_instancesize:
184 DiagSelect = 7;
185 break;
186 case CommandTraits::KCI_ownership:
187 DiagSelect = 8;
188 break;
189 case CommandTraits::KCI_performance:
190 DiagSelect = 9;
191 break;
192 case CommandTraits::KCI_security:
193 DiagSelect = 10;
194 break;
195 case CommandTraits::KCI_superclass:
196 DiagSelect = 11;
197 break;
198 default:
199 DiagSelect = 0;
200 break;
201 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000202 if (DiagSelect)
203 Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
204 << Comment->getCommandMarker()
205 << (DiagSelect-1)
206 << Comment->getSourceRange();
207}
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000208
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000209void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
210 SourceLocation ArgLocBegin,
211 SourceLocation ArgLocEnd,
212 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000213 ParamCommandComment::PassDirection Direction;
214 std::string ArgLower = Arg.lower();
215 // TODO: optimize: lower Name first (need an API in SmallString for that),
216 // after that StringSwitch.
217 if (ArgLower == "[in]")
218 Direction = ParamCommandComment::In;
219 else if (ArgLower == "[out]")
220 Direction = ParamCommandComment::Out;
221 else if (ArgLower == "[in,out]" || ArgLower == "[out,in]")
222 Direction = ParamCommandComment::InOut;
223 else {
224 // Remove spaces.
225 std::string::iterator O = ArgLower.begin();
226 for (std::string::iterator I = ArgLower.begin(), E = ArgLower.end();
227 I != E; ++I) {
228 const char C = *I;
229 if (C != ' ' && C != '\n' && C != '\r' &&
230 C != '\t' && C != '\v' && C != '\f')
231 *O++ = C;
232 }
233 ArgLower.resize(O - ArgLower.begin());
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000234
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000235 bool RemovingWhitespaceHelped = false;
236 if (ArgLower == "[in]") {
237 Direction = ParamCommandComment::In;
238 RemovingWhitespaceHelped = true;
239 } else if (ArgLower == "[out]") {
240 Direction = ParamCommandComment::Out;
241 RemovingWhitespaceHelped = true;
242 } else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") {
243 Direction = ParamCommandComment::InOut;
244 RemovingWhitespaceHelped = true;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000245 } else {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000246 Direction = ParamCommandComment::In;
247 RemovingWhitespaceHelped = false;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000248 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000249
250 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
251 if (RemovingWhitespaceHelped)
252 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
253 << ArgRange
254 << FixItHint::CreateReplacement(
255 ArgRange,
256 ParamCommandComment::getDirectionAsString(Direction));
257 else
258 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction)
259 << ArgRange;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000260 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000261 Command->setDirection(Direction, /* Explicit = */ true);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000262}
263
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000264void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
265 SourceLocation ArgLocBegin,
266 SourceLocation ArgLocEnd,
267 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000268 // Parser will not feed us more arguments than needed.
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000269 assert(Command->getNumArgs() == 0);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000270
271 if (!Command->isDirectionExplicit()) {
272 // User didn't provide a direction argument.
273 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
274 }
275 typedef BlockCommandComment::Argument Argument;
276 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
277 ArgLocEnd),
278 Arg);
279 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000280}
281
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000282void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
283 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000284 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000285 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000286}
287
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000288TParamCommandComment *Sema::actOnTParamCommandStart(
289 SourceLocation LocBegin,
290 SourceLocation LocEnd,
291 unsigned CommandID,
292 CommandMarkerKind CommandMarker) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000293 TParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000294 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
295 CommandMarker);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000296
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000297 if (!isTemplateOrSpecialization())
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000298 Diag(Command->getLocation(),
299 diag::warn_doc_tparam_not_attached_to_a_template_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000300 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000301 << Command->getCommandNameRange(Traits);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000302
303 return Command;
304}
305
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000306void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
307 SourceLocation ArgLocBegin,
308 SourceLocation ArgLocEnd,
309 StringRef Arg) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000310 // Parser will not feed us more arguments than needed.
311 assert(Command->getNumArgs() == 0);
312
313 typedef BlockCommandComment::Argument Argument;
314 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
315 ArgLocEnd),
316 Arg);
317 Command->setArgs(llvm::makeArrayRef(A, 1));
318
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000319 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000320 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000321 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000322 }
323
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000324 const TemplateParameterList *TemplateParameters =
325 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000326 SmallVector<unsigned, 2> Position;
327 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
328 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
329 llvm::StringMap<TParamCommandComment *>::iterator PrevCommandIt =
330 TemplateParameterDocs.find(Arg);
331 if (PrevCommandIt != TemplateParameterDocs.end()) {
332 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
333 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
334 << Arg << ArgRange;
335 TParamCommandComment *PrevCommand = PrevCommandIt->second;
336 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
337 << PrevCommand->getParamNameRange();
338 }
339 TemplateParameterDocs[Arg] = Command;
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000340 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000341 }
342
343 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
344 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
345 << Arg << ArgRange;
346
347 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000348 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000349
350 StringRef CorrectedName;
351 if (TemplateParameters->size() == 1) {
352 const NamedDecl *Param = TemplateParameters->getParam(0);
353 const IdentifierInfo *II = Param->getIdentifier();
354 if (II)
355 CorrectedName = II->getName();
356 } else {
357 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
358 }
359
360 if (!CorrectedName.empty()) {
361 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
362 << CorrectedName
363 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
364 }
365
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000366 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000367}
368
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000369void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
370 ParagraphComment *Paragraph) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000371 Command->setParagraph(Paragraph);
372 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000373}
374
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000375InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
376 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000377 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000378 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000379 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000380 return new (Allocator) InlineCommandComment(
381 CommandLocBegin,
382 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000383 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000384 getInlineCommandRenderKind(CommandName),
385 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000386}
387
388InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
389 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000390 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000391 SourceLocation ArgLocBegin,
392 SourceLocation ArgLocEnd,
393 StringRef Arg) {
394 typedef InlineCommandComment::Argument Argument;
395 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
396 ArgLocEnd),
397 Arg);
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000398 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000399
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000400 return new (Allocator) InlineCommandComment(
401 CommandLocBegin,
402 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000403 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000404 getInlineCommandRenderKind(CommandName),
405 llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000406}
407
408InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
409 SourceLocation LocEnd,
Dmitri Gribenko9304d862012-09-11 19:22:03 +0000410 StringRef CommandName) {
411 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
412 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
413}
414
415InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
416 SourceLocation LocEnd,
417 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000418 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000419 return new (Allocator) InlineCommandComment(
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000420 LocBegin, LocEnd, CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000421 InlineCommandComment::RenderNormal,
422 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000423}
424
425TextComment *Sema::actOnText(SourceLocation LocBegin,
426 SourceLocation LocEnd,
427 StringRef Text) {
428 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
429}
430
431VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000432 unsigned CommandID) {
433 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000434 return new (Allocator) VerbatimBlockComment(
435 Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000436 Loc.getLocWithOffset(1 + CommandName.size()),
437 CommandID);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000438}
439
440VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
441 StringRef Text) {
442 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
443}
444
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000445void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000446 VerbatimBlockComment *Block,
447 SourceLocation CloseNameLocBegin,
448 StringRef CloseName,
449 ArrayRef<VerbatimBlockLineComment *> Lines) {
450 Block->setCloseName(CloseName, CloseNameLocBegin);
451 Block->setLines(Lines);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000452}
453
454VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000455 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000456 SourceLocation TextBegin,
457 StringRef Text) {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000458 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000459 LocBegin,
460 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000461 CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000462 TextBegin,
463 Text);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000464 checkFunctionDeclVerbatimLine(VL);
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000465 checkContainerDeclVerbatimLine(VL);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000466 return VL;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000467}
468
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000469HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
470 StringRef TagName) {
471 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000472}
473
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000474void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000475 HTMLStartTagComment *Tag,
476 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000477 SourceLocation GreaterLoc,
478 bool IsSelfClosing) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000479 Tag->setAttrs(Attrs);
480 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000481 if (IsSelfClosing)
482 Tag->setSelfClosing();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000483 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000484 HTMLOpenTags.push_back(Tag);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000485}
486
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000487HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
488 SourceLocation LocEnd,
489 StringRef TagName) {
490 HTMLEndTagComment *HET =
491 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
492 if (isHTMLEndTagForbidden(TagName)) {
493 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
494 << TagName << HET->getSourceRange();
495 return HET;
Dmitri Gribenko9460fbf2012-07-12 23:37:09 +0000496 }
497
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000498 bool FoundOpen = false;
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000499 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000500 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
501 I != E; ++I) {
502 if ((*I)->getTagName() == TagName) {
503 FoundOpen = true;
504 break;
505 }
506 }
507 if (!FoundOpen) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000508 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
509 << HET->getSourceRange();
510 return HET;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000511 }
512
513 while (!HTMLOpenTags.empty()) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000514 const HTMLStartTagComment *HST = HTMLOpenTags.back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000515 HTMLOpenTags.pop_back();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000516 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000517 if (LastNotClosedTagName == TagName)
518 break;
519
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000520 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000521 continue;
522
523 bool OpenLineInvalid;
524 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000525 HST->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000526 &OpenLineInvalid);
527 bool CloseLineInvalid;
528 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000529 HET->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000530 &CloseLineInvalid);
531
532 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000533 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
534 << HST->getTagName() << HET->getTagName()
535 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000536 else {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000537 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
538 << HST->getTagName() << HET->getTagName()
539 << HST->getSourceRange();
540 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
541 << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000542 }
543 }
544
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000545 return HET;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000546}
547
548FullComment *Sema::actOnFullComment(
549 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian42e31322012-10-11 23:52:50 +0000550 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000551 resolveParamCommandIndexes(FC);
552 return FC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000553}
554
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000555void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkob37d5e82012-09-13 20:36:01 +0000556 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
557 return;
558
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000559 ParagraphComment *Paragraph = Command->getParagraph();
560 if (Paragraph->isWhitespace()) {
561 SourceLocation DiagLoc;
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000562 if (Command->getNumArgs() > 0)
563 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000564 if (!DiagLoc.isValid())
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000565 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000566 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000567 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000568 << Command->getCommandName(Traits)
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000569 << Command->getSourceRange();
570 }
571}
572
Dmitri Gribenko64305832012-08-03 21:15:32 +0000573void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000574 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000575 return;
576 if (isFunctionDecl()) {
577 if (ThisDeclInfo->ResultType->isVoidType()) {
578 unsigned DiagKind;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000579 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000580 default:
Dmitri Gribenko558babc2012-08-06 16:29:26 +0000581 if (ThisDeclInfo->IsObjCMethod)
582 DiagKind = 3;
583 else
584 DiagKind = 0;
Dmitri Gribenko64305832012-08-03 21:15:32 +0000585 break;
586 case Decl::CXXConstructor:
587 DiagKind = 1;
588 break;
589 case Decl::CXXDestructor:
590 DiagKind = 2;
591 break;
592 }
593 Diag(Command->getLocation(),
594 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000595 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000596 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000597 << DiagKind
598 << Command->getSourceRange();
599 }
600 return;
601 }
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000602 else if (isObjCPropertyDecl())
603 return;
604
Dmitri Gribenko64305832012-08-03 21:15:32 +0000605 Diag(Command->getLocation(),
606 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000607 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000608 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000609 << Command->getSourceRange();
610}
611
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000612void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000613 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000614 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000615 if (Info->IsBriefCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000616 if (!BriefCommand) {
617 BriefCommand = Command;
618 return;
619 }
620 PrevCommand = BriefCommand;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000621 } else if (Info->IsReturnsCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000622 if (!ReturnsCommand) {
623 ReturnsCommand = Command;
624 return;
625 }
626 PrevCommand = ReturnsCommand;
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +0000627 } else if (Info->IsHeaderfileCommand) {
628 if (!HeaderfileCommand) {
629 HeaderfileCommand = Command;
630 return;
631 }
632 PrevCommand = HeaderfileCommand;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000633 } else {
634 // We don't want to check this command for duplicates.
635 return;
636 }
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000637 StringRef CommandName = Command->getCommandName(Traits);
638 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000639 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000640 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000641 << CommandName
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000642 << Command->getSourceRange();
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000643 if (CommandName == PrevCommandName)
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000644 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000645 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000646 << PrevCommandName
647 << PrevCommand->getSourceRange();
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000648 else
649 Diag(PrevCommand->getLocation(),
650 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000651 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000652 << PrevCommandName
653 << CommandName;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000654}
655
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000656void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
657 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
658 return;
659
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000660 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000661 if (!D)
662 return;
663
664 if (D->hasAttr<DeprecatedAttr>() ||
665 D->hasAttr<AvailabilityAttr>() ||
666 D->hasAttr<UnavailableAttr>())
667 return;
668
669 Diag(Command->getLocation(),
670 diag::warn_doc_deprecated_not_sync)
671 << Command->getSourceRange();
672
673 // Try to emit a fixit with a deprecation attribute.
674 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
675 // Don't emit a Fix-It for non-member function definitions. GCC does not
676 // accept attributes on them.
677 const DeclContext *Ctx = FD->getDeclContext();
678 if ((!Ctx || !Ctx->isRecord()) &&
679 FD->doesThisDeclarationHaveABody())
680 return;
681
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000682 StringRef AttributeSpelling = "__attribute__((deprecated))";
683 if (PP) {
684 TokenValue Tokens[] = {
685 tok::kw___attribute, tok::l_paren, tok::l_paren,
686 PP->getIdentifierInfo("deprecated"),
687 tok::r_paren, tok::r_paren
688 };
689 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
690 Tokens);
691 if (!MacroName.empty())
692 AttributeSpelling = MacroName;
693 }
694
695 SmallString<64> TextToInsert(" ");
696 TextToInsert += AttributeSpelling;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000697 Diag(FD->getLocEnd(),
698 diag::note_add_deprecation_attr)
699 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000700 TextToInsert);
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000701 }
702}
703
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000704void Sema::resolveParamCommandIndexes(const FullComment *FC) {
705 if (!isFunctionDecl()) {
706 // We already warned that \\param commands are not attached to a function
707 // decl.
708 return;
709 }
710
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000711 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000712
713 // Comment AST nodes that correspond to \c ParamVars for which we have
714 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000715 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000716
717 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
718 ParamVarDocs.resize(ParamVars.size(), NULL);
719
720 // First pass over all \\param commands: resolve all parameter names.
721 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
722 I != E; ++I) {
723 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
724 if (!PCC || !PCC->hasParamName())
725 continue;
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000726 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000727
728 // Check that referenced parameter name is in the function decl.
729 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
730 ParamVars);
731 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
732 UnresolvedParamCommands.push_back(PCC);
733 continue;
734 }
735 PCC->setParamIndex(ResolvedParamIndex);
736 if (ParamVarDocs[ResolvedParamIndex]) {
737 SourceRange ArgRange = PCC->getParamNameRange();
738 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
739 << ParamName << ArgRange;
740 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
741 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
742 << PrevCommand->getParamNameRange();
743 }
744 ParamVarDocs[ResolvedParamIndex] = PCC;
745 }
746
747 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000748 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000749 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
750 if (!ParamVarDocs[i])
751 OrphanedParamDecls.push_back(ParamVars[i]);
752 }
753
754 // Second pass over unresolved \\param commands: do typo correction.
755 // Suggest corrections from a set of parameter declarations that have no
756 // corresponding \\param.
757 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
758 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
759
760 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000761 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000762 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
763 << ParamName << ArgRange;
764
765 // All parameters documented -- can't suggest a correction.
766 if (OrphanedParamDecls.size() == 0)
767 continue;
768
769 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
770 if (OrphanedParamDecls.size() == 1) {
771 // If one parameter is not documented then that parameter is the only
772 // possible suggestion.
773 CorrectedParamIndex = 0;
774 } else {
775 // Do typo correction.
776 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
777 OrphanedParamDecls);
778 }
779 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
780 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
781 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
782 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
783 << CorrectedII->getName()
784 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
785 }
786 }
787}
788
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000789bool Sema::isFunctionDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000790 if (!ThisDeclInfo)
791 return false;
792 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000793 inspectThisDecl();
Dmitri Gribenko37a7faf2012-08-02 21:45:39 +0000794 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000795}
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000796
Fariborz Jahaniana668bf52013-03-05 23:20:29 +0000797bool Sema::isAnyFunctionDecl() {
798 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
799 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
800}
801
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000802bool Sema::isObjCMethodDecl() {
803 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
804 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
805}
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000806
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000807/// isFunctionPointerVarDecl - returns 'true' if declaration is a pointer to
808/// function decl.
809bool Sema::isFunctionPointerVarDecl() {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000810 if (!ThisDeclInfo)
811 return false;
812 if (!ThisDeclInfo->IsFilled)
813 inspectThisDecl();
814 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
815 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
816 QualType QT = VD->getType();
817 return QT->isFunctionPointerType();
818 }
819 }
820 return false;
821}
822
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000823bool Sema::isObjCPropertyDecl() {
824 if (!ThisDeclInfo)
825 return false;
826 if (!ThisDeclInfo->IsFilled)
827 inspectThisDecl();
828 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
829}
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000830
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000831bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000832 if (!ThisDeclInfo)
833 return false;
834 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000835 inspectThisDecl();
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000836 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000837}
838
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000839bool Sema::isRecordLikeDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000840 if (!ThisDeclInfo)
841 return false;
842 if (!ThisDeclInfo->IsFilled)
843 inspectThisDecl();
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000844 return isUnionDecl() || isClassOrStructDecl()
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000845 || isObjCInterfaceDecl() || isObjCProtocolDecl();
846}
847
848bool Sema::isUnionDecl() {
849 if (!ThisDeclInfo)
850 return false;
851 if (!ThisDeclInfo->IsFilled)
852 inspectThisDecl();
853 if (const RecordDecl *RD =
854 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
855 return RD->isUnion();
856 return false;
857}
858
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000859bool Sema::isClassOrStructDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000860 if (!ThisDeclInfo)
861 return false;
862 if (!ThisDeclInfo->IsFilled)
863 inspectThisDecl();
864 return ThisDeclInfo->CurrentDecl &&
865 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
866 !isUnionDecl();
867}
868
869bool Sema::isObjCInterfaceDecl() {
870 if (!ThisDeclInfo)
871 return false;
872 if (!ThisDeclInfo->IsFilled)
873 inspectThisDecl();
874 return ThisDeclInfo->CurrentDecl &&
875 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
876}
877
878bool Sema::isObjCProtocolDecl() {
879 if (!ThisDeclInfo)
880 return false;
881 if (!ThisDeclInfo->IsFilled)
882 inspectThisDecl();
883 return ThisDeclInfo->CurrentDecl &&
884 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
885}
886
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000887ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000888 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000889 inspectThisDecl();
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000890 return ThisDeclInfo->ParamVars;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000891}
892
893void Sema::inspectThisDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000894 ThisDeclInfo->fill();
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000895}
896
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000897unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000898 ArrayRef<const ParmVarDecl *> ParamVars) {
899 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000900 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
901 if (II && II->getName() == Name)
902 return i;
903 }
904 return ParamCommandComment::InvalidParamIndex;
905}
906
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000907namespace {
908class SimpleTypoCorrector {
909 StringRef Typo;
910 const unsigned MaxEditDistance;
911
912 const NamedDecl *BestDecl;
913 unsigned BestEditDistance;
914 unsigned BestIndex;
915 unsigned NextIndex;
916
917public:
918 SimpleTypoCorrector(StringRef Typo) :
919 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
920 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
921 BestIndex(0), NextIndex(0)
922 { }
923
924 void addDecl(const NamedDecl *ND);
925
926 const NamedDecl *getBestDecl() const {
927 if (BestEditDistance > MaxEditDistance)
928 return NULL;
929
930 return BestDecl;
931 }
932
933 unsigned getBestDeclIndex() const {
934 assert(getBestDecl());
935 return BestIndex;
936 }
937};
938
939void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
940 unsigned CurrIndex = NextIndex++;
941
942 const IdentifierInfo *II = ND->getIdentifier();
943 if (!II)
944 return;
945
946 StringRef Name = II->getName();
947 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
948 if (MinPossibleEditDistance > 0 &&
949 Typo.size() / MinPossibleEditDistance < 3)
950 return;
951
952 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
953 if (EditDistance < BestEditDistance) {
954 BestEditDistance = EditDistance;
955 BestDecl = ND;
956 BestIndex = CurrIndex;
957 }
958}
959} // unnamed namespace
960
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000961unsigned Sema::correctTypoInParmVarReference(
962 StringRef Typo,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000963 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000964 SimpleTypoCorrector Corrector(Typo);
965 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
966 Corrector.addDecl(ParamVars[i]);
967 if (Corrector.getBestDecl())
968 return Corrector.getBestDeclIndex();
969 else
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000970 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000971}
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000972
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000973namespace {
974bool ResolveTParamReferenceHelper(
975 StringRef Name,
976 const TemplateParameterList *TemplateParameters,
977 SmallVectorImpl<unsigned> *Position) {
978 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
979 const NamedDecl *Param = TemplateParameters->getParam(i);
980 const IdentifierInfo *II = Param->getIdentifier();
981 if (II && II->getName() == Name) {
982 Position->push_back(i);
983 return true;
984 }
985
986 if (const TemplateTemplateParmDecl *TTP =
987 dyn_cast<TemplateTemplateParmDecl>(Param)) {
988 Position->push_back(i);
989 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
990 Position))
991 return true;
992 Position->pop_back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000993 }
994 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000995 return false;
996}
997} // unnamed namespace
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000998
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000999bool Sema::resolveTParamReference(
1000 StringRef Name,
1001 const TemplateParameterList *TemplateParameters,
1002 SmallVectorImpl<unsigned> *Position) {
1003 Position->clear();
1004 if (!TemplateParameters)
1005 return false;
1006
1007 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1008}
1009
1010namespace {
1011void CorrectTypoInTParamReferenceHelper(
1012 const TemplateParameterList *TemplateParameters,
1013 SimpleTypoCorrector &Corrector) {
1014 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1015 const NamedDecl *Param = TemplateParameters->getParam(i);
1016 Corrector.addDecl(Param);
1017
1018 if (const TemplateTemplateParmDecl *TTP =
1019 dyn_cast<TemplateTemplateParmDecl>(Param))
1020 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1021 Corrector);
1022 }
1023}
1024} // unnamed namespace
1025
1026StringRef Sema::correctTypoInTParamReference(
1027 StringRef Typo,
1028 const TemplateParameterList *TemplateParameters) {
1029 SimpleTypoCorrector Corrector(Typo);
1030 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1031 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1032 const IdentifierInfo *II = ND->getIdentifier();
1033 assert(II && "SimpleTypoCorrector should not return this decl");
1034 return II->getName();
1035 }
1036 return StringRef();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001037}
1038
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001039InlineCommandComment::RenderKind
1040Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +00001041 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001042
1043 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1044 .Case("b", InlineCommandComment::RenderBold)
1045 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1046 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1047 .Default(InlineCommandComment::RenderNormal);
1048}
1049
Dmitri Gribenkoec925312012-07-06 00:28:32 +00001050} // end namespace comments
1051} // end namespace clang
1052