blob: c0dc647ca4536e0a5857b1503c3bf460b6fb9d5f [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}
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000125
126void 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
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000213void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
214 SourceLocation ArgLocBegin,
215 SourceLocation ArgLocEnd,
216 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000217 ParamCommandComment::PassDirection Direction;
218 std::string ArgLower = Arg.lower();
219 // TODO: optimize: lower Name first (need an API in SmallString for that),
220 // after that StringSwitch.
221 if (ArgLower == "[in]")
222 Direction = ParamCommandComment::In;
223 else if (ArgLower == "[out]")
224 Direction = ParamCommandComment::Out;
225 else if (ArgLower == "[in,out]" || ArgLower == "[out,in]")
226 Direction = ParamCommandComment::InOut;
227 else {
228 // Remove spaces.
229 std::string::iterator O = ArgLower.begin();
230 for (std::string::iterator I = ArgLower.begin(), E = ArgLower.end();
231 I != E; ++I) {
232 const char C = *I;
233 if (C != ' ' && C != '\n' && C != '\r' &&
234 C != '\t' && C != '\v' && C != '\f')
235 *O++ = C;
236 }
237 ArgLower.resize(O - ArgLower.begin());
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000238
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000239 bool RemovingWhitespaceHelped = false;
240 if (ArgLower == "[in]") {
241 Direction = ParamCommandComment::In;
242 RemovingWhitespaceHelped = true;
243 } else if (ArgLower == "[out]") {
244 Direction = ParamCommandComment::Out;
245 RemovingWhitespaceHelped = true;
246 } else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") {
247 Direction = ParamCommandComment::InOut;
248 RemovingWhitespaceHelped = true;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000249 } else {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000250 Direction = ParamCommandComment::In;
251 RemovingWhitespaceHelped = false;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000252 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000253
254 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
255 if (RemovingWhitespaceHelped)
256 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
257 << ArgRange
258 << FixItHint::CreateReplacement(
259 ArgRange,
260 ParamCommandComment::getDirectionAsString(Direction));
261 else
262 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction)
263 << ArgRange;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000264 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000265 Command->setDirection(Direction, /* Explicit = */ true);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000266}
267
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000268void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
269 SourceLocation ArgLocBegin,
270 SourceLocation ArgLocEnd,
271 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000272 // Parser will not feed us more arguments than needed.
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000273 assert(Command->getNumArgs() == 0);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000274
275 if (!Command->isDirectionExplicit()) {
276 // User didn't provide a direction argument.
277 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
278 }
279 typedef BlockCommandComment::Argument Argument;
280 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
281 ArgLocEnd),
282 Arg);
283 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000284}
285
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000286void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
287 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000288 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000289 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000290}
291
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000292TParamCommandComment *Sema::actOnTParamCommandStart(
293 SourceLocation LocBegin,
294 SourceLocation LocEnd,
295 unsigned CommandID,
296 CommandMarkerKind CommandMarker) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000297 TParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000298 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
299 CommandMarker);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000300
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000301 if (!isTemplateOrSpecialization())
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000302 Diag(Command->getLocation(),
303 diag::warn_doc_tparam_not_attached_to_a_template_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000304 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000305 << Command->getCommandNameRange(Traits);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000306
307 return Command;
308}
309
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000310void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
311 SourceLocation ArgLocBegin,
312 SourceLocation ArgLocEnd,
313 StringRef Arg) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000314 // Parser will not feed us more arguments than needed.
315 assert(Command->getNumArgs() == 0);
316
317 typedef BlockCommandComment::Argument Argument;
318 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
319 ArgLocEnd),
320 Arg);
321 Command->setArgs(llvm::makeArrayRef(A, 1));
322
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000323 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000324 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000325 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000326 }
327
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000328 const TemplateParameterList *TemplateParameters =
329 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000330 SmallVector<unsigned, 2> Position;
331 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
332 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
333 llvm::StringMap<TParamCommandComment *>::iterator PrevCommandIt =
334 TemplateParameterDocs.find(Arg);
335 if (PrevCommandIt != TemplateParameterDocs.end()) {
336 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
337 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
338 << Arg << ArgRange;
339 TParamCommandComment *PrevCommand = PrevCommandIt->second;
340 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
341 << PrevCommand->getParamNameRange();
342 }
343 TemplateParameterDocs[Arg] = Command;
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000344 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000345 }
346
347 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
348 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
349 << Arg << ArgRange;
350
351 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000352 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000353
354 StringRef CorrectedName;
355 if (TemplateParameters->size() == 1) {
356 const NamedDecl *Param = TemplateParameters->getParam(0);
357 const IdentifierInfo *II = Param->getIdentifier();
358 if (II)
359 CorrectedName = II->getName();
360 } else {
361 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
362 }
363
364 if (!CorrectedName.empty()) {
365 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
366 << CorrectedName
367 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
368 }
369
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000370 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000371}
372
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000373void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
374 ParagraphComment *Paragraph) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000375 Command->setParagraph(Paragraph);
376 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000377}
378
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000379InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
380 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000381 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000382 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000383 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000384 return new (Allocator) InlineCommandComment(
385 CommandLocBegin,
386 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000387 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000388 getInlineCommandRenderKind(CommandName),
389 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000390}
391
392InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
393 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000394 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000395 SourceLocation ArgLocBegin,
396 SourceLocation ArgLocEnd,
397 StringRef Arg) {
398 typedef InlineCommandComment::Argument Argument;
399 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
400 ArgLocEnd),
401 Arg);
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000402 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000403
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000404 return new (Allocator) InlineCommandComment(
405 CommandLocBegin,
406 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000407 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000408 getInlineCommandRenderKind(CommandName),
409 llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000410}
411
412InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
413 SourceLocation LocEnd,
Dmitri Gribenko9304d862012-09-11 19:22:03 +0000414 StringRef CommandName) {
415 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
416 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
417}
418
419InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
420 SourceLocation LocEnd,
421 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000422 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000423 return new (Allocator) InlineCommandComment(
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000424 LocBegin, LocEnd, CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000425 InlineCommandComment::RenderNormal,
426 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000427}
428
429TextComment *Sema::actOnText(SourceLocation LocBegin,
430 SourceLocation LocEnd,
431 StringRef Text) {
432 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
433}
434
435VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000436 unsigned CommandID) {
437 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000438 return new (Allocator) VerbatimBlockComment(
439 Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000440 Loc.getLocWithOffset(1 + CommandName.size()),
441 CommandID);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000442}
443
444VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
445 StringRef Text) {
446 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
447}
448
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000449void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000450 VerbatimBlockComment *Block,
451 SourceLocation CloseNameLocBegin,
452 StringRef CloseName,
453 ArrayRef<VerbatimBlockLineComment *> Lines) {
454 Block->setCloseName(CloseName, CloseNameLocBegin);
455 Block->setLines(Lines);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000456}
457
458VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000459 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000460 SourceLocation TextBegin,
461 StringRef Text) {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000462 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000463 LocBegin,
464 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000465 CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000466 TextBegin,
467 Text);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000468 checkFunctionDeclVerbatimLine(VL);
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000469 checkContainerDeclVerbatimLine(VL);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000470 return VL;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000471}
472
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000473HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
474 StringRef TagName) {
475 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000476}
477
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000478void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000479 HTMLStartTagComment *Tag,
480 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000481 SourceLocation GreaterLoc,
482 bool IsSelfClosing) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000483 Tag->setAttrs(Attrs);
484 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000485 if (IsSelfClosing)
486 Tag->setSelfClosing();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000487 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000488 HTMLOpenTags.push_back(Tag);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000489}
490
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000491HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
492 SourceLocation LocEnd,
493 StringRef TagName) {
494 HTMLEndTagComment *HET =
495 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
496 if (isHTMLEndTagForbidden(TagName)) {
497 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
498 << TagName << HET->getSourceRange();
499 return HET;
Dmitri Gribenko9460fbf2012-07-12 23:37:09 +0000500 }
501
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000502 bool FoundOpen = false;
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000503 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000504 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
505 I != E; ++I) {
506 if ((*I)->getTagName() == TagName) {
507 FoundOpen = true;
508 break;
509 }
510 }
511 if (!FoundOpen) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000512 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
513 << HET->getSourceRange();
514 return HET;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000515 }
516
517 while (!HTMLOpenTags.empty()) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000518 const HTMLStartTagComment *HST = HTMLOpenTags.back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000519 HTMLOpenTags.pop_back();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000520 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000521 if (LastNotClosedTagName == TagName)
522 break;
523
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000524 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000525 continue;
526
527 bool OpenLineInvalid;
528 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000529 HST->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000530 &OpenLineInvalid);
531 bool CloseLineInvalid;
532 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000533 HET->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000534 &CloseLineInvalid);
535
536 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
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() << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000540 else {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000541 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
542 << HST->getTagName() << HET->getTagName()
543 << HST->getSourceRange();
544 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
545 << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000546 }
547 }
548
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000549 return HET;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000550}
551
552FullComment *Sema::actOnFullComment(
553 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian42e31322012-10-11 23:52:50 +0000554 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000555 resolveParamCommandIndexes(FC);
556 return FC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000557}
558
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000559void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkob37d5e82012-09-13 20:36:01 +0000560 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
561 return;
562
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000563 ParagraphComment *Paragraph = Command->getParagraph();
564 if (Paragraph->isWhitespace()) {
565 SourceLocation DiagLoc;
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000566 if (Command->getNumArgs() > 0)
567 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000568 if (!DiagLoc.isValid())
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000569 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000570 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000571 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000572 << Command->getCommandName(Traits)
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000573 << Command->getSourceRange();
574 }
575}
576
Dmitri Gribenko64305832012-08-03 21:15:32 +0000577void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000578 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000579 return;
580 if (isFunctionDecl()) {
581 if (ThisDeclInfo->ResultType->isVoidType()) {
582 unsigned DiagKind;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000583 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000584 default:
Dmitri Gribenko558babc2012-08-06 16:29:26 +0000585 if (ThisDeclInfo->IsObjCMethod)
586 DiagKind = 3;
587 else
588 DiagKind = 0;
Dmitri Gribenko64305832012-08-03 21:15:32 +0000589 break;
590 case Decl::CXXConstructor:
591 DiagKind = 1;
592 break;
593 case Decl::CXXDestructor:
594 DiagKind = 2;
595 break;
596 }
597 Diag(Command->getLocation(),
598 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000599 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000600 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000601 << DiagKind
602 << Command->getSourceRange();
603 }
604 return;
605 }
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000606 else if (isObjCPropertyDecl())
607 return;
608
Dmitri Gribenko64305832012-08-03 21:15:32 +0000609 Diag(Command->getLocation(),
610 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000611 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000612 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000613 << Command->getSourceRange();
614}
615
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000616void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000617 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000618 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000619 if (Info->IsBriefCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000620 if (!BriefCommand) {
621 BriefCommand = Command;
622 return;
623 }
624 PrevCommand = BriefCommand;
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +0000625 } else if (Info->IsHeaderfileCommand) {
626 if (!HeaderfileCommand) {
627 HeaderfileCommand = Command;
628 return;
629 }
630 PrevCommand = HeaderfileCommand;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000631 } else {
632 // We don't want to check this command for duplicates.
633 return;
634 }
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000635 StringRef CommandName = Command->getCommandName(Traits);
636 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000637 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000638 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000639 << CommandName
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000640 << Command->getSourceRange();
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000641 if (CommandName == PrevCommandName)
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000642 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000643 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000644 << PrevCommandName
645 << PrevCommand->getSourceRange();
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000646 else
647 Diag(PrevCommand->getLocation(),
648 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000649 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000650 << PrevCommandName
651 << CommandName;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000652}
653
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000654void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
655 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
656 return;
657
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000658 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000659 if (!D)
660 return;
661
662 if (D->hasAttr<DeprecatedAttr>() ||
663 D->hasAttr<AvailabilityAttr>() ||
664 D->hasAttr<UnavailableAttr>())
665 return;
666
667 Diag(Command->getLocation(),
668 diag::warn_doc_deprecated_not_sync)
669 << Command->getSourceRange();
670
671 // Try to emit a fixit with a deprecation attribute.
672 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
673 // Don't emit a Fix-It for non-member function definitions. GCC does not
674 // accept attributes on them.
675 const DeclContext *Ctx = FD->getDeclContext();
676 if ((!Ctx || !Ctx->isRecord()) &&
677 FD->doesThisDeclarationHaveABody())
678 return;
679
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000680 StringRef AttributeSpelling = "__attribute__((deprecated))";
681 if (PP) {
682 TokenValue Tokens[] = {
683 tok::kw___attribute, tok::l_paren, tok::l_paren,
684 PP->getIdentifierInfo("deprecated"),
685 tok::r_paren, tok::r_paren
686 };
687 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
688 Tokens);
689 if (!MacroName.empty())
690 AttributeSpelling = MacroName;
691 }
692
693 SmallString<64> TextToInsert(" ");
694 TextToInsert += AttributeSpelling;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000695 Diag(FD->getLocEnd(),
696 diag::note_add_deprecation_attr)
697 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000698 TextToInsert);
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000699 }
700}
701
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000702void Sema::resolveParamCommandIndexes(const FullComment *FC) {
703 if (!isFunctionDecl()) {
704 // We already warned that \\param commands are not attached to a function
705 // decl.
706 return;
707 }
708
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000709 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000710
711 // Comment AST nodes that correspond to \c ParamVars for which we have
712 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000713 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000714
715 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
NAKAMURA Takumidc2e2fb2013-06-19 06:58:14 +0000716 ParamVarDocs.resize(ParamVars.size(), NULL);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000717
718 // First pass over all \\param commands: resolve all parameter names.
719 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
720 I != E; ++I) {
721 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
722 if (!PCC || !PCC->hasParamName())
723 continue;
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000724 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000725
726 // Check that referenced parameter name is in the function decl.
727 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
728 ParamVars);
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000729 if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
730 PCC->setIsVarArgParam();
731 continue;
732 }
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000733 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
734 UnresolvedParamCommands.push_back(PCC);
735 continue;
736 }
737 PCC->setParamIndex(ResolvedParamIndex);
738 if (ParamVarDocs[ResolvedParamIndex]) {
739 SourceRange ArgRange = PCC->getParamNameRange();
740 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
741 << ParamName << ArgRange;
742 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
743 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
744 << PrevCommand->getParamNameRange();
745 }
746 ParamVarDocs[ResolvedParamIndex] = PCC;
747 }
748
749 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000750 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000751 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
752 if (!ParamVarDocs[i])
753 OrphanedParamDecls.push_back(ParamVars[i]);
754 }
755
756 // Second pass over unresolved \\param commands: do typo correction.
757 // Suggest corrections from a set of parameter declarations that have no
758 // corresponding \\param.
759 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
760 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
761
762 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000763 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000764 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
765 << ParamName << ArgRange;
766
767 // All parameters documented -- can't suggest a correction.
768 if (OrphanedParamDecls.size() == 0)
769 continue;
770
771 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
772 if (OrphanedParamDecls.size() == 1) {
773 // If one parameter is not documented then that parameter is the only
774 // possible suggestion.
775 CorrectedParamIndex = 0;
776 } else {
777 // Do typo correction.
778 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
779 OrphanedParamDecls);
780 }
781 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
782 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
783 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
784 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
785 << CorrectedII->getName()
786 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
787 }
788 }
789}
790
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000791bool Sema::isFunctionDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000792 if (!ThisDeclInfo)
793 return false;
794 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000795 inspectThisDecl();
Dmitri Gribenko37a7faf2012-08-02 21:45:39 +0000796 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000797}
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000798
Fariborz Jahaniana668bf52013-03-05 23:20:29 +0000799bool Sema::isAnyFunctionDecl() {
800 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
801 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
802}
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000803
804bool Sema::isFunctionOrMethodVariadic() {
805 if (!isAnyFunctionDecl() && !isObjCMethodDecl())
806 return false;
807 if (const FunctionDecl *FD =
808 dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
809 return FD->isVariadic();
810 if (const ObjCMethodDecl *MD =
811 dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
812 return MD->isVariadic();
813 return false;
814}
815
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000816bool Sema::isObjCMethodDecl() {
817 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
818 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
819}
Dmitri Gribenkoc0510b92013-06-24 01:33:34 +0000820
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000821bool Sema::isFunctionPointerVarDecl() {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000822 if (!ThisDeclInfo)
823 return false;
824 if (!ThisDeclInfo->IsFilled)
825 inspectThisDecl();
826 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
827 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
828 QualType QT = VD->getType();
829 return QT->isFunctionPointerType();
830 }
831 }
832 return false;
833}
834
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000835bool Sema::isObjCPropertyDecl() {
836 if (!ThisDeclInfo)
837 return false;
838 if (!ThisDeclInfo->IsFilled)
839 inspectThisDecl();
840 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
841}
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000842
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000843bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000844 if (!ThisDeclInfo)
845 return false;
846 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000847 inspectThisDecl();
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000848 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000849}
850
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000851bool Sema::isRecordLikeDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000852 if (!ThisDeclInfo)
853 return false;
854 if (!ThisDeclInfo->IsFilled)
855 inspectThisDecl();
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000856 return isUnionDecl() || isClassOrStructDecl()
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000857 || isObjCInterfaceDecl() || isObjCProtocolDecl();
858}
859
860bool Sema::isUnionDecl() {
861 if (!ThisDeclInfo)
862 return false;
863 if (!ThisDeclInfo->IsFilled)
864 inspectThisDecl();
865 if (const RecordDecl *RD =
866 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
867 return RD->isUnion();
868 return false;
869}
870
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000871bool Sema::isClassOrStructDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000872 if (!ThisDeclInfo)
873 return false;
874 if (!ThisDeclInfo->IsFilled)
875 inspectThisDecl();
876 return ThisDeclInfo->CurrentDecl &&
877 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
878 !isUnionDecl();
879}
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000880
881bool Sema::isClassTemplateDecl() {
882 if (!ThisDeclInfo)
883 return false;
884 if (!ThisDeclInfo->IsFilled)
885 inspectThisDecl();
886 return ThisDeclInfo->CurrentDecl &&
887 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
888}
889
890bool Sema::isFunctionTemplateDecl() {
891 if (!ThisDeclInfo)
892 return false;
893 if (!ThisDeclInfo->IsFilled)
894 inspectThisDecl();
895 return ThisDeclInfo->CurrentDecl &&
896 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
897}
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000898
899bool Sema::isObjCInterfaceDecl() {
900 if (!ThisDeclInfo)
901 return false;
902 if (!ThisDeclInfo->IsFilled)
903 inspectThisDecl();
904 return ThisDeclInfo->CurrentDecl &&
905 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
906}
907
908bool Sema::isObjCProtocolDecl() {
909 if (!ThisDeclInfo)
910 return false;
911 if (!ThisDeclInfo->IsFilled)
912 inspectThisDecl();
913 return ThisDeclInfo->CurrentDecl &&
914 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
915}
916
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000917ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000918 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000919 inspectThisDecl();
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000920 return ThisDeclInfo->ParamVars;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000921}
922
923void Sema::inspectThisDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000924 ThisDeclInfo->fill();
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000925}
926
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000927unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000928 ArrayRef<const ParmVarDecl *> ParamVars) {
929 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000930 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
931 if (II && II->getName() == Name)
932 return i;
933 }
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000934 if (Name == "..." && isFunctionOrMethodVariadic())
935 return ParamCommandComment::VarArgParamIndex;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000936 return ParamCommandComment::InvalidParamIndex;
937}
938
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000939namespace {
940class SimpleTypoCorrector {
941 StringRef Typo;
942 const unsigned MaxEditDistance;
943
944 const NamedDecl *BestDecl;
945 unsigned BestEditDistance;
946 unsigned BestIndex;
947 unsigned NextIndex;
948
949public:
950 SimpleTypoCorrector(StringRef Typo) :
951 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
952 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
953 BestIndex(0), NextIndex(0)
954 { }
955
956 void addDecl(const NamedDecl *ND);
957
958 const NamedDecl *getBestDecl() const {
959 if (BestEditDistance > MaxEditDistance)
960 return NULL;
961
962 return BestDecl;
963 }
964
965 unsigned getBestDeclIndex() const {
966 assert(getBestDecl());
967 return BestIndex;
968 }
969};
970
971void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
972 unsigned CurrIndex = NextIndex++;
973
974 const IdentifierInfo *II = ND->getIdentifier();
975 if (!II)
976 return;
977
978 StringRef Name = II->getName();
979 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
980 if (MinPossibleEditDistance > 0 &&
981 Typo.size() / MinPossibleEditDistance < 3)
982 return;
983
984 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
985 if (EditDistance < BestEditDistance) {
986 BestEditDistance = EditDistance;
987 BestDecl = ND;
988 BestIndex = CurrIndex;
989 }
990}
991} // unnamed namespace
992
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000993unsigned Sema::correctTypoInParmVarReference(
994 StringRef Typo,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000995 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000996 SimpleTypoCorrector Corrector(Typo);
997 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
998 Corrector.addDecl(ParamVars[i]);
999 if (Corrector.getBestDecl())
1000 return Corrector.getBestDeclIndex();
1001 else
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001002 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001003}
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001004
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001005namespace {
1006bool ResolveTParamReferenceHelper(
1007 StringRef Name,
1008 const TemplateParameterList *TemplateParameters,
1009 SmallVectorImpl<unsigned> *Position) {
1010 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1011 const NamedDecl *Param = TemplateParameters->getParam(i);
1012 const IdentifierInfo *II = Param->getIdentifier();
1013 if (II && II->getName() == Name) {
1014 Position->push_back(i);
1015 return true;
1016 }
1017
1018 if (const TemplateTemplateParmDecl *TTP =
1019 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1020 Position->push_back(i);
1021 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1022 Position))
1023 return true;
1024 Position->pop_back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001025 }
1026 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001027 return false;
1028}
1029} // unnamed namespace
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001030
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001031bool Sema::resolveTParamReference(
1032 StringRef Name,
1033 const TemplateParameterList *TemplateParameters,
1034 SmallVectorImpl<unsigned> *Position) {
1035 Position->clear();
1036 if (!TemplateParameters)
1037 return false;
1038
1039 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1040}
1041
1042namespace {
1043void CorrectTypoInTParamReferenceHelper(
1044 const TemplateParameterList *TemplateParameters,
1045 SimpleTypoCorrector &Corrector) {
1046 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1047 const NamedDecl *Param = TemplateParameters->getParam(i);
1048 Corrector.addDecl(Param);
1049
1050 if (const TemplateTemplateParmDecl *TTP =
1051 dyn_cast<TemplateTemplateParmDecl>(Param))
1052 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1053 Corrector);
1054 }
1055}
1056} // unnamed namespace
1057
1058StringRef Sema::correctTypoInTParamReference(
1059 StringRef Typo,
1060 const TemplateParameterList *TemplateParameters) {
1061 SimpleTypoCorrector Corrector(Typo);
1062 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1063 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1064 const IdentifierInfo *II = ND->getIdentifier();
1065 assert(II && "SimpleTypoCorrector should not return this decl");
1066 return II->getName();
1067 }
1068 return StringRef();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001069}
1070
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001071InlineCommandComment::RenderKind
1072Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +00001073 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001074
1075 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1076 .Case("b", InlineCommandComment::RenderBold)
1077 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1078 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1079 .Default(InlineCommandComment::RenderNormal);
1080}
1081
Dmitri Gribenkoec925312012-07-06 00:28:32 +00001082} // end namespace comments
1083} // end namespace clang
1084