Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/SpecialCaseList.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
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
// This is a utility class for instrumentation passes (like AddressSanitizer
10
// or ThreadSanitizer) to avoid instrumenting some functions or global
11
// variables, or to instrument some functions or global variables in a specific
12
// way, based on a user-supplied list.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "llvm/Support/SpecialCaseList.h"
17
#include "llvm/ADT/SmallVector.h"
18
#include "llvm/ADT/StringExtras.h"
19
#include "llvm/Support/MemoryBuffer.h"
20
#include "llvm/Support/Regex.h"
21
#include <string>
22
#include <system_error>
23
#include <utility>
24
25
#include <stdio.h>
26
namespace llvm {
27
28
bool SpecialCaseList::Matcher::insert(std::string Regexp,
29
                                      unsigned LineNumber,
30
798
                                      std::string &REError) {
31
798
  if (Regexp.empty()) {
32
1
    REError = "Supplied regexp was blank";
33
1
    return false;
34
1
  }
35
797
36
797
  if (Regex::isLiteralERE(Regexp)) {
37
111
    Strings[Regexp] = LineNumber;
38
111
    return true;
39
111
  }
40
686
  Trigrams.insert(Regexp);
41
686
42
686
  // Replace * with .*
43
1.81k
  for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos;
44
1.12k
       pos += strlen(".*")) {
45
1.12k
    Regexp.replace(pos, strlen("*"), ".*");
46
1.12k
  }
47
686
48
686
  Regexp = (Twine("^(") + StringRef(Regexp) + ")$").str();
49
686
50
686
  // Check that the regexp is valid.
51
686
  Regex CheckRE(Regexp);
52
686
  if (!CheckRE.isValid(REError))
53
2
    return false;
54
684
55
684
  RegExes.emplace_back(
56
684
      std::make_pair(make_unique<Regex>(std::move(CheckRE)), LineNumber));
57
684
  return true;
58
684
}
59
60
3.11k
unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
61
3.11k
  auto It = Strings.find(Query);
62
3.11k
  if (It != Strings.end())
63
72
    return It->second;
64
3.04k
  if (Trigrams.isDefinitelyOut(Query))
65
811
    return false;
66
2.23k
  for (auto& RegExKV : RegExes)
67
2.29k
    if (RegExKV.first->match(Query))
68
2.02k
      return RegExKV.second;
69
2.23k
  
return 0209
;
70
2.23k
}
71
72
std::unique_ptr<SpecialCaseList>
73
SpecialCaseList::create(const std::vector<std::string> &Paths,
74
156k
                        std::string &Error) {
75
156k
  std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
76
156k
  if (SCL->createInternal(Paths, Error))
77
156k
    return SCL;
78
1
  return nullptr;
79
1
}
80
81
std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
82
22
                                                         std::string &Error) {
83
22
  std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
84
22
  if (SCL->createInternal(MB, Error))
85
14
    return SCL;
86
8
  return nullptr;
87
8
}
88
89
std::unique_ptr<SpecialCaseList>
90
125k
SpecialCaseList::createOrDie(const std::vector<std::string> &Paths) {
91
125k
  std::string Error;
92
125k
  if (auto SCL = create(Paths, Error))
93
125k
    return SCL;
94
0
  report_fatal_error(Error);
95
0
}
96
97
bool SpecialCaseList::createInternal(const std::vector<std::string> &Paths,
98
197k
                                     std::string &Error) {
99
197k
  StringMap<size_t> Sections;
100
197k
  for (const auto &Path : Paths) {
101
269
    ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
102
269
        MemoryBuffer::getFile(Path);
103
269
    if (std::error_code EC = FileOrErr.getError()) {
104
1
      Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
105
1
      return false;
106
1
    }
107
268
    std::string ParseError;
108
268
    if (!parse(FileOrErr.get().get(), Sections, ParseError)) {
109
1
      Error = (Twine("error parsing file '") + Path + "': " + ParseError).str();
110
1
      return false;
111
1
    }
112
268
  }
113
197k
  
return true197k
;
114
197k
}
115
116
bool SpecialCaseList::createInternal(const MemoryBuffer *MB,
117
22
                                     std::string &Error) {
118
22
  StringMap<size_t> Sections;
119
22
  if (!parse(MB, Sections, Error))
120
8
    return false;
121
14
  return true;
122
14
}
123
124
bool SpecialCaseList::parse(const MemoryBuffer *MB,
125
                            StringMap<size_t> &SectionsMap,
126
290
                            std::string &Error) {
127
290
  // Iterate through each line in the blacklist file.
128
290
  SmallVector<StringRef, 16> Lines;
129
290
  MB->getBuffer().split(Lines, '\n');
130
290
131
290
  unsigned LineNo = 1;
132
290
  StringRef Section = "*";
133
290
134
3.30k
  for (auto I = Lines.begin(), E = Lines.end(); I != E; 
++I, ++LineNo3.01k
) {
135
3.02k
    *I = I->trim();
136
3.02k
    // Ignore empty lines and lines starting with "#"
137
3.02k
    if (I->empty() || 
I->startswith("#")2.30k
)
138
2.41k
      continue;
139
608
140
608
    // Save section names
141
608
    if (I->startswith("[")) {
142
64
      if (!I->endswith("]")) {
143
3
        Error = (Twine("malformed section header on line ") + Twine(LineNo) +
144
3
                 ": " + *I).str();
145
3
        return false;
146
3
      }
147
61
148
61
      Section = I->slice(1, I->size() - 1);
149
61
150
61
      std::string REError;
151
61
      Regex CheckRE(Section);
152
61
      if (!CheckRE.isValid(REError)) {
153
1
        Error =
154
1
            (Twine("malformed regex for section ") + Section + ": '" + REError)
155
1
                .str();
156
1
        return false;
157
1
      }
158
60
159
60
      continue;
160
60
    }
161
544
162
544
    // Get our prefix and unparsed regexp.
163
544
    std::pair<StringRef, StringRef> SplitLine = I->split(":");
164
544
    StringRef Prefix = SplitLine.first;
165
544
    if (SplitLine.second.empty()) {
166
2
      // Missing ':' in the line.
167
2
      Error = (Twine("malformed line ") + Twine(LineNo) + ": '" +
168
2
               SplitLine.first + "'").str();
169
2
      return false;
170
2
    }
171
542
172
542
    std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
173
542
    std::string Regexp = SplitRegexp.first;
174
542
    StringRef Category = SplitRegexp.second;
175
542
176
542
    // Create this section if it has not been seen before.
177
542
    if (SectionsMap.find(Section) == SectionsMap.end()) {
178
256
      std::unique_ptr<Matcher> M = make_unique<Matcher>();
179
256
      std::string REError;
180
256
      if (!M->insert(Section, LineNo, REError)) {
181
0
        Error = (Twine("malformed section ") + Section + ": '" + REError).str();
182
0
        return false;
183
0
      }
184
256
185
256
      SectionsMap[Section] = Sections.size();
186
256
      Sections.emplace_back(std::move(M));
187
256
    }
188
542
189
542
    auto &Entry = Sections[SectionsMap[Section]].Entries[Prefix][Category];
190
542
    std::string REError;
191
542
    if (!Entry.insert(std::move(Regexp), LineNo, REError)) {
192
3
      Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" +
193
3
               SplitLine.second + "': " + REError).str();
194
3
      return false;
195
3
    }
196
542
  }
197
290
  
return true281
;
198
290
}
199
200
163k
SpecialCaseList::~SpecialCaseList() {}
201
202
bool SpecialCaseList::inSection(StringRef Section, StringRef Prefix,
203
615
                                StringRef Query, StringRef Category) const {
204
615
  return inSectionBlame(Section, Prefix, Query, Category);
205
615
}
206
207
unsigned SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
208
                                         StringRef Query,
209
626
                                         StringRef Category) const {
210
626
  for (auto &SectionIter : Sections)
211
449
    if (SectionIter.SectionMatcher->match(Section)) {
212
425
      unsigned Blame =
213
425
          inSectionBlame(SectionIter.Entries, Prefix, Query, Category);
214
425
      if (Blame)
215
112
        return Blame;
216
425
    }
217
626
  
return 0514
;
218
626
}
219
220
unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries,
221
                                         StringRef Prefix, StringRef Query,
222
1.65k
                                         StringRef Category) const {
223
1.65k
  SectionEntries::const_iterator I = Entries.find(Prefix);
224
1.65k
  if (I == Entries.end()) 
return 0960
;
225
695
  StringMap<Matcher>::const_iterator II = I->second.find(Category);
226
695
  if (II == I->second.end()) 
return 0206
;
227
489
228
489
  return II->getValue().match(Query);
229
489
}
230
231
}  // namespace llvm