blob: 12823c37dfc616a19a216797015d20fb83ec96f3 [file] [log] [blame]
Fariborz Jahanian43c8df82013-03-05 19:52:24 +00001//===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===//
Dmitri Gribenkoec925312012-07-06 00:28:32 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/AST/CommentSema.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000011#include "clang/AST/Attr.h"
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000012#include "clang/AST/CommentCommandTraits.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000013#include "clang/AST/CommentDiagnostic.h"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000014#include "clang/AST/Decl.h"
Dmitri Gribenko34df2202012-07-31 22:37:06 +000015#include "clang/AST/DeclTemplate.h"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000016#include "clang/Basic/SourceManager.h"
Dmitri Gribenko6743e042012-09-29 11:40:46 +000017#include "clang/Lex/Preprocessor.h"
Dmitri Gribenko6743e042012-09-29 11:40:46 +000018#include "llvm/ADT/SmallString.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "llvm/ADT/StringSwitch.h"
Dmitri Gribenkoec925312012-07-06 00:28:32 +000020
21namespace clang {
22namespace comments {
23
Dmitri Gribenko3ca956f2012-08-31 02:21:44 +000024namespace {
25#include "clang/AST/CommentHTMLTagsProperties.inc"
26} // unnamed namespace
27
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000028Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
Dmitri Gribenko6743e042012-09-29 11:40:46 +000029 DiagnosticsEngine &Diags, CommandTraits &Traits,
30 const Preprocessor *PP) :
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000031 Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits),
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 }
356
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000357 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000358}
359
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000360void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
361 ParagraphComment *Paragraph) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000362 Command->setParagraph(Paragraph);
363 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000364}
365
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000366InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
367 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000368 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000369 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000370 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000371 return new (Allocator) InlineCommandComment(
372 CommandLocBegin,
373 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000374 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000375 getInlineCommandRenderKind(CommandName),
376 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000377}
378
379InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
380 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000381 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000382 SourceLocation ArgLocBegin,
383 SourceLocation ArgLocEnd,
384 StringRef Arg) {
385 typedef InlineCommandComment::Argument Argument;
386 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
387 ArgLocEnd),
388 Arg);
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000389 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000390
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000391 return new (Allocator) InlineCommandComment(
392 CommandLocBegin,
393 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000394 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000395 getInlineCommandRenderKind(CommandName),
396 llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000397}
398
399InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
400 SourceLocation LocEnd,
Dmitri Gribenko9304d862012-09-11 19:22:03 +0000401 StringRef CommandName) {
402 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
403 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
404}
405
406InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
407 SourceLocation LocEnd,
408 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000409 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000410 return new (Allocator) InlineCommandComment(
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000411 LocBegin, LocEnd, CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000412 InlineCommandComment::RenderNormal,
413 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000414}
415
416TextComment *Sema::actOnText(SourceLocation LocBegin,
417 SourceLocation LocEnd,
418 StringRef Text) {
419 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
420}
421
422VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000423 unsigned CommandID) {
424 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000425 return new (Allocator) VerbatimBlockComment(
426 Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000427 Loc.getLocWithOffset(1 + CommandName.size()),
428 CommandID);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000429}
430
431VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
432 StringRef Text) {
433 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
434}
435
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000436void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000437 VerbatimBlockComment *Block,
438 SourceLocation CloseNameLocBegin,
439 StringRef CloseName,
440 ArrayRef<VerbatimBlockLineComment *> Lines) {
441 Block->setCloseName(CloseName, CloseNameLocBegin);
442 Block->setLines(Lines);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000443}
444
445VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000446 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000447 SourceLocation TextBegin,
448 StringRef Text) {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000449 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000450 LocBegin,
451 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000452 CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000453 TextBegin,
454 Text);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000455 checkFunctionDeclVerbatimLine(VL);
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000456 checkContainerDeclVerbatimLine(VL);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000457 return VL;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000458}
459
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000460HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
461 StringRef TagName) {
462 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000463}
464
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000465void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000466 HTMLStartTagComment *Tag,
467 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000468 SourceLocation GreaterLoc,
469 bool IsSelfClosing) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000470 Tag->setAttrs(Attrs);
471 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000472 if (IsSelfClosing)
473 Tag->setSelfClosing();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000474 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000475 HTMLOpenTags.push_back(Tag);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000476}
477
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000478HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
479 SourceLocation LocEnd,
480 StringRef TagName) {
481 HTMLEndTagComment *HET =
482 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
483 if (isHTMLEndTagForbidden(TagName)) {
484 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
485 << TagName << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000486 HET->setIsMalformed();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000487 return HET;
Dmitri Gribenko9460fbf2012-07-12 23:37:09 +0000488 }
489
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000490 bool FoundOpen = false;
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000491 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000492 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
493 I != E; ++I) {
494 if ((*I)->getTagName() == TagName) {
495 FoundOpen = true;
496 break;
497 }
498 }
499 if (!FoundOpen) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000500 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
501 << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000502 HET->setIsMalformed();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000503 return HET;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000504 }
505
506 while (!HTMLOpenTags.empty()) {
Dmitri Gribenko93043622014-04-22 10:59:13 +0000507 HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000508 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenko93043622014-04-22 10:59:13 +0000509 if (LastNotClosedTagName == TagName) {
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000510 // If the start tag is malformed, end tag is malformed as well.
511 if (HST->isMalformed())
512 HET->setIsMalformed();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000513 break;
Dmitri Gribenko93043622014-04-22 10:59:13 +0000514 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000515
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000516 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000517 continue;
518
519 bool OpenLineInvalid;
520 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000521 HST->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000522 &OpenLineInvalid);
523 bool CloseLineInvalid;
524 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000525 HET->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000526 &CloseLineInvalid);
527
Dmitri Gribenko93043622014-04-22 10:59:13 +0000528 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000529 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
530 << HST->getTagName() << HET->getTagName()
531 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000532 HST->setIsMalformed();
Dmitri Gribenko93043622014-04-22 10:59:13 +0000533 } else {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000534 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
535 << HST->getTagName() << HET->getTagName()
536 << HST->getSourceRange();
537 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
538 << HET->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000539 HST->setIsMalformed();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000540 }
541 }
542
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000543 return HET;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000544}
545
546FullComment *Sema::actOnFullComment(
547 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian42e31322012-10-11 23:52:50 +0000548 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000549 resolveParamCommandIndexes(FC);
Dmitri Gribenko93043622014-04-22 10:59:13 +0000550
551 // Complain about HTML tags that are not closed.
552 while (!HTMLOpenTags.empty()) {
553 HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
554 if (isHTMLEndTagOptional(HST->getTagName()))
555 continue;
556
557 Diag(HST->getLocation(), diag::warn_doc_html_missing_end_tag)
558 << HST->getTagName() << HST->getSourceRange();
Dmitri Gribenko0b2026d2014-04-30 21:54:30 +0000559 HST->setIsMalformed();
Dmitri Gribenko93043622014-04-22 10:59:13 +0000560 }
561
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000562 return FC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000563}
564
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000565void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkob37d5e82012-09-13 20:36:01 +0000566 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
567 return;
568
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000569 ParagraphComment *Paragraph = Command->getParagraph();
570 if (Paragraph->isWhitespace()) {
571 SourceLocation DiagLoc;
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000572 if (Command->getNumArgs() > 0)
573 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000574 if (!DiagLoc.isValid())
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000575 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000576 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000577 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000578 << Command->getCommandName(Traits)
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000579 << Command->getSourceRange();
580 }
581}
582
Dmitri Gribenko64305832012-08-03 21:15:32 +0000583void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000584 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000585 return;
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +0000586
587 assert(ThisDeclInfo && "should not call this check on a bare comment");
588
Dmitri Gribenko64305832012-08-03 21:15:32 +0000589 if (isFunctionDecl()) {
Alp Toker314cc812014-01-25 16:55:45 +0000590 if (ThisDeclInfo->ReturnType->isVoidType()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000591 unsigned DiagKind;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000592 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000593 default:
Dmitri Gribenko558babc2012-08-06 16:29:26 +0000594 if (ThisDeclInfo->IsObjCMethod)
595 DiagKind = 3;
596 else
597 DiagKind = 0;
Dmitri Gribenko64305832012-08-03 21:15:32 +0000598 break;
599 case Decl::CXXConstructor:
600 DiagKind = 1;
601 break;
602 case Decl::CXXDestructor:
603 DiagKind = 2;
604 break;
605 }
606 Diag(Command->getLocation(),
607 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000608 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000609 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000610 << DiagKind
611 << Command->getSourceRange();
612 }
613 return;
614 }
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000615 else if (isObjCPropertyDecl())
616 return;
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000617
Dmitri Gribenko64305832012-08-03 21:15:32 +0000618 Diag(Command->getLocation(),
619 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000620 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000621 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000622 << Command->getSourceRange();
623}
624
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000625void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000626 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Craig Topper36250ad2014-05-12 05:36:57 +0000627 const BlockCommandComment *PrevCommand = nullptr;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000628 if (Info->IsBriefCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000629 if (!BriefCommand) {
630 BriefCommand = Command;
631 return;
632 }
633 PrevCommand = BriefCommand;
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +0000634 } else if (Info->IsHeaderfileCommand) {
635 if (!HeaderfileCommand) {
636 HeaderfileCommand = Command;
637 return;
638 }
639 PrevCommand = HeaderfileCommand;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000640 } else {
641 // We don't want to check this command for duplicates.
642 return;
643 }
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000644 StringRef CommandName = Command->getCommandName(Traits);
645 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000646 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000647 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000648 << CommandName
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000649 << Command->getSourceRange();
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000650 if (CommandName == PrevCommandName)
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000651 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000652 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000653 << PrevCommandName
654 << PrevCommand->getSourceRange();
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000655 else
656 Diag(PrevCommand->getLocation(),
657 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000658 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000659 << PrevCommandName
660 << CommandName;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000661}
662
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000663void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
664 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
665 return;
666
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +0000667 assert(ThisDeclInfo && "should not call this check on a bare comment");
668
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000669 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000670 if (!D)
671 return;
672
673 if (D->hasAttr<DeprecatedAttr>() ||
674 D->hasAttr<AvailabilityAttr>() ||
675 D->hasAttr<UnavailableAttr>())
676 return;
677
678 Diag(Command->getLocation(),
679 diag::warn_doc_deprecated_not_sync)
680 << Command->getSourceRange();
681
682 // Try to emit a fixit with a deprecation attribute.
683 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
684 // Don't emit a Fix-It for non-member function definitions. GCC does not
685 // accept attributes on them.
686 const DeclContext *Ctx = FD->getDeclContext();
687 if ((!Ctx || !Ctx->isRecord()) &&
688 FD->doesThisDeclarationHaveABody())
689 return;
690
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000691 StringRef AttributeSpelling = "__attribute__((deprecated))";
692 if (PP) {
693 TokenValue Tokens[] = {
694 tok::kw___attribute, tok::l_paren, tok::l_paren,
695 PP->getIdentifierInfo("deprecated"),
696 tok::r_paren, tok::r_paren
697 };
698 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
699 Tokens);
700 if (!MacroName.empty())
701 AttributeSpelling = MacroName;
702 }
703
704 SmallString<64> TextToInsert(" ");
705 TextToInsert += AttributeSpelling;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000706 Diag(FD->getLocEnd(),
707 diag::note_add_deprecation_attr)
708 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000709 TextToInsert);
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000710 }
711}
712
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000713void Sema::resolveParamCommandIndexes(const FullComment *FC) {
714 if (!isFunctionDecl()) {
715 // We already warned that \\param commands are not attached to a function
716 // decl.
717 return;
718 }
719
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000720 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000721
722 // Comment AST nodes that correspond to \c ParamVars for which we have
723 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000724 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000725
726 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
Craig Topper36250ad2014-05-12 05:36:57 +0000727 ParamVarDocs.resize(ParamVars.size(), nullptr);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000728
729 // First pass over all \\param commands: resolve all parameter names.
730 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
731 I != E; ++I) {
732 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
733 if (!PCC || !PCC->hasParamName())
734 continue;
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000735 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000736
737 // Check that referenced parameter name is in the function decl.
738 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
739 ParamVars);
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000740 if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
741 PCC->setIsVarArgParam();
742 continue;
743 }
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000744 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
745 UnresolvedParamCommands.push_back(PCC);
746 continue;
747 }
748 PCC->setParamIndex(ResolvedParamIndex);
749 if (ParamVarDocs[ResolvedParamIndex]) {
750 SourceRange ArgRange = PCC->getParamNameRange();
751 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
752 << ParamName << ArgRange;
753 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
754 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
755 << PrevCommand->getParamNameRange();
756 }
757 ParamVarDocs[ResolvedParamIndex] = PCC;
758 }
759
760 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000761 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000762 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
763 if (!ParamVarDocs[i])
764 OrphanedParamDecls.push_back(ParamVars[i]);
765 }
766
767 // Second pass over unresolved \\param commands: do typo correction.
768 // Suggest corrections from a set of parameter declarations that have no
769 // corresponding \\param.
770 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
771 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
772
773 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000774 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000775 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
776 << ParamName << ArgRange;
777
778 // All parameters documented -- can't suggest a correction.
779 if (OrphanedParamDecls.size() == 0)
780 continue;
781
782 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
783 if (OrphanedParamDecls.size() == 1) {
784 // If one parameter is not documented then that parameter is the only
785 // possible suggestion.
786 CorrectedParamIndex = 0;
787 } else {
788 // Do typo correction.
789 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
790 OrphanedParamDecls);
791 }
792 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
793 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
794 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
795 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
796 << CorrectedII->getName()
797 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
798 }
799 }
800}
801
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000802bool Sema::isFunctionDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000803 if (!ThisDeclInfo)
804 return false;
805 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000806 inspectThisDecl();
Dmitri Gribenko37a7faf2012-08-02 21:45:39 +0000807 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000808}
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000809
Fariborz Jahaniana668bf52013-03-05 23:20:29 +0000810bool Sema::isAnyFunctionDecl() {
811 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
812 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
813}
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000814
815bool Sema::isFunctionOrMethodVariadic() {
Dmitri Gribenkod9eb05a2014-03-19 13:59:36 +0000816 if (!isAnyFunctionDecl() && !isObjCMethodDecl() && !isFunctionTemplateDecl())
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000817 return false;
818 if (const FunctionDecl *FD =
819 dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
820 return FD->isVariadic();
Dmitri Gribenkod9eb05a2014-03-19 13:59:36 +0000821 if (const FunctionTemplateDecl *FTD =
822 dyn_cast<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl))
823 return FTD->getTemplatedDecl()->isVariadic();
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000824 if (const ObjCMethodDecl *MD =
825 dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
826 return MD->isVariadic();
827 return false;
828}
829
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000830bool Sema::isObjCMethodDecl() {
831 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
832 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
833}
Dmitri Gribenkoc0510b92013-06-24 01:33:34 +0000834
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000835bool Sema::isFunctionPointerVarDecl() {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000836 if (!ThisDeclInfo)
837 return false;
838 if (!ThisDeclInfo->IsFilled)
839 inspectThisDecl();
840 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
841 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
842 QualType QT = VD->getType();
843 return QT->isFunctionPointerType();
844 }
845 }
846 return false;
847}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000848
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000849bool Sema::isObjCPropertyDecl() {
850 if (!ThisDeclInfo)
851 return false;
852 if (!ThisDeclInfo->IsFilled)
853 inspectThisDecl();
854 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
855}
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000856
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000857bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000858 if (!ThisDeclInfo)
859 return false;
860 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000861 inspectThisDecl();
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000862 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000863}
864
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000865bool Sema::isRecordLikeDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000866 if (!ThisDeclInfo)
867 return false;
868 if (!ThisDeclInfo->IsFilled)
869 inspectThisDecl();
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000870 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
871 isObjCProtocolDecl();
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000872}
873
874bool Sema::isUnionDecl() {
875 if (!ThisDeclInfo)
876 return false;
877 if (!ThisDeclInfo->IsFilled)
878 inspectThisDecl();
879 if (const RecordDecl *RD =
880 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
881 return RD->isUnion();
882 return false;
883}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000884
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000885bool Sema::isClassOrStructDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000886 if (!ThisDeclInfo)
887 return false;
888 if (!ThisDeclInfo->IsFilled)
889 inspectThisDecl();
890 return ThisDeclInfo->CurrentDecl &&
891 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
892 !isUnionDecl();
893}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000894
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000895bool Sema::isClassTemplateDecl() {
896 if (!ThisDeclInfo)
897 return false;
898 if (!ThisDeclInfo->IsFilled)
899 inspectThisDecl();
900 return ThisDeclInfo->CurrentDecl &&
901 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
902}
903
904bool Sema::isFunctionTemplateDecl() {
905 if (!ThisDeclInfo)
906 return false;
907 if (!ThisDeclInfo->IsFilled)
908 inspectThisDecl();
909 return ThisDeclInfo->CurrentDecl &&
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000910 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000911}
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000912
913bool Sema::isObjCInterfaceDecl() {
914 if (!ThisDeclInfo)
915 return false;
916 if (!ThisDeclInfo->IsFilled)
917 inspectThisDecl();
918 return ThisDeclInfo->CurrentDecl &&
919 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
920}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000921
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000922bool Sema::isObjCProtocolDecl() {
923 if (!ThisDeclInfo)
924 return false;
925 if (!ThisDeclInfo->IsFilled)
926 inspectThisDecl();
927 return ThisDeclInfo->CurrentDecl &&
928 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
929}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000930
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000931ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000932 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000933 inspectThisDecl();
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000934 return ThisDeclInfo->ParamVars;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000935}
936
937void Sema::inspectThisDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000938 ThisDeclInfo->fill();
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000939}
940
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000941unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000942 ArrayRef<const ParmVarDecl *> ParamVars) {
943 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000944 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
945 if (II && II->getName() == Name)
946 return i;
947 }
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000948 if (Name == "..." && isFunctionOrMethodVariadic())
949 return ParamCommandComment::VarArgParamIndex;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000950 return ParamCommandComment::InvalidParamIndex;
951}
952
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000953namespace {
954class SimpleTypoCorrector {
955 StringRef Typo;
956 const unsigned MaxEditDistance;
957
958 const NamedDecl *BestDecl;
959 unsigned BestEditDistance;
960 unsigned BestIndex;
961 unsigned NextIndex;
962
963public:
964 SimpleTypoCorrector(StringRef Typo) :
965 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
Craig Topper36250ad2014-05-12 05:36:57 +0000966 BestDecl(nullptr), BestEditDistance(MaxEditDistance + 1),
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000967 BestIndex(0), NextIndex(0)
968 { }
969
970 void addDecl(const NamedDecl *ND);
971
972 const NamedDecl *getBestDecl() const {
973 if (BestEditDistance > MaxEditDistance)
Craig Topper36250ad2014-05-12 05:36:57 +0000974 return nullptr;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000975
976 return BestDecl;
977 }
978
979 unsigned getBestDeclIndex() const {
980 assert(getBestDecl());
981 return BestIndex;
982 }
983};
984
985void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
986 unsigned CurrIndex = NextIndex++;
987
988 const IdentifierInfo *II = ND->getIdentifier();
989 if (!II)
990 return;
991
992 StringRef Name = II->getName();
993 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
994 if (MinPossibleEditDistance > 0 &&
995 Typo.size() / MinPossibleEditDistance < 3)
996 return;
997
998 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
999 if (EditDistance < BestEditDistance) {
1000 BestEditDistance = EditDistance;
1001 BestDecl = ND;
1002 BestIndex = CurrIndex;
1003 }
1004}
1005} // unnamed namespace
1006
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001007unsigned Sema::correctTypoInParmVarReference(
1008 StringRef Typo,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +00001009 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001010 SimpleTypoCorrector Corrector(Typo);
1011 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
1012 Corrector.addDecl(ParamVars[i]);
1013 if (Corrector.getBestDecl())
1014 return Corrector.getBestDeclIndex();
1015 else
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001016 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001017}
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001018
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001019namespace {
1020bool ResolveTParamReferenceHelper(
1021 StringRef Name,
1022 const TemplateParameterList *TemplateParameters,
1023 SmallVectorImpl<unsigned> *Position) {
1024 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1025 const NamedDecl *Param = TemplateParameters->getParam(i);
1026 const IdentifierInfo *II = Param->getIdentifier();
1027 if (II && II->getName() == Name) {
1028 Position->push_back(i);
1029 return true;
1030 }
1031
1032 if (const TemplateTemplateParmDecl *TTP =
1033 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1034 Position->push_back(i);
1035 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1036 Position))
1037 return true;
1038 Position->pop_back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001039 }
1040 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001041 return false;
1042}
1043} // unnamed namespace
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001044
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001045bool Sema::resolveTParamReference(
1046 StringRef Name,
1047 const TemplateParameterList *TemplateParameters,
1048 SmallVectorImpl<unsigned> *Position) {
1049 Position->clear();
1050 if (!TemplateParameters)
1051 return false;
1052
1053 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1054}
1055
1056namespace {
1057void CorrectTypoInTParamReferenceHelper(
1058 const TemplateParameterList *TemplateParameters,
1059 SimpleTypoCorrector &Corrector) {
1060 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1061 const NamedDecl *Param = TemplateParameters->getParam(i);
1062 Corrector.addDecl(Param);
1063
1064 if (const TemplateTemplateParmDecl *TTP =
1065 dyn_cast<TemplateTemplateParmDecl>(Param))
1066 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1067 Corrector);
1068 }
1069}
1070} // unnamed namespace
1071
1072StringRef Sema::correctTypoInTParamReference(
1073 StringRef Typo,
1074 const TemplateParameterList *TemplateParameters) {
1075 SimpleTypoCorrector Corrector(Typo);
1076 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1077 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1078 const IdentifierInfo *II = ND->getIdentifier();
1079 assert(II && "SimpleTypoCorrector should not return this decl");
1080 return II->getName();
1081 }
1082 return StringRef();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001083}
1084
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001085InlineCommandComment::RenderKind
1086Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +00001087 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001088
1089 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1090 .Case("b", InlineCommandComment::RenderBold)
1091 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1092 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1093 .Default(InlineCommandComment::RenderNormal);
1094}
1095
Dmitri Gribenkoec925312012-07-06 00:28:32 +00001096} // end namespace comments
1097} // end namespace clang
1098