Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/CommentSema.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "clang/AST/CommentSema.h"
10
#include "clang/AST/Attr.h"
11
#include "clang/AST/CommentCommandTraits.h"
12
#include "clang/AST/CommentDiagnostic.h"
13
#include "clang/AST/Decl.h"
14
#include "clang/AST/DeclTemplate.h"
15
#include "clang/Basic/LLVM.h"
16
#include "clang/Basic/SourceManager.h"
17
#include "clang/Lex/Preprocessor.h"
18
#include "llvm/ADT/SmallString.h"
19
#include "llvm/ADT/StringSwitch.h"
20
21
namespace clang {
22
namespace comments {
23
24
namespace {
25
#include "clang/AST/CommentHTMLTagsProperties.inc"
26
} // end anonymous namespace
27
28
Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
29
           DiagnosticsEngine &Diags, CommandTraits &Traits,
30
           const Preprocessor *PP) :
31
    Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits),
32
    PP(PP), ThisDeclInfo(nullptr), BriefCommand(nullptr),
33
1.68k
    HeaderfileCommand(nullptr) {
34
1.68k
}
35
36
1.60k
void Sema::setDecl(const Decl *D) {
37
1.60k
  if (!D)
38
0
    return;
39
1.60k
40
1.60k
  ThisDeclInfo = new (Allocator) DeclInfo;
41
1.60k
  ThisDeclInfo->CommentDecl = D;
42
1.60k
  ThisDeclInfo->IsFilled = false;
43
1.60k
}
44
45
ParagraphComment *Sema::actOnParagraphComment(
46
3.58k
                              ArrayRef<InlineContentComment *> Content) {
47
3.58k
  return new (Allocator) ParagraphComment(Content);
48
3.58k
}
49
50
BlockCommandComment *Sema::actOnBlockCommandStart(
51
                                      SourceLocation LocBegin,
52
                                      SourceLocation LocEnd,
53
                                      unsigned CommandID,
54
1.00k
                                      CommandMarkerKind CommandMarker) {
55
1.00k
  BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd,
56
1.00k
                                                                CommandID,
57
1.00k
                                                                CommandMarker);
58
1.00k
  checkContainerDecl(BC);
59
1.00k
  return BC;
60
1.00k
}
61
62
void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
63
0
                                 ArrayRef<BlockCommandComment::Argument> Args) {
64
0
  Command->setArgs(Args);
65
0
}
66
67
void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
68
1.00k
                                   ParagraphComment *Paragraph) {
69
1.00k
  Command->setParagraph(Paragraph);
70
1.00k
  checkBlockCommandEmptyParagraph(Command);
71
1.00k
  checkBlockCommandDuplicate(Command);
72
1.00k
  if (ThisDeclInfo) {
73
    // These checks only make sense if the comment is attached to a
74
    // declaration.
75
995
    checkReturnsCommand(Command);
76
995
    checkDeprecatedCommand(Command);
77
995
  }
78
1.00k
}
79
80
ParamCommandComment *Sema::actOnParamCommandStart(
81
                                      SourceLocation LocBegin,
82
                                      SourceLocation LocEnd,
83
                                      unsigned CommandID,
84
501
                                      CommandMarkerKind CommandMarker) {
85
501
  ParamCommandComment *Command =
86
501
      new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
87
501
                                          CommandMarker);
88
501
89
501
  if (!isFunctionDecl() && 
!isFunctionOrBlockPointerVarLikeDecl()71
)
90
53
    Diag(Command->getLocation(),
91
53
         diag::warn_doc_param_not_attached_to_a_function_decl)
92
53
      << CommandMarker
93
53
      << Command->getCommandNameRange(Traits);
94
501
95
501
  return Command;
96
501
}
97
98
104
void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
99
104
  const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
100
104
  if (!Info->IsFunctionDeclarationCommand)
101
82
    return;
102
22
103
22
  unsigned DiagSelect;
104
22
  switch (Comment->getCommandID()) {
105
11
    case CommandTraits::KCI_function:
106
11
      DiagSelect = (!isAnyFunctionDecl() && 
!isFunctionTemplateDecl()6
)?
13
:
08
;
107
11
      break;
108
0
    case CommandTraits::KCI_functiongroup:
109
0
      DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0;
110
0
      break;
111
6
    case CommandTraits::KCI_method:
112
6
      DiagSelect = !isObjCMethodDecl() ? 3 : 
00
;
113
6
      break;
114
2
    case CommandTraits::KCI_methodgroup:
115
2
      DiagSelect = !isObjCMethodDecl() ? 4 : 
00
;
116
2
      break;
117
3
    case CommandTraits::KCI_callback:
118
3
      DiagSelect = !isFunctionPointerVarDecl() ? 5 : 
00
;
119
3
      break;
120
0
    default:
121
0
      DiagSelect = 0;
122
0
      break;
123
22
  }
124
22
  if (DiagSelect)
125
14
    Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
126
14
    << Comment->getCommandMarker()
127
14
    << (DiagSelect-1) << (DiagSelect-1)
128
14
    << Comment->getSourceRange();
129
22
}
130
131
104
void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
132
104
  const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
133
104
  if (!Info->IsRecordLikeDeclarationCommand)
134
54
    return;
135
50
  unsigned DiagSelect;
136
50
  switch (Comment->getCommandID()) {
137
18
    case CommandTraits::KCI_class:
138
18
      DiagSelect =
139
18
          (!isClassOrStructOrTagTypedefDecl() && 
!isClassTemplateDecl()7
) ?
14
140
14
                                                                         : 0;
141
      // Allow @class command on @interface declarations.
142
      // FIXME. Currently, \class and @class are indistinguishable. So,
143
      // \class is also allowed on an @interface declaration
144
18
      if (DiagSelect && 
Comment->getCommandMarker()4
&&
isObjCInterfaceDecl()4
)
145
2
        DiagSelect = 0;
146
18
      break;
147
9
    case CommandTraits::KCI_interface:
148
5
      DiagSelect = !isObjCInterfaceDecl() ? 
24
: 0;
149
9
      break;
150
6
    case CommandTraits::KCI_protocol:
151
4
      DiagSelect = !isObjCProtocolDecl() ? 3 : 
02
;
152
6
      break;
153
11
    case CommandTraits::KCI_struct:
154
6
      DiagSelect = !isClassOrStructOrTagTypedefDecl() ? 
45
: 0;
155
11
      break;
156
6
    case CommandTraits::KCI_union:
157
3
      DiagSelect = !isUnionDecl() ? 5 : 0;
158
6
      break;
159
0
    default:
160
0
      DiagSelect = 0;
161
0
      break;
162
50
  }
163
50
  if (DiagSelect)
164
18
    Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
165
18
    << Comment->getCommandMarker()
166
18
    << (DiagSelect-1) << (DiagSelect-1)
167
18
    << Comment->getSourceRange();
168
50
}
169
170
1.00k
void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
171
1.00k
  const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
172
1.00k
  if (!Info->IsRecordLikeDetailCommand || 
isRecordLikeDecl()4
)
173
1.00k
    return;
174
4
  unsigned DiagSelect;
175
4
  switch (Comment->getCommandID()) {
176
2
    case CommandTraits::KCI_classdesign:
177
2
      DiagSelect = 1;
178
2
      break;
179
2
    case CommandTraits::KCI_coclass:
180
2
      DiagSelect = 2;
181
2
      break;
182
0
    case CommandTraits::KCI_dependency:
183
0
      DiagSelect = 3;
184
0
      break;
185
0
    case CommandTraits::KCI_helper:
186
0
      DiagSelect = 4;
187
0
      break;
188
0
    case CommandTraits::KCI_helperclass:
189
0
      DiagSelect = 5;
190
0
      break;
191
0
    case CommandTraits::KCI_helps:
192
0
      DiagSelect = 6;
193
0
      break;
194
0
    case CommandTraits::KCI_instancesize:
195
0
      DiagSelect = 7;
196
0
      break;
197
0
    case CommandTraits::KCI_ownership:
198
0
      DiagSelect = 8;
199
0
      break;
200
0
    case CommandTraits::KCI_performance:
201
0
      DiagSelect = 9;
202
0
      break;
203
0
    case CommandTraits::KCI_security:
204
0
      DiagSelect = 10;
205
0
      break;
206
0
    case CommandTraits::KCI_superclass:
207
0
      DiagSelect = 11;
208
0
      break;
209
0
    default:
210
0
      DiagSelect = 0;
211
0
      break;
212
4
  }
213
4
  if (DiagSelect)
214
4
    Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
215
4
    << Comment->getCommandMarker()
216
4
    << (DiagSelect-1)
217
4
    << Comment->getSourceRange();
218
4
}
219
220
/// Turn a string into the corresponding PassDirection or -1 if it's not
221
/// valid.
222
65
static int getParamPassDirection(StringRef Arg) {
223
65
  return llvm::StringSwitch<int>(Arg)
224
65
      .Case("[in]", ParamCommandComment::In)
225
65
      .Case("[out]", ParamCommandComment::Out)
226
65
      .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
227
65
      .Default(-1);
228
65
}
229
230
void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
231
                                         SourceLocation ArgLocBegin,
232
                                         SourceLocation ArgLocEnd,
233
54
                                         StringRef Arg) {
234
54
  std::string ArgLower = Arg.lower();
235
54
  int Direction = getParamPassDirection(ArgLower);
236
54
237
54
  if (Direction == -1) {
238
    // Try again with whitespace removed.
239
11
    ArgLower.erase(
240
11
        std::remove_if(ArgLower.begin(), ArgLower.end(), clang::isWhitespace),
241
11
        ArgLower.end());
242
11
    Direction = getParamPassDirection(ArgLower);
243
11
244
11
    SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
245
11
    if (Direction != -1) {
246
8
      const char *FixedName = ParamCommandComment::getDirectionAsString(
247
8
          (ParamCommandComment::PassDirection)Direction);
248
8
      Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
249
8
          << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
250
3
    } else {
251
3
      Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
252
3
      Direction = ParamCommandComment::In; // Sane fall back.
253
3
    }
254
11
  }
255
54
  Command->setDirection((ParamCommandComment::PassDirection)Direction,
256
54
                        /*Explicit=*/true);
257
54
}
258
259
void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
260
                                         SourceLocation ArgLocBegin,
261
                                         SourceLocation ArgLocEnd,
262
492
                                         StringRef Arg) {
263
  // Parser will not feed us more arguments than needed.
264
492
  assert(Command->getNumArgs() == 0);
265
492
266
492
  if (!Command->isDirectionExplicit()) {
267
    // User didn't provide a direction argument.
268
438
    Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
269
438
  }
270
492
  typedef BlockCommandComment::Argument Argument;
271
492
  Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
272
492
                                                     ArgLocEnd),
273
492
                                         Arg);
274
492
  Command->setArgs(llvm::makeArrayRef(A, 1));
275
492
}
276
277
void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
278
501
                                   ParagraphComment *Paragraph) {
279
501
  Command->setParagraph(Paragraph);
280
501
  checkBlockCommandEmptyParagraph(Command);
281
501
}
282
283
TParamCommandComment *Sema::actOnTParamCommandStart(
284
                                      SourceLocation LocBegin,
285
                                      SourceLocation LocEnd,
286
                                      unsigned CommandID,
287
202
                                      CommandMarkerKind CommandMarker) {
288
202
  TParamCommandComment *Command =
289
202
      new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
290
202
                                           CommandMarker);
291
202
292
202
  if (!isTemplateOrSpecialization())
293
28
    Diag(Command->getLocation(),
294
28
         diag::warn_doc_tparam_not_attached_to_a_template_decl)
295
28
      << CommandMarker
296
28
      << Command->getCommandNameRange(Traits);
297
202
298
202
  return Command;
299
202
}
300
301
void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
302
                                          SourceLocation ArgLocBegin,
303
                                          SourceLocation ArgLocEnd,
304
194
                                          StringRef Arg) {
305
  // Parser will not feed us more arguments than needed.
306
194
  assert(Command->getNumArgs() == 0);
307
194
308
194
  typedef BlockCommandComment::Argument Argument;
309
194
  Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
310
194
                                                     ArgLocEnd),
311
194
                                         Arg);
312
194
  Command->setArgs(llvm::makeArrayRef(A, 1));
313
194
314
194
  if (!isTemplateOrSpecialization()) {
315
    // We already warned that this \\tparam is not attached to a template decl.
316
27
    return;
317
27
  }
318
167
319
167
  const TemplateParameterList *TemplateParameters =
320
167
      ThisDeclInfo->TemplateParameters;
321
167
  SmallVector<unsigned, 2> Position;
322
167
  if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
323
118
    Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
324
118
    TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg];
325
118
    if (PrevCommand) {
326
3
      SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
327
3
      Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
328
3
        << Arg << ArgRange;
329
3
      Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
330
3
        << PrevCommand->getParamNameRange();
331
3
    }
332
118
    PrevCommand = Command;
333
118
    return;
334
118
  }
335
49
336
49
  SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
337
49
  Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
338
49
    << Arg << ArgRange;
339
49
340
49
  if (!TemplateParameters || 
TemplateParameters->size() == 046
)
341
12
    return;
342
37
343
37
  StringRef CorrectedName;
344
37
  if (TemplateParameters->size() == 1) {
345
26
    const NamedDecl *Param = TemplateParameters->getParam(0);
346
26
    const IdentifierInfo *II = Param->getIdentifier();
347
26
    if (II)
348
26
      CorrectedName = II->getName();
349
11
  } else {
350
11
    CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
351
11
  }
352
37
353
37
  if (!CorrectedName.empty()) {
354
33
    Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
355
33
      << CorrectedName
356
33
      << FixItHint::CreateReplacement(ArgRange, CorrectedName);
357
33
  }
358
37
}
359
360
void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
361
202
                                    ParagraphComment *Paragraph) {
362
202
  Command->setParagraph(Paragraph);
363
202
  checkBlockCommandEmptyParagraph(Command);
364
202
}
365
366
InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
367
                                               SourceLocation CommandLocEnd,
368
23
                                               unsigned CommandID) {
369
23
  ArrayRef<InlineCommandComment::Argument> Args;
370
23
  StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
371
23
  return new (Allocator) InlineCommandComment(
372
23
                                  CommandLocBegin,
373
23
                                  CommandLocEnd,
374
23
                                  CommandID,
375
23
                                  getInlineCommandRenderKind(CommandName),
376
23
                                  Args);
377
23
}
378
379
InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
380
                                               SourceLocation CommandLocEnd,
381
                                               unsigned CommandID,
382
                                               SourceLocation ArgLocBegin,
383
                                               SourceLocation ArgLocEnd,
384
61
                                               StringRef Arg) {
385
61
  typedef InlineCommandComment::Argument Argument;
386
61
  Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
387
61
                                                     ArgLocEnd),
388
61
                                         Arg);
389
61
  StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
390
61
391
61
  return new (Allocator) InlineCommandComment(
392
61
                                  CommandLocBegin,
393
61
                                  CommandLocEnd,
394
61
                                  CommandID,
395
61
                                  getInlineCommandRenderKind(CommandName),
396
61
                                  llvm::makeArrayRef(A, 1));
397
61
}
398
399
InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
400
                                                SourceLocation LocEnd,
401
153
                                                StringRef CommandName) {
402
153
  unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
403
153
  return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
404
153
}
405
406
InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
407
                                                SourceLocation LocEnd,
408
157
                                                unsigned CommandID) {
409
157
  ArrayRef<InlineCommandComment::Argument> Args;
410
157
  return new (Allocator) InlineCommandComment(
411
157
                                  LocBegin, LocEnd, CommandID,
412
157
                                  InlineCommandComment::RenderNormal,
413
157
                                  Args);
414
157
}
415
416
TextComment *Sema::actOnText(SourceLocation LocBegin,
417
                             SourceLocation LocEnd,
418
4.03k
                             StringRef Text) {
419
4.03k
  return new (Allocator) TextComment(LocBegin, LocEnd, Text);
420
4.03k
}
421
422
VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
423
37
                                                    unsigned CommandID) {
424
37
  StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
425
37
  return new (Allocator) VerbatimBlockComment(
426
37
                                  Loc,
427
37
                                  Loc.getLocWithOffset(1 + CommandName.size()),
428
37
                                  CommandID);
429
37
}
430
431
VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
432
57
                                                       StringRef Text) {
433
57
  return new (Allocator) VerbatimBlockLineComment(Loc, Text);
434
57
}
435
436
void Sema::actOnVerbatimBlockFinish(
437
                            VerbatimBlockComment *Block,
438
                            SourceLocation CloseNameLocBegin,
439
                            StringRef CloseName,
440
37
                            ArrayRef<VerbatimBlockLineComment *> Lines) {
441
37
  Block->setCloseName(CloseName, CloseNameLocBegin);
442
37
  Block->setLines(Lines);
443
37
}
444
445
VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
446
                                             unsigned CommandID,
447
                                             SourceLocation TextBegin,
448
104
                                             StringRef Text) {
449
104
  VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
450
104
                              LocBegin,
451
104
                              TextBegin.getLocWithOffset(Text.size()),
452
104
                              CommandID,
453
104
                              TextBegin,
454
104
                              Text);
455
104
  checkFunctionDeclVerbatimLine(VL);
456
104
  checkContainerDeclVerbatimLine(VL);
457
104
  return VL;
458
104
}
459
460
HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
461
123
                                                  StringRef TagName) {
462
123
  return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
463
123
}
464
465
void Sema::actOnHTMLStartTagFinish(
466
                              HTMLStartTagComment *Tag,
467
                              ArrayRef<HTMLStartTagComment::Attribute> Attrs,
468
                              SourceLocation GreaterLoc,
469
123
                              bool IsSelfClosing) {
470
123
  Tag->setAttrs(Attrs);
471
123
  Tag->setGreaterLoc(GreaterLoc);
472
123
  if (IsSelfClosing)
473
12
    Tag->setSelfClosing();
474
111
  else if (!isHTMLEndTagForbidden(Tag->getTagName()))
475
97
    HTMLOpenTags.push_back(Tag);
476
123
}
477
478
HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
479
                                         SourceLocation LocEnd,
480
64
                                         StringRef TagName) {
481
64
  HTMLEndTagComment *HET =
482
64
      new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
483
64
  if (isHTMLEndTagForbidden(TagName)) {
484
3
    Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
485
3
      << TagName << HET->getSourceRange();
486
3
    HET->setIsMalformed();
487
3
    return HET;
488
3
  }
489
61
490
61
  bool FoundOpen = false;
491
61
  for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
492
61
       I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
493
75
       I != E; 
++I14
) {
494
57
    if ((*I)->getTagName() == TagName) {
495
43
      FoundOpen = true;
496
43
      break;
497
43
    }
498
57
  }
499
61
  if (!FoundOpen) {
500
18
    Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
501
18
      << HET->getSourceRange();
502
18
    HET->setIsMalformed();
503
18
    return HET;
504
18
  }
505
43
506
49
  
while (43
!HTMLOpenTags.empty()) {
507
49
    HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
508
49
    StringRef LastNotClosedTagName = HST->getTagName();
509
49
    if (LastNotClosedTagName == TagName) {
510
      // If the start tag is malformed, end tag is malformed as well.
511
43
      if (HST->isMalformed())
512
0
        HET->setIsMalformed();
513
43
      break;
514
43
    }
515
6
516
6
    if (isHTMLEndTagOptional(LastNotClosedTagName))
517
0
      continue;
518
6
519
6
    bool OpenLineInvalid;
520
6
    const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
521
6
                                                HST->getLocation(),
522
6
                                                &OpenLineInvalid);
523
6
    bool CloseLineInvalid;
524
6
    const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
525
6
                                                HET->getLocation(),
526
6
                                                &CloseLineInvalid);
527
6
528
6
    if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) {
529
6
      Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
530
6
        << HST->getTagName() << HET->getTagName()
531
6
        << HST->getSourceRange() << HET->getSourceRange();
532
6
      HST->setIsMalformed();
533
0
    } else {
534
0
      Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
535
0
        << HST->getTagName() << HET->getTagName()
536
0
        << HST->getSourceRange();
537
0
      Diag(HET->getLocation(), diag::note_doc_html_end_tag)
538
0
        << HET->getSourceRange();
539
0
      HST->setIsMalformed();
540
0
    }
541
6
  }
542
43
543
43
  return HET;
544
43
}
545
546
FullComment *Sema::actOnFullComment(
547
1.68k
                              ArrayRef<BlockContentComment *> Blocks) {
548
1.68k
  FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
549
1.68k
  resolveParamCommandIndexes(FC);
550
1.68k
551
  // Complain about HTML tags that are not closed.
552
1.73k
  while (!HTMLOpenTags.empty()) {
553
48
    HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
554
48
    if (isHTMLEndTagOptional(HST->getTagName()))
555
0
      continue;
556
48
557
48
    Diag(HST->getLocation(), diag::warn_doc_html_missing_end_tag)
558
48
      << HST->getTagName() << HST->getSourceRange();
559
48
    HST->setIsMalformed();
560
48
  }
561
1.68k
562
1.68k
  return FC;
563
1.68k
}
564
565
1.70k
void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
566
1.70k
  if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
567
76
    return;
568
1.63k
569
1.63k
  ParagraphComment *Paragraph = Command->getParagraph();
570
1.63k
  if (Paragraph->isWhitespace()) {
571
258
    SourceLocation DiagLoc;
572
258
    if (Command->getNumArgs() > 0)
573
36
      DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
574
258
    if (!DiagLoc.isValid())
575
222
      DiagLoc = Command->getCommandNameRange(Traits).getEnd();
576
258
    Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
577
258
      << Command->getCommandMarker()
578
258
      << Command->getCommandName(Traits)
579
258
      << Command->getSourceRange();
580
258
  }
581
1.63k
}
582
583
995
void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
584
995
  if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
585
746
    return;
586
249
587
249
  assert(ThisDeclInfo && "should not call this check on a bare comment");
588
249
589
  // We allow the return command for all @properties because it can be used
590
  // to document the value that the property getter returns.
591
249
  if (isObjCPropertyDecl())
592
8
    return;
593
241
  if (isFunctionDecl() || 
isFunctionOrBlockPointerVarLikeDecl()77
) {
594
178
    assert(!ThisDeclInfo->ReturnType.isNull() &&
595
178
           "should have a valid return type");
596
178
    if (ThisDeclInfo->ReturnType->isVoidType()) {
597
42
      unsigned DiagKind;
598
42
      switch (ThisDeclInfo->CommentDecl->getKind()) {
599
36
      default:
600
36
        if (ThisDeclInfo->IsObjCMethod)
601
2
          DiagKind = 3;
602
34
        else
603
34
          DiagKind = 0;
604
36
        break;
605
3
      case Decl::CXXConstructor:
606
3
        DiagKind = 1;
607
3
        break;
608
3
      case Decl::CXXDestructor:
609
3
        DiagKind = 2;
610
3
        break;
611
42
      }
612
42
      Diag(Command->getLocation(),
613
42
           diag::warn_doc_returns_attached_to_a_void_function)
614
42
        << Command->getCommandMarker()
615
42
        << Command->getCommandName(Traits)
616
42
        << DiagKind
617
42
        << Command->getSourceRange();
618
42
    }
619
178
    return;
620
63
  }
621
63
622
63
  Diag(Command->getLocation(),
623
63
       diag::warn_doc_returns_not_attached_to_a_function_decl)
624
63
    << Command->getCommandMarker()
625
63
    << Command->getCommandName(Traits)
626
63
    << Command->getSourceRange();
627
63
}
628
629
1.00k
void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
630
1.00k
  const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
631
1.00k
  const BlockCommandComment *PrevCommand = nullptr;
632
1.00k
  if (Info->IsBriefCommand) {
633
437
    if (!BriefCommand) {
634
423
      BriefCommand = Command;
635
423
      return;
636
423
    }
637
14
    PrevCommand = BriefCommand;
638
567
  } else if (Info->IsHeaderfileCommand) {
639
11
    if (!HeaderfileCommand) {
640
8
      HeaderfileCommand = Command;
641
8
      return;
642
8
    }
643
3
    PrevCommand = HeaderfileCommand;
644
556
  } else {
645
    // We don't want to check this command for duplicates.
646
556
    return;
647
556
  }
648
17
  StringRef CommandName = Command->getCommandName(Traits);
649
17
  StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
650
17
  Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
651
17
      << Command->getCommandMarker()
652
17
      << CommandName
653
17
      << Command->getSourceRange();
654
17
  if (CommandName == PrevCommandName)
655
13
    Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
656
13
        << PrevCommand->getCommandMarker()
657
13
        << PrevCommandName
658
13
        << PrevCommand->getSourceRange();
659
4
  else
660
4
    Diag(PrevCommand->getLocation(),
661
4
         diag::note_doc_block_command_previous_alias)
662
4
        << PrevCommand->getCommandMarker()
663
4
        << PrevCommandName
664
4
        << CommandName;
665
17
}
666
667
995
void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
668
995
  if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
669
921
    return;
670
74
671
74
  assert(ThisDeclInfo && "should not call this check on a bare comment");
672
74
673
74
  const Decl *D = ThisDeclInfo->CommentDecl;
674
74
  if (!D)
675
0
    return;
676
74
677
74
  if (D->hasAttr<DeprecatedAttr>() ||
678
66
      D->hasAttr<AvailabilityAttr>() ||
679
63
      D->hasAttr<UnavailableAttr>())
680
14
    return;
681
60
682
60
  Diag(Command->getLocation(), diag::warn_doc_deprecated_not_sync)
683
60
      << Command->getSourceRange() << Command->getCommandMarker();
684
60
685
  // Try to emit a fixit with a deprecation attribute.
686
60
  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
687
    // Don't emit a Fix-It for non-member function definitions.  GCC does not
688
    // accept attributes on them.
689
57
    const DeclContext *Ctx = FD->getDeclContext();
690
57
    if ((!Ctx || !Ctx->isRecord()) &&
691
21
        FD->doesThisDeclarationHaveABody())
692
1
      return;
693
56
694
56
    const LangOptions &LO = FD->getLangOpts();
695
56
    const bool DoubleSquareBracket = LO.CPlusPlus14 || 
LO.C2x13
;
696
56
    StringRef AttributeSpelling =
697
47
        DoubleSquareBracket ? "[[deprecated]]" : 
"__attribute__((deprecated))"9
;
698
56
    if (PP) {
699
      // Try to find a replacement macro:
700
      // - In C2x/C++14 we prefer [[deprecated]].
701
      // - If not found or an older C/C++ look for __attribute__((deprecated)).
702
49
      StringRef MacroName;
703
49
      if (DoubleSquareBracket) {
704
40
        TokenValue Tokens[] = {tok::l_square, tok::l_square,
705
40
                               PP->getIdentifierInfo("deprecated"),
706
40
                               tok::r_square, tok::r_square};
707
40
        MacroName = PP->getLastMacroWithSpelling(FD->getLocation(), Tokens);
708
40
        if (!MacroName.empty())
709
3
          AttributeSpelling = MacroName;
710
40
      }
711
49
712
49
      if (MacroName.empty()) {
713
46
        TokenValue Tokens[] = {
714
46
            tok::kw___attribute, tok::l_paren,
715
46
            tok::l_paren,        PP->getIdentifierInfo("deprecated"),
716
46
            tok::r_paren,        tok::r_paren};
717
46
        StringRef MacroName =
718
46
            PP->getLastMacroWithSpelling(FD->getLocation(), Tokens);
719
46
        if (!MacroName.empty())
720
4
          AttributeSpelling = MacroName;
721
46
      }
722
49
    }
723
56
724
56
    SmallString<64> TextToInsert = AttributeSpelling;
725
56
    TextToInsert += " ";
726
56
    SourceLocation Loc = FD->getSourceRange().getBegin();
727
56
    Diag(Loc, diag::note_add_deprecation_attr)
728
56
        << FixItHint::CreateInsertion(Loc, TextToInsert);
729
56
  }
730
60
}
731
732
1.68k
void Sema::resolveParamCommandIndexes(const FullComment *FC) {
733
1.68k
  if (!isFunctionDecl()) {
734
    // We already warned that \\param commands are not attached to a function
735
    // decl.
736
750
    return;
737
750
  }
738
933
739
933
  SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
740
933
741
  // Comment AST nodes that correspond to \c ParamVars for which we have
742
  // found a \\param command or NULL if no documentation was found so far.
743
933
  SmallVector<ParamCommandComment *, 8> ParamVarDocs;
744
933
745
933
  ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
746
933
  ParamVarDocs.resize(ParamVars.size(), nullptr);
747
933
748
  // First pass over all \\param commands: resolve all parameter names.
749
933
  for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
750
3.13k
       I != E; 
++I2.20k
) {
751
2.20k
    ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
752
2.20k
    if (!PCC || 
!PCC->hasParamName()430
)
753
1.78k
      continue;
754
422
    StringRef ParamName = PCC->getParamNameAsWritten();
755
422
756
    // Check that referenced parameter name is in the function decl.
757
422
    const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
758
422
                                                                ParamVars);
759
422
    if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
760
35
      PCC->setIsVarArgParam();
761
35
      continue;
762
35
    }
763
387
    if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
764
140
      UnresolvedParamCommands.push_back(PCC);
765
140
      continue;
766
140
    }
767
247
    PCC->setParamIndex(ResolvedParamIndex);
768
247
    if (ParamVarDocs[ResolvedParamIndex]) {
769
6
      SourceRange ArgRange = PCC->getParamNameRange();
770
6
      Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
771
6
        << ParamName << ArgRange;
772
6
      ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
773
6
      Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
774
6
        << PrevCommand->getParamNameRange();
775
6
    }
776
247
    ParamVarDocs[ResolvedParamIndex] = PCC;
777
247
  }
778
933
779
  // Find parameter declarations that have no corresponding \\param.
780
933
  SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
781
1.69k
  for (unsigned i = 0, e = ParamVarDocs.size(); i != e; 
++i766
) {
782
766
    if (!ParamVarDocs[i])
783
525
      OrphanedParamDecls.push_back(ParamVars[i]);
784
766
  }
785
933
786
  // Second pass over unresolved \\param commands: do typo correction.
787
  // Suggest corrections from a set of parameter declarations that have no
788
  // corresponding \\param.
789
1.07k
  for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; 
++i140
) {
790
140
    const ParamCommandComment *PCC = UnresolvedParamCommands[i];
791
140
792
140
    SourceRange ArgRange = PCC->getParamNameRange();
793
140
    StringRef ParamName = PCC->getParamNameAsWritten();
794
140
    Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
795
140
      << ParamName << ArgRange;
796
140
797
    // All parameters documented -- can't suggest a correction.
798
140
    if (OrphanedParamDecls.size() == 0)
799
34
      continue;
800
106
801
106
    unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
802
106
    if (OrphanedParamDecls.size() == 1) {
803
      // If one parameter is not documented then that parameter is the only
804
      // possible suggestion.
805
89
      CorrectedParamIndex = 0;
806
17
    } else {
807
      // Do typo correction.
808
17
      CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
809
17
                                                          OrphanedParamDecls);
810
17
    }
811
106
    if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
812
94
      const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
813
94
      if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
814
90
        Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
815
90
          << CorrectedII->getName()
816
90
          << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
817
94
    }
818
106
  }
819
933
}
820
821
2.49k
bool Sema::isFunctionDecl() {
822
2.49k
  if (!ThisDeclInfo)
823
102
    return false;
824
2.38k
  if (!ThisDeclInfo->IsFilled)
825
1.27k
    inspectThisDecl();
826
2.38k
  return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
827
2.38k
}
828
829
11
bool Sema::isAnyFunctionDecl() {
830
11
  return isFunctionDecl() && 
ThisDeclInfo->CurrentDecl8
&&
831
8
         isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
832
11
}
833
834
47
bool Sema::isFunctionOrMethodVariadic() {
835
47
  if (!isFunctionDecl() || !ThisDeclInfo->CurrentDecl)
836
0
    return false;
837
47
  if (const FunctionDecl *FD =
838
17
        dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
839
17
    return FD->isVariadic();
840
30
  if (const FunctionTemplateDecl *FTD =
841
18
        dyn_cast<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl))
842
18
    return FTD->getTemplatedDecl()->isVariadic();
843
12
  if (const ObjCMethodDecl *MD =
844
4
        dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
845
4
    return MD->isVariadic();
846
8
  if (const TypedefNameDecl *TD =
847
8
          dyn_cast<TypedefNameDecl>(ThisDeclInfo->CurrentDecl)) {
848
8
    QualType Type = TD->getUnderlyingType();
849
8
    if (Type->isFunctionPointerType() || 
Type->isBlockPointerType()2
)
850
8
      Type = Type->getPointeeType();
851
8
    if (const auto *FT = Type->getAs<FunctionProtoType>())
852
8
      return FT->isVariadic();
853
0
  }
854
0
  return false;
855
0
}
856
857
8
bool Sema::isObjCMethodDecl() {
858
8
  return isFunctionDecl() && 
ThisDeclInfo->CurrentDecl2
&&
859
2
         isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
860
8
}
861
862
3
bool Sema::isFunctionPointerVarDecl() {
863
3
  if (!ThisDeclInfo)
864
0
    return false;
865
3
  if (!ThisDeclInfo->IsFilled)
866
3
    inspectThisDecl();
867
3
  if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
868
0
    if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
869
0
      QualType QT = VD->getType();
870
0
      return QT->isFunctionPointerType();
871
0
    }
872
3
  }
873
3
  return false;
874
3
}
875
876
148
bool Sema::isFunctionOrBlockPointerVarLikeDecl() {
877
148
  if (!ThisDeclInfo)
878
23
    return false;
879
125
  if (!ThisDeclInfo->IsFilled)
880
0
    inspectThisDecl();
881
125
  if (ThisDeclInfo->getKind() != DeclInfo::VariableKind ||
882
89
      !ThisDeclInfo->CurrentDecl)
883
36
    return false;
884
89
  QualType QT;
885
89
  if (const auto *VD = dyn_cast<DeclaratorDecl>(ThisDeclInfo->CurrentDecl))
886
82
    QT = VD->getType();
887
7
  else if (const auto *PD =
888
4
               dyn_cast<ObjCPropertyDecl>(ThisDeclInfo->CurrentDecl))
889
4
    QT = PD->getType();
890
3
  else
891
3
    return false;
892
  // We would like to warn about the 'returns'/'param' commands for
893
  // variables that don't directly specify the function type, so type aliases
894
  // can be ignored.
895
86
  if (QT->getAs<TypedefType>())
896
12
    return false;
897
74
  if (const auto *P = QT->getAs<PointerType>())
898
36
    if (P->getPointeeType()->getAs<TypedefType>())
899
14
      return false;
900
60
  if (const auto *P = QT->getAs<BlockPointerType>())
901
14
    if (P->getPointeeType()->getAs<TypedefType>())
902
2
      return false;
903
58
  return QT->isFunctionPointerType() || 
QT->isBlockPointerType()38
;
904
58
}
905
906
249
bool Sema::isObjCPropertyDecl() {
907
249
  if (!ThisDeclInfo)
908
0
    return false;
909
249
  if (!ThisDeclInfo->IsFilled)
910
148
    inspectThisDecl();
911
249
  return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
912
249
}
913
914
396
bool Sema::isTemplateOrSpecialization() {
915
396
  if (!ThisDeclInfo)
916
9
    return false;
917
387
  if (!ThisDeclInfo->IsFilled)
918
132
    inspectThisDecl();
919
387
  return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
920
387
}
921
922
4
bool Sema::isRecordLikeDecl() {
923
4
  if (!ThisDeclInfo)
924
0
    return false;
925
4
  if (!ThisDeclInfo->IsFilled)
926
0
    inspectThisDecl();
927
4
  return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
928
4
         isObjCProtocolDecl();
929
4
}
930
931
10
bool Sema::isUnionDecl() {
932
10
  if (!ThisDeclInfo)
933
0
    return false;
934
10
  if (!ThisDeclInfo->IsFilled)
935
6
    inspectThisDecl();
936
10
  if (const RecordDecl *RD =
937
6
        dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
938
6
    return RD->isUnion();
939
4
  return false;
940
4
}
941
35
static bool isClassOrStructDeclImpl(const Decl *D) {
942
35
  if (auto *record = dyn_cast_or_null<RecordDecl>(D))
943
20
    return !record->isUnion();
944
15
945
15
  return false;
946
15
}
947
948
4
bool Sema::isClassOrStructDecl() {
949
4
  if (!ThisDeclInfo)
950
0
    return false;
951
4
  if (!ThisDeclInfo->IsFilled)
952
0
    inspectThisDecl();
953
4
954
4
  if (!ThisDeclInfo->CurrentDecl)
955
0
    return false;
956
4
957
4
  return isClassOrStructDeclImpl(ThisDeclInfo->CurrentDecl);
958
4
}
959
960
29
bool Sema::isClassOrStructOrTagTypedefDecl() {
961
29
  if (!ThisDeclInfo)
962
0
    return false;
963
29
  if (!ThisDeclInfo->IsFilled)
964
27
    inspectThisDecl();
965
29
966
29
  if (!ThisDeclInfo->CurrentDecl)
967
0
    return false;
968
29
969
29
  if (isClassOrStructDeclImpl(ThisDeclInfo->CurrentDecl))
970
15
    return true;
971
14
972
14
  if (auto *ThisTypedefDecl = dyn_cast<TypedefDecl>(ThisDeclInfo->CurrentDecl)) {
973
2
    auto UnderlyingType = ThisTypedefDecl->getUnderlyingType();
974
2
    if (auto ThisElaboratedType = dyn_cast<ElaboratedType>(UnderlyingType)) {
975
2
      auto DesugaredType = ThisElaboratedType->desugar();
976
2
      if (auto *DesugaredTypePtr = DesugaredType.getTypePtrOrNull()) {
977
2
        if (auto *ThisRecordType = dyn_cast<RecordType>(DesugaredTypePtr)) {
978
2
          return isClassOrStructDeclImpl(ThisRecordType->getAsRecordDecl());
979
2
        }
980
12
      }
981
2
    }
982
2
  }
983
12
984
12
  return false;
985
12
}
986
987
7
bool Sema::isClassTemplateDecl() {
988
7
  if (!ThisDeclInfo)
989
0
    return false;
990
7
  if (!ThisDeclInfo->IsFilled)
991
0
    inspectThisDecl();
992
7
  return ThisDeclInfo->CurrentDecl &&
993
7
          (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
994
7
}
995
996
6
bool Sema::isFunctionTemplateDecl() {
997
6
  if (!ThisDeclInfo)
998
0
    return false;
999
6
  if (!ThisDeclInfo->IsFilled)
1000
0
    inspectThisDecl();
1001
6
  return ThisDeclInfo->CurrentDecl &&
1002
6
         (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
1003
6
}
1004
1005
17
bool Sema::isObjCInterfaceDecl() {
1006
17
  if (!ThisDeclInfo)
1007
0
    return false;
1008
17
  if (!ThisDeclInfo->IsFilled)
1009
7
    inspectThisDecl();
1010
17
  return ThisDeclInfo->CurrentDecl &&
1011
17
         isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
1012
17
}
1013
1014
10
bool Sema::isObjCProtocolDecl() {
1015
10
  if (!ThisDeclInfo)
1016
0
    return false;
1017
10
  if (!ThisDeclInfo->IsFilled)
1018
6
    inspectThisDecl();
1019
10
  return ThisDeclInfo->CurrentDecl &&
1020
10
         isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
1021
10
}
1022
1023
933
ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
1024
933
  if (!ThisDeclInfo->IsFilled)
1025
0
    inspectThisDecl();
1026
933
  return ThisDeclInfo->ParamVars;
1027
933
}
1028
1029
1.60k
void Sema::inspectThisDecl() {
1030
1.60k
  ThisDeclInfo->fill();
1031
1.60k
}
1032
1033
unsigned Sema::resolveParmVarReference(StringRef Name,
1034
422
                                       ArrayRef<const ParmVarDecl *> ParamVars) {
1035
708
  for (unsigned i = 0, e = ParamVars.size(); i != e; 
++i286
) {
1036
533
    const IdentifierInfo *II = ParamVars[i]->getIdentifier();
1037
533
    if (II && 
II->getName() == Name529
)
1038
247
      return i;
1039
533
  }
1040
175
  if (Name == "..." && 
isFunctionOrMethodVariadic()47
)
1041
35
    return ParamCommandComment::VarArgParamIndex;
1042
140
  return ParamCommandComment::InvalidParamIndex;
1043
140
}
1044
1045
namespace {
1046
class SimpleTypoCorrector {
1047
  const NamedDecl *BestDecl;
1048
1049
  StringRef Typo;
1050
  const unsigned MaxEditDistance;
1051
1052
  unsigned BestEditDistance;
1053
  unsigned BestIndex;
1054
  unsigned NextIndex;
1055
1056
public:
1057
  explicit SimpleTypoCorrector(StringRef Typo)
1058
      : BestDecl(nullptr), Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
1059
28
        BestEditDistance(MaxEditDistance + 1), BestIndex(0), NextIndex(0) {}
1060
1061
  void addDecl(const NamedDecl *ND);
1062
1063
33
  const NamedDecl *getBestDecl() const {
1064
33
    if (BestEditDistance > MaxEditDistance)
1065
16
      return nullptr;
1066
17
1067
17
    return BestDecl;
1068
17
  }
1069
1070
5
  unsigned getBestDeclIndex() const {
1071
5
    assert(getBestDecl());
1072
5
    return BestIndex;
1073
5
  }
1074
};
1075
1076
59
void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
1077
59
  unsigned CurrIndex = NextIndex++;
1078
59
1079
59
  const IdentifierInfo *II = ND->getIdentifier();
1080
59
  if (!II)
1081
0
    return;
1082
59
1083
59
  StringRef Name = II->getName();
1084
59
  unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
1085
59
  if (MinPossibleEditDistance > 0 &&
1086
20
      Typo.size() / MinPossibleEditDistance < 3)
1087
15
    return;
1088
44
1089
44
  unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
1090
44
  if (EditDistance < BestEditDistance) {
1091
12
    BestEditDistance = EditDistance;
1092
12
    BestDecl = ND;
1093
12
    BestIndex = CurrIndex;
1094
12
  }
1095
44
}
1096
} // end anonymous namespace
1097
1098
unsigned Sema::correctTypoInParmVarReference(
1099
                                    StringRef Typo,
1100
17
                                    ArrayRef<const ParmVarDecl *> ParamVars) {
1101
17
  SimpleTypoCorrector Corrector(Typo);
1102
51
  for (unsigned i = 0, e = ParamVars.size(); i != e; 
++i34
)
1103
34
    Corrector.addDecl(ParamVars[i]);
1104
17
  if (Corrector.getBestDecl())
1105
5
    return Corrector.getBestDeclIndex();
1106
12
  else
1107
12
    return ParamCommandComment::InvalidParamIndex;
1108
17
}
1109
1110
namespace {
1111
bool ResolveTParamReferenceHelper(
1112
                            StringRef Name,
1113
                            const TemplateParameterList *TemplateParameters,
1114
197
                            SmallVectorImpl<unsigned> *Position) {
1115
284
  for (unsigned i = 0, e = TemplateParameters->size(); i != e; 
++i87
) {
1116
235
    const NamedDecl *Param = TemplateParameters->getParam(i);
1117
235
    const IdentifierInfo *II = Param->getIdentifier();
1118
235
    if (II && II->getName() == Name) {
1119
118
      Position->push_back(i);
1120
118
      return true;
1121
118
    }
1122
117
1123
117
    if (const TemplateTemplateParmDecl *TTP =
1124
33
            dyn_cast<TemplateTemplateParmDecl>(Param)) {
1125
33
      Position->push_back(i);
1126
33
      if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1127
33
                                       Position))
1128
30
        return true;
1129
3
      Position->pop_back();
1130
3
    }
1131
117
  }
1132
49
  return false;
1133
197
}
1134
} // end anonymous namespace
1135
1136
bool Sema::resolveTParamReference(
1137
                            StringRef Name,
1138
                            const TemplateParameterList *TemplateParameters,
1139
167
                            SmallVectorImpl<unsigned> *Position) {
1140
167
  Position->clear();
1141
167
  if (!TemplateParameters)
1142
3
    return false;
1143
164
1144
164
  return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1145
164
}
1146
1147
namespace {
1148
void CorrectTypoInTParamReferenceHelper(
1149
                            const TemplateParameterList *TemplateParameters,
1150
11
                            SimpleTypoCorrector &Corrector) {
1151
36
  for (unsigned i = 0, e = TemplateParameters->size(); i != e; 
++i25
) {
1152
25
    const NamedDecl *Param = TemplateParameters->getParam(i);
1153
25
    Corrector.addDecl(Param);
1154
25
1155
25
    if (const TemplateTemplateParmDecl *TTP =
1156
0
            dyn_cast<TemplateTemplateParmDecl>(Param))
1157
0
      CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1158
0
                                         Corrector);
1159
25
  }
1160
11
}
1161
} // end anonymous namespace
1162
1163
StringRef Sema::correctTypoInTParamReference(
1164
                            StringRef Typo,
1165
11
                            const TemplateParameterList *TemplateParameters) {
1166
11
  SimpleTypoCorrector Corrector(Typo);
1167
11
  CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1168
11
  if (const NamedDecl *ND = Corrector.getBestDecl()) {
1169
7
    const IdentifierInfo *II = ND->getIdentifier();
1170
7
    assert(II && "SimpleTypoCorrector should not return this decl");
1171
7
    return II->getName();
1172
7
  }
1173
4
  return StringRef();
1174
4
}
1175
1176
InlineCommandComment::RenderKind
1177
84
Sema::getInlineCommandRenderKind(StringRef Name) const {
1178
84
  assert(Traits.getCommandInfo(Name)->IsInlineCommand);
1179
84
1180
84
  return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1181
84
      .Case("b", InlineCommandComment::RenderBold)
1182
84
      .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1183
84
      .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1184
84
      .Case("anchor", InlineCommandComment::RenderAnchor)
1185
84
      .Default(InlineCommandComment::RenderNormal);
1186
84
}
1187
1188
} // end namespace comments
1189
} // end namespace clang