blob: 403454d3ab7e63eaeb43b3191168cbdaa3be5087 [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"
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +000026} // end anonymous namespace
Dmitri Gribenko3ca956f2012-08-31 02:21:44 +000027
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),
Craig Topper36250ad2014-05-12 05:36:57 +000032 PP(PP), ThisDeclInfo(nullptr), BriefCommand(nullptr),
33 HeaderfileCommand(nullptr) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000034}
35
36void Sema::setDecl(const Decl *D) {
Dmitri Gribenko527ab212012-08-01 23:08:09 +000037 if (!D)
38 return;
39
40 ThisDeclInfo = new (Allocator) DeclInfo;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +000041 ThisDeclInfo->CommentDecl = D;
Dmitri Gribenkoe6213dd2012-08-01 23:21:57 +000042 ThisDeclInfo->IsFilled = false;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000043}
44
45ParagraphComment *Sema::actOnParagraphComment(
46 ArrayRef<InlineContentComment *> Content) {
47 return new (Allocator) ParagraphComment(Content);
48}
49
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000050BlockCommandComment *Sema::actOnBlockCommandStart(
51 SourceLocation LocBegin,
52 SourceLocation LocEnd,
53 unsigned CommandID,
54 CommandMarkerKind CommandMarker) {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +000055 BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd,
56 CommandID,
57 CommandMarker);
58 checkContainerDecl(BC);
59 return BC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000060}
61
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +000062void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
63 ArrayRef<BlockCommandComment::Argument> Args) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +000064 Command->setArgs(Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +000065}
66
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +000067void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
68 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +000069 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000070 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +000071 checkBlockCommandDuplicate(Command);
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +000072 if (ThisDeclInfo) {
73 // These checks only make sense if the comment is attached to a
74 // declaration.
75 checkReturnsCommand(Command);
76 checkDeprecatedCommand(Command);
77 }
Dmitri Gribenkoec925312012-07-06 00:28:32 +000078}
79
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000080ParamCommandComment *Sema::actOnParamCommandStart(
81 SourceLocation LocBegin,
82 SourceLocation LocEnd,
83 unsigned CommandID,
84 CommandMarkerKind CommandMarker) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000085 ParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000086 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
87 CommandMarker);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000088
Alex Lorenz6b82a752017-04-21 14:17:49 +000089 if (!isFunctionDecl() && !isFunctionOrBlockPointerVarLikeDecl())
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000090 Diag(Command->getLocation(),
91 diag::warn_doc_param_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000092 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000093 << Command->getCommandNameRange(Traits);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000094
95 return Command;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000096}
97
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +000098void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
99 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000100 if (!Info->IsFunctionDeclarationCommand)
101 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000102
103 unsigned DiagSelect;
104 switch (Comment->getCommandID()) {
105 case CommandTraits::KCI_function:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000106 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000107 break;
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000108 case CommandTraits::KCI_functiongroup:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000109 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0;
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000110 break;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000111 case CommandTraits::KCI_method:
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000112 DiagSelect = !isObjCMethodDecl() ? 3 : 0;
113 break;
114 case CommandTraits::KCI_methodgroup:
115 DiagSelect = !isObjCMethodDecl() ? 4 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000116 break;
117 case CommandTraits::KCI_callback:
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000118 DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000119 break;
120 default:
121 DiagSelect = 0;
122 break;
123 }
Fariborz Jahanian41bb7132013-03-06 17:36:51 +0000124 if (DiagSelect)
125 Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
126 << Comment->getCommandMarker()
127 << (DiagSelect-1) << (DiagSelect-1)
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000128 << Comment->getSourceRange();
129}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000130
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000131void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
132 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000133 if (!Info->IsRecordLikeDeclarationCommand)
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000134 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000135 unsigned DiagSelect;
136 switch (Comment->getCommandID()) {
137 case CommandTraits::KCI_class:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000138 DiagSelect = (!isClassOrStructDecl() && !isClassTemplateDecl()) ? 1 : 0;
Fariborz Jahanian04eb8ab2013-05-20 23:40:39 +0000139 // Allow @class command on @interface declarations.
140 // FIXME. Currently, \class and @class are indistinguishable. So,
141 // \class is also allowed on an @interface declaration
142 if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
143 DiagSelect = 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000144 break;
145 case CommandTraits::KCI_interface:
146 DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
147 break;
148 case CommandTraits::KCI_protocol:
149 DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
150 break;
151 case CommandTraits::KCI_struct:
152 DiagSelect = !isClassOrStructDecl() ? 4 : 0;
153 break;
154 case CommandTraits::KCI_union:
155 DiagSelect = !isUnionDecl() ? 5 : 0;
156 break;
157 default:
158 DiagSelect = 0;
159 break;
160 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000161 if (DiagSelect)
162 Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
163 << Comment->getCommandMarker()
164 << (DiagSelect-1) << (DiagSelect-1)
165 << Comment->getSourceRange();
166}
167
168void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
169 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000170 if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000171 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000172 unsigned DiagSelect;
173 switch (Comment->getCommandID()) {
174 case CommandTraits::KCI_classdesign:
175 DiagSelect = 1;
176 break;
177 case CommandTraits::KCI_coclass:
178 DiagSelect = 2;
179 break;
180 case CommandTraits::KCI_dependency:
181 DiagSelect = 3;
182 break;
183 case CommandTraits::KCI_helper:
184 DiagSelect = 4;
185 break;
186 case CommandTraits::KCI_helperclass:
187 DiagSelect = 5;
188 break;
189 case CommandTraits::KCI_helps:
190 DiagSelect = 6;
191 break;
192 case CommandTraits::KCI_instancesize:
193 DiagSelect = 7;
194 break;
195 case CommandTraits::KCI_ownership:
196 DiagSelect = 8;
197 break;
198 case CommandTraits::KCI_performance:
199 DiagSelect = 9;
200 break;
201 case CommandTraits::KCI_security:
202 DiagSelect = 10;
203 break;
204 case CommandTraits::KCI_superclass:
205 DiagSelect = 11;
206 break;
207 default:
208 DiagSelect = 0;
209 break;
210 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000211 if (DiagSelect)
212 Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
213 << Comment->getCommandMarker()
214 << (DiagSelect-1)
215 << Comment->getSourceRange();
216}
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000217
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000218/// \brief Turn a string into the corresponding PassDirection or -1 if it's not
219/// valid.
220static int getParamPassDirection(StringRef Arg) {
221 return llvm::StringSwitch<int>(Arg)
222 .Case("[in]", ParamCommandComment::In)
223 .Case("[out]", ParamCommandComment::Out)
224 .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
225 .Default(-1);
226}
227
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000228void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
229 SourceLocation ArgLocBegin,
230 SourceLocation ArgLocEnd,
231 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000232 std::string ArgLower = Arg.lower();
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000233 int Direction = getParamPassDirection(ArgLower);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000234
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000235 if (Direction == -1) {
236 // Try again with whitespace removed.
237 ArgLower.erase(
238 std::remove_if(ArgLower.begin(), ArgLower.end(), clang::isWhitespace),
239 ArgLower.end());
240 Direction = getParamPassDirection(ArgLower);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000241
242 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000243 if (Direction != -1) {
244 const char *FixedName = ParamCommandComment::getDirectionAsString(
245 (ParamCommandComment::PassDirection)Direction);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000246 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000247 << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
248 } else {
249 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
250 Direction = ParamCommandComment::In; // Sane fall back.
251 }
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000252 }
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000253 Command->setDirection((ParamCommandComment::PassDirection)Direction,
254 /*Explicit=*/true);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000255}
256
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000257void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
258 SourceLocation ArgLocBegin,
259 SourceLocation ArgLocEnd,
260 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000261 // Parser will not feed us more arguments than needed.
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000262 assert(Command->getNumArgs() == 0);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000263
264 if (!Command->isDirectionExplicit()) {
265 // User didn't provide a direction argument.
266 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
267 }
268 typedef BlockCommandComment::Argument Argument;
269 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
270 ArgLocEnd),
271 Arg);
272 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000273}
274
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000275void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
276 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000277 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000278 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000279}
280
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000281TParamCommandComment *Sema::actOnTParamCommandStart(
282 SourceLocation LocBegin,
283 SourceLocation LocEnd,
284 unsigned CommandID,
285 CommandMarkerKind CommandMarker) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000286 TParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000287 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
288 CommandMarker);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000289
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000290 if (!isTemplateOrSpecialization())
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000291 Diag(Command->getLocation(),
292 diag::warn_doc_tparam_not_attached_to_a_template_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000293 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000294 << Command->getCommandNameRange(Traits);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000295
296 return Command;
297}
298
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000299void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
300 SourceLocation ArgLocBegin,
301 SourceLocation ArgLocEnd,
302 StringRef Arg) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000303 // Parser will not feed us more arguments than needed.
304 assert(Command->getNumArgs() == 0);
305
306 typedef BlockCommandComment::Argument Argument;
307 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
308 ArgLocEnd),
309 Arg);
310 Command->setArgs(llvm::makeArrayRef(A, 1));
311
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000312 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000313 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000314 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000315 }
316
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000317 const TemplateParameterList *TemplateParameters =
318 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000319 SmallVector<unsigned, 2> Position;
320 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
321 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
Benjamin Kramereed80612013-11-10 16:55:11 +0000322 TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg];
323 if (PrevCommand) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000324 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
325 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
326 << Arg << ArgRange;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000327 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
328 << PrevCommand->getParamNameRange();
329 }
Benjamin Kramereed80612013-11-10 16:55:11 +0000330 PrevCommand = Command;
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000331 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000332 }
333
334 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
335 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
336 << Arg << ArgRange;
337
338 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000339 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000340
341 StringRef CorrectedName;
342 if (TemplateParameters->size() == 1) {
343 const NamedDecl *Param = TemplateParameters->getParam(0);
344 const IdentifierInfo *II = Param->getIdentifier();
345 if (II)
346 CorrectedName = II->getName();
347 } else {
348 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
349 }
350
351 if (!CorrectedName.empty()) {
352 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
353 << CorrectedName
354 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
355 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000356}
357
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000358void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
359 ParagraphComment *Paragraph) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000360 Command->setParagraph(Paragraph);
361 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000362}
363
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000364InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
365 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000366 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000367 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000368 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000369 return new (Allocator) InlineCommandComment(
370 CommandLocBegin,
371 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000372 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000373 getInlineCommandRenderKind(CommandName),
374 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000375}
376
377InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
378 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000379 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000380 SourceLocation ArgLocBegin,
381 SourceLocation ArgLocEnd,
382 StringRef Arg) {
383 typedef InlineCommandComment::Argument Argument;
384 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
385 ArgLocEnd),
386 Arg);
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000387 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000388
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000389 return new (Allocator) InlineCommandComment(
390 CommandLocBegin,
391 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000392 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000393 getInlineCommandRenderKind(CommandName),
394 llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000395}
396
397InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
398 SourceLocation LocEnd,
Dmitri Gribenko9304d862012-09-11 19:22:03 +0000399 StringRef CommandName) {
400 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
401 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
402}
403
404InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
405 SourceLocation LocEnd,
406 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000407 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000408 return new (Allocator) InlineCommandComment(
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000409 LocBegin, LocEnd, CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000410 InlineCommandComment::RenderNormal,
411 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000412}
413
414TextComment *Sema::actOnText(SourceLocation LocBegin,
415 SourceLocation LocEnd,
416 StringRef Text) {
417 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
418}
419
420VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000421 unsigned CommandID) {
422 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000423 return new (Allocator) VerbatimBlockComment(
424 Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000425 Loc.getLocWithOffset(1 + CommandName.size()),
426 CommandID);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000427}
428
429VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
430 StringRef Text) {
431 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
432}
433
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000434void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000435 VerbatimBlockComment *Block,
436 SourceLocation CloseNameLocBegin,
437 StringRef CloseName,
438 ArrayRef<VerbatimBlockLineComment *> Lines) {
439 Block->setCloseName(CloseName, CloseNameLocBegin);
440 Block->setLines(Lines);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000441}
442
443VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000444 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000445 SourceLocation TextBegin,
446 StringRef Text) {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000447 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000448 LocBegin,
449 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000450 CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000451 TextBegin,
452 Text);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000453 checkFunctionDeclVerbatimLine(VL);
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000454 checkContainerDeclVerbatimLine(VL);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000455 return VL;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000456}
457
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000458HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
459 StringRef TagName) {
460 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000461}
462
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000463void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000464 HTMLStartTagComment *Tag,
465 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000466 SourceLocation GreaterLoc,
467 bool IsSelfClosing) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000468 Tag->setAttrs(Attrs);
469 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000470 if (IsSelfClosing)
471 Tag->setSelfClosing();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000472 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000473 HTMLOpenTags.push_back(Tag);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000474}
475
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000476HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
477 SourceLocation LocEnd,
478 StringRef TagName) {
479 HTMLEndTagComment *HET =
480 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
481 if (isHTMLEndTagForbidden(TagName)) {
482 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
483 << TagName << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000484 HET->setIsMalformed();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000485 return HET;
Dmitri Gribenko9460fbf2012-07-12 23:37:09 +0000486 }
487
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000488 bool FoundOpen = false;
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000489 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000490 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
491 I != E; ++I) {
492 if ((*I)->getTagName() == TagName) {
493 FoundOpen = true;
494 break;
495 }
496 }
497 if (!FoundOpen) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000498 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
499 << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000500 HET->setIsMalformed();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000501 return HET;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000502 }
503
504 while (!HTMLOpenTags.empty()) {
Dmitri Gribenko93043622014-04-22 10:59:13 +0000505 HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000506 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenko93043622014-04-22 10:59:13 +0000507 if (LastNotClosedTagName == TagName) {
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000508 // If the start tag is malformed, end tag is malformed as well.
509 if (HST->isMalformed())
510 HET->setIsMalformed();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000511 break;
Dmitri Gribenko93043622014-04-22 10:59:13 +0000512 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000513
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000514 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000515 continue;
516
517 bool OpenLineInvalid;
518 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000519 HST->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000520 &OpenLineInvalid);
521 bool CloseLineInvalid;
522 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000523 HET->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000524 &CloseLineInvalid);
525
Dmitri Gribenko93043622014-04-22 10:59:13 +0000526 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000527 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
528 << HST->getTagName() << HET->getTagName()
529 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000530 HST->setIsMalformed();
Dmitri Gribenko93043622014-04-22 10:59:13 +0000531 } else {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000532 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
533 << HST->getTagName() << HET->getTagName()
534 << HST->getSourceRange();
535 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
536 << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000537 HST->setIsMalformed();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000538 }
539 }
540
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000541 return HET;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000542}
543
544FullComment *Sema::actOnFullComment(
545 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian42e31322012-10-11 23:52:50 +0000546 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000547 resolveParamCommandIndexes(FC);
Dmitri Gribenko93043622014-04-22 10:59:13 +0000548
549 // Complain about HTML tags that are not closed.
550 while (!HTMLOpenTags.empty()) {
551 HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
552 if (isHTMLEndTagOptional(HST->getTagName()))
553 continue;
554
555 Diag(HST->getLocation(), diag::warn_doc_html_missing_end_tag)
556 << HST->getTagName() << HST->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000557 HST->setIsMalformed();
Dmitri Gribenko93043622014-04-22 10:59:13 +0000558 }
559
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000560 return FC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000561}
562
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000563void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkob37d5e82012-09-13 20:36:01 +0000564 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
565 return;
566
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000567 ParagraphComment *Paragraph = Command->getParagraph();
568 if (Paragraph->isWhitespace()) {
569 SourceLocation DiagLoc;
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000570 if (Command->getNumArgs() > 0)
571 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000572 if (!DiagLoc.isValid())
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000573 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000574 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000575 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000576 << Command->getCommandName(Traits)
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000577 << Command->getSourceRange();
578 }
579}
580
Dmitri Gribenko64305832012-08-03 21:15:32 +0000581void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000582 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000583 return;
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +0000584
585 assert(ThisDeclInfo && "should not call this check on a bare comment");
586
Alex Lorenz00353a02017-04-26 13:09:28 +0000587 // We allow the return command for all @properties because it can be used
588 // to document the value that the property getter returns.
589 if (isObjCPropertyDecl())
590 return;
Alex Lorenz6b82a752017-04-21 14:17:49 +0000591 if (isFunctionDecl() || isFunctionOrBlockPointerVarLikeDecl()) {
Alp Toker314cc812014-01-25 16:55:45 +0000592 if (ThisDeclInfo->ReturnType->isVoidType()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000593 unsigned DiagKind;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000594 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000595 default:
Dmitri Gribenko558babc2012-08-06 16:29:26 +0000596 if (ThisDeclInfo->IsObjCMethod)
597 DiagKind = 3;
598 else
599 DiagKind = 0;
Dmitri Gribenko64305832012-08-03 21:15:32 +0000600 break;
601 case Decl::CXXConstructor:
602 DiagKind = 1;
603 break;
604 case Decl::CXXDestructor:
605 DiagKind = 2;
606 break;
607 }
608 Diag(Command->getLocation(),
609 diag::warn_doc_returns_attached_to_a_void_function)
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 << DiagKind
613 << Command->getSourceRange();
614 }
615 return;
616 }
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000617
Dmitri Gribenko64305832012-08-03 21:15:32 +0000618 Diag(Command->getLocation(),
619 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000620 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000621 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000622 << Command->getSourceRange();
623}
624
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000625void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000626 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Craig Topper36250ad2014-05-12 05:36:57 +0000627 const BlockCommandComment *PrevCommand = nullptr;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000628 if (Info->IsBriefCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000629 if (!BriefCommand) {
630 BriefCommand = Command;
631 return;
632 }
633 PrevCommand = BriefCommand;
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +0000634 } else if (Info->IsHeaderfileCommand) {
635 if (!HeaderfileCommand) {
636 HeaderfileCommand = Command;
637 return;
638 }
639 PrevCommand = HeaderfileCommand;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000640 } else {
641 // We don't want to check this command for duplicates.
642 return;
643 }
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000644 StringRef CommandName = Command->getCommandName(Traits);
645 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000646 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000647 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000648 << CommandName
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000649 << Command->getSourceRange();
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000650 if (CommandName == PrevCommandName)
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000651 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000652 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000653 << PrevCommandName
654 << PrevCommand->getSourceRange();
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000655 else
656 Diag(PrevCommand->getLocation(),
657 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000658 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000659 << PrevCommandName
660 << CommandName;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000661}
662
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000663void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
664 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
665 return;
666
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +0000667 assert(ThisDeclInfo && "should not call this check on a bare comment");
668
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000669 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000670 if (!D)
671 return;
672
673 if (D->hasAttr<DeprecatedAttr>() ||
674 D->hasAttr<AvailabilityAttr>() ||
675 D->hasAttr<UnavailableAttr>())
676 return;
677
678 Diag(Command->getLocation(),
679 diag::warn_doc_deprecated_not_sync)
680 << Command->getSourceRange();
681
682 // Try to emit a fixit with a deprecation attribute.
683 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
684 // Don't emit a Fix-It for non-member function definitions. GCC does not
685 // accept attributes on them.
686 const DeclContext *Ctx = FD->getDeclContext();
687 if ((!Ctx || !Ctx->isRecord()) &&
688 FD->doesThisDeclarationHaveABody())
689 return;
690
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000691 StringRef AttributeSpelling = "__attribute__((deprecated))";
692 if (PP) {
693 TokenValue Tokens[] = {
694 tok::kw___attribute, tok::l_paren, tok::l_paren,
695 PP->getIdentifierInfo("deprecated"),
696 tok::r_paren, tok::r_paren
697 };
698 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
699 Tokens);
700 if (!MacroName.empty())
701 AttributeSpelling = MacroName;
702 }
703
704 SmallString<64> TextToInsert(" ");
705 TextToInsert += AttributeSpelling;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000706 Diag(FD->getLocEnd(),
707 diag::note_add_deprecation_attr)
708 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000709 TextToInsert);
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000710 }
711}
712
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000713void Sema::resolveParamCommandIndexes(const FullComment *FC) {
714 if (!isFunctionDecl()) {
715 // We already warned that \\param commands are not attached to a function
716 // decl.
717 return;
718 }
719
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000720 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000721
722 // Comment AST nodes that correspond to \c ParamVars for which we have
723 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000724 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000725
726 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
Craig Topper36250ad2014-05-12 05:36:57 +0000727 ParamVarDocs.resize(ParamVars.size(), nullptr);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000728
729 // First pass over all \\param commands: resolve all parameter names.
730 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
731 I != E; ++I) {
732 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
733 if (!PCC || !PCC->hasParamName())
734 continue;
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000735 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000736
737 // Check that referenced parameter name is in the function decl.
738 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
739 ParamVars);
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000740 if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
741 PCC->setIsVarArgParam();
742 continue;
743 }
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000744 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
745 UnresolvedParamCommands.push_back(PCC);
746 continue;
747 }
748 PCC->setParamIndex(ResolvedParamIndex);
749 if (ParamVarDocs[ResolvedParamIndex]) {
750 SourceRange ArgRange = PCC->getParamNameRange();
751 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
752 << ParamName << ArgRange;
753 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
754 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
755 << PrevCommand->getParamNameRange();
756 }
757 ParamVarDocs[ResolvedParamIndex] = PCC;
758 }
759
760 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000761 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000762 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
763 if (!ParamVarDocs[i])
764 OrphanedParamDecls.push_back(ParamVars[i]);
765 }
766
767 // Second pass over unresolved \\param commands: do typo correction.
768 // Suggest corrections from a set of parameter declarations that have no
769 // corresponding \\param.
770 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
771 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
772
773 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000774 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000775 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
776 << ParamName << ArgRange;
777
778 // All parameters documented -- can't suggest a correction.
779 if (OrphanedParamDecls.size() == 0)
780 continue;
781
782 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
783 if (OrphanedParamDecls.size() == 1) {
784 // If one parameter is not documented then that parameter is the only
785 // possible suggestion.
786 CorrectedParamIndex = 0;
787 } else {
788 // Do typo correction.
789 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
790 OrphanedParamDecls);
791 }
792 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
793 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
794 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
795 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
796 << CorrectedII->getName()
797 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
798 }
799 }
800}
801
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000802bool Sema::isFunctionDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000803 if (!ThisDeclInfo)
804 return false;
805 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000806 inspectThisDecl();
Dmitri Gribenko37a7faf2012-08-02 21:45:39 +0000807 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000808}
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000809
Fariborz Jahaniana668bf52013-03-05 23:20:29 +0000810bool Sema::isAnyFunctionDecl() {
811 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
812 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
813}
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000814
815bool Sema::isFunctionOrMethodVariadic() {
Dmitri Gribenkod9eb05a2014-03-19 13:59:36 +0000816 if (!isAnyFunctionDecl() && !isObjCMethodDecl() && !isFunctionTemplateDecl())
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000817 return false;
818 if (const FunctionDecl *FD =
819 dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
820 return FD->isVariadic();
Dmitri Gribenkod9eb05a2014-03-19 13:59:36 +0000821 if (const FunctionTemplateDecl *FTD =
822 dyn_cast<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl))
823 return FTD->getTemplatedDecl()->isVariadic();
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000824 if (const ObjCMethodDecl *MD =
825 dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
826 return MD->isVariadic();
827 return false;
828}
829
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000830bool Sema::isObjCMethodDecl() {
831 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
832 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
833}
Dmitri Gribenkoc0510b92013-06-24 01:33:34 +0000834
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000835bool Sema::isFunctionPointerVarDecl() {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000836 if (!ThisDeclInfo)
837 return false;
838 if (!ThisDeclInfo->IsFilled)
839 inspectThisDecl();
840 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
841 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
842 QualType QT = VD->getType();
843 return QT->isFunctionPointerType();
844 }
845 }
846 return false;
847}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000848
Alex Lorenz6b82a752017-04-21 14:17:49 +0000849bool Sema::isFunctionOrBlockPointerVarLikeDecl() {
850 if (!ThisDeclInfo)
851 return false;
852 if (!ThisDeclInfo->IsFilled)
853 inspectThisDecl();
854 if (ThisDeclInfo->getKind() != DeclInfo::VariableKind ||
855 !ThisDeclInfo->CurrentDecl)
856 return false;
857 QualType QT;
858 if (const auto *VD = dyn_cast<DeclaratorDecl>(ThisDeclInfo->CurrentDecl))
859 QT = VD->getType();
860 else if (const auto *PD =
861 dyn_cast<ObjCPropertyDecl>(ThisDeclInfo->CurrentDecl))
862 QT = PD->getType();
863 else
864 return false;
865 // We would like to warn about the 'returns'/'param' commands for
866 // variables that don't directly specify the function type, so type aliases
867 // can be ignored.
868 if (QT->getAs<TypedefType>())
869 return false;
870 return QT->isFunctionPointerType() || QT->isBlockPointerType();
871}
872
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000873bool Sema::isObjCPropertyDecl() {
874 if (!ThisDeclInfo)
875 return false;
876 if (!ThisDeclInfo->IsFilled)
877 inspectThisDecl();
878 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
879}
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000880
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000881bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000882 if (!ThisDeclInfo)
883 return false;
884 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000885 inspectThisDecl();
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000886 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000887}
888
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000889bool Sema::isRecordLikeDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000890 if (!ThisDeclInfo)
891 return false;
892 if (!ThisDeclInfo->IsFilled)
893 inspectThisDecl();
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000894 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
895 isObjCProtocolDecl();
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000896}
897
898bool Sema::isUnionDecl() {
899 if (!ThisDeclInfo)
900 return false;
901 if (!ThisDeclInfo->IsFilled)
902 inspectThisDecl();
903 if (const RecordDecl *RD =
904 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
905 return RD->isUnion();
906 return false;
907}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000908
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000909bool Sema::isClassOrStructDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000910 if (!ThisDeclInfo)
911 return false;
912 if (!ThisDeclInfo->IsFilled)
913 inspectThisDecl();
914 return ThisDeclInfo->CurrentDecl &&
915 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
916 !isUnionDecl();
917}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000918
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000919bool Sema::isClassTemplateDecl() {
920 if (!ThisDeclInfo)
921 return false;
922 if (!ThisDeclInfo->IsFilled)
923 inspectThisDecl();
924 return ThisDeclInfo->CurrentDecl &&
925 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
926}
927
928bool Sema::isFunctionTemplateDecl() {
929 if (!ThisDeclInfo)
930 return false;
931 if (!ThisDeclInfo->IsFilled)
932 inspectThisDecl();
933 return ThisDeclInfo->CurrentDecl &&
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000934 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000935}
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000936
937bool Sema::isObjCInterfaceDecl() {
938 if (!ThisDeclInfo)
939 return false;
940 if (!ThisDeclInfo->IsFilled)
941 inspectThisDecl();
942 return ThisDeclInfo->CurrentDecl &&
943 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
944}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000945
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000946bool Sema::isObjCProtocolDecl() {
947 if (!ThisDeclInfo)
948 return false;
949 if (!ThisDeclInfo->IsFilled)
950 inspectThisDecl();
951 return ThisDeclInfo->CurrentDecl &&
952 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
953}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000954
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000955ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000956 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000957 inspectThisDecl();
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000958 return ThisDeclInfo->ParamVars;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000959}
960
961void Sema::inspectThisDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000962 ThisDeclInfo->fill();
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000963}
964
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000965unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000966 ArrayRef<const ParmVarDecl *> ParamVars) {
967 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000968 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
969 if (II && II->getName() == Name)
970 return i;
971 }
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000972 if (Name == "..." && isFunctionOrMethodVariadic())
973 return ParamCommandComment::VarArgParamIndex;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000974 return ParamCommandComment::InvalidParamIndex;
975}
976
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000977namespace {
978class SimpleTypoCorrector {
Saleem Abdulrasool61c0b0c2016-08-28 21:33:30 +0000979 const NamedDecl *BestDecl;
980
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000981 StringRef Typo;
982 const unsigned MaxEditDistance;
983
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000984 unsigned BestEditDistance;
985 unsigned BestIndex;
986 unsigned NextIndex;
987
988public:
Saleem Abdulrasool61c0b0c2016-08-28 21:33:30 +0000989 explicit SimpleTypoCorrector(StringRef Typo)
990 : BestDecl(nullptr), Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
991 BestEditDistance(MaxEditDistance + 1), BestIndex(0), NextIndex(0) {}
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000992
993 void addDecl(const NamedDecl *ND);
994
995 const NamedDecl *getBestDecl() const {
996 if (BestEditDistance > MaxEditDistance)
Craig Topper36250ad2014-05-12 05:36:57 +0000997 return nullptr;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000998
999 return BestDecl;
1000 }
1001
1002 unsigned getBestDeclIndex() const {
1003 assert(getBestDecl());
1004 return BestIndex;
1005 }
1006};
1007
1008void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
1009 unsigned CurrIndex = NextIndex++;
1010
1011 const IdentifierInfo *II = ND->getIdentifier();
1012 if (!II)
1013 return;
1014
1015 StringRef Name = II->getName();
1016 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
1017 if (MinPossibleEditDistance > 0 &&
1018 Typo.size() / MinPossibleEditDistance < 3)
1019 return;
1020
1021 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
1022 if (EditDistance < BestEditDistance) {
1023 BestEditDistance = EditDistance;
1024 BestDecl = ND;
1025 BestIndex = CurrIndex;
1026 }
1027}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001028} // end anonymous namespace
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001029
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001030unsigned Sema::correctTypoInParmVarReference(
1031 StringRef Typo,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +00001032 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001033 SimpleTypoCorrector Corrector(Typo);
1034 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
1035 Corrector.addDecl(ParamVars[i]);
1036 if (Corrector.getBestDecl())
1037 return Corrector.getBestDeclIndex();
1038 else
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001039 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001040}
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001041
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001042namespace {
1043bool ResolveTParamReferenceHelper(
1044 StringRef Name,
1045 const TemplateParameterList *TemplateParameters,
1046 SmallVectorImpl<unsigned> *Position) {
1047 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1048 const NamedDecl *Param = TemplateParameters->getParam(i);
1049 const IdentifierInfo *II = Param->getIdentifier();
1050 if (II && II->getName() == Name) {
1051 Position->push_back(i);
1052 return true;
1053 }
1054
1055 if (const TemplateTemplateParmDecl *TTP =
1056 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1057 Position->push_back(i);
1058 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1059 Position))
1060 return true;
1061 Position->pop_back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001062 }
1063 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001064 return false;
1065}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001066} // end anonymous namespace
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001067
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001068bool Sema::resolveTParamReference(
1069 StringRef Name,
1070 const TemplateParameterList *TemplateParameters,
1071 SmallVectorImpl<unsigned> *Position) {
1072 Position->clear();
1073 if (!TemplateParameters)
1074 return false;
1075
1076 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1077}
1078
1079namespace {
1080void CorrectTypoInTParamReferenceHelper(
1081 const TemplateParameterList *TemplateParameters,
1082 SimpleTypoCorrector &Corrector) {
1083 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1084 const NamedDecl *Param = TemplateParameters->getParam(i);
1085 Corrector.addDecl(Param);
1086
1087 if (const TemplateTemplateParmDecl *TTP =
1088 dyn_cast<TemplateTemplateParmDecl>(Param))
1089 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1090 Corrector);
1091 }
1092}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001093} // end anonymous namespace
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001094
1095StringRef Sema::correctTypoInTParamReference(
1096 StringRef Typo,
1097 const TemplateParameterList *TemplateParameters) {
1098 SimpleTypoCorrector Corrector(Typo);
1099 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1100 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1101 const IdentifierInfo *II = ND->getIdentifier();
1102 assert(II && "SimpleTypoCorrector should not return this decl");
1103 return II->getName();
1104 }
1105 return StringRef();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001106}
1107
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001108InlineCommandComment::RenderKind
1109Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +00001110 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001111
1112 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1113 .Case("b", InlineCommandComment::RenderBold)
1114 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1115 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1116 .Default(InlineCommandComment::RenderNormal);
1117}
1118
Dmitri Gribenkoec925312012-07-06 00:28:32 +00001119} // end namespace comments
1120} // end namespace clang