blob: d324c5c20d586822717c358d2546f85261cb0007 [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),
Dmitri Gribenkod6662932013-06-22 23:03:37 +000032 PP(PP), ThisDeclInfo(NULL), BriefCommand(NULL), HeaderfileCommand(NULL) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000033}
34
35void Sema::setDecl(const Decl *D) {
Dmitri Gribenko527ab212012-08-01 23:08:09 +000036 if (!D)
37 return;
38
39 ThisDeclInfo = new (Allocator) DeclInfo;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +000040 ThisDeclInfo->CommentDecl = D;
Dmitri Gribenkoe6213dd2012-08-01 23:21:57 +000041 ThisDeclInfo->IsFilled = false;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000042}
43
44ParagraphComment *Sema::actOnParagraphComment(
45 ArrayRef<InlineContentComment *> Content) {
46 return new (Allocator) ParagraphComment(Content);
47}
48
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000049BlockCommandComment *Sema::actOnBlockCommandStart(
50 SourceLocation LocBegin,
51 SourceLocation LocEnd,
52 unsigned CommandID,
53 CommandMarkerKind CommandMarker) {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +000054 BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd,
55 CommandID,
56 CommandMarker);
57 checkContainerDecl(BC);
58 return BC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000059}
60
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +000061void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
62 ArrayRef<BlockCommandComment::Argument> Args) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +000063 Command->setArgs(Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +000064}
65
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +000066void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
67 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +000068 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000069 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +000070 checkBlockCommandDuplicate(Command);
Dmitri Gribenko64305832012-08-03 21:15:32 +000071 checkReturnsCommand(Command);
Dmitri Gribenko1da88862012-09-22 21:47:50 +000072 checkDeprecatedCommand(Command);
Dmitri Gribenkoec925312012-07-06 00:28:32 +000073}
74
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000075ParamCommandComment *Sema::actOnParamCommandStart(
76 SourceLocation LocBegin,
77 SourceLocation LocEnd,
78 unsigned CommandID,
79 CommandMarkerKind CommandMarker) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000080 ParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000081 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
82 CommandMarker);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000083
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +000084 if (!isFunctionDecl())
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000085 Diag(Command->getLocation(),
86 diag::warn_doc_param_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000087 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000088 << Command->getCommandNameRange(Traits);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000089
90 return Command;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000091}
92
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +000093void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
94 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahanian56fe4062013-03-05 22:46:07 +000095 if (!Info->IsFunctionDeclarationCommand)
96 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +000097
98 unsigned DiagSelect;
99 switch (Comment->getCommandID()) {
100 case CommandTraits::KCI_function:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000101 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000102 break;
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000103 case CommandTraits::KCI_functiongroup:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000104 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0;
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000105 break;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000106 case CommandTraits::KCI_method:
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000107 DiagSelect = !isObjCMethodDecl() ? 3 : 0;
108 break;
109 case CommandTraits::KCI_methodgroup:
110 DiagSelect = !isObjCMethodDecl() ? 4 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000111 break;
112 case CommandTraits::KCI_callback:
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000113 DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000114 break;
115 default:
116 DiagSelect = 0;
117 break;
118 }
Fariborz Jahanian41bb7132013-03-06 17:36:51 +0000119 if (DiagSelect)
120 Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
121 << Comment->getCommandMarker()
122 << (DiagSelect-1) << (DiagSelect-1)
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000123 << Comment->getSourceRange();
124}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000125
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000126void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
127 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000128 if (!Info->IsRecordLikeDeclarationCommand)
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000129 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000130 unsigned DiagSelect;
131 switch (Comment->getCommandID()) {
132 case CommandTraits::KCI_class:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000133 DiagSelect = (!isClassOrStructDecl() && !isClassTemplateDecl()) ? 1 : 0;
Fariborz Jahanian04eb8ab2013-05-20 23:40:39 +0000134 // Allow @class command on @interface declarations.
135 // FIXME. Currently, \class and @class are indistinguishable. So,
136 // \class is also allowed on an @interface declaration
137 if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
138 DiagSelect = 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000139 break;
140 case CommandTraits::KCI_interface:
141 DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
142 break;
143 case CommandTraits::KCI_protocol:
144 DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
145 break;
146 case CommandTraits::KCI_struct:
147 DiagSelect = !isClassOrStructDecl() ? 4 : 0;
148 break;
149 case CommandTraits::KCI_union:
150 DiagSelect = !isUnionDecl() ? 5 : 0;
151 break;
152 default:
153 DiagSelect = 0;
154 break;
155 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000156 if (DiagSelect)
157 Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
158 << Comment->getCommandMarker()
159 << (DiagSelect-1) << (DiagSelect-1)
160 << Comment->getSourceRange();
161}
162
163void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
164 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000165 if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000166 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000167 unsigned DiagSelect;
168 switch (Comment->getCommandID()) {
169 case CommandTraits::KCI_classdesign:
170 DiagSelect = 1;
171 break;
172 case CommandTraits::KCI_coclass:
173 DiagSelect = 2;
174 break;
175 case CommandTraits::KCI_dependency:
176 DiagSelect = 3;
177 break;
178 case CommandTraits::KCI_helper:
179 DiagSelect = 4;
180 break;
181 case CommandTraits::KCI_helperclass:
182 DiagSelect = 5;
183 break;
184 case CommandTraits::KCI_helps:
185 DiagSelect = 6;
186 break;
187 case CommandTraits::KCI_instancesize:
188 DiagSelect = 7;
189 break;
190 case CommandTraits::KCI_ownership:
191 DiagSelect = 8;
192 break;
193 case CommandTraits::KCI_performance:
194 DiagSelect = 9;
195 break;
196 case CommandTraits::KCI_security:
197 DiagSelect = 10;
198 break;
199 case CommandTraits::KCI_superclass:
200 DiagSelect = 11;
201 break;
202 default:
203 DiagSelect = 0;
204 break;
205 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000206 if (DiagSelect)
207 Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
208 << Comment->getCommandMarker()
209 << (DiagSelect-1)
210 << Comment->getSourceRange();
211}
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000212
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000213/// \brief Turn a string into the corresponding PassDirection or -1 if it's not
214/// valid.
215static int getParamPassDirection(StringRef Arg) {
216 return llvm::StringSwitch<int>(Arg)
217 .Case("[in]", ParamCommandComment::In)
218 .Case("[out]", ParamCommandComment::Out)
219 .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
220 .Default(-1);
221}
222
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000223void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
224 SourceLocation ArgLocBegin,
225 SourceLocation ArgLocEnd,
226 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000227 std::string ArgLower = Arg.lower();
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000228 int Direction = getParamPassDirection(ArgLower);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000229
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000230 if (Direction == -1) {
231 // Try again with whitespace removed.
232 ArgLower.erase(
233 std::remove_if(ArgLower.begin(), ArgLower.end(), clang::isWhitespace),
234 ArgLower.end());
235 Direction = getParamPassDirection(ArgLower);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000236
237 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000238 if (Direction != -1) {
239 const char *FixedName = ParamCommandComment::getDirectionAsString(
240 (ParamCommandComment::PassDirection)Direction);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000241 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000242 << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
243 } else {
244 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
245 Direction = ParamCommandComment::In; // Sane fall back.
246 }
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000247 }
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000248 Command->setDirection((ParamCommandComment::PassDirection)Direction,
249 /*Explicit=*/true);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000250}
251
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000252void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
253 SourceLocation ArgLocBegin,
254 SourceLocation ArgLocEnd,
255 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000256 // Parser will not feed us more arguments than needed.
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000257 assert(Command->getNumArgs() == 0);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000258
259 if (!Command->isDirectionExplicit()) {
260 // User didn't provide a direction argument.
261 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
262 }
263 typedef BlockCommandComment::Argument Argument;
264 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
265 ArgLocEnd),
266 Arg);
267 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000268}
269
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000270void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
271 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000272 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000273 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000274}
275
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000276TParamCommandComment *Sema::actOnTParamCommandStart(
277 SourceLocation LocBegin,
278 SourceLocation LocEnd,
279 unsigned CommandID,
280 CommandMarkerKind CommandMarker) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000281 TParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000282 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
283 CommandMarker);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000284
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000285 if (!isTemplateOrSpecialization())
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000286 Diag(Command->getLocation(),
287 diag::warn_doc_tparam_not_attached_to_a_template_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000288 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000289 << Command->getCommandNameRange(Traits);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000290
291 return Command;
292}
293
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000294void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
295 SourceLocation ArgLocBegin,
296 SourceLocation ArgLocEnd,
297 StringRef Arg) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000298 // Parser will not feed us more arguments than needed.
299 assert(Command->getNumArgs() == 0);
300
301 typedef BlockCommandComment::Argument Argument;
302 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
303 ArgLocEnd),
304 Arg);
305 Command->setArgs(llvm::makeArrayRef(A, 1));
306
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000307 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000308 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000309 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000310 }
311
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000312 const TemplateParameterList *TemplateParameters =
313 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000314 SmallVector<unsigned, 2> Position;
315 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
316 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
Benjamin Kramereed80612013-11-10 16:55:11 +0000317 TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg];
318 if (PrevCommand) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000319 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
320 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
321 << Arg << ArgRange;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000322 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
323 << PrevCommand->getParamNameRange();
324 }
Benjamin Kramereed80612013-11-10 16:55:11 +0000325 PrevCommand = Command;
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000326 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000327 }
328
329 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
330 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
331 << Arg << ArgRange;
332
333 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000334 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000335
336 StringRef CorrectedName;
337 if (TemplateParameters->size() == 1) {
338 const NamedDecl *Param = TemplateParameters->getParam(0);
339 const IdentifierInfo *II = Param->getIdentifier();
340 if (II)
341 CorrectedName = II->getName();
342 } else {
343 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
344 }
345
346 if (!CorrectedName.empty()) {
347 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
348 << CorrectedName
349 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
350 }
351
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000352 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000353}
354
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000355void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
356 ParagraphComment *Paragraph) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000357 Command->setParagraph(Paragraph);
358 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000359}
360
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000361InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
362 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000363 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000364 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000365 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000366 return new (Allocator) InlineCommandComment(
367 CommandLocBegin,
368 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000369 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000370 getInlineCommandRenderKind(CommandName),
371 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000372}
373
374InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
375 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000376 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000377 SourceLocation ArgLocBegin,
378 SourceLocation ArgLocEnd,
379 StringRef Arg) {
380 typedef InlineCommandComment::Argument Argument;
381 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
382 ArgLocEnd),
383 Arg);
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000384 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000385
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000386 return new (Allocator) InlineCommandComment(
387 CommandLocBegin,
388 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000389 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000390 getInlineCommandRenderKind(CommandName),
391 llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000392}
393
394InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
395 SourceLocation LocEnd,
Dmitri Gribenko9304d862012-09-11 19:22:03 +0000396 StringRef CommandName) {
397 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
398 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
399}
400
401InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
402 SourceLocation LocEnd,
403 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000404 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000405 return new (Allocator) InlineCommandComment(
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000406 LocBegin, LocEnd, CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000407 InlineCommandComment::RenderNormal,
408 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000409}
410
411TextComment *Sema::actOnText(SourceLocation LocBegin,
412 SourceLocation LocEnd,
413 StringRef Text) {
414 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
415}
416
417VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000418 unsigned CommandID) {
419 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000420 return new (Allocator) VerbatimBlockComment(
421 Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000422 Loc.getLocWithOffset(1 + CommandName.size()),
423 CommandID);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000424}
425
426VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
427 StringRef Text) {
428 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
429}
430
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000431void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000432 VerbatimBlockComment *Block,
433 SourceLocation CloseNameLocBegin,
434 StringRef CloseName,
435 ArrayRef<VerbatimBlockLineComment *> Lines) {
436 Block->setCloseName(CloseName, CloseNameLocBegin);
437 Block->setLines(Lines);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000438}
439
440VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000441 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000442 SourceLocation TextBegin,
443 StringRef Text) {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000444 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000445 LocBegin,
446 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000447 CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000448 TextBegin,
449 Text);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000450 checkFunctionDeclVerbatimLine(VL);
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000451 checkContainerDeclVerbatimLine(VL);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000452 return VL;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000453}
454
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000455HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
456 StringRef TagName) {
457 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000458}
459
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000460void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000461 HTMLStartTagComment *Tag,
462 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000463 SourceLocation GreaterLoc,
464 bool IsSelfClosing) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000465 Tag->setAttrs(Attrs);
466 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000467 if (IsSelfClosing)
468 Tag->setSelfClosing();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000469 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000470 HTMLOpenTags.push_back(Tag);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000471}
472
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000473HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
474 SourceLocation LocEnd,
475 StringRef TagName) {
476 HTMLEndTagComment *HET =
477 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
478 if (isHTMLEndTagForbidden(TagName)) {
479 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
480 << TagName << HET->getSourceRange();
481 return HET;
Dmitri Gribenko9460fbf2012-07-12 23:37:09 +0000482 }
483
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000484 bool FoundOpen = false;
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000485 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000486 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
487 I != E; ++I) {
488 if ((*I)->getTagName() == TagName) {
489 FoundOpen = true;
490 break;
491 }
492 }
493 if (!FoundOpen) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000494 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
495 << HET->getSourceRange();
496 return HET;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000497 }
498
499 while (!HTMLOpenTags.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000500 const HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000501 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000502 if (LastNotClosedTagName == TagName)
503 break;
504
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000505 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000506 continue;
507
508 bool OpenLineInvalid;
509 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000510 HST->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000511 &OpenLineInvalid);
512 bool CloseLineInvalid;
513 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000514 HET->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000515 &CloseLineInvalid);
516
517 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000518 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
519 << HST->getTagName() << HET->getTagName()
520 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000521 else {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000522 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
523 << HST->getTagName() << HET->getTagName()
524 << HST->getSourceRange();
525 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
526 << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000527 }
528 }
529
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000530 return HET;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000531}
532
533FullComment *Sema::actOnFullComment(
534 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian42e31322012-10-11 23:52:50 +0000535 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000536 resolveParamCommandIndexes(FC);
537 return FC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000538}
539
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000540void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkob37d5e82012-09-13 20:36:01 +0000541 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
542 return;
543
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000544 ParagraphComment *Paragraph = Command->getParagraph();
545 if (Paragraph->isWhitespace()) {
546 SourceLocation DiagLoc;
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000547 if (Command->getNumArgs() > 0)
548 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000549 if (!DiagLoc.isValid())
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000550 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000551 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000552 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000553 << Command->getCommandName(Traits)
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000554 << Command->getSourceRange();
555 }
556}
557
Dmitri Gribenko64305832012-08-03 21:15:32 +0000558void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000559 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000560 return;
561 if (isFunctionDecl()) {
562 if (ThisDeclInfo->ResultType->isVoidType()) {
563 unsigned DiagKind;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000564 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000565 default:
Dmitri Gribenko558babc2012-08-06 16:29:26 +0000566 if (ThisDeclInfo->IsObjCMethod)
567 DiagKind = 3;
568 else
569 DiagKind = 0;
Dmitri Gribenko64305832012-08-03 21:15:32 +0000570 break;
571 case Decl::CXXConstructor:
572 DiagKind = 1;
573 break;
574 case Decl::CXXDestructor:
575 DiagKind = 2;
576 break;
577 }
578 Diag(Command->getLocation(),
579 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000580 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000581 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000582 << DiagKind
583 << Command->getSourceRange();
584 }
585 return;
586 }
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000587 else if (isObjCPropertyDecl())
588 return;
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000589
Dmitri Gribenko64305832012-08-03 21:15:32 +0000590 Diag(Command->getLocation(),
591 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000592 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000593 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000594 << Command->getSourceRange();
595}
596
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000597void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000598 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000599 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000600 if (Info->IsBriefCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000601 if (!BriefCommand) {
602 BriefCommand = Command;
603 return;
604 }
605 PrevCommand = BriefCommand;
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +0000606 } else if (Info->IsHeaderfileCommand) {
607 if (!HeaderfileCommand) {
608 HeaderfileCommand = Command;
609 return;
610 }
611 PrevCommand = HeaderfileCommand;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000612 } else {
613 // We don't want to check this command for duplicates.
614 return;
615 }
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000616 StringRef CommandName = Command->getCommandName(Traits);
617 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000618 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000619 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000620 << CommandName
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000621 << Command->getSourceRange();
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000622 if (CommandName == PrevCommandName)
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000623 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000624 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000625 << PrevCommandName
626 << PrevCommand->getSourceRange();
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000627 else
628 Diag(PrevCommand->getLocation(),
629 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000630 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000631 << PrevCommandName
632 << CommandName;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000633}
634
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000635void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
636 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
637 return;
638
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000639 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000640 if (!D)
641 return;
642
643 if (D->hasAttr<DeprecatedAttr>() ||
644 D->hasAttr<AvailabilityAttr>() ||
645 D->hasAttr<UnavailableAttr>())
646 return;
647
648 Diag(Command->getLocation(),
649 diag::warn_doc_deprecated_not_sync)
650 << Command->getSourceRange();
651
652 // Try to emit a fixit with a deprecation attribute.
653 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
654 // Don't emit a Fix-It for non-member function definitions. GCC does not
655 // accept attributes on them.
656 const DeclContext *Ctx = FD->getDeclContext();
657 if ((!Ctx || !Ctx->isRecord()) &&
658 FD->doesThisDeclarationHaveABody())
659 return;
660
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000661 StringRef AttributeSpelling = "__attribute__((deprecated))";
662 if (PP) {
663 TokenValue Tokens[] = {
664 tok::kw___attribute, tok::l_paren, tok::l_paren,
665 PP->getIdentifierInfo("deprecated"),
666 tok::r_paren, tok::r_paren
667 };
668 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
669 Tokens);
670 if (!MacroName.empty())
671 AttributeSpelling = MacroName;
672 }
673
674 SmallString<64> TextToInsert(" ");
675 TextToInsert += AttributeSpelling;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000676 Diag(FD->getLocEnd(),
677 diag::note_add_deprecation_attr)
678 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000679 TextToInsert);
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000680 }
681}
682
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000683void Sema::resolveParamCommandIndexes(const FullComment *FC) {
684 if (!isFunctionDecl()) {
685 // We already warned that \\param commands are not attached to a function
686 // decl.
687 return;
688 }
689
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000690 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000691
692 // Comment AST nodes that correspond to \c ParamVars for which we have
693 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000694 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000695
696 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
NAKAMURA Takumidc2e2fb2013-06-19 06:58:14 +0000697 ParamVarDocs.resize(ParamVars.size(), NULL);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000698
699 // First pass over all \\param commands: resolve all parameter names.
700 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
701 I != E; ++I) {
702 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
703 if (!PCC || !PCC->hasParamName())
704 continue;
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000705 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000706
707 // Check that referenced parameter name is in the function decl.
708 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
709 ParamVars);
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000710 if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
711 PCC->setIsVarArgParam();
712 continue;
713 }
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000714 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
715 UnresolvedParamCommands.push_back(PCC);
716 continue;
717 }
718 PCC->setParamIndex(ResolvedParamIndex);
719 if (ParamVarDocs[ResolvedParamIndex]) {
720 SourceRange ArgRange = PCC->getParamNameRange();
721 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
722 << ParamName << ArgRange;
723 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
724 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
725 << PrevCommand->getParamNameRange();
726 }
727 ParamVarDocs[ResolvedParamIndex] = PCC;
728 }
729
730 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000731 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000732 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
733 if (!ParamVarDocs[i])
734 OrphanedParamDecls.push_back(ParamVars[i]);
735 }
736
737 // Second pass over unresolved \\param commands: do typo correction.
738 // Suggest corrections from a set of parameter declarations that have no
739 // corresponding \\param.
740 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
741 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
742
743 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000744 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000745 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
746 << ParamName << ArgRange;
747
748 // All parameters documented -- can't suggest a correction.
749 if (OrphanedParamDecls.size() == 0)
750 continue;
751
752 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
753 if (OrphanedParamDecls.size() == 1) {
754 // If one parameter is not documented then that parameter is the only
755 // possible suggestion.
756 CorrectedParamIndex = 0;
757 } else {
758 // Do typo correction.
759 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
760 OrphanedParamDecls);
761 }
762 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
763 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
764 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
765 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
766 << CorrectedII->getName()
767 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
768 }
769 }
770}
771
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000772bool Sema::isFunctionDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000773 if (!ThisDeclInfo)
774 return false;
775 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000776 inspectThisDecl();
Dmitri Gribenko37a7faf2012-08-02 21:45:39 +0000777 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000778}
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000779
Fariborz Jahaniana668bf52013-03-05 23:20:29 +0000780bool Sema::isAnyFunctionDecl() {
781 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
782 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
783}
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000784
785bool Sema::isFunctionOrMethodVariadic() {
786 if (!isAnyFunctionDecl() && !isObjCMethodDecl())
787 return false;
788 if (const FunctionDecl *FD =
789 dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
790 return FD->isVariadic();
791 if (const ObjCMethodDecl *MD =
792 dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
793 return MD->isVariadic();
794 return false;
795}
796
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000797bool Sema::isObjCMethodDecl() {
798 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
799 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
800}
Dmitri Gribenkoc0510b92013-06-24 01:33:34 +0000801
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000802bool Sema::isFunctionPointerVarDecl() {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000803 if (!ThisDeclInfo)
804 return false;
805 if (!ThisDeclInfo->IsFilled)
806 inspectThisDecl();
807 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
808 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
809 QualType QT = VD->getType();
810 return QT->isFunctionPointerType();
811 }
812 }
813 return false;
814}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000815
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000816bool Sema::isObjCPropertyDecl() {
817 if (!ThisDeclInfo)
818 return false;
819 if (!ThisDeclInfo->IsFilled)
820 inspectThisDecl();
821 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
822}
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000823
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000824bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000825 if (!ThisDeclInfo)
826 return false;
827 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000828 inspectThisDecl();
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000829 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000830}
831
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000832bool Sema::isRecordLikeDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000833 if (!ThisDeclInfo)
834 return false;
835 if (!ThisDeclInfo->IsFilled)
836 inspectThisDecl();
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000837 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
838 isObjCProtocolDecl();
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000839}
840
841bool Sema::isUnionDecl() {
842 if (!ThisDeclInfo)
843 return false;
844 if (!ThisDeclInfo->IsFilled)
845 inspectThisDecl();
846 if (const RecordDecl *RD =
847 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
848 return RD->isUnion();
849 return false;
850}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000851
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000852bool Sema::isClassOrStructDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000853 if (!ThisDeclInfo)
854 return false;
855 if (!ThisDeclInfo->IsFilled)
856 inspectThisDecl();
857 return ThisDeclInfo->CurrentDecl &&
858 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
859 !isUnionDecl();
860}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000861
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000862bool Sema::isClassTemplateDecl() {
863 if (!ThisDeclInfo)
864 return false;
865 if (!ThisDeclInfo->IsFilled)
866 inspectThisDecl();
867 return ThisDeclInfo->CurrentDecl &&
868 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
869}
870
871bool Sema::isFunctionTemplateDecl() {
872 if (!ThisDeclInfo)
873 return false;
874 if (!ThisDeclInfo->IsFilled)
875 inspectThisDecl();
876 return ThisDeclInfo->CurrentDecl &&
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000877 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000878}
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000879
880bool Sema::isObjCInterfaceDecl() {
881 if (!ThisDeclInfo)
882 return false;
883 if (!ThisDeclInfo->IsFilled)
884 inspectThisDecl();
885 return ThisDeclInfo->CurrentDecl &&
886 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
887}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000888
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000889bool Sema::isObjCProtocolDecl() {
890 if (!ThisDeclInfo)
891 return false;
892 if (!ThisDeclInfo->IsFilled)
893 inspectThisDecl();
894 return ThisDeclInfo->CurrentDecl &&
895 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
896}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000897
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000898ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000899 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000900 inspectThisDecl();
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000901 return ThisDeclInfo->ParamVars;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000902}
903
904void Sema::inspectThisDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000905 ThisDeclInfo->fill();
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000906}
907
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000908unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000909 ArrayRef<const ParmVarDecl *> ParamVars) {
910 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000911 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
912 if (II && II->getName() == Name)
913 return i;
914 }
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000915 if (Name == "..." && isFunctionOrMethodVariadic())
916 return ParamCommandComment::VarArgParamIndex;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000917 return ParamCommandComment::InvalidParamIndex;
918}
919
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000920namespace {
921class SimpleTypoCorrector {
922 StringRef Typo;
923 const unsigned MaxEditDistance;
924
925 const NamedDecl *BestDecl;
926 unsigned BestEditDistance;
927 unsigned BestIndex;
928 unsigned NextIndex;
929
930public:
931 SimpleTypoCorrector(StringRef Typo) :
932 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
933 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
934 BestIndex(0), NextIndex(0)
935 { }
936
937 void addDecl(const NamedDecl *ND);
938
939 const NamedDecl *getBestDecl() const {
940 if (BestEditDistance > MaxEditDistance)
941 return NULL;
942
943 return BestDecl;
944 }
945
946 unsigned getBestDeclIndex() const {
947 assert(getBestDecl());
948 return BestIndex;
949 }
950};
951
952void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
953 unsigned CurrIndex = NextIndex++;
954
955 const IdentifierInfo *II = ND->getIdentifier();
956 if (!II)
957 return;
958
959 StringRef Name = II->getName();
960 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
961 if (MinPossibleEditDistance > 0 &&
962 Typo.size() / MinPossibleEditDistance < 3)
963 return;
964
965 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
966 if (EditDistance < BestEditDistance) {
967 BestEditDistance = EditDistance;
968 BestDecl = ND;
969 BestIndex = CurrIndex;
970 }
971}
972} // unnamed namespace
973
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000974unsigned Sema::correctTypoInParmVarReference(
975 StringRef Typo,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000976 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000977 SimpleTypoCorrector Corrector(Typo);
978 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
979 Corrector.addDecl(ParamVars[i]);
980 if (Corrector.getBestDecl())
981 return Corrector.getBestDeclIndex();
982 else
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000983 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000984}
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000985
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000986namespace {
987bool ResolveTParamReferenceHelper(
988 StringRef Name,
989 const TemplateParameterList *TemplateParameters,
990 SmallVectorImpl<unsigned> *Position) {
991 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
992 const NamedDecl *Param = TemplateParameters->getParam(i);
993 const IdentifierInfo *II = Param->getIdentifier();
994 if (II && II->getName() == Name) {
995 Position->push_back(i);
996 return true;
997 }
998
999 if (const TemplateTemplateParmDecl *TTP =
1000 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1001 Position->push_back(i);
1002 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1003 Position))
1004 return true;
1005 Position->pop_back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001006 }
1007 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001008 return false;
1009}
1010} // unnamed namespace
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001011
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001012bool Sema::resolveTParamReference(
1013 StringRef Name,
1014 const TemplateParameterList *TemplateParameters,
1015 SmallVectorImpl<unsigned> *Position) {
1016 Position->clear();
1017 if (!TemplateParameters)
1018 return false;
1019
1020 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1021}
1022
1023namespace {
1024void CorrectTypoInTParamReferenceHelper(
1025 const TemplateParameterList *TemplateParameters,
1026 SimpleTypoCorrector &Corrector) {
1027 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1028 const NamedDecl *Param = TemplateParameters->getParam(i);
1029 Corrector.addDecl(Param);
1030
1031 if (const TemplateTemplateParmDecl *TTP =
1032 dyn_cast<TemplateTemplateParmDecl>(Param))
1033 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1034 Corrector);
1035 }
1036}
1037} // unnamed namespace
1038
1039StringRef Sema::correctTypoInTParamReference(
1040 StringRef Typo,
1041 const TemplateParameterList *TemplateParameters) {
1042 SimpleTypoCorrector Corrector(Typo);
1043 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1044 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1045 const IdentifierInfo *II = ND->getIdentifier();
1046 assert(II && "SimpleTypoCorrector should not return this decl");
1047 return II->getName();
1048 }
1049 return StringRef();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001050}
1051
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001052InlineCommandComment::RenderKind
1053Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +00001054 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001055
1056 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1057 .Case("b", InlineCommandComment::RenderBold)
1058 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1059 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1060 .Default(InlineCommandComment::RenderNormal);
1061}
1062
Dmitri Gribenkoec925312012-07-06 00:28:32 +00001063} // end namespace comments
1064} // end namespace clang
1065