Coverage Report

Created: 2020-02-25 14:32

/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.61k
    HeaderfileCommand(nullptr) {
34
1.61k
}
35
36
1.53k
void Sema::setDecl(const Decl *D) {
37
1.53k
  if (!D)
38
0
    return;
39
1.53k
40
1.53k
  ThisDeclInfo = new (Allocator) DeclInfo;
41
1.53k
  ThisDeclInfo->CommentDecl = D;
42
1.53k
  ThisDeclInfo->IsFilled = false;
43
1.53k
}
44
45
ParagraphComment *Sema::actOnParagraphComment(
46
3.44k
                              ArrayRef<InlineContentComment *> Content) {
47
3.44k
  return new (Allocator) ParagraphComment(Content);
48
3.44k
}
49
50
BlockCommandComment *Sema::actOnBlockCommandStart(
51
                                      SourceLocation LocBegin,
52
                                      SourceLocation LocEnd,
53
                                      unsigned CommandID,
54
963
                                      CommandMarkerKind CommandMarker) {
55
963
  BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd,
56
963
                                                                CommandID,
57
963
                                                                CommandMarker);
58
963
  checkContainerDecl(BC);
59
963
  return BC;
60
963
}
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
963
                                   ParagraphComment *Paragraph) {
69
963
  Command->setParagraph(Paragraph);
70
963
  checkBlockCommandEmptyParagraph(Command);
71
963
  checkBlockCommandDuplicate(Command);
72
963
  if (ThisDeclInfo) {
73
954
    // These checks only make sense if the comment is attached to a
74
954
    // declaration.
75
954
    checkReturnsCommand(Command);
76
954
    checkDeprecatedCommand(Command);
77
954
  }
78
963
}
79
80
ParamCommandComment *Sema::actOnParamCommandStart(
81
                                      SourceLocation LocBegin,
82
                                      SourceLocation LocEnd,
83
                                      unsigned CommandID,
84
475
                                      CommandMarkerKind CommandMarker) {
85
475
  ParamCommandComment *Command =
86
475
      new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
87
475
                                          CommandMarker);
88
475
89
475
  if (!isFunctionDecl() && 
!isFunctionOrBlockPointerVarLikeDecl()66
)
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
475
95
475
  return Command;
96
475
}
97
98
93
void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
99
93
  const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
100
93
  if (!Info->IsFunctionDeclarationCommand)
101
74
    return;
102
19
103
19
  unsigned DiagSelect;
104
19
  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
4
    case CommandTraits::KCI_method:
112
4
      DiagSelect = !isObjCMethodDecl() ? 3 : 
00
;
113
4
      break;
114
1
    case CommandTraits::KCI_methodgroup:
115
1
      DiagSelect = !isObjCMethodDecl() ? 4 : 
00
;
116
1
      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
19
  }
124
19
  if (DiagSelect)
125
11
    Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
126
11
    << Comment->getCommandMarker()
127
11
    << (DiagSelect-1) << (DiagSelect-1)
128
11
    << Comment->getSourceRange();
129
19
}
130
131
93
void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
132
93
  const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
133
93
  if (!Info->IsRecordLikeDeclarationCommand)
134
51
    return;
135
42
  unsigned DiagSelect;
136
42
  switch (Comment->getCommandID()) {
137
17
    case CommandTraits::KCI_class:
138
17
      DiagSelect =
139
17
          (!isClassOrStructOrTagTypedefDecl() && 
!isClassTemplateDecl()6
) ?
13
140
17
                                                                         : 
014
;
141
17
      // Allow @class command on @interface declarations.
142
17
      // FIXME. Currently, \class and @class are indistinguishable. So,
143
17
      // \class is also allowed on an @interface declaration
144
17
      if (DiagSelect && 
Comment->getCommandMarker()3
&&
isObjCInterfaceDecl()3
)
145
1
        DiagSelect = 0;
146
17
      break;
147
6
    case CommandTraits::KCI_interface:
148
6
      DiagSelect = !isObjCInterfaceDecl() ? 
23
:
03
;
149
6
      break;
150
3
    case CommandTraits::KCI_protocol:
151
3
      DiagSelect = !isObjCProtocolDecl() ? 
32
:
01
;
152
3
      break;
153
10
    case CommandTraits::KCI_struct:
154
10
      DiagSelect = !isClassOrStructOrTagTypedefDecl() ? 
44
:
06
;
155
10
      break;
156
6
    case CommandTraits::KCI_union:
157
6
      DiagSelect = !isUnionDecl() ? 
53
:
03
;
158
6
      break;
159
0
    default:
160
0
      DiagSelect = 0;
161
0
      break;
162
42
  }
163
42
  if (DiagSelect)
164
14
    Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
165
14
    << Comment->getCommandMarker()
166
14
    << (DiagSelect-1) << (DiagSelect-1)
167
14
    << Comment->getSourceRange();
168
42
}
169
170
963
void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
171
963
  const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
172
963
  if (!Info->IsRecordLikeDetailCommand || 
isRecordLikeDecl()2
)
173
961
    return;
174
2
  unsigned DiagSelect;
175
2
  switch (Comment->getCommandID()) {
176
1
    case CommandTraits::KCI_classdesign:
177
1
      DiagSelect = 1;
178
1
      break;
179
1
    case CommandTraits::KCI_coclass:
180
1
      DiagSelect = 2;
181
1
      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
2
  }
213
2
  if (DiagSelect)
214
2
    Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
215
2
    << Comment->getCommandMarker()
216
2
    << (DiagSelect-1)
217
2
    << Comment->getSourceRange();
218
2
}
219
220
/// Turn a string into the corresponding PassDirection or -1 if it's not
221
/// valid.
222
59
static int getParamPassDirection(StringRef Arg) {
223
59
  return llvm::StringSwitch<int>(Arg)
224
59
      .Case("[in]", ParamCommandComment::In)
225
59
      .Case("[out]", ParamCommandComment::Out)
226
59
      .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
227
59
      .Default(-1);
228
59
}
229
230
void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
231
                                         SourceLocation ArgLocBegin,
232
                                         SourceLocation ArgLocEnd,
233
48
                                         StringRef Arg) {
234
48
  std::string ArgLower = Arg.lower();
235
48
  int Direction = getParamPassDirection(ArgLower);
236
48
237
48
  if (Direction == -1) {
238
11
    // 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
8
    } 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
48
  Command->setDirection((ParamCommandComment::PassDirection)Direction,
256
48
                        /*Explicit=*/true);
257
48
}
258
259
void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
260
                                         SourceLocation ArgLocBegin,
261
                                         SourceLocation ArgLocEnd,
262
466
                                         StringRef Arg) {
263
466
  // Parser will not feed us more arguments than needed.
264
466
  assert(Command->getNumArgs() == 0);
265
466
266
466
  if (!Command->isDirectionExplicit()) {
267
418
    // User didn't provide a direction argument.
268
418
    Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
269
418
  }
270
466
  typedef BlockCommandComment::Argument Argument;
271
466
  Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
272
466
                                                     ArgLocEnd),
273
466
                                         Arg);
274
466
  Command->setArgs(llvm::makeArrayRef(A, 1));
275
466
}
276
277
void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
278
475
                                   ParagraphComment *Paragraph) {
279
475
  Command->setParagraph(Paragraph);
280
475
  checkBlockCommandEmptyParagraph(Command);
281
475
}
282
283
TParamCommandComment *Sema::actOnTParamCommandStart(
284
                                      SourceLocation LocBegin,
285
                                      SourceLocation LocEnd,
286
                                      unsigned CommandID,
287
199
                                      CommandMarkerKind CommandMarker) {
288
199
  TParamCommandComment *Command =
289
199
      new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
290
199
                                           CommandMarker);
291
199
292
199
  if (!isTemplateOrSpecialization())
293
27
    Diag(Command->getLocation(),
294
27
         diag::warn_doc_tparam_not_attached_to_a_template_decl)
295
27
      << CommandMarker
296
27
      << Command->getCommandNameRange(Traits);
297
199
298
199
  return Command;
299
199
}
300
301
void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
302
                                          SourceLocation ArgLocBegin,
303
                                          SourceLocation ArgLocEnd,
304
191
                                          StringRef Arg) {
305
191
  // Parser will not feed us more arguments than needed.
306
191
  assert(Command->getNumArgs() == 0);
307
191
308
191
  typedef BlockCommandComment::Argument Argument;
309
191
  Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
310
191
                                                     ArgLocEnd),
311
191
                                         Arg);
312
191
  Command->setArgs(llvm::makeArrayRef(A, 1));
313
191
314
191
  if (!isTemplateOrSpecialization()) {
315
26
    // We already warned that this \\tparam is not attached to a template decl.
316
26
    return;
317
26
  }
318
165
319
165
  const TemplateParameterList *TemplateParameters =
320
165
      ThisDeclInfo->TemplateParameters;
321
165
  SmallVector<unsigned, 2> Position;
322
165
  if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
323
116
    Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
324
116
    TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg];
325
116
    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
116
    PrevCommand = Command;
333
116
    return;
334
116
  }
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
26
  } 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
199
                                    ParagraphComment *Paragraph) {
362
199
  Command->setParagraph(Paragraph);
363
199
  checkBlockCommandEmptyParagraph(Command);
364
199
}
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
54
                                               StringRef Arg) {
385
54
  typedef InlineCommandComment::Argument Argument;
386
54
  Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
387
54
                                                     ArgLocEnd),
388
54
                                         Arg);
389
54
  StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
390
54
391
54
  return new (Allocator) InlineCommandComment(
392
54
                                  CommandLocBegin,
393
54
                                  CommandLocEnd,
394
54
                                  CommandID,
395
54
                                  getInlineCommandRenderKind(CommandName),
396
54
                                  llvm::makeArrayRef(A, 1));
397
54
}
398
399
InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
400
                                                SourceLocation LocEnd,
401
151
                                                StringRef CommandName) {
402
151
  unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
403
151
  return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
404
151
}
405
406
InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
407
                                                SourceLocation LocEnd,
408
155
                                                unsigned CommandID) {
409
155
  ArrayRef<InlineCommandComment::Argument> Args;
410
155
  return new (Allocator) InlineCommandComment(
411
155
                                  LocBegin, LocEnd, CommandID,
412
155
                                  InlineCommandComment::RenderNormal,
413
155
                                  Args);
414
155
}
415
416
TextComment *Sema::actOnText(SourceLocation LocBegin,
417
                             SourceLocation LocEnd,
418
3.87k
                             StringRef Text) {
419
3.87k
  return new (Allocator) TextComment(LocBegin, LocEnd, Text);
420
3.87k
}
421
422
VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
423
35
                                                    unsigned CommandID) {
424
35
  StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
425
35
  return new (Allocator) VerbatimBlockComment(
426
35
                                  Loc,
427
35
                                  Loc.getLocWithOffset(1 + CommandName.size()),
428
35
                                  CommandID);
429
35
}
430
431
VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
432
55
                                                       StringRef Text) {
433
55
  return new (Allocator) VerbatimBlockLineComment(Loc, Text);
434
55
}
435
436
void Sema::actOnVerbatimBlockFinish(
437
                            VerbatimBlockComment *Block,
438
                            SourceLocation CloseNameLocBegin,
439
                            StringRef CloseName,
440
35
                            ArrayRef<VerbatimBlockLineComment *> Lines) {
441
35
  Block->setCloseName(CloseName, CloseNameLocBegin);
442
35
  Block->setLines(Lines);
443
35
}
444
445
VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
446
                                             unsigned CommandID,
447
                                             SourceLocation TextBegin,
448
93
                                             StringRef Text) {
449
93
  VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
450
93
                              LocBegin,
451
93
                              TextBegin.getLocWithOffset(Text.size()),
452
93
                              CommandID,
453
93
                              TextBegin,
454
93
                              Text);
455
93
  checkFunctionDeclVerbatimLine(VL);
456
93
  checkContainerDeclVerbatimLine(VL);
457
93
  return VL;
458
93
}
459
460
HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
461
119
                                                  StringRef TagName) {
462
119
  return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
463
119
}
464
465
void Sema::actOnHTMLStartTagFinish(
466
                              HTMLStartTagComment *Tag,
467
                              ArrayRef<HTMLStartTagComment::Attribute> Attrs,
468
                              SourceLocation GreaterLoc,
469
119
                              bool IsSelfClosing) {
470
119
  Tag->setAttrs(Attrs);
471
119
  Tag->setGreaterLoc(GreaterLoc);
472
119
  if (IsSelfClosing)
473
10
    Tag->setSelfClosing();
474
109
  else if (!isHTMLEndTagForbidden(Tag->getTagName()))
475
95
    HTMLOpenTags.push_back(Tag);
476
119
}
477
478
HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
479
                                         SourceLocation LocEnd,
480
62
                                         StringRef TagName) {
481
62
  HTMLEndTagComment *HET =
482
62
      new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
483
62
  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
59
490
59
  bool FoundOpen = false;
491
59
  for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
492
59
       I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
493
73
       I != E; 
++I14
) {
494
55
    if ((*I)->getTagName() == TagName) {
495
41
      FoundOpen = true;
496
41
      break;
497
41
    }
498
55
  }
499
59
  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
41
506
47
  
while (41
!HTMLOpenTags.empty()) {
507
47
    HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
508
47
    StringRef LastNotClosedTagName = HST->getTagName();
509
47
    if (LastNotClosedTagName == TagName) {
510
41
      // If the start tag is malformed, end tag is malformed as well.
511
41
      if (HST->isMalformed())
512
0
        HET->setIsMalformed();
513
41
      break;
514
41
    }
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
6
    } 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
41
543
41
  return HET;
544
41
}
545
546
FullComment *Sema::actOnFullComment(
547
1.61k
                              ArrayRef<BlockContentComment *> Blocks) {
548
1.61k
  FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
549
1.61k
  resolveParamCommandIndexes(FC);
550
1.61k
551
1.61k
  // Complain about HTML tags that are not closed.
552
1.66k
  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.61k
562
1.61k
  return FC;
563
1.61k
}
564
565
1.63k
void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
566
1.63k
  if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
567
76
    return;
568
1.56k
569
1.56k
  ParagraphComment *Paragraph = Command->getParagraph();
570
1.56k
  if (Paragraph->isWhitespace()) {
571
246
    SourceLocation DiagLoc;
572
246
    if (Command->getNumArgs() > 0)
573
36
      DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
574
246
    if (!DiagLoc.isValid())
575
210
      DiagLoc = Command->getCommandNameRange(Traits).getEnd();
576
246
    Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
577
246
      << Command->getCommandMarker()
578
246
      << Command->getCommandName(Traits)
579
246
      << Command->getSourceRange();
580
246
  }
581
1.56k
}
582
583
954
void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
584
954
  if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
585
716
    return;
586
238
587
238
  assert(ThisDeclInfo && "should not call this check on a bare comment");
588
238
589
238
  // We allow the return command for all @properties because it can be used
590
238
  // to document the value that the property getter returns.
591
238
  if (isObjCPropertyDecl())
592
5
    return;
593
233
  if (isFunctionDecl() || 
isFunctionOrBlockPointerVarLikeDecl()72
) {
594
172
    assert(!ThisDeclInfo->ReturnType.isNull() &&
595
172
           "should have a valid return type");
596
172
    if (ThisDeclInfo->ReturnType->isVoidType()) {
597
40
      unsigned DiagKind;
598
40
      switch (ThisDeclInfo->CommentDecl->getKind()) {
599
34
      default:
600
34
        if (ThisDeclInfo->IsObjCMethod)
601
1
          DiagKind = 3;
602
33
        else
603
33
          DiagKind = 0;
604
34
        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
40
      }
612
40
      Diag(Command->getLocation(),
613
40
           diag::warn_doc_returns_attached_to_a_void_function)
614
40
        << Command->getCommandMarker()
615
40
        << Command->getCommandName(Traits)
616
40
        << DiagKind
617
40
        << Command->getSourceRange();
618
40
    }
619
172
    return;
620
61
  }
621
61
622
61
  Diag(Command->getLocation(),
623
61
       diag::warn_doc_returns_not_attached_to_a_function_decl)
624
61
    << Command->getCommandMarker()
625
61
    << Command->getCommandName(Traits)
626
61
    << Command->getSourceRange();
627
61
}
628
629
963
void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
630
963
  const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
631
963
  const BlockCommandComment *PrevCommand = nullptr;
632
963
  if (Info->IsBriefCommand) {
633
421
    if (!BriefCommand) {
634
407
      BriefCommand = Command;
635
407
      return;
636
407
    }
637
14
    PrevCommand = BriefCommand;
638
542
  } else if (Info->IsHeaderfileCommand) {
639
11
    if (!HeaderfileCommand) {
640
8
      HeaderfileCommand = Command;
641
8
      return;
642
8
    }
643
3
    PrevCommand = HeaderfileCommand;
644
531
  } else {
645
531
    // We don't want to check this command for duplicates.
646
531
    return;
647
531
  }
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
954
void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
668
954
  if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
669
880
    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
74
      
D->hasAttr<AvailabilityAttr>()66
||
679
74
      
D->hasAttr<UnavailableAttr>()63
)
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
60
  // Try to emit a fixit with a deprecation attribute.
686
60
  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
687
57
    // Don't emit a Fix-It for non-member function definitions.  GCC does not
688
57
    // accept attributes on them.
689
57
    const DeclContext *Ctx = FD->getDeclContext();
690
57
    if ((!Ctx || !Ctx->isRecord()) &&
691
57
        
FD->doesThisDeclarationHaveABody()21
)
692
1
      return;
693
56
694
56
    const LangOptions &LO = FD->getASTContext().getLangOpts();
695
56
    const bool DoubleSquareBracket = LO.CPlusPlus14 || 
LO.C2x13
;
696
56
    StringRef AttributeSpelling =
697
56
        DoubleSquareBracket ? 
"[[deprecated]]"47
:
"__attribute__((deprecated))"9
;
698
56
    if (PP) {
699
49
      // Try to find a replacement macro:
700
49
      // - In C2x/C++14 we prefer [[deprecated]].
701
49
      // - 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.61k
void Sema::resolveParamCommandIndexes(const FullComment *FC) {
733
1.61k
  if (!isFunctionDecl()) {
734
700
    // We already warned that \\param commands are not attached to a function
735
700
    // decl.
736
700
    return;
737
700
  }
738
914
739
914
  SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
740
914
741
914
  // Comment AST nodes that correspond to \c ParamVars for which we have
742
914
  // found a \\param command or NULL if no documentation was found so far.
743
914
  SmallVector<ParamCommandComment *, 8> ParamVarDocs;
744
914
745
914
  ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
746
914
  ParamVarDocs.resize(ParamVars.size(), nullptr);
747
914
748
914
  // First pass over all \\param commands: resolve all parameter names.
749
914
  for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
750
3.07k
       I != E; 
++I2.15k
) {
751
2.15k
    ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
752
2.15k
    if (!PCC || 
!PCC->hasParamName()409
)
753
1.75k
      continue;
754
401
    StringRef ParamName = PCC->getParamNameAsWritten();
755
401
756
401
    // Check that referenced parameter name is in the function decl.
757
401
    const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
758
401
                                                                ParamVars);
759
401
    if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
760
29
      PCC->setIsVarArgParam();
761
29
      continue;
762
29
    }
763
372
    if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
764
138
      UnresolvedParamCommands.push_back(PCC);
765
138
      continue;
766
138
    }
767
234
    PCC->setParamIndex(ResolvedParamIndex);
768
234
    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
234
    ParamVarDocs[ResolvedParamIndex] = PCC;
777
234
  }
778
914
779
914
  // Find parameter declarations that have no corresponding \\param.
780
914
  SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
781
1.65k
  for (unsigned i = 0, e = ParamVarDocs.size(); i != e; 
++i745
) {
782
745
    if (!ParamVarDocs[i])
783
517
      OrphanedParamDecls.push_back(ParamVars[i]);
784
745
  }
785
914
786
914
  // Second pass over unresolved \\param commands: do typo correction.
787
914
  // Suggest corrections from a set of parameter declarations that have no
788
914
  // corresponding \\param.
789
1.05k
  for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; 
++i138
) {
790
138
    const ParamCommandComment *PCC = UnresolvedParamCommands[i];
791
138
792
138
    SourceRange ArgRange = PCC->getParamNameRange();
793
138
    StringRef ParamName = PCC->getParamNameAsWritten();
794
138
    Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
795
138
      << ParamName << ArgRange;
796
138
797
138
    // All parameters documented -- can't suggest a correction.
798
138
    if (OrphanedParamDecls.size() == 0)
799
34
      continue;
800
104
801
104
    unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
802
104
    if (OrphanedParamDecls.size() == 1) {
803
87
      // If one parameter is not documented then that parameter is the only
804
87
      // possible suggestion.
805
87
      CorrectedParamIndex = 0;
806
87
    } else {
807
17
      // Do typo correction.
808
17
      CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
809
17
                                                          OrphanedParamDecls);
810
17
    }
811
104
    if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
812
92
      const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
813
92
      if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
814
88
        Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
815
88
          << CorrectedII->getName()
816
88
          << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
817
92
    }
818
104
  }
819
914
}
820
821
2.37k
bool Sema::isFunctionDecl() {
822
2.37k
  if (!ThisDeclInfo)
823
102
    return false;
824
2.27k
  if (!ThisDeclInfo->IsFilled)
825
1.22k
    inspectThisDecl();
826
2.27k
  return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
827
2.27k
}
828
829
11
bool Sema::isAnyFunctionDecl() {
830
11
  return isFunctionDecl() && 
ThisDeclInfo->CurrentDecl8
&&
831
11
         
isa<FunctionDecl>(ThisDeclInfo->CurrentDecl)8
;
832
11
}
833
834
41
bool Sema::isFunctionOrMethodVariadic() {
835
41
  if (!isFunctionDecl() || !ThisDeclInfo->CurrentDecl)
836
0
    return false;
837
41
  if (const FunctionDecl *FD =
838
16
        dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
839
16
    return FD->isVariadic();
840
25
  if (const FunctionTemplateDecl *FTD =
841
16
        dyn_cast<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl))
842
16
    return FTD->getTemplatedDecl()->isVariadic();
843
9
  if (const ObjCMethodDecl *MD =
844
2
        dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
845
2
    return MD->isVariadic();
846
7
  if (const TypedefNameDecl *TD =
847
7
          dyn_cast<TypedefNameDecl>(ThisDeclInfo->CurrentDecl)) {
848
7
    QualType Type = TD->getUnderlyingType();
849
7
    if (Type->isFunctionPointerType() || 
Type->isBlockPointerType()1
)
850
7
      Type = Type->getPointeeType();
851
7
    if (const auto *FT = Type->getAs<FunctionProtoType>())
852
7
      return FT->isVariadic();
853
0
  }
854
0
  return false;
855
0
}
856
857
5
bool Sema::isObjCMethodDecl() {
858
5
  return isFunctionDecl() && 
ThisDeclInfo->CurrentDecl2
&&
859
5
         
isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl)2
;
860
5
}
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
138
bool Sema::isFunctionOrBlockPointerVarLikeDecl() {
877
138
  if (!ThisDeclInfo)
878
23
    return false;
879
115
  if (!ThisDeclInfo->IsFilled)
880
0
    inspectThisDecl();
881
115
  if (ThisDeclInfo->getKind() != DeclInfo::VariableKind ||
882
115
      
!ThisDeclInfo->CurrentDecl79
)
883
36
    return false;
884
79
  QualType QT;
885
79
  if (const auto *VD = dyn_cast<DeclaratorDecl>(ThisDeclInfo->CurrentDecl))
886
74
    QT = VD->getType();
887
5
  else if (const auto *PD =
888
2
               dyn_cast<ObjCPropertyDecl>(ThisDeclInfo->CurrentDecl))
889
2
    QT = PD->getType();
890
3
  else
891
3
    return false;
892
76
  // We would like to warn about the 'returns'/'param' commands for
893
76
  // variables that don't directly specify the function type, so type aliases
894
76
  // can be ignored.
895
76
  if (QT->getAs<TypedefType>())
896
12
    return false;
897
64
  if (const auto *P = QT->getAs<PointerType>())
898
34
    if (P->getPointeeType()->getAs<TypedefType>())
899
13
      return false;
900
51
  if (const auto *P = QT->getAs<BlockPointerType>())
901
6
    if (P->getPointeeType()->getAs<TypedefType>())
902
1
      return false;
903
50
  return QT->isFunctionPointerType() || 
QT->isBlockPointerType()31
;
904
50
}
905
906
238
bool Sema::isObjCPropertyDecl() {
907
238
  if (!ThisDeclInfo)
908
0
    return false;
909
238
  if (!ThisDeclInfo->IsFilled)
910
143
    inspectThisDecl();
911
238
  return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
912
238
}
913
914
390
bool Sema::isTemplateOrSpecialization() {
915
390
  if (!ThisDeclInfo)
916
9
    return false;
917
381
  if (!ThisDeclInfo->IsFilled)
918
129
    inspectThisDecl();
919
381
  return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
920
381
}
921
922
2
bool Sema::isRecordLikeDecl() {
923
2
  if (!ThisDeclInfo)
924
0
    return false;
925
2
  if (!ThisDeclInfo->IsFilled)
926
0
    inspectThisDecl();
927
2
  return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
928
2
         isObjCProtocolDecl();
929
2
}
930
931
8
bool Sema::isUnionDecl() {
932
8
  if (!ThisDeclInfo)
933
0
    return false;
934
8
  if (!ThisDeclInfo->IsFilled)
935
6
    inspectThisDecl();
936
8
  if (const RecordDecl *RD =
937
6
        dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
938
6
    return RD->isUnion();
939
2
  return false;
940
2
}
941
31
static bool isClassOrStructDeclImpl(const Decl *D) {
942
31
  if (auto *record = dyn_cast_or_null<RecordDecl>(D))
943
20
    return !record->isUnion();
944
11
945
11
  return false;
946
11
}
947
948
2
bool Sema::isClassOrStructDecl() {
949
2
  if (!ThisDeclInfo)
950
0
    return false;
951
2
  if (!ThisDeclInfo->IsFilled)
952
0
    inspectThisDecl();
953
2
954
2
  if (!ThisDeclInfo->CurrentDecl)
955
0
    return false;
956
2
957
2
  return isClassOrStructDeclImpl(ThisDeclInfo->CurrentDecl);
958
2
}
959
960
27
bool Sema::isClassOrStructOrTagTypedefDecl() {
961
27
  if (!ThisDeclInfo)
962
0
    return false;
963
27
  if (!ThisDeclInfo->IsFilled)
964
25
    inspectThisDecl();
965
27
966
27
  if (!ThisDeclInfo->CurrentDecl)
967
0
    return false;
968
27
969
27
  if (isClassOrStructDeclImpl(ThisDeclInfo->CurrentDecl))
970
15
    return true;
971
12
972
12
  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
10
      }
981
2
    }
982
2
  }
983
10
984
10
  return false;
985
10
}
986
987
6
bool Sema::isClassTemplateDecl() {
988
6
  if (!ThisDeclInfo)
989
0
    return false;
990
6
  if (!ThisDeclInfo->IsFilled)
991
0
    inspectThisDecl();
992
6
  return ThisDeclInfo->CurrentDecl &&
993
6
          (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
994
6
}
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
11
bool Sema::isObjCInterfaceDecl() {
1006
11
  if (!ThisDeclInfo)
1007
0
    return false;
1008
11
  if (!ThisDeclInfo->IsFilled)
1009
4
    inspectThisDecl();
1010
11
  return ThisDeclInfo->CurrentDecl &&
1011
11
         isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
1012
11
}
1013
1014
5
bool Sema::isObjCProtocolDecl() {
1015
5
  if (!ThisDeclInfo)
1016
0
    return false;
1017
5
  if (!ThisDeclInfo->IsFilled)
1018
3
    inspectThisDecl();
1019
5
  return ThisDeclInfo->CurrentDecl &&
1020
5
         isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
1021
5
}
1022
1023
914
ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
1024
914
  if (!ThisDeclInfo->IsFilled)
1025
0
    inspectThisDecl();
1026
914
  return ThisDeclInfo->ParamVars;
1027
914
}
1028
1029
1.53k
void Sema::inspectThisDecl() {
1030
1.53k
  ThisDeclInfo->fill();
1031
1.53k
}
1032
1033
unsigned Sema::resolveParmVarReference(StringRef Name,
1034
401
                                       ArrayRef<const ParmVarDecl *> ParamVars) {
1035
675
  for (unsigned i = 0, e = ParamVars.size(); i != e; 
++i274
) {
1036
508
    const IdentifierInfo *II = ParamVars[i]->getIdentifier();
1037
508
    if (II && 
II->getName() == Name504
)
1038
234
      return i;
1039
508
  }
1040
401
  
if (167
Name == "..."167
&&
isFunctionOrMethodVariadic()41
)
1041
29
    return ParamCommandComment::VarArgParamIndex;
1042
138
  return ParamCommandComment::InvalidParamIndex;
1043
138
}
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
59
      
Typo.size() / MinPossibleEditDistance < 320
)
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
195
                            SmallVectorImpl<unsigned> *Position) {
1115
282
  for (unsigned i = 0, e = TemplateParameters->size(); i != e; 
++i87
) {
1116
233
    const NamedDecl *Param = TemplateParameters->getParam(i);
1117
233
    const IdentifierInfo *II = Param->getIdentifier();
1118
233
    if (II && II->getName() == Name) {
1119
116
      Position->push_back(i);
1120
116
      return true;
1121
116
    }
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
195
  
return false49
;
1133
195
}
1134
} // end anonymous namespace
1135
1136
bool Sema::resolveTParamReference(
1137
                            StringRef Name,
1138
                            const TemplateParameterList *TemplateParameters,
1139
165
                            SmallVectorImpl<unsigned> *Position) {
1140
165
  Position->clear();
1141
165
  if (!TemplateParameters)
1142
3
    return false;
1143
162
1144
162
  return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1145
162
}
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
77
Sema::getInlineCommandRenderKind(StringRef Name) const {
1178
77
  assert(Traits.getCommandInfo(Name)->IsInlineCommand);
1179
77
1180
77
  return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1181
77
      .Case("b", InlineCommandComment::RenderBold)
1182
77
      .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1183
77
      .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1184
77
      .Case("anchor", InlineCommandComment::RenderAnchor)
1185
77
      .Default(InlineCommandComment::RenderNormal);
1186
77
}
1187
1188
} // end namespace comments
1189
} // end namespace clang