blob: 69d61dc55162b2a6cf88a64b61a1ba9511fa7fb8 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dmitri Gribenkoec925312012-07-06 00:28:32 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "clang/AST/CommentSema.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000010#include "clang/AST/Attr.h"
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000011#include "clang/AST/CommentCommandTraits.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000012#include "clang/AST/CommentDiagnostic.h"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000013#include "clang/AST/Decl.h"
Dmitri Gribenko34df2202012-07-31 22:37:06 +000014#include "clang/AST/DeclTemplate.h"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000015#include "clang/Basic/SourceManager.h"
Dmitri Gribenko6743e042012-09-29 11:40:46 +000016#include "clang/Lex/Preprocessor.h"
Dmitri Gribenko6743e042012-09-29 11:40:46 +000017#include "llvm/ADT/SmallString.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "llvm/ADT/StringSwitch.h"
Dmitri Gribenkoec925312012-07-06 00:28:32 +000019
20namespace clang {
21namespace comments {
22
Dmitri Gribenko3ca956f2012-08-31 02:21:44 +000023namespace {
24#include "clang/AST/CommentHTMLTagsProperties.inc"
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +000025} // end anonymous namespace
Dmitri Gribenko3ca956f2012-08-31 02:21:44 +000026
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000027Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
Dmitri Gribenko6743e042012-09-29 11:40:46 +000028 DiagnosticsEngine &Diags, CommandTraits &Traits,
29 const Preprocessor *PP) :
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000030 Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits),
Craig Topper36250ad2014-05-12 05:36:57 +000031 PP(PP), ThisDeclInfo(nullptr), BriefCommand(nullptr),
32 HeaderfileCommand(nullptr) {
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 Gribenko6bf8f802014-01-27 17:55:43 +000071 if (ThisDeclInfo) {
72 // These checks only make sense if the comment is attached to a
73 // declaration.
74 checkReturnsCommand(Command);
75 checkDeprecatedCommand(Command);
76 }
Dmitri Gribenkoec925312012-07-06 00:28:32 +000077}
78
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000079ParamCommandComment *Sema::actOnParamCommandStart(
80 SourceLocation LocBegin,
81 SourceLocation LocEnd,
82 unsigned CommandID,
83 CommandMarkerKind CommandMarker) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000084 ParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000085 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
86 CommandMarker);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000087
Alex Lorenz6b82a752017-04-21 14:17:49 +000088 if (!isFunctionDecl() && !isFunctionOrBlockPointerVarLikeDecl())
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000089 Diag(Command->getLocation(),
90 diag::warn_doc_param_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000091 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000092 << Command->getCommandNameRange(Traits);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000093
94 return Command;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000095}
96
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +000097void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
98 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahanian56fe4062013-03-05 22:46:07 +000099 if (!Info->IsFunctionDeclarationCommand)
100 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000101
102 unsigned DiagSelect;
103 switch (Comment->getCommandID()) {
104 case CommandTraits::KCI_function:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000105 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000106 break;
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000107 case CommandTraits::KCI_functiongroup:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000108 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0;
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000109 break;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000110 case CommandTraits::KCI_method:
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000111 DiagSelect = !isObjCMethodDecl() ? 3 : 0;
112 break;
113 case CommandTraits::KCI_methodgroup:
114 DiagSelect = !isObjCMethodDecl() ? 4 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000115 break;
116 case CommandTraits::KCI_callback:
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000117 DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000118 break;
119 default:
120 DiagSelect = 0;
121 break;
122 }
Fariborz Jahanian41bb7132013-03-06 17:36:51 +0000123 if (DiagSelect)
124 Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
125 << Comment->getCommandMarker()
126 << (DiagSelect-1) << (DiagSelect-1)
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000127 << Comment->getSourceRange();
128}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000129
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000130void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
131 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000132 if (!Info->IsRecordLikeDeclarationCommand)
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000133 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000134 unsigned DiagSelect;
135 switch (Comment->getCommandID()) {
136 case CommandTraits::KCI_class:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000137 DiagSelect = (!isClassOrStructDecl() && !isClassTemplateDecl()) ? 1 : 0;
Fariborz Jahanian04eb8ab2013-05-20 23:40:39 +0000138 // Allow @class command on @interface declarations.
139 // FIXME. Currently, \class and @class are indistinguishable. So,
140 // \class is also allowed on an @interface declaration
141 if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
142 DiagSelect = 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000143 break;
144 case CommandTraits::KCI_interface:
145 DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
146 break;
147 case CommandTraits::KCI_protocol:
148 DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
149 break;
150 case CommandTraits::KCI_struct:
151 DiagSelect = !isClassOrStructDecl() ? 4 : 0;
152 break;
153 case CommandTraits::KCI_union:
154 DiagSelect = !isUnionDecl() ? 5 : 0;
155 break;
156 default:
157 DiagSelect = 0;
158 break;
159 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000160 if (DiagSelect)
161 Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
162 << Comment->getCommandMarker()
163 << (DiagSelect-1) << (DiagSelect-1)
164 << Comment->getSourceRange();
165}
166
167void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
168 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000169 if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000170 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000171 unsigned DiagSelect;
172 switch (Comment->getCommandID()) {
173 case CommandTraits::KCI_classdesign:
174 DiagSelect = 1;
175 break;
176 case CommandTraits::KCI_coclass:
177 DiagSelect = 2;
178 break;
179 case CommandTraits::KCI_dependency:
180 DiagSelect = 3;
181 break;
182 case CommandTraits::KCI_helper:
183 DiagSelect = 4;
184 break;
185 case CommandTraits::KCI_helperclass:
186 DiagSelect = 5;
187 break;
188 case CommandTraits::KCI_helps:
189 DiagSelect = 6;
190 break;
191 case CommandTraits::KCI_instancesize:
192 DiagSelect = 7;
193 break;
194 case CommandTraits::KCI_ownership:
195 DiagSelect = 8;
196 break;
197 case CommandTraits::KCI_performance:
198 DiagSelect = 9;
199 break;
200 case CommandTraits::KCI_security:
201 DiagSelect = 10;
202 break;
203 case CommandTraits::KCI_superclass:
204 DiagSelect = 11;
205 break;
206 default:
207 DiagSelect = 0;
208 break;
209 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000210 if (DiagSelect)
211 Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
212 << Comment->getCommandMarker()
213 << (DiagSelect-1)
214 << Comment->getSourceRange();
215}
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000216
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000217/// Turn a string into the corresponding PassDirection or -1 if it's not
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000218/// valid.
219static int getParamPassDirection(StringRef Arg) {
220 return llvm::StringSwitch<int>(Arg)
221 .Case("[in]", ParamCommandComment::In)
222 .Case("[out]", ParamCommandComment::Out)
223 .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
224 .Default(-1);
225}
226
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000227void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
228 SourceLocation ArgLocBegin,
229 SourceLocation ArgLocEnd,
230 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000231 std::string ArgLower = Arg.lower();
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000232 int Direction = getParamPassDirection(ArgLower);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000233
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000234 if (Direction == -1) {
235 // Try again with whitespace removed.
236 ArgLower.erase(
237 std::remove_if(ArgLower.begin(), ArgLower.end(), clang::isWhitespace),
238 ArgLower.end());
239 Direction = getParamPassDirection(ArgLower);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000240
241 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000242 if (Direction != -1) {
243 const char *FixedName = ParamCommandComment::getDirectionAsString(
244 (ParamCommandComment::PassDirection)Direction);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000245 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000246 << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
247 } else {
248 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
249 Direction = ParamCommandComment::In; // Sane fall back.
250 }
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000251 }
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000252 Command->setDirection((ParamCommandComment::PassDirection)Direction,
253 /*Explicit=*/true);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000254}
255
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000256void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
257 SourceLocation ArgLocBegin,
258 SourceLocation ArgLocEnd,
259 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000260 // Parser will not feed us more arguments than needed.
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000261 assert(Command->getNumArgs() == 0);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000262
263 if (!Command->isDirectionExplicit()) {
264 // User didn't provide a direction argument.
265 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
266 }
267 typedef BlockCommandComment::Argument Argument;
268 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
269 ArgLocEnd),
270 Arg);
271 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000272}
273
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000274void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
275 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000276 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000277 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000278}
279
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000280TParamCommandComment *Sema::actOnTParamCommandStart(
281 SourceLocation LocBegin,
282 SourceLocation LocEnd,
283 unsigned CommandID,
284 CommandMarkerKind CommandMarker) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000285 TParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000286 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
287 CommandMarker);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000288
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000289 if (!isTemplateOrSpecialization())
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000290 Diag(Command->getLocation(),
291 diag::warn_doc_tparam_not_attached_to_a_template_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000292 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000293 << Command->getCommandNameRange(Traits);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000294
295 return Command;
296}
297
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000298void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
299 SourceLocation ArgLocBegin,
300 SourceLocation ArgLocEnd,
301 StringRef Arg) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000302 // Parser will not feed us more arguments than needed.
303 assert(Command->getNumArgs() == 0);
304
305 typedef BlockCommandComment::Argument Argument;
306 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
307 ArgLocEnd),
308 Arg);
309 Command->setArgs(llvm::makeArrayRef(A, 1));
310
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000311 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000312 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000313 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000314 }
315
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000316 const TemplateParameterList *TemplateParameters =
317 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000318 SmallVector<unsigned, 2> Position;
319 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
320 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
Benjamin Kramereed80612013-11-10 16:55:11 +0000321 TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg];
322 if (PrevCommand) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000323 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
324 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
325 << Arg << ArgRange;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000326 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
327 << PrevCommand->getParamNameRange();
328 }
Benjamin Kramereed80612013-11-10 16:55:11 +0000329 PrevCommand = Command;
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000330 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000331 }
332
333 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
334 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
335 << Arg << ArgRange;
336
337 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000338 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000339
340 StringRef CorrectedName;
341 if (TemplateParameters->size() == 1) {
342 const NamedDecl *Param = TemplateParameters->getParam(0);
343 const IdentifierInfo *II = Param->getIdentifier();
344 if (II)
345 CorrectedName = II->getName();
346 } else {
347 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
348 }
349
350 if (!CorrectedName.empty()) {
351 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
352 << CorrectedName
353 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
354 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000355}
356
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000357void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
358 ParagraphComment *Paragraph) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000359 Command->setParagraph(Paragraph);
360 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000361}
362
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000363InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
364 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000365 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000366 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000367 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000368 return new (Allocator) InlineCommandComment(
369 CommandLocBegin,
370 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000371 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000372 getInlineCommandRenderKind(CommandName),
373 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000374}
375
376InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
377 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000378 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000379 SourceLocation ArgLocBegin,
380 SourceLocation ArgLocEnd,
381 StringRef Arg) {
382 typedef InlineCommandComment::Argument Argument;
383 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
384 ArgLocEnd),
385 Arg);
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000386 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000387
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000388 return new (Allocator) InlineCommandComment(
389 CommandLocBegin,
390 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000391 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000392 getInlineCommandRenderKind(CommandName),
393 llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000394}
395
396InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
397 SourceLocation LocEnd,
Dmitri Gribenko9304d862012-09-11 19:22:03 +0000398 StringRef CommandName) {
399 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
400 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
401}
402
403InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
404 SourceLocation LocEnd,
405 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000406 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000407 return new (Allocator) InlineCommandComment(
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000408 LocBegin, LocEnd, CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000409 InlineCommandComment::RenderNormal,
410 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000411}
412
413TextComment *Sema::actOnText(SourceLocation LocBegin,
414 SourceLocation LocEnd,
415 StringRef Text) {
416 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
417}
418
419VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000420 unsigned CommandID) {
421 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000422 return new (Allocator) VerbatimBlockComment(
423 Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000424 Loc.getLocWithOffset(1 + CommandName.size()),
425 CommandID);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000426}
427
428VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
429 StringRef Text) {
430 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
431}
432
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000433void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000434 VerbatimBlockComment *Block,
435 SourceLocation CloseNameLocBegin,
436 StringRef CloseName,
437 ArrayRef<VerbatimBlockLineComment *> Lines) {
438 Block->setCloseName(CloseName, CloseNameLocBegin);
439 Block->setLines(Lines);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000440}
441
442VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000443 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000444 SourceLocation TextBegin,
445 StringRef Text) {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000446 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000447 LocBegin,
448 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000449 CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000450 TextBegin,
451 Text);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000452 checkFunctionDeclVerbatimLine(VL);
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000453 checkContainerDeclVerbatimLine(VL);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000454 return VL;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000455}
456
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000457HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
458 StringRef TagName) {
459 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000460}
461
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000462void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000463 HTMLStartTagComment *Tag,
464 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000465 SourceLocation GreaterLoc,
466 bool IsSelfClosing) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000467 Tag->setAttrs(Attrs);
468 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000469 if (IsSelfClosing)
470 Tag->setSelfClosing();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000471 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000472 HTMLOpenTags.push_back(Tag);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000473}
474
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000475HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
476 SourceLocation LocEnd,
477 StringRef TagName) {
478 HTMLEndTagComment *HET =
479 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
480 if (isHTMLEndTagForbidden(TagName)) {
481 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
482 << TagName << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000483 HET->setIsMalformed();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000484 return HET;
Dmitri Gribenko9460fbf2012-07-12 23:37:09 +0000485 }
486
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000487 bool FoundOpen = false;
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000488 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000489 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
490 I != E; ++I) {
491 if ((*I)->getTagName() == TagName) {
492 FoundOpen = true;
493 break;
494 }
495 }
496 if (!FoundOpen) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000497 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
498 << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000499 HET->setIsMalformed();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000500 return HET;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000501 }
502
503 while (!HTMLOpenTags.empty()) {
Dmitri Gribenko93043622014-04-22 10:59:13 +0000504 HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000505 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenko93043622014-04-22 10:59:13 +0000506 if (LastNotClosedTagName == TagName) {
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000507 // If the start tag is malformed, end tag is malformed as well.
508 if (HST->isMalformed())
509 HET->setIsMalformed();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000510 break;
Dmitri Gribenko93043622014-04-22 10:59:13 +0000511 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000512
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000513 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000514 continue;
515
516 bool OpenLineInvalid;
517 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000518 HST->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000519 &OpenLineInvalid);
520 bool CloseLineInvalid;
521 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000522 HET->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000523 &CloseLineInvalid);
524
Dmitri Gribenko93043622014-04-22 10:59:13 +0000525 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000526 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
527 << HST->getTagName() << HET->getTagName()
528 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000529 HST->setIsMalformed();
Dmitri Gribenko93043622014-04-22 10:59:13 +0000530 } else {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000531 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
532 << HST->getTagName() << HET->getTagName()
533 << HST->getSourceRange();
534 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
535 << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000536 HST->setIsMalformed();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000537 }
538 }
539
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000540 return HET;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000541}
542
543FullComment *Sema::actOnFullComment(
544 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian42e31322012-10-11 23:52:50 +0000545 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000546 resolveParamCommandIndexes(FC);
Dmitri Gribenko93043622014-04-22 10:59:13 +0000547
548 // Complain about HTML tags that are not closed.
549 while (!HTMLOpenTags.empty()) {
550 HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
551 if (isHTMLEndTagOptional(HST->getTagName()))
552 continue;
553
554 Diag(HST->getLocation(), diag::warn_doc_html_missing_end_tag)
555 << HST->getTagName() << HST->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000556 HST->setIsMalformed();
Dmitri Gribenko93043622014-04-22 10:59:13 +0000557 }
558
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000559 return FC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000560}
561
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000562void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkob37d5e82012-09-13 20:36:01 +0000563 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
564 return;
565
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000566 ParagraphComment *Paragraph = Command->getParagraph();
567 if (Paragraph->isWhitespace()) {
568 SourceLocation DiagLoc;
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000569 if (Command->getNumArgs() > 0)
570 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000571 if (!DiagLoc.isValid())
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000572 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000573 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000574 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000575 << Command->getCommandName(Traits)
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000576 << Command->getSourceRange();
577 }
578}
579
Dmitri Gribenko64305832012-08-03 21:15:32 +0000580void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000581 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000582 return;
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +0000583
584 assert(ThisDeclInfo && "should not call this check on a bare comment");
585
Alex Lorenz00353a02017-04-26 13:09:28 +0000586 // We allow the return command for all @properties because it can be used
587 // to document the value that the property getter returns.
588 if (isObjCPropertyDecl())
589 return;
Alex Lorenz6b82a752017-04-21 14:17:49 +0000590 if (isFunctionDecl() || isFunctionOrBlockPointerVarLikeDecl()) {
Dmitri Gribenkod94b42f2019-09-02 18:24:33 +0000591 assert(!ThisDeclInfo->ReturnType.isNull() &&
592 "should have a valid return type");
Alp Toker314cc812014-01-25 16:55:45 +0000593 if (ThisDeclInfo->ReturnType->isVoidType()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000594 unsigned DiagKind;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000595 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000596 default:
Dmitri Gribenko558babc2012-08-06 16:29:26 +0000597 if (ThisDeclInfo->IsObjCMethod)
598 DiagKind = 3;
599 else
600 DiagKind = 0;
Dmitri Gribenko64305832012-08-03 21:15:32 +0000601 break;
602 case Decl::CXXConstructor:
603 DiagKind = 1;
604 break;
605 case Decl::CXXDestructor:
606 DiagKind = 2;
607 break;
608 }
609 Diag(Command->getLocation(),
610 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000611 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000612 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000613 << DiagKind
614 << Command->getSourceRange();
615 }
616 return;
617 }
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000618
Dmitri Gribenko64305832012-08-03 21:15:32 +0000619 Diag(Command->getLocation(),
620 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000621 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000622 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000623 << Command->getSourceRange();
624}
625
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000626void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000627 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Craig Topper36250ad2014-05-12 05:36:57 +0000628 const BlockCommandComment *PrevCommand = nullptr;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000629 if (Info->IsBriefCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000630 if (!BriefCommand) {
631 BriefCommand = Command;
632 return;
633 }
634 PrevCommand = BriefCommand;
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +0000635 } else if (Info->IsHeaderfileCommand) {
636 if (!HeaderfileCommand) {
637 HeaderfileCommand = Command;
638 return;
639 }
640 PrevCommand = HeaderfileCommand;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000641 } else {
642 // We don't want to check this command for duplicates.
643 return;
644 }
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000645 StringRef CommandName = Command->getCommandName(Traits);
646 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000647 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000648 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000649 << CommandName
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000650 << Command->getSourceRange();
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000651 if (CommandName == PrevCommandName)
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000652 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000653 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000654 << PrevCommandName
655 << PrevCommand->getSourceRange();
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000656 else
657 Diag(PrevCommand->getLocation(),
658 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000659 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000660 << PrevCommandName
661 << CommandName;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000662}
663
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000664void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
665 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
666 return;
667
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +0000668 assert(ThisDeclInfo && "should not call this check on a bare comment");
669
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000670 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000671 if (!D)
672 return;
673
674 if (D->hasAttr<DeprecatedAttr>() ||
675 D->hasAttr<AvailabilityAttr>() ||
676 D->hasAttr<UnavailableAttr>())
677 return;
678
679 Diag(Command->getLocation(),
680 diag::warn_doc_deprecated_not_sync)
681 << Command->getSourceRange();
682
683 // Try to emit a fixit with a deprecation attribute.
684 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
685 // Don't emit a Fix-It for non-member function definitions. GCC does not
686 // accept attributes on them.
687 const DeclContext *Ctx = FD->getDeclContext();
688 if ((!Ctx || !Ctx->isRecord()) &&
689 FD->doesThisDeclarationHaveABody())
690 return;
691
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000692 StringRef AttributeSpelling = "__attribute__((deprecated))";
693 if (PP) {
694 TokenValue Tokens[] = {
695 tok::kw___attribute, tok::l_paren, tok::l_paren,
696 PP->getIdentifierInfo("deprecated"),
697 tok::r_paren, tok::r_paren
698 };
699 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
700 Tokens);
701 if (!MacroName.empty())
702 AttributeSpelling = MacroName;
703 }
704
705 SmallString<64> TextToInsert(" ");
706 TextToInsert += AttributeSpelling;
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000707 Diag(FD->getEndLoc(), diag::note_add_deprecation_attr)
708 << FixItHint::CreateInsertion(FD->getEndLoc().getLocWithOffset(1),
709 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() {
Alex Lorenze9baea82017-10-06 20:51:04 +0000816 if (!isFunctionDecl() || !ThisDeclInfo->CurrentDecl)
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();
Alex Lorenze9baea82017-10-06 20:51:04 +0000827 if (const TypedefNameDecl *TD =
828 dyn_cast<TypedefNameDecl>(ThisDeclInfo->CurrentDecl)) {
829 QualType Type = TD->getUnderlyingType();
830 if (Type->isFunctionPointerType() || Type->isBlockPointerType())
831 Type = Type->getPointeeType();
832 if (const auto *FT = Type->getAs<FunctionProtoType>())
833 return FT->isVariadic();
834 }
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000835 return false;
836}
837
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000838bool Sema::isObjCMethodDecl() {
839 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
840 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
841}
Dmitri Gribenkoc0510b92013-06-24 01:33:34 +0000842
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000843bool Sema::isFunctionPointerVarDecl() {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000844 if (!ThisDeclInfo)
845 return false;
846 if (!ThisDeclInfo->IsFilled)
847 inspectThisDecl();
848 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
849 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
850 QualType QT = VD->getType();
851 return QT->isFunctionPointerType();
852 }
853 }
854 return false;
855}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000856
Alex Lorenz6b82a752017-04-21 14:17:49 +0000857bool Sema::isFunctionOrBlockPointerVarLikeDecl() {
858 if (!ThisDeclInfo)
859 return false;
860 if (!ThisDeclInfo->IsFilled)
861 inspectThisDecl();
862 if (ThisDeclInfo->getKind() != DeclInfo::VariableKind ||
863 !ThisDeclInfo->CurrentDecl)
864 return false;
865 QualType QT;
866 if (const auto *VD = dyn_cast<DeclaratorDecl>(ThisDeclInfo->CurrentDecl))
867 QT = VD->getType();
868 else if (const auto *PD =
869 dyn_cast<ObjCPropertyDecl>(ThisDeclInfo->CurrentDecl))
870 QT = PD->getType();
871 else
872 return false;
873 // We would like to warn about the 'returns'/'param' commands for
874 // variables that don't directly specify the function type, so type aliases
875 // can be ignored.
876 if (QT->getAs<TypedefType>())
877 return false;
Dmitri Gribenkod94b42f2019-09-02 18:24:33 +0000878 if (const auto *P = QT->getAs<PointerType>())
879 if (P->getPointeeType()->getAs<TypedefType>())
880 return false;
881 if (const auto *P = QT->getAs<BlockPointerType>())
882 if (P->getPointeeType()->getAs<TypedefType>())
883 return false;
Alex Lorenz6b82a752017-04-21 14:17:49 +0000884 return QT->isFunctionPointerType() || QT->isBlockPointerType();
885}
886
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000887bool Sema::isObjCPropertyDecl() {
888 if (!ThisDeclInfo)
889 return false;
890 if (!ThisDeclInfo->IsFilled)
891 inspectThisDecl();
892 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
893}
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000894
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000895bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000896 if (!ThisDeclInfo)
897 return false;
898 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000899 inspectThisDecl();
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000900 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000901}
902
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000903bool Sema::isRecordLikeDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000904 if (!ThisDeclInfo)
905 return false;
906 if (!ThisDeclInfo->IsFilled)
907 inspectThisDecl();
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000908 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
909 isObjCProtocolDecl();
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000910}
911
912bool Sema::isUnionDecl() {
913 if (!ThisDeclInfo)
914 return false;
915 if (!ThisDeclInfo->IsFilled)
916 inspectThisDecl();
917 if (const RecordDecl *RD =
918 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
919 return RD->isUnion();
920 return false;
921}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000922
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000923bool Sema::isClassOrStructDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000924 if (!ThisDeclInfo)
925 return false;
926 if (!ThisDeclInfo->IsFilled)
927 inspectThisDecl();
928 return ThisDeclInfo->CurrentDecl &&
929 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
930 !isUnionDecl();
931}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000932
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000933bool Sema::isClassTemplateDecl() {
934 if (!ThisDeclInfo)
935 return false;
936 if (!ThisDeclInfo->IsFilled)
937 inspectThisDecl();
938 return ThisDeclInfo->CurrentDecl &&
939 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
940}
941
942bool Sema::isFunctionTemplateDecl() {
943 if (!ThisDeclInfo)
944 return false;
945 if (!ThisDeclInfo->IsFilled)
946 inspectThisDecl();
947 return ThisDeclInfo->CurrentDecl &&
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000948 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000949}
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000950
951bool Sema::isObjCInterfaceDecl() {
952 if (!ThisDeclInfo)
953 return false;
954 if (!ThisDeclInfo->IsFilled)
955 inspectThisDecl();
956 return ThisDeclInfo->CurrentDecl &&
957 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
958}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000959
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000960bool Sema::isObjCProtocolDecl() {
961 if (!ThisDeclInfo)
962 return false;
963 if (!ThisDeclInfo->IsFilled)
964 inspectThisDecl();
965 return ThisDeclInfo->CurrentDecl &&
966 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
967}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000968
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000969ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000970 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000971 inspectThisDecl();
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000972 return ThisDeclInfo->ParamVars;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000973}
974
975void Sema::inspectThisDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000976 ThisDeclInfo->fill();
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000977}
978
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000979unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000980 ArrayRef<const ParmVarDecl *> ParamVars) {
981 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000982 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
983 if (II && II->getName() == Name)
984 return i;
985 }
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000986 if (Name == "..." && isFunctionOrMethodVariadic())
987 return ParamCommandComment::VarArgParamIndex;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000988 return ParamCommandComment::InvalidParamIndex;
989}
990
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000991namespace {
992class SimpleTypoCorrector {
Saleem Abdulrasool61c0b0c2016-08-28 21:33:30 +0000993 const NamedDecl *BestDecl;
994
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000995 StringRef Typo;
996 const unsigned MaxEditDistance;
997
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000998 unsigned BestEditDistance;
999 unsigned BestIndex;
1000 unsigned NextIndex;
1001
1002public:
Saleem Abdulrasool61c0b0c2016-08-28 21:33:30 +00001003 explicit SimpleTypoCorrector(StringRef Typo)
1004 : BestDecl(nullptr), Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
1005 BestEditDistance(MaxEditDistance + 1), BestIndex(0), NextIndex(0) {}
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001006
1007 void addDecl(const NamedDecl *ND);
1008
1009 const NamedDecl *getBestDecl() const {
1010 if (BestEditDistance > MaxEditDistance)
Craig Topper36250ad2014-05-12 05:36:57 +00001011 return nullptr;
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001012
1013 return BestDecl;
1014 }
1015
1016 unsigned getBestDeclIndex() const {
1017 assert(getBestDecl());
1018 return BestIndex;
1019 }
1020};
1021
1022void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
1023 unsigned CurrIndex = NextIndex++;
1024
1025 const IdentifierInfo *II = ND->getIdentifier();
1026 if (!II)
1027 return;
1028
1029 StringRef Name = II->getName();
1030 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
1031 if (MinPossibleEditDistance > 0 &&
1032 Typo.size() / MinPossibleEditDistance < 3)
1033 return;
1034
1035 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
1036 if (EditDistance < BestEditDistance) {
1037 BestEditDistance = EditDistance;
1038 BestDecl = ND;
1039 BestIndex = CurrIndex;
1040 }
1041}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001042} // end anonymous namespace
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001043
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001044unsigned Sema::correctTypoInParmVarReference(
1045 StringRef Typo,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +00001046 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001047 SimpleTypoCorrector Corrector(Typo);
1048 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
1049 Corrector.addDecl(ParamVars[i]);
1050 if (Corrector.getBestDecl())
1051 return Corrector.getBestDeclIndex();
1052 else
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001053 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001054}
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001055
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001056namespace {
1057bool ResolveTParamReferenceHelper(
1058 StringRef Name,
1059 const TemplateParameterList *TemplateParameters,
1060 SmallVectorImpl<unsigned> *Position) {
1061 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1062 const NamedDecl *Param = TemplateParameters->getParam(i);
1063 const IdentifierInfo *II = Param->getIdentifier();
1064 if (II && II->getName() == Name) {
1065 Position->push_back(i);
1066 return true;
1067 }
1068
1069 if (const TemplateTemplateParmDecl *TTP =
1070 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1071 Position->push_back(i);
1072 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1073 Position))
1074 return true;
1075 Position->pop_back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001076 }
1077 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001078 return false;
1079}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001080} // end anonymous namespace
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001081
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001082bool Sema::resolveTParamReference(
1083 StringRef Name,
1084 const TemplateParameterList *TemplateParameters,
1085 SmallVectorImpl<unsigned> *Position) {
1086 Position->clear();
1087 if (!TemplateParameters)
1088 return false;
1089
1090 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1091}
1092
1093namespace {
1094void CorrectTypoInTParamReferenceHelper(
1095 const TemplateParameterList *TemplateParameters,
1096 SimpleTypoCorrector &Corrector) {
1097 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1098 const NamedDecl *Param = TemplateParameters->getParam(i);
1099 Corrector.addDecl(Param);
1100
1101 if (const TemplateTemplateParmDecl *TTP =
1102 dyn_cast<TemplateTemplateParmDecl>(Param))
1103 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1104 Corrector);
1105 }
1106}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001107} // end anonymous namespace
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001108
1109StringRef Sema::correctTypoInTParamReference(
1110 StringRef Typo,
1111 const TemplateParameterList *TemplateParameters) {
1112 SimpleTypoCorrector Corrector(Typo);
1113 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1114 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1115 const IdentifierInfo *II = ND->getIdentifier();
1116 assert(II && "SimpleTypoCorrector should not return this decl");
1117 return II->getName();
1118 }
1119 return StringRef();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001120}
1121
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001122InlineCommandComment::RenderKind
1123Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +00001124 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001125
1126 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1127 .Case("b", InlineCommandComment::RenderBold)
1128 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1129 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1130 .Default(InlineCommandComment::RenderNormal);
1131}
1132
Dmitri Gribenkoec925312012-07-06 00:28:32 +00001133} // end namespace comments
1134} // end namespace clang