Coverage Report

Created: 2019-07-24 05:18

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