Coverage Report

Created: 2020-11-24 06:42

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/CommentCommandTraits.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CommentCommandTraits.cpp - Comment command properties --*- C++ -*-===//
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/CommentCommandTraits.h"
10
#include "llvm/ADT/STLExtras.h"
11
#include <cassert>
12
13
namespace clang {
14
namespace comments {
15
16
#include "clang/AST/CommentCommandInfo.inc"
17
18
CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator,
19
                             const CommentOptions &CommentOptions) :
20
86.0k
    NextID(llvm::array_lengthof(Commands)), Allocator(Allocator) {
21
86.0k
  registerCommentOptions(CommentOptions);
22
86.0k
}
23
24
void CommandTraits::registerCommentOptions(
25
86.2k
    const CommentOptions &CommentOptions) {
26
86.2k
  for (CommentOptions::BlockCommandNamesTy::const_iterator
27
86.2k
           I = CommentOptions.BlockCommandNames.begin(),
28
86.2k
           E = CommentOptions.BlockCommandNames.end();
29
86.2k
       I != E; 
I++13
) {
30
13
    registerBlockCommand(*I);
31
13
  }
32
86.2k
}
33
34
3.18k
const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
35
3.18k
  if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
36
2.86k
    return Info;
37
326
  return getRegisteredCommandInfo(Name);
38
326
}
39
40
15.1k
const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
41
15.1k
  if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
42
14.9k
    return Info;
43
198
  return getRegisteredCommandInfo(CommandID);
44
198
}
45
46
const CommandInfo *
47
312
CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
48
  // Single-character command impostures, such as \t or \n, should not go
49
  // through the fixit logic.
50
312
  if (Typo.size() <= 1)
51
56
    return nullptr;
52
53
  // The maximum edit distance we're prepared to accept.
54
256
  const unsigned MaxEditDistance = 1;
55
56
256
  unsigned BestEditDistance = MaxEditDistance;
57
256
  SmallVector<const CommandInfo *, 2> BestCommand;
58
59
31.2k
  auto ConsiderCorrection = [&](const CommandInfo *Command) {
60
31.2k
    StringRef Name = Command->Name;
61
62
31.2k
    unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
63
31.2k
    if (MinPossibleEditDistance <= BestEditDistance) {
64
6.71k
      unsigned EditDistance = Typo.edit_distance(Name, true, BestEditDistance);
65
6.71k
      if (EditDistance < BestEditDistance) {
66
0
        BestEditDistance = EditDistance;
67
0
        BestCommand.clear();
68
0
      }
69
6.71k
      if (EditDistance == BestEditDistance)
70
235
        BestCommand.push_back(Command);
71
6.71k
    }
72
31.2k
  };
73
74
256
  for (const auto &Command : Commands)
75
31.2k
    ConsiderCorrection(&Command);
76
77
256
  for (const auto *Command : RegisteredCommands)
78
8.30k
    if (!Command->IsUnknownCommand)
79
6
      ConsiderCorrection(Command);
80
81
245
  return BestCommand.size() == 1 ? 
BestCommand[0]11
: nullptr;
82
256
}
83
84
170
CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
85
170
  char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
86
170
  memcpy(Name, CommandName.data(), CommandName.size());
87
170
  Name[CommandName.size()] = '\0';
88
89
  // Value-initialize (=zero-initialize in this case) a new CommandInfo.
90
170
  CommandInfo *Info = new (Allocator) CommandInfo();
91
170
  Info->Name = Name;
92
  // We only have a limited number of bits to encode command IDs in the
93
  // CommandInfo structure, so the ID numbers can potentially wrap around.
94
170
  assert((NextID < (1 << CommandInfo::NumCommandIDBits))
95
170
         && "Too many commands. We have limited bits for the command ID.");
96
170
  Info->ID = NextID++;
97
98
170
  RegisteredCommands.push_back(Info);
99
100
170
  return Info;
101
170
}
102
103
const CommandInfo *CommandTraits::registerUnknownCommand(
104
153
                                                  StringRef CommandName) {
105
153
  CommandInfo *Info = createCommandInfoWithName(CommandName);
106
153
  Info->IsUnknownCommand = true;
107
153
  return Info;
108
153
}
109
110
17
const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
111
17
  CommandInfo *Info = createCommandInfoWithName(CommandName);
112
17
  Info->IsBlockCommand = true;
113
17
  return Info;
114
17
}
115
116
const CommandInfo *CommandTraits::getBuiltinCommandInfo(
117
15.1k
                                                  unsigned CommandID) {
118
15.1k
  if (CommandID < llvm::array_lengthof(Commands))
119
14.9k
    return &Commands[CommandID];
120
199
  return nullptr;
121
199
}
122
123
const CommandInfo *CommandTraits::getRegisteredCommandInfo(
124
326
                                                  StringRef Name) const {
125
10.0k
  for (unsigned i = 0, e = RegisteredCommands.size(); i != e; 
++i9.74k
) {
126
9.76k
    if (RegisteredCommands[i]->Name == Name)
127
14
      return RegisteredCommands[i];
128
9.76k
  }
129
312
  return nullptr;
130
326
}
131
132
const CommandInfo *CommandTraits::getRegisteredCommandInfo(
133
198
                                                  unsigned CommandID) const {
134
198
  return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
135
198
}
136
137
} // end namespace comments
138
} // end namespace clang
139