Coverage Report

Created: 2020-02-25 14:32

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