blob: db30fb6aa39f4a81ad0c5c449c8ef78439bcdd61 [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()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000518 const HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000519 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000520 if (LastNotClosedTagName == TagName)
521 break;
522
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000523 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000524 continue;
525
526 bool OpenLineInvalid;
527 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000528 HST->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000529 &OpenLineInvalid);
530 bool CloseLineInvalid;
531 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000532 HET->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000533 &CloseLineInvalid);
534
535 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000536 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
537 << HST->getTagName() << HET->getTagName()
538 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000539 else {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000540 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
541 << HST->getTagName() << HET->getTagName()
542 << HST->getSourceRange();
543 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
544 << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000545 }
546 }
547
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000548 return HET;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000549}
550
551FullComment *Sema::actOnFullComment(
552 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian42e31322012-10-11 23:52:50 +0000553 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000554 resolveParamCommandIndexes(FC);
555 return FC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000556}
557
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000558void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkob37d5e82012-09-13 20:36:01 +0000559 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
560 return;
561
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000562 ParagraphComment *Paragraph = Command->getParagraph();
563 if (Paragraph->isWhitespace()) {
564 SourceLocation DiagLoc;
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000565 if (Command->getNumArgs() > 0)
566 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000567 if (!DiagLoc.isValid())
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000568 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000569 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000570 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000571 << Command->getCommandName(Traits)
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000572 << Command->getSourceRange();
573 }
574}
575
Dmitri Gribenko64305832012-08-03 21:15:32 +0000576void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000577 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000578 return;
579 if (isFunctionDecl()) {
580 if (ThisDeclInfo->ResultType->isVoidType()) {
581 unsigned DiagKind;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000582 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000583 default:
Dmitri Gribenko558babc2012-08-06 16:29:26 +0000584 if (ThisDeclInfo->IsObjCMethod)
585 DiagKind = 3;
586 else
587 DiagKind = 0;
Dmitri Gribenko64305832012-08-03 21:15:32 +0000588 break;
589 case Decl::CXXConstructor:
590 DiagKind = 1;
591 break;
592 case Decl::CXXDestructor:
593 DiagKind = 2;
594 break;
595 }
596 Diag(Command->getLocation(),
597 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000598 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000599 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000600 << DiagKind
601 << Command->getSourceRange();
602 }
603 return;
604 }
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000605 else if (isObjCPropertyDecl())
606 return;
607
Dmitri Gribenko64305832012-08-03 21:15:32 +0000608 Diag(Command->getLocation(),
609 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000610 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000611 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000612 << Command->getSourceRange();
613}
614
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000615void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000616 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000617 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000618 if (Info->IsBriefCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000619 if (!BriefCommand) {
620 BriefCommand = Command;
621 return;
622 }
623 PrevCommand = BriefCommand;
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +0000624 } else if (Info->IsHeaderfileCommand) {
625 if (!HeaderfileCommand) {
626 HeaderfileCommand = Command;
627 return;
628 }
629 PrevCommand = HeaderfileCommand;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000630 } else {
631 // We don't want to check this command for duplicates.
632 return;
633 }
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000634 StringRef CommandName = Command->getCommandName(Traits);
635 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000636 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000637 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000638 << CommandName
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000639 << Command->getSourceRange();
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000640 if (CommandName == PrevCommandName)
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000641 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000642 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000643 << PrevCommandName
644 << PrevCommand->getSourceRange();
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000645 else
646 Diag(PrevCommand->getLocation(),
647 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000648 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000649 << PrevCommandName
650 << CommandName;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000651}
652
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000653void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
654 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
655 return;
656
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000657 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000658 if (!D)
659 return;
660
661 if (D->hasAttr<DeprecatedAttr>() ||
662 D->hasAttr<AvailabilityAttr>() ||
663 D->hasAttr<UnavailableAttr>())
664 return;
665
666 Diag(Command->getLocation(),
667 diag::warn_doc_deprecated_not_sync)
668 << Command->getSourceRange();
669
670 // Try to emit a fixit with a deprecation attribute.
671 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
672 // Don't emit a Fix-It for non-member function definitions. GCC does not
673 // accept attributes on them.
674 const DeclContext *Ctx = FD->getDeclContext();
675 if ((!Ctx || !Ctx->isRecord()) &&
676 FD->doesThisDeclarationHaveABody())
677 return;
678
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000679 StringRef AttributeSpelling = "__attribute__((deprecated))";
680 if (PP) {
681 TokenValue Tokens[] = {
682 tok::kw___attribute, tok::l_paren, tok::l_paren,
683 PP->getIdentifierInfo("deprecated"),
684 tok::r_paren, tok::r_paren
685 };
686 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
687 Tokens);
688 if (!MacroName.empty())
689 AttributeSpelling = MacroName;
690 }
691
692 SmallString<64> TextToInsert(" ");
693 TextToInsert += AttributeSpelling;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000694 Diag(FD->getLocEnd(),
695 diag::note_add_deprecation_attr)
696 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000697 TextToInsert);
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000698 }
699}
700
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000701void Sema::resolveParamCommandIndexes(const FullComment *FC) {
702 if (!isFunctionDecl()) {
703 // We already warned that \\param commands are not attached to a function
704 // decl.
705 return;
706 }
707
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000708 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000709
710 // Comment AST nodes that correspond to \c ParamVars for which we have
711 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000712 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000713
714 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
NAKAMURA Takumidc2e2fb2013-06-19 06:58:14 +0000715 ParamVarDocs.resize(ParamVars.size(), NULL);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000716
717 // First pass over all \\param commands: resolve all parameter names.
718 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
719 I != E; ++I) {
720 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
721 if (!PCC || !PCC->hasParamName())
722 continue;
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000723 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000724
725 // Check that referenced parameter name is in the function decl.
726 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
727 ParamVars);
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000728 if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
729 PCC->setIsVarArgParam();
730 continue;
731 }
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000732 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
733 UnresolvedParamCommands.push_back(PCC);
734 continue;
735 }
736 PCC->setParamIndex(ResolvedParamIndex);
737 if (ParamVarDocs[ResolvedParamIndex]) {
738 SourceRange ArgRange = PCC->getParamNameRange();
739 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
740 << ParamName << ArgRange;
741 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
742 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
743 << PrevCommand->getParamNameRange();
744 }
745 ParamVarDocs[ResolvedParamIndex] = PCC;
746 }
747
748 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000749 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000750 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
751 if (!ParamVarDocs[i])
752 OrphanedParamDecls.push_back(ParamVars[i]);
753 }
754
755 // Second pass over unresolved \\param commands: do typo correction.
756 // Suggest corrections from a set of parameter declarations that have no
757 // corresponding \\param.
758 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
759 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
760
761 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000762 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000763 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
764 << ParamName << ArgRange;
765
766 // All parameters documented -- can't suggest a correction.
767 if (OrphanedParamDecls.size() == 0)
768 continue;
769
770 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
771 if (OrphanedParamDecls.size() == 1) {
772 // If one parameter is not documented then that parameter is the only
773 // possible suggestion.
774 CorrectedParamIndex = 0;
775 } else {
776 // Do typo correction.
777 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
778 OrphanedParamDecls);
779 }
780 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
781 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
782 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
783 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
784 << CorrectedII->getName()
785 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
786 }
787 }
788}
789
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000790bool Sema::isFunctionDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000791 if (!ThisDeclInfo)
792 return false;
793 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000794 inspectThisDecl();
Dmitri Gribenko37a7faf2012-08-02 21:45:39 +0000795 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000796}
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000797
Fariborz Jahaniana668bf52013-03-05 23:20:29 +0000798bool Sema::isAnyFunctionDecl() {
799 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
800 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
801}
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000802
803bool Sema::isFunctionOrMethodVariadic() {
804 if (!isAnyFunctionDecl() && !isObjCMethodDecl())
805 return false;
806 if (const FunctionDecl *FD =
807 dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
808 return FD->isVariadic();
809 if (const ObjCMethodDecl *MD =
810 dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
811 return MD->isVariadic();
812 return false;
813}
814
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000815bool Sema::isObjCMethodDecl() {
816 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
817 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
818}
Dmitri Gribenkoc0510b92013-06-24 01:33:34 +0000819
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000820bool Sema::isFunctionPointerVarDecl() {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000821 if (!ThisDeclInfo)
822 return false;
823 if (!ThisDeclInfo->IsFilled)
824 inspectThisDecl();
825 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
826 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
827 QualType QT = VD->getType();
828 return QT->isFunctionPointerType();
829 }
830 }
831 return false;
832}
833
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000834bool Sema::isObjCPropertyDecl() {
835 if (!ThisDeclInfo)
836 return false;
837 if (!ThisDeclInfo->IsFilled)
838 inspectThisDecl();
839 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
840}
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000841
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000842bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000843 if (!ThisDeclInfo)
844 return false;
845 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000846 inspectThisDecl();
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000847 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000848}
849
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000850bool Sema::isRecordLikeDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000851 if (!ThisDeclInfo)
852 return false;
853 if (!ThisDeclInfo->IsFilled)
854 inspectThisDecl();
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000855 return isUnionDecl() || isClassOrStructDecl()
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000856 || isObjCInterfaceDecl() || isObjCProtocolDecl();
857}
858
859bool Sema::isUnionDecl() {
860 if (!ThisDeclInfo)
861 return false;
862 if (!ThisDeclInfo->IsFilled)
863 inspectThisDecl();
864 if (const RecordDecl *RD =
865 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
866 return RD->isUnion();
867 return false;
868}
869
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000870bool Sema::isClassOrStructDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000871 if (!ThisDeclInfo)
872 return false;
873 if (!ThisDeclInfo->IsFilled)
874 inspectThisDecl();
875 return ThisDeclInfo->CurrentDecl &&
876 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
877 !isUnionDecl();
878}
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000879
880bool Sema::isClassTemplateDecl() {
881 if (!ThisDeclInfo)
882 return false;
883 if (!ThisDeclInfo->IsFilled)
884 inspectThisDecl();
885 return ThisDeclInfo->CurrentDecl &&
886 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
887}
888
889bool Sema::isFunctionTemplateDecl() {
890 if (!ThisDeclInfo)
891 return false;
892 if (!ThisDeclInfo->IsFilled)
893 inspectThisDecl();
894 return ThisDeclInfo->CurrentDecl &&
895 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
896}
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000897
898bool Sema::isObjCInterfaceDecl() {
899 if (!ThisDeclInfo)
900 return false;
901 if (!ThisDeclInfo->IsFilled)
902 inspectThisDecl();
903 return ThisDeclInfo->CurrentDecl &&
904 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
905}
906
907bool Sema::isObjCProtocolDecl() {
908 if (!ThisDeclInfo)
909 return false;
910 if (!ThisDeclInfo->IsFilled)
911 inspectThisDecl();
912 return ThisDeclInfo->CurrentDecl &&
913 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
914}
915
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000916ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000917 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000918 inspectThisDecl();
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000919 return ThisDeclInfo->ParamVars;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000920}
921
922void Sema::inspectThisDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000923 ThisDeclInfo->fill();
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000924}
925
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000926unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000927 ArrayRef<const ParmVarDecl *> ParamVars) {
928 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000929 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
930 if (II && II->getName() == Name)
931 return i;
932 }
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000933 if (Name == "..." && isFunctionOrMethodVariadic())
934 return ParamCommandComment::VarArgParamIndex;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000935 return ParamCommandComment::InvalidParamIndex;
936}
937
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000938namespace {
939class SimpleTypoCorrector {
940 StringRef Typo;
941 const unsigned MaxEditDistance;
942
943 const NamedDecl *BestDecl;
944 unsigned BestEditDistance;
945 unsigned BestIndex;
946 unsigned NextIndex;
947
948public:
949 SimpleTypoCorrector(StringRef Typo) :
950 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
951 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
952 BestIndex(0), NextIndex(0)
953 { }
954
955 void addDecl(const NamedDecl *ND);
956
957 const NamedDecl *getBestDecl() const {
958 if (BestEditDistance > MaxEditDistance)
959 return NULL;
960
961 return BestDecl;
962 }
963
964 unsigned getBestDeclIndex() const {
965 assert(getBestDecl());
966 return BestIndex;
967 }
968};
969
970void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
971 unsigned CurrIndex = NextIndex++;
972
973 const IdentifierInfo *II = ND->getIdentifier();
974 if (!II)
975 return;
976
977 StringRef Name = II->getName();
978 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
979 if (MinPossibleEditDistance > 0 &&
980 Typo.size() / MinPossibleEditDistance < 3)
981 return;
982
983 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
984 if (EditDistance < BestEditDistance) {
985 BestEditDistance = EditDistance;
986 BestDecl = ND;
987 BestIndex = CurrIndex;
988 }
989}
990} // unnamed namespace
991
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000992unsigned Sema::correctTypoInParmVarReference(
993 StringRef Typo,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000994 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000995 SimpleTypoCorrector Corrector(Typo);
996 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
997 Corrector.addDecl(ParamVars[i]);
998 if (Corrector.getBestDecl())
999 return Corrector.getBestDeclIndex();
1000 else
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001001 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001002}
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001003
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001004namespace {
1005bool ResolveTParamReferenceHelper(
1006 StringRef Name,
1007 const TemplateParameterList *TemplateParameters,
1008 SmallVectorImpl<unsigned> *Position) {
1009 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1010 const NamedDecl *Param = TemplateParameters->getParam(i);
1011 const IdentifierInfo *II = Param->getIdentifier();
1012 if (II && II->getName() == Name) {
1013 Position->push_back(i);
1014 return true;
1015 }
1016
1017 if (const TemplateTemplateParmDecl *TTP =
1018 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1019 Position->push_back(i);
1020 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1021 Position))
1022 return true;
1023 Position->pop_back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001024 }
1025 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001026 return false;
1027}
1028} // unnamed namespace
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001029
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001030bool Sema::resolveTParamReference(
1031 StringRef Name,
1032 const TemplateParameterList *TemplateParameters,
1033 SmallVectorImpl<unsigned> *Position) {
1034 Position->clear();
1035 if (!TemplateParameters)
1036 return false;
1037
1038 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1039}
1040
1041namespace {
1042void CorrectTypoInTParamReferenceHelper(
1043 const TemplateParameterList *TemplateParameters,
1044 SimpleTypoCorrector &Corrector) {
1045 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1046 const NamedDecl *Param = TemplateParameters->getParam(i);
1047 Corrector.addDecl(Param);
1048
1049 if (const TemplateTemplateParmDecl *TTP =
1050 dyn_cast<TemplateTemplateParmDecl>(Param))
1051 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1052 Corrector);
1053 }
1054}
1055} // unnamed namespace
1056
1057StringRef Sema::correctTypoInTParamReference(
1058 StringRef Typo,
1059 const TemplateParameterList *TemplateParameters) {
1060 SimpleTypoCorrector Corrector(Typo);
1061 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1062 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1063 const IdentifierInfo *II = ND->getIdentifier();
1064 assert(II && "SimpleTypoCorrector should not return this decl");
1065 return II->getName();
1066 }
1067 return StringRef();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001068}
1069
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001070InlineCommandComment::RenderKind
1071Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +00001072 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001073
1074 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1075 .Case("b", InlineCommandComment::RenderBold)
1076 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1077 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1078 .Default(InlineCommandComment::RenderNormal);
1079}
1080
Dmitri Gribenkoec925312012-07-06 00:28:32 +00001081} // end namespace comments
1082} // end namespace clang
1083