blob: e51f8781c1b0ca67d6b7daf2c142c9dc6a7af5e7 [file] [log] [blame]
Fariborz Jahanian43c8df82013-03-05 19:52:24 +00001//===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===//
Dmitri Gribenkoec925312012-07-06 00:28:32 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/AST/CommentSema.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000011#include "clang/AST/Attr.h"
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000012#include "clang/AST/CommentCommandTraits.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000013#include "clang/AST/CommentDiagnostic.h"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000014#include "clang/AST/Decl.h"
Dmitri Gribenko34df2202012-07-31 22:37:06 +000015#include "clang/AST/DeclTemplate.h"
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000016#include "clang/Basic/SourceManager.h"
Dmitri Gribenko6743e042012-09-29 11:40:46 +000017#include "clang/Lex/Preprocessor.h"
Dmitri Gribenko6743e042012-09-29 11:40:46 +000018#include "llvm/ADT/SmallString.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "llvm/ADT/StringSwitch.h"
Dmitri Gribenkoec925312012-07-06 00:28:32 +000020
21namespace clang {
22namespace comments {
23
Dmitri Gribenko3ca956f2012-08-31 02:21:44 +000024namespace {
25#include "clang/AST/CommentHTMLTagsProperties.inc"
26} // unnamed namespace
27
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000028Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
Dmitri Gribenko6743e042012-09-29 11:40:46 +000029 DiagnosticsEngine &Diags, CommandTraits &Traits,
30 const Preprocessor *PP) :
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000031 Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits),
Dmitri Gribenkod6662932013-06-22 23:03:37 +000032 PP(PP), ThisDeclInfo(NULL), BriefCommand(NULL), HeaderfileCommand(NULL) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000033}
34
35void Sema::setDecl(const Decl *D) {
Dmitri Gribenko527ab212012-08-01 23:08:09 +000036 if (!D)
37 return;
38
39 ThisDeclInfo = new (Allocator) DeclInfo;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +000040 ThisDeclInfo->CommentDecl = D;
Dmitri Gribenkoe6213dd2012-08-01 23:21:57 +000041 ThisDeclInfo->IsFilled = false;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000042}
43
44ParagraphComment *Sema::actOnParagraphComment(
45 ArrayRef<InlineContentComment *> Content) {
46 return new (Allocator) ParagraphComment(Content);
47}
48
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000049BlockCommandComment *Sema::actOnBlockCommandStart(
50 SourceLocation LocBegin,
51 SourceLocation LocEnd,
52 unsigned CommandID,
53 CommandMarkerKind CommandMarker) {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +000054 BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd,
55 CommandID,
56 CommandMarker);
57 checkContainerDecl(BC);
58 return BC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000059}
60
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +000061void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
62 ArrayRef<BlockCommandComment::Argument> Args) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +000063 Command->setArgs(Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +000064}
65
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +000066void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
67 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +000068 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000069 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +000070 checkBlockCommandDuplicate(Command);
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +000071 if (ThisDeclInfo) {
72 // These checks only make sense if the comment is attached to a
73 // declaration.
74 checkReturnsCommand(Command);
75 checkDeprecatedCommand(Command);
76 }
Dmitri Gribenkoec925312012-07-06 00:28:32 +000077}
78
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000079ParamCommandComment *Sema::actOnParamCommandStart(
80 SourceLocation LocBegin,
81 SourceLocation LocEnd,
82 unsigned CommandID,
83 CommandMarkerKind CommandMarker) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000084 ParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000085 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
86 CommandMarker);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000087
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +000088 if (!isFunctionDecl())
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000089 Diag(Command->getLocation(),
90 diag::warn_doc_param_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +000091 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000092 << Command->getCommandNameRange(Traits);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +000093
94 return Command;
Dmitri Gribenkoec925312012-07-06 00:28:32 +000095}
96
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +000097void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
98 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahanian56fe4062013-03-05 22:46:07 +000099 if (!Info->IsFunctionDeclarationCommand)
100 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000101
102 unsigned DiagSelect;
103 switch (Comment->getCommandID()) {
104 case CommandTraits::KCI_function:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000105 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000106 break;
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000107 case CommandTraits::KCI_functiongroup:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000108 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0;
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000109 break;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000110 case CommandTraits::KCI_method:
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000111 DiagSelect = !isObjCMethodDecl() ? 3 : 0;
112 break;
113 case CommandTraits::KCI_methodgroup:
114 DiagSelect = !isObjCMethodDecl() ? 4 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000115 break;
116 case CommandTraits::KCI_callback:
Fariborz Jahanianabbcbae2013-03-18 23:45:52 +0000117 DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000118 break;
119 default:
120 DiagSelect = 0;
121 break;
122 }
Fariborz Jahanian41bb7132013-03-06 17:36:51 +0000123 if (DiagSelect)
124 Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
125 << Comment->getCommandMarker()
126 << (DiagSelect-1) << (DiagSelect-1)
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000127 << Comment->getSourceRange();
128}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000129
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000130void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
131 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000132 if (!Info->IsRecordLikeDeclarationCommand)
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000133 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000134 unsigned DiagSelect;
135 switch (Comment->getCommandID()) {
136 case CommandTraits::KCI_class:
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000137 DiagSelect = (!isClassOrStructDecl() && !isClassTemplateDecl()) ? 1 : 0;
Fariborz Jahanian04eb8ab2013-05-20 23:40:39 +0000138 // Allow @class command on @interface declarations.
139 // FIXME. Currently, \class and @class are indistinguishable. So,
140 // \class is also allowed on an @interface declaration
141 if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
142 DiagSelect = 0;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000143 break;
144 case CommandTraits::KCI_interface:
145 DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
146 break;
147 case CommandTraits::KCI_protocol:
148 DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
149 break;
150 case CommandTraits::KCI_struct:
151 DiagSelect = !isClassOrStructDecl() ? 4 : 0;
152 break;
153 case CommandTraits::KCI_union:
154 DiagSelect = !isUnionDecl() ? 5 : 0;
155 break;
156 default:
157 DiagSelect = 0;
158 break;
159 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000160 if (DiagSelect)
161 Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
162 << Comment->getCommandMarker()
163 << (DiagSelect-1) << (DiagSelect-1)
164 << Comment->getSourceRange();
165}
166
167void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
168 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000169 if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000170 return;
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000171 unsigned DiagSelect;
172 switch (Comment->getCommandID()) {
173 case CommandTraits::KCI_classdesign:
174 DiagSelect = 1;
175 break;
176 case CommandTraits::KCI_coclass:
177 DiagSelect = 2;
178 break;
179 case CommandTraits::KCI_dependency:
180 DiagSelect = 3;
181 break;
182 case CommandTraits::KCI_helper:
183 DiagSelect = 4;
184 break;
185 case CommandTraits::KCI_helperclass:
186 DiagSelect = 5;
187 break;
188 case CommandTraits::KCI_helps:
189 DiagSelect = 6;
190 break;
191 case CommandTraits::KCI_instancesize:
192 DiagSelect = 7;
193 break;
194 case CommandTraits::KCI_ownership:
195 DiagSelect = 8;
196 break;
197 case CommandTraits::KCI_performance:
198 DiagSelect = 9;
199 break;
200 case CommandTraits::KCI_security:
201 DiagSelect = 10;
202 break;
203 case CommandTraits::KCI_superclass:
204 DiagSelect = 11;
205 break;
206 default:
207 DiagSelect = 0;
208 break;
209 }
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000210 if (DiagSelect)
211 Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
212 << Comment->getCommandMarker()
213 << (DiagSelect-1)
214 << Comment->getSourceRange();
215}
Fariborz Jahanian8a7a5922013-03-05 01:05:07 +0000216
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000217/// \brief Turn a string into the corresponding PassDirection or -1 if it's not
218/// valid.
219static int getParamPassDirection(StringRef Arg) {
220 return llvm::StringSwitch<int>(Arg)
221 .Case("[in]", ParamCommandComment::In)
222 .Case("[out]", ParamCommandComment::Out)
223 .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
224 .Default(-1);
225}
226
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000227void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
228 SourceLocation ArgLocBegin,
229 SourceLocation ArgLocEnd,
230 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000231 std::string ArgLower = Arg.lower();
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000232 int Direction = getParamPassDirection(ArgLower);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000233
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000234 if (Direction == -1) {
235 // Try again with whitespace removed.
236 ArgLower.erase(
237 std::remove_if(ArgLower.begin(), ArgLower.end(), clang::isWhitespace),
238 ArgLower.end());
239 Direction = getParamPassDirection(ArgLower);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000240
241 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000242 if (Direction != -1) {
243 const char *FixedName = ParamCommandComment::getDirectionAsString(
244 (ParamCommandComment::PassDirection)Direction);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000245 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000246 << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
247 } else {
248 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
249 Direction = ParamCommandComment::In; // Sane fall back.
250 }
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000251 }
Benjamin Kramer70e2e172013-11-10 16:26:43 +0000252 Command->setDirection((ParamCommandComment::PassDirection)Direction,
253 /*Explicit=*/true);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000254}
255
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000256void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
257 SourceLocation ArgLocBegin,
258 SourceLocation ArgLocEnd,
259 StringRef Arg) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000260 // Parser will not feed us more arguments than needed.
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000261 assert(Command->getNumArgs() == 0);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000262
263 if (!Command->isDirectionExplicit()) {
264 // User didn't provide a direction argument.
265 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
266 }
267 typedef BlockCommandComment::Argument Argument;
268 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
269 ArgLocEnd),
270 Arg);
271 Command->setArgs(llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000272}
273
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000274void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
275 ParagraphComment *Paragraph) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000276 Command->setParagraph(Paragraph);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000277 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000278}
279
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000280TParamCommandComment *Sema::actOnTParamCommandStart(
281 SourceLocation LocBegin,
282 SourceLocation LocEnd,
283 unsigned CommandID,
284 CommandMarkerKind CommandMarker) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000285 TParamCommandComment *Command =
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000286 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
287 CommandMarker);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000288
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000289 if (!isTemplateOrSpecialization())
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000290 Diag(Command->getLocation(),
291 diag::warn_doc_tparam_not_attached_to_a_template_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000292 << CommandMarker
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000293 << Command->getCommandNameRange(Traits);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000294
295 return Command;
296}
297
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000298void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
299 SourceLocation ArgLocBegin,
300 SourceLocation ArgLocEnd,
301 StringRef Arg) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000302 // Parser will not feed us more arguments than needed.
303 assert(Command->getNumArgs() == 0);
304
305 typedef BlockCommandComment::Argument Argument;
306 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
307 ArgLocEnd),
308 Arg);
309 Command->setArgs(llvm::makeArrayRef(A, 1));
310
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000311 if (!isTemplateOrSpecialization()) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000312 // We already warned that this \\tparam is not attached to a template decl.
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000313 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000314 }
315
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000316 const TemplateParameterList *TemplateParameters =
317 ThisDeclInfo->TemplateParameters;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000318 SmallVector<unsigned, 2> Position;
319 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
320 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
Benjamin Kramereed80612013-11-10 16:55:11 +0000321 TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg];
322 if (PrevCommand) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000323 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
324 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
325 << Arg << ArgRange;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000326 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
327 << PrevCommand->getParamNameRange();
328 }
Benjamin Kramereed80612013-11-10 16:55:11 +0000329 PrevCommand = Command;
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000330 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000331 }
332
333 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
334 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
335 << Arg << ArgRange;
336
337 if (!TemplateParameters || TemplateParameters->size() == 0)
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000338 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000339
340 StringRef CorrectedName;
341 if (TemplateParameters->size() == 1) {
342 const NamedDecl *Param = TemplateParameters->getParam(0);
343 const IdentifierInfo *II = Param->getIdentifier();
344 if (II)
345 CorrectedName = II->getName();
346 } else {
347 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
348 }
349
350 if (!CorrectedName.empty()) {
351 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
352 << CorrectedName
353 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
354 }
355
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000356 return;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000357}
358
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000359void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
360 ParagraphComment *Paragraph) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000361 Command->setParagraph(Paragraph);
362 checkBlockCommandEmptyParagraph(Command);
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000363}
364
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000365InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
366 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000367 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000368 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000369 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000370 return new (Allocator) InlineCommandComment(
371 CommandLocBegin,
372 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000373 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000374 getInlineCommandRenderKind(CommandName),
375 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000376}
377
378InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
379 SourceLocation CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000380 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000381 SourceLocation ArgLocBegin,
382 SourceLocation ArgLocEnd,
383 StringRef Arg) {
384 typedef InlineCommandComment::Argument Argument;
385 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
386 ArgLocEnd),
387 Arg);
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000388 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000389
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000390 return new (Allocator) InlineCommandComment(
391 CommandLocBegin,
392 CommandLocEnd,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000393 CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000394 getInlineCommandRenderKind(CommandName),
395 llvm::makeArrayRef(A, 1));
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000396}
397
398InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
399 SourceLocation LocEnd,
Dmitri Gribenko9304d862012-09-11 19:22:03 +0000400 StringRef CommandName) {
401 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
402 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
403}
404
405InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
406 SourceLocation LocEnd,
407 unsigned CommandID) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000408 ArrayRef<InlineCommandComment::Argument> Args;
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000409 return new (Allocator) InlineCommandComment(
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000410 LocBegin, LocEnd, CommandID,
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000411 InlineCommandComment::RenderNormal,
412 Args);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000413}
414
415TextComment *Sema::actOnText(SourceLocation LocBegin,
416 SourceLocation LocEnd,
417 StringRef Text) {
418 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
419}
420
421VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000422 unsigned CommandID) {
423 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000424 return new (Allocator) VerbatimBlockComment(
425 Loc,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000426 Loc.getLocWithOffset(1 + CommandName.size()),
427 CommandID);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000428}
429
430VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
431 StringRef Text) {
432 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
433}
434
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000435void Sema::actOnVerbatimBlockFinish(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000436 VerbatimBlockComment *Block,
437 SourceLocation CloseNameLocBegin,
438 StringRef CloseName,
439 ArrayRef<VerbatimBlockLineComment *> Lines) {
440 Block->setCloseName(CloseName, CloseNameLocBegin);
441 Block->setLines(Lines);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000442}
443
444VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000445 unsigned CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000446 SourceLocation TextBegin,
447 StringRef Text) {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000448 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000449 LocBegin,
450 TextBegin.getLocWithOffset(Text.size()),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000451 CommandID,
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000452 TextBegin,
453 Text);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000454 checkFunctionDeclVerbatimLine(VL);
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000455 checkContainerDeclVerbatimLine(VL);
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000456 return VL;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000457}
458
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000459HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
460 StringRef TagName) {
461 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000462}
463
Dmitri Gribenkoa9770ad2012-08-06 19:03:12 +0000464void Sema::actOnHTMLStartTagFinish(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000465 HTMLStartTagComment *Tag,
466 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000467 SourceLocation GreaterLoc,
468 bool IsSelfClosing) {
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000469 Tag->setAttrs(Attrs);
470 Tag->setGreaterLoc(GreaterLoc);
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000471 if (IsSelfClosing)
472 Tag->setSelfClosing();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000473 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000474 HTMLOpenTags.push_back(Tag);
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000475}
476
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000477HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
478 SourceLocation LocEnd,
479 StringRef TagName) {
480 HTMLEndTagComment *HET =
481 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
482 if (isHTMLEndTagForbidden(TagName)) {
483 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
484 << TagName << HET->getSourceRange();
485 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();
500 return HET;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000501 }
502
503 while (!HTMLOpenTags.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000504 const HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000505 StringRef LastNotClosedTagName = HST->getTagName();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000506 if (LastNotClosedTagName == TagName)
507 break;
508
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000509 if (isHTMLEndTagOptional(LastNotClosedTagName))
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000510 continue;
511
512 bool OpenLineInvalid;
513 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000514 HST->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000515 &OpenLineInvalid);
516 bool CloseLineInvalid;
517 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000518 HET->getLocation(),
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000519 &CloseLineInvalid);
520
521 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000522 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
523 << HST->getTagName() << HET->getTagName()
524 << HST->getSourceRange() << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000525 else {
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000526 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
527 << HST->getTagName() << HET->getTagName()
528 << HST->getSourceRange();
529 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
530 << HET->getSourceRange();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000531 }
532 }
533
Dmitri Gribenkoe00ffc72012-07-13 00:44:24 +0000534 return HET;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000535}
536
537FullComment *Sema::actOnFullComment(
538 ArrayRef<BlockContentComment *> Blocks) {
Fariborz Jahanian42e31322012-10-11 23:52:50 +0000539 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000540 resolveParamCommandIndexes(FC);
541 return FC;
Dmitri Gribenkoec925312012-07-06 00:28:32 +0000542}
543
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000544void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
Dmitri Gribenkob37d5e82012-09-13 20:36:01 +0000545 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
546 return;
547
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000548 ParagraphComment *Paragraph = Command->getParagraph();
549 if (Paragraph->isWhitespace()) {
550 SourceLocation DiagLoc;
Dmitri Gribenko619e75e2012-07-13 19:02:42 +0000551 if (Command->getNumArgs() > 0)
552 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000553 if (!DiagLoc.isValid())
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000554 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000555 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000556 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000557 << Command->getCommandName(Traits)
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000558 << Command->getSourceRange();
559 }
560}
561
Dmitri Gribenko64305832012-08-03 21:15:32 +0000562void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000563 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000564 return;
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +0000565
566 assert(ThisDeclInfo && "should not call this check on a bare comment");
567
Dmitri Gribenko64305832012-08-03 21:15:32 +0000568 if (isFunctionDecl()) {
Alp Toker314cc812014-01-25 16:55:45 +0000569 if (ThisDeclInfo->ReturnType->isVoidType()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000570 unsigned DiagKind;
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000571 switch (ThisDeclInfo->CommentDecl->getKind()) {
Dmitri Gribenko64305832012-08-03 21:15:32 +0000572 default:
Dmitri Gribenko558babc2012-08-06 16:29:26 +0000573 if (ThisDeclInfo->IsObjCMethod)
574 DiagKind = 3;
575 else
576 DiagKind = 0;
Dmitri Gribenko64305832012-08-03 21:15:32 +0000577 break;
578 case Decl::CXXConstructor:
579 DiagKind = 1;
580 break;
581 case Decl::CXXDestructor:
582 DiagKind = 2;
583 break;
584 }
585 Diag(Command->getLocation(),
586 diag::warn_doc_returns_attached_to_a_void_function)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000587 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000588 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000589 << DiagKind
590 << Command->getSourceRange();
591 }
592 return;
593 }
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000594 else if (isObjCPropertyDecl())
595 return;
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000596
Dmitri Gribenko64305832012-08-03 21:15:32 +0000597 Diag(Command->getLocation(),
598 diag::warn_doc_returns_not_attached_to_a_function_decl)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000599 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000600 << Command->getCommandName(Traits)
Dmitri Gribenko64305832012-08-03 21:15:32 +0000601 << Command->getSourceRange();
602}
603
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000604void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000605 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000606 const BlockCommandComment *PrevCommand = NULL;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000607 if (Info->IsBriefCommand) {
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000608 if (!BriefCommand) {
609 BriefCommand = Command;
610 return;
611 }
612 PrevCommand = BriefCommand;
Fariborz Jahanian1a0cf802013-01-31 23:12:39 +0000613 } else if (Info->IsHeaderfileCommand) {
614 if (!HeaderfileCommand) {
615 HeaderfileCommand = Command;
616 return;
617 }
618 PrevCommand = HeaderfileCommand;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000619 } else {
620 // We don't want to check this command for duplicates.
621 return;
622 }
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000623 StringRef CommandName = Command->getCommandName(Traits);
624 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000625 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000626 << Command->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000627 << CommandName
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000628 << Command->getSourceRange();
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000629 if (CommandName == PrevCommandName)
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000630 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000631 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000632 << PrevCommandName
633 << PrevCommand->getSourceRange();
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000634 else
635 Diag(PrevCommand->getLocation(),
636 diag::note_doc_block_command_previous_alias)
Dmitri Gribenkobcf7f4d2013-03-04 23:06:15 +0000637 << PrevCommand->getCommandMarker()
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000638 << PrevCommandName
639 << CommandName;
Dmitri Gribenko5ec0c752012-08-06 17:08:27 +0000640}
641
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000642void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
643 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
644 return;
645
Dmitri Gribenko6bf8f802014-01-27 17:55:43 +0000646 assert(ThisDeclInfo && "should not call this check on a bare comment");
647
Fariborz Jahanian1c883b92012-10-10 18:34:52 +0000648 const Decl *D = ThisDeclInfo->CommentDecl;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000649 if (!D)
650 return;
651
652 if (D->hasAttr<DeprecatedAttr>() ||
653 D->hasAttr<AvailabilityAttr>() ||
654 D->hasAttr<UnavailableAttr>())
655 return;
656
657 Diag(Command->getLocation(),
658 diag::warn_doc_deprecated_not_sync)
659 << Command->getSourceRange();
660
661 // Try to emit a fixit with a deprecation attribute.
662 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
663 // Don't emit a Fix-It for non-member function definitions. GCC does not
664 // accept attributes on them.
665 const DeclContext *Ctx = FD->getDeclContext();
666 if ((!Ctx || !Ctx->isRecord()) &&
667 FD->doesThisDeclarationHaveABody())
668 return;
669
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000670 StringRef AttributeSpelling = "__attribute__((deprecated))";
671 if (PP) {
672 TokenValue Tokens[] = {
673 tok::kw___attribute, tok::l_paren, tok::l_paren,
674 PP->getIdentifierInfo("deprecated"),
675 tok::r_paren, tok::r_paren
676 };
677 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
678 Tokens);
679 if (!MacroName.empty())
680 AttributeSpelling = MacroName;
681 }
682
683 SmallString<64> TextToInsert(" ");
684 TextToInsert += AttributeSpelling;
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000685 Diag(FD->getLocEnd(),
686 diag::note_add_deprecation_attr)
687 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000688 TextToInsert);
Dmitri Gribenko1da88862012-09-22 21:47:50 +0000689 }
690}
691
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000692void Sema::resolveParamCommandIndexes(const FullComment *FC) {
693 if (!isFunctionDecl()) {
694 // We already warned that \\param commands are not attached to a function
695 // decl.
696 return;
697 }
698
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000699 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000700
701 // Comment AST nodes that correspond to \c ParamVars for which we have
702 // found a \\param command or NULL if no documentation was found so far.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000703 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000704
705 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
NAKAMURA Takumidc2e2fb2013-06-19 06:58:14 +0000706 ParamVarDocs.resize(ParamVars.size(), NULL);
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000707
708 // First pass over all \\param commands: resolve all parameter names.
709 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
710 I != E; ++I) {
711 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
712 if (!PCC || !PCC->hasParamName())
713 continue;
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000714 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000715
716 // Check that referenced parameter name is in the function decl.
717 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
718 ParamVars);
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000719 if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
720 PCC->setIsVarArgParam();
721 continue;
722 }
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000723 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
724 UnresolvedParamCommands.push_back(PCC);
725 continue;
726 }
727 PCC->setParamIndex(ResolvedParamIndex);
728 if (ParamVarDocs[ResolvedParamIndex]) {
729 SourceRange ArgRange = PCC->getParamNameRange();
730 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
731 << ParamName << ArgRange;
732 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
733 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
734 << PrevCommand->getParamNameRange();
735 }
736 ParamVarDocs[ResolvedParamIndex] = PCC;
737 }
738
739 // Find parameter declarations that have no corresponding \\param.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000740 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000741 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
742 if (!ParamVarDocs[i])
743 OrphanedParamDecls.push_back(ParamVars[i]);
744 }
745
746 // Second pass over unresolved \\param commands: do typo correction.
747 // Suggest corrections from a set of parameter declarations that have no
748 // corresponding \\param.
749 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
750 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
751
752 SourceRange ArgRange = PCC->getParamNameRange();
Fariborz Jahanian9d2f1e72012-10-18 21:42:42 +0000753 StringRef ParamName = PCC->getParamNameAsWritten();
Dmitri Gribenko219bd152012-08-24 17:45:39 +0000754 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
755 << ParamName << ArgRange;
756
757 // All parameters documented -- can't suggest a correction.
758 if (OrphanedParamDecls.size() == 0)
759 continue;
760
761 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
762 if (OrphanedParamDecls.size() == 1) {
763 // If one parameter is not documented then that parameter is the only
764 // possible suggestion.
765 CorrectedParamIndex = 0;
766 } else {
767 // Do typo correction.
768 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
769 OrphanedParamDecls);
770 }
771 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
772 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
773 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
774 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
775 << CorrectedII->getName()
776 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
777 }
778 }
779}
780
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000781bool Sema::isFunctionDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000782 if (!ThisDeclInfo)
783 return false;
784 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000785 inspectThisDecl();
Dmitri Gribenko37a7faf2012-08-02 21:45:39 +0000786 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000787}
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000788
Fariborz Jahaniana668bf52013-03-05 23:20:29 +0000789bool Sema::isAnyFunctionDecl() {
790 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
791 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
792}
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000793
794bool Sema::isFunctionOrMethodVariadic() {
Dmitri Gribenkod9eb05a2014-03-19 13:59:36 +0000795 if (!isAnyFunctionDecl() && !isObjCMethodDecl() && !isFunctionTemplateDecl())
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000796 return false;
797 if (const FunctionDecl *FD =
798 dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
799 return FD->isVariadic();
Dmitri Gribenkod9eb05a2014-03-19 13:59:36 +0000800 if (const FunctionTemplateDecl *FTD =
801 dyn_cast<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl))
802 return FTD->getTemplatedDecl()->isVariadic();
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000803 if (const ObjCMethodDecl *MD =
804 dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
805 return MD->isVariadic();
806 return false;
807}
808
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000809bool Sema::isObjCMethodDecl() {
810 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
811 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
812}
Dmitri Gribenkoc0510b92013-06-24 01:33:34 +0000813
Fariborz Jahanian56fe4062013-03-05 22:46:07 +0000814bool Sema::isFunctionPointerVarDecl() {
Fariborz Jahanianf4ba35d2013-03-05 19:40:47 +0000815 if (!ThisDeclInfo)
816 return false;
817 if (!ThisDeclInfo->IsFilled)
818 inspectThisDecl();
819 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
820 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
821 QualType QT = VD->getType();
822 return QT->isFunctionPointerType();
823 }
824 }
825 return false;
826}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000827
Fariborz Jahanian81bbee12013-02-27 00:46:06 +0000828bool Sema::isObjCPropertyDecl() {
829 if (!ThisDeclInfo)
830 return false;
831 if (!ThisDeclInfo->IsFilled)
832 inspectThisDecl();
833 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
834}
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000835
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000836bool Sema::isTemplateOrSpecialization() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000837 if (!ThisDeclInfo)
838 return false;
839 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000840 inspectThisDecl();
Dmitri Gribenko8e5d5f12012-08-06 21:31:15 +0000841 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000842}
843
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000844bool Sema::isRecordLikeDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000845 if (!ThisDeclInfo)
846 return false;
847 if (!ThisDeclInfo->IsFilled)
848 inspectThisDecl();
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000849 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
850 isObjCProtocolDecl();
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000851}
852
853bool Sema::isUnionDecl() {
854 if (!ThisDeclInfo)
855 return false;
856 if (!ThisDeclInfo->IsFilled)
857 inspectThisDecl();
858 if (const RecordDecl *RD =
859 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
860 return RD->isUnion();
861 return false;
862}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000863
Fariborz Jahaniancfbac5d2013-03-08 23:59:23 +0000864bool Sema::isClassOrStructDecl() {
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000865 if (!ThisDeclInfo)
866 return false;
867 if (!ThisDeclInfo->IsFilled)
868 inspectThisDecl();
869 return ThisDeclInfo->CurrentDecl &&
870 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
871 !isUnionDecl();
872}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000873
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000874bool Sema::isClassTemplateDecl() {
875 if (!ThisDeclInfo)
876 return false;
877 if (!ThisDeclInfo->IsFilled)
878 inspectThisDecl();
879 return ThisDeclInfo->CurrentDecl &&
880 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
881}
882
883bool Sema::isFunctionTemplateDecl() {
884 if (!ThisDeclInfo)
885 return false;
886 if (!ThisDeclInfo->IsFilled)
887 inspectThisDecl();
888 return ThisDeclInfo->CurrentDecl &&
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000889 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
Fariborz Jahanianc0607ed2013-06-19 18:08:03 +0000890}
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000891
892bool Sema::isObjCInterfaceDecl() {
893 if (!ThisDeclInfo)
894 return false;
895 if (!ThisDeclInfo->IsFilled)
896 inspectThisDecl();
897 return ThisDeclInfo->CurrentDecl &&
898 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
899}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000900
Fariborz Jahaniana649eee2013-03-07 23:33:11 +0000901bool Sema::isObjCProtocolDecl() {
902 if (!ThisDeclInfo)
903 return false;
904 if (!ThisDeclInfo->IsFilled)
905 inspectThisDecl();
906 return ThisDeclInfo->CurrentDecl &&
907 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
908}
Dmitri Gribenko110dfa82013-12-17 19:28:18 +0000909
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000910ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000911 if (!ThisDeclInfo->IsFilled)
Dmitri Gribenko52cb2182012-07-24 20:58:46 +0000912 inspectThisDecl();
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000913 return ThisDeclInfo->ParamVars;
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000914}
915
916void Sema::inspectThisDecl() {
Dmitri Gribenko527ab212012-08-01 23:08:09 +0000917 ThisDeclInfo->fill();
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000918}
919
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000920unsigned Sema::resolveParmVarReference(StringRef Name,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000921 ArrayRef<const ParmVarDecl *> ParamVars) {
922 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000923 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
924 if (II && II->getName() == Name)
925 return i;
926 }
Dmitri Gribenko02489eb2013-06-24 04:41:32 +0000927 if (Name == "..." && isFunctionOrMethodVariadic())
928 return ParamCommandComment::VarArgParamIndex;
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000929 return ParamCommandComment::InvalidParamIndex;
930}
931
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000932namespace {
933class SimpleTypoCorrector {
934 StringRef Typo;
935 const unsigned MaxEditDistance;
936
937 const NamedDecl *BestDecl;
938 unsigned BestEditDistance;
939 unsigned BestIndex;
940 unsigned NextIndex;
941
942public:
943 SimpleTypoCorrector(StringRef Typo) :
944 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
945 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
946 BestIndex(0), NextIndex(0)
947 { }
948
949 void addDecl(const NamedDecl *ND);
950
951 const NamedDecl *getBestDecl() const {
952 if (BestEditDistance > MaxEditDistance)
953 return NULL;
954
955 return BestDecl;
956 }
957
958 unsigned getBestDeclIndex() const {
959 assert(getBestDecl());
960 return BestIndex;
961 }
962};
963
964void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
965 unsigned CurrIndex = NextIndex++;
966
967 const IdentifierInfo *II = ND->getIdentifier();
968 if (!II)
969 return;
970
971 StringRef Name = II->getName();
972 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
973 if (MinPossibleEditDistance > 0 &&
974 Typo.size() / MinPossibleEditDistance < 3)
975 return;
976
977 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
978 if (EditDistance < BestEditDistance) {
979 BestEditDistance = EditDistance;
980 BestDecl = ND;
981 BestIndex = CurrIndex;
982 }
983}
984} // unnamed namespace
985
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000986unsigned Sema::correctTypoInParmVarReference(
987 StringRef Typo,
Dmitri Gribenko4b7f5fe2012-07-23 17:40:30 +0000988 ArrayRef<const ParmVarDecl *> ParamVars) {
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000989 SimpleTypoCorrector Corrector(Typo);
990 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
991 Corrector.addDecl(ParamVars[i]);
992 if (Corrector.getBestDecl())
993 return Corrector.getBestDeclIndex();
994 else
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000995 return ParamCommandComment::InvalidParamIndex;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000996}
Dmitri Gribenkof26054f2012-07-11 21:38:39 +0000997
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000998namespace {
999bool ResolveTParamReferenceHelper(
1000 StringRef Name,
1001 const TemplateParameterList *TemplateParameters,
1002 SmallVectorImpl<unsigned> *Position) {
1003 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1004 const NamedDecl *Param = TemplateParameters->getParam(i);
1005 const IdentifierInfo *II = Param->getIdentifier();
1006 if (II && II->getName() == Name) {
1007 Position->push_back(i);
1008 return true;
1009 }
1010
1011 if (const TemplateTemplateParmDecl *TTP =
1012 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1013 Position->push_back(i);
1014 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1015 Position))
1016 return true;
1017 Position->pop_back();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001018 }
1019 }
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001020 return false;
1021}
1022} // unnamed namespace
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001023
Dmitri Gribenko34df2202012-07-31 22:37:06 +00001024bool Sema::resolveTParamReference(
1025 StringRef Name,
1026 const TemplateParameterList *TemplateParameters,
1027 SmallVectorImpl<unsigned> *Position) {
1028 Position->clear();
1029 if (!TemplateParameters)
1030 return false;
1031
1032 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1033}
1034
1035namespace {
1036void CorrectTypoInTParamReferenceHelper(
1037 const TemplateParameterList *TemplateParameters,
1038 SimpleTypoCorrector &Corrector) {
1039 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1040 const NamedDecl *Param = TemplateParameters->getParam(i);
1041 Corrector.addDecl(Param);
1042
1043 if (const TemplateTemplateParmDecl *TTP =
1044 dyn_cast<TemplateTemplateParmDecl>(Param))
1045 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1046 Corrector);
1047 }
1048}
1049} // unnamed namespace
1050
1051StringRef Sema::correctTypoInTParamReference(
1052 StringRef Typo,
1053 const TemplateParameterList *TemplateParameters) {
1054 SimpleTypoCorrector Corrector(Typo);
1055 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1056 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1057 const IdentifierInfo *II = ND->getIdentifier();
1058 assert(II && "SimpleTypoCorrector should not return this decl");
1059 return II->getName();
1060 }
1061 return StringRef();
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00001062}
1063
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001064InlineCommandComment::RenderKind
1065Sema::getInlineCommandRenderKind(StringRef Name) const {
Dmitri Gribenko7acbf002012-09-10 20:32:42 +00001066 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +00001067
1068 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1069 .Case("b", InlineCommandComment::RenderBold)
1070 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1071 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1072 .Default(InlineCommandComment::RenderNormal);
1073}
1074
Dmitri Gribenkoec925312012-07-06 00:28:32 +00001075} // end namespace comments
1076} // end namespace clang
1077