blob: f5f4f70dcbbf4b15bbb1b0e0bbdb6c7df12e341a [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
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +000089 if (!isFunctionDecl())
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
Dmitri Gribenko64305832012-08-03 21:15:32 +0000587 if (isFunctionDecl()) {
Alp Toker314cc812014-01-25 16:55:45 +0000588 if (ThisDeclInfo->ReturnType->isVoidType()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000589 unsigned DiagKind;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000590 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000591 default:
Dmitri Gribenko558babc2012-08-06 16:29:26 +0000592 if (ThisDeclInfo->IsObjCMethod)
593 DiagKind = 3;
594 else
595 DiagKind = 0;
Dmitri Gribenko64305832012-08-03 21:15:32 +0000596 break;
597 case Decl::CXXConstructor:
598 DiagKind = 1;
599 break;
600 case Decl::CXXDestructor:
601 DiagKind = 2;
602 break;
603 }
604 Diag(Command->getLocation(),
605 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000606 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000607 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000608 << DiagKind
609 << Command->getSourceRange();
610 }
611 return;
612 }
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000613 else if (isObjCPropertyDecl())
614 return;
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000615
Dmitri Gribenko64305832012-08-03 21:15:32 +0000616 Diag(Command->getLocation(),
617 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000618 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000619 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000620 << Command->getSourceRange();
621}
622
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000623void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000624 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Craig Topper36250ad2014-05-12 05:36:57 +0000625 const BlockCommandComment *PrevCommand = nullptr;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000626 if (Info->IsBriefCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000627 if (!BriefCommand) {
628 BriefCommand = Command;
629 return;
630 }
631 PrevCommand = BriefCommand;
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +0000632 } else if (Info->IsHeaderfileCommand) {
633 if (!HeaderfileCommand) {
634 HeaderfileCommand = Command;
635 return;
636 }
637 PrevCommand = HeaderfileCommand;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000638 } else {
639 // We don't want to check this command for duplicates.
640 return;
641 }
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000642 StringRef CommandName = Command->getCommandName(Traits);
643 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000644 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000645 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000646 << CommandName
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000647 << Command->getSourceRange();
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000648 if (CommandName == PrevCommandName)
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000649 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000650 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000651 << PrevCommandName
652 << PrevCommand->getSourceRange();
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000653 else
654 Diag(PrevCommand->getLocation(),
655 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000656 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000657 << PrevCommandName
658 << CommandName;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000659}
660
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000661void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
662 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
663 return;
664
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +0000665 assert(ThisDeclInfo && "should not call this check on a bare comment");
666
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000667 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000668 if (!D)
669 return;
670
671 if (D->hasAttr<DeprecatedAttr>() ||
672 D->hasAttr<AvailabilityAttr>() ||
673 D->hasAttr<UnavailableAttr>())
674 return;
675
676 Diag(Command->getLocation(),
677 diag::warn_doc_deprecated_not_sync)
678 << Command->getSourceRange();
679
680 // Try to emit a fixit with a deprecation attribute.
681 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
682 // Don't emit a Fix-It for non-member function definitions. GCC does not
683 // accept attributes on them.
684 const DeclContext *Ctx = FD->getDeclContext();
685 if ((!Ctx || !Ctx->isRecord()) &&
686 FD->doesThisDeclarationHaveABody())
687 return;
688
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000689 StringRef AttributeSpelling = "__attribute__((deprecated))";
690 if (PP) {
691 TokenValue Tokens[] = {
692 tok::kw___attribute, tok::l_paren, tok::l_paren,
693 PP->getIdentifierInfo("deprecated"),
694 tok::r_paren, tok::r_paren
695 };
696 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
697 Tokens);
698 if (!MacroName.empty())
699 AttributeSpelling = MacroName;
700 }
701
702 SmallString<64> TextToInsert(" ");
703 TextToInsert += AttributeSpelling;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000704 Diag(FD->getLocEnd(),
705 diag::note_add_deprecation_attr)
706 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000707 TextToInsert);
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000708 }
709}
710
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000711void Sema::resolveParamCommandIndexes(const FullComment *FC) {
712 if (!isFunctionDecl()) {
713 // We already warned that \\param commands are not attached to a function
714 // decl.
715 return;
716 }
717
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000718 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000719
720 // Comment AST nodes that correspond to \c ParamVars for which we have
721 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000722 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000723
724 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
Craig Topper36250ad2014-05-12 05:36:57 +0000725 ParamVarDocs.resize(ParamVars.size(), nullptr);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000726
727 // First pass over all \\param commands: resolve all parameter names.
728 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
729 I != E; ++I) {
730 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
731 if (!PCC || !PCC->hasParamName())
732 continue;
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000733 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000734
735 // Check that referenced parameter name is in the function decl.
736 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
737 ParamVars);
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000738 if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
739 PCC->setIsVarArgParam();
740 continue;
741 }
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000742 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
743 UnresolvedParamCommands.push_back(PCC);
744 continue;
745 }
746 PCC->setParamIndex(ResolvedParamIndex);
747 if (ParamVarDocs[ResolvedParamIndex]) {
748 SourceRange ArgRange = PCC->getParamNameRange();
749 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
750 << ParamName << ArgRange;
751 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
752 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
753 << PrevCommand->getParamNameRange();
754 }
755 ParamVarDocs[ResolvedParamIndex] = PCC;
756 }
757
758 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000759 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000760 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
761 if (!ParamVarDocs[i])
762 OrphanedParamDecls.push_back(ParamVars[i]);
763 }
764
765 // Second pass over unresolved \\param commands: do typo correction.
766 // Suggest corrections from a set of parameter declarations that have no
767 // corresponding \\param.
768 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
769 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
770
771 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000772 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000773 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
774 << ParamName << ArgRange;
775
776 // All parameters documented -- can't suggest a correction.
777 if (OrphanedParamDecls.size() == 0)
778 continue;
779
780 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
781 if (OrphanedParamDecls.size() == 1) {
782 // If one parameter is not documented then that parameter is the only
783 // possible suggestion.
784 CorrectedParamIndex = 0;
785 } else {
786 // Do typo correction.
787 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
788 OrphanedParamDecls);
789 }
790 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
791 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
792 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
793 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
794 << CorrectedII->getName()
795 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
796 }
797 }
798}
799
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000800bool Sema::isFunctionDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000801 if (!ThisDeclInfo)
802 return false;
803 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000804 inspectThisDecl();
Dmitri Gribenko37a7faf2012-08-02 21:45:39 +0000805 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000806}
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000807
Fariborz Jahaniana668bf52013-03-05 23:20:29 +0000808bool Sema::isAnyFunctionDecl() {
809 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
810 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
811}
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000812
813bool Sema::isFunctionOrMethodVariadic() {
Dmitri Gribenkod9eb05a2014-03-19 13:59:36 +0000814 if (!isAnyFunctionDecl() && !isObjCMethodDecl() && !isFunctionTemplateDecl())
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000815 return false;
816 if (const FunctionDecl *FD =
817 dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
818 return FD->isVariadic();
Dmitri Gribenkod9eb05a2014-03-19 13:59:36 +0000819 if (const FunctionTemplateDecl *FTD =
820 dyn_cast<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl))
821 return FTD->getTemplatedDecl()->isVariadic();
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000822 if (const ObjCMethodDecl *MD =
823 dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
824 return MD->isVariadic();
825 return false;
826}
827
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000828bool Sema::isObjCMethodDecl() {
829 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
830 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
831}
Dmitri Gribenkoc0510b92013-06-24 01:33:34 +0000832
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000833bool Sema::isFunctionPointerVarDecl() {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000834 if (!ThisDeclInfo)
835 return false;
836 if (!ThisDeclInfo->IsFilled)
837 inspectThisDecl();
838 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
839 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
840 QualType QT = VD->getType();
841 return QT->isFunctionPointerType();
842 }
843 }
844 return false;
845}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000846
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000847bool Sema::isObjCPropertyDecl() {
848 if (!ThisDeclInfo)
849 return false;
850 if (!ThisDeclInfo->IsFilled)
851 inspectThisDecl();
852 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
853}
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000854
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000855bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000856 if (!ThisDeclInfo)
857 return false;
858 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000859 inspectThisDecl();
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000860 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000861}
862
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000863bool Sema::isRecordLikeDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000864 if (!ThisDeclInfo)
865 return false;
866 if (!ThisDeclInfo->IsFilled)
867 inspectThisDecl();
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000868 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
869 isObjCProtocolDecl();
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000870}
871
872bool Sema::isUnionDecl() {
873 if (!ThisDeclInfo)
874 return false;
875 if (!ThisDeclInfo->IsFilled)
876 inspectThisDecl();
877 if (const RecordDecl *RD =
878 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
879 return RD->isUnion();
880 return false;
881}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000882
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000883bool Sema::isClassOrStructDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000884 if (!ThisDeclInfo)
885 return false;
886 if (!ThisDeclInfo->IsFilled)
887 inspectThisDecl();
888 return ThisDeclInfo->CurrentDecl &&
889 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
890 !isUnionDecl();
891}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000892
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000893bool Sema::isClassTemplateDecl() {
894 if (!ThisDeclInfo)
895 return false;
896 if (!ThisDeclInfo->IsFilled)
897 inspectThisDecl();
898 return ThisDeclInfo->CurrentDecl &&
899 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
900}
901
902bool Sema::isFunctionTemplateDecl() {
903 if (!ThisDeclInfo)
904 return false;
905 if (!ThisDeclInfo->IsFilled)
906 inspectThisDecl();
907 return ThisDeclInfo->CurrentDecl &&
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000908 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000909}
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000910
911bool Sema::isObjCInterfaceDecl() {
912 if (!ThisDeclInfo)
913 return false;
914 if (!ThisDeclInfo->IsFilled)
915 inspectThisDecl();
916 return ThisDeclInfo->CurrentDecl &&
917 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
918}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000919
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000920bool Sema::isObjCProtocolDecl() {
921 if (!ThisDeclInfo)
922 return false;
923 if (!ThisDeclInfo->IsFilled)
924 inspectThisDecl();
925 return ThisDeclInfo->CurrentDecl &&
926 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
927}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000928
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000929ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000930 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000931 inspectThisDecl();
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000932 return ThisDeclInfo->ParamVars;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000933}
934
935void Sema::inspectThisDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000936 ThisDeclInfo->fill();
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000937}
938
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000939unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000940 ArrayRef<const ParmVarDecl *> ParamVars) {
941 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000942 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
943 if (II && II->getName() == Name)
944 return i;
945 }
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000946 if (Name == "..." && isFunctionOrMethodVariadic())
947 return ParamCommandComment::VarArgParamIndex;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000948 return ParamCommandComment::InvalidParamIndex;
949}
950
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000951namespace {
952class SimpleTypoCorrector {
953 StringRef Typo;
954 const unsigned MaxEditDistance;
955
956 const NamedDecl *BestDecl;
957 unsigned BestEditDistance;
958 unsigned BestIndex;
959 unsigned NextIndex;
960
961public:
962 SimpleTypoCorrector(StringRef Typo) :
963 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
Craig Topper36250ad2014-05-12 05:36:57 +0000964 BestDecl(nullptr), BestEditDistance(MaxEditDistance + 1),
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000965 BestIndex(0), NextIndex(0)
966 { }
967
968 void addDecl(const NamedDecl *ND);
969
970 const NamedDecl *getBestDecl() const {
971 if (BestEditDistance > MaxEditDistance)
Craig Topper36250ad2014-05-12 05:36:57 +0000972 return nullptr;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000973
974 return BestDecl;
975 }
976
977 unsigned getBestDeclIndex() const {
978 assert(getBestDecl());
979 return BestIndex;
980 }
981};
982
983void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
984 unsigned CurrIndex = NextIndex++;
985
986 const IdentifierInfo *II = ND->getIdentifier();
987 if (!II)
988 return;
989
990 StringRef Name = II->getName();
991 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
992 if (MinPossibleEditDistance > 0 &&
993 Typo.size() / MinPossibleEditDistance < 3)
994 return;
995
996 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
997 if (EditDistance < BestEditDistance) {
998 BestEditDistance = EditDistance;
999 BestDecl = ND;
1000 BestIndex = CurrIndex;
1001 }
1002}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001003} // end anonymous namespace
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001004
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001005unsigned Sema::correctTypoInParmVarReference(
1006 StringRef Typo,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +00001007 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001008 SimpleTypoCorrector Corrector(Typo);
1009 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
1010 Corrector.addDecl(ParamVars[i]);
1011 if (Corrector.getBestDecl())
1012 return Corrector.getBestDeclIndex();
1013 else
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001014 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001015}
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001016
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001017namespace {
1018bool ResolveTParamReferenceHelper(
1019 StringRef Name,
1020 const TemplateParameterList *TemplateParameters,
1021 SmallVectorImpl<unsigned> *Position) {
1022 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1023 const NamedDecl *Param = TemplateParameters->getParam(i);
1024 const IdentifierInfo *II = Param->getIdentifier();
1025 if (II && II->getName() == Name) {
1026 Position->push_back(i);
1027 return true;
1028 }
1029
1030 if (const TemplateTemplateParmDecl *TTP =
1031 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1032 Position->push_back(i);
1033 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1034 Position))
1035 return true;
1036 Position->pop_back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001037 }
1038 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001039 return false;
1040}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001041} // end anonymous namespace
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001042
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001043bool Sema::resolveTParamReference(
1044 StringRef Name,
1045 const TemplateParameterList *TemplateParameters,
1046 SmallVectorImpl<unsigned> *Position) {
1047 Position->clear();
1048 if (!TemplateParameters)
1049 return false;
1050
1051 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1052}
1053
1054namespace {
1055void CorrectTypoInTParamReferenceHelper(
1056 const TemplateParameterList *TemplateParameters,
1057 SimpleTypoCorrector &Corrector) {
1058 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1059 const NamedDecl *Param = TemplateParameters->getParam(i);
1060 Corrector.addDecl(Param);
1061
1062 if (const TemplateTemplateParmDecl *TTP =
1063 dyn_cast<TemplateTemplateParmDecl>(Param))
1064 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1065 Corrector);
1066 }
1067}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001068} // end anonymous namespace
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001069
1070StringRef Sema::correctTypoInTParamReference(
1071 StringRef Typo,
1072 const TemplateParameterList *TemplateParameters) {
1073 SimpleTypoCorrector Corrector(Typo);
1074 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1075 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1076 const IdentifierInfo *II = ND->getIdentifier();
1077 assert(II && "SimpleTypoCorrector should not return this decl");
1078 return II->getName();
1079 }
1080 return StringRef();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001081}
1082
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001083InlineCommandComment::RenderKind
1084Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +00001085 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001086
1087 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1088 .Case("b", InlineCommandComment::RenderBold)
1089 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1090 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1091 .Default(InlineCommandComment::RenderNormal);
1092}
1093
Dmitri Gribenkoec925312012-07-06 00:28:32 +00001094} // end namespace comments
1095} // end namespace clang