]> git.proxmox.com Git - rustc.git/blob - src/llvm/lib/Support/SpecialCaseList.cpp
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / lib / Support / SpecialCaseList.cpp
1 //===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is a utility class for instrumentation passes (like AddressSanitizer
11 // or ThreadSanitizer) to avoid instrumenting some functions or global
12 // variables, or to instrument some functions or global variables in a specific
13 // way, based on a user-supplied list.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Support/SpecialCaseList.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/Regex.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <string>
26 #include <system_error>
27 #include <utility>
28
29 namespace llvm {
30
31 /// Represents a set of regular expressions. Regular expressions which are
32 /// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
33 /// others are represented as a single pipe-separated regex in RegEx. The
34 /// reason for doing so is efficiency; StringSet is much faster at matching
35 /// literal strings than Regex.
36 struct SpecialCaseList::Entry {
37 Entry() {}
38 Entry(Entry &&Other)
39 : Strings(std::move(Other.Strings)), RegEx(std::move(Other.RegEx)) {}
40
41 StringSet<> Strings;
42 std::unique_ptr<Regex> RegEx;
43
44 bool match(StringRef Query) const {
45 return Strings.count(Query) || (RegEx && RegEx->match(Query));
46 }
47 };
48
49 SpecialCaseList::SpecialCaseList() : Entries() {}
50
51 std::unique_ptr<SpecialCaseList> SpecialCaseList::create(StringRef Path,
52 std::string &Error) {
53 if (Path.empty())
54 return std::unique_ptr<SpecialCaseList>(new SpecialCaseList());
55 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
56 MemoryBuffer::getFile(Path);
57 if (std::error_code EC = FileOrErr.getError()) {
58 Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
59 return nullptr;
60 }
61 return create(FileOrErr.get().get(), Error);
62 }
63
64 std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
65 std::string &Error) {
66 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
67 if (!SCL->parse(MB, Error))
68 return nullptr;
69 return SCL;
70 }
71
72 std::unique_ptr<SpecialCaseList> SpecialCaseList::createOrDie(StringRef Path) {
73 std::string Error;
74 if (auto SCL = create(Path, Error))
75 return SCL;
76 report_fatal_error(Error);
77 }
78
79 bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
80 // Iterate through each line in the blacklist file.
81 SmallVector<StringRef, 16> Lines;
82 SplitString(MB->getBuffer(), Lines, "\n\r");
83 StringMap<StringMap<std::string> > Regexps;
84 assert(Entries.empty() &&
85 "parse() should be called on an empty SpecialCaseList");
86 int LineNo = 1;
87 for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
88 I != E; ++I, ++LineNo) {
89 // Ignore empty lines and lines starting with "#"
90 if (I->empty() || I->startswith("#"))
91 continue;
92 // Get our prefix and unparsed regexp.
93 std::pair<StringRef, StringRef> SplitLine = I->split(":");
94 StringRef Prefix = SplitLine.first;
95 if (SplitLine.second.empty()) {
96 // Missing ':' in the line.
97 Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
98 SplitLine.first + "'").str();
99 return false;
100 }
101
102 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
103 std::string Regexp = SplitRegexp.first;
104 StringRef Category = SplitRegexp.second;
105
106 // See if we can store Regexp in Strings.
107 if (Regex::isLiteralERE(Regexp)) {
108 Entries[Prefix][Category].Strings.insert(Regexp);
109 continue;
110 }
111
112 // Replace * with .*
113 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
114 pos += strlen(".*")) {
115 Regexp.replace(pos, strlen("*"), ".*");
116 }
117
118 // Check that the regexp is valid.
119 Regex CheckRE(Regexp);
120 std::string REError;
121 if (!CheckRE.isValid(REError)) {
122 Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
123 SplitLine.second + "': " + REError).str();
124 return false;
125 }
126
127 // Add this regexp into the proper group by its prefix.
128 if (!Regexps[Prefix][Category].empty())
129 Regexps[Prefix][Category] += "|";
130 Regexps[Prefix][Category] += "^" + Regexp + "$";
131 }
132
133 // Iterate through each of the prefixes, and create Regexs for them.
134 for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
135 E = Regexps.end();
136 I != E; ++I) {
137 for (StringMap<std::string>::const_iterator II = I->second.begin(),
138 IE = I->second.end();
139 II != IE; ++II) {
140 Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
141 }
142 }
143 return true;
144 }
145
146 SpecialCaseList::~SpecialCaseList() {}
147
148 bool SpecialCaseList::inSection(StringRef Section, StringRef Query,
149 StringRef Category) const {
150 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
151 if (I == Entries.end()) return false;
152 StringMap<Entry>::const_iterator II = I->second.find(Category);
153 if (II == I->second.end()) return false;
154
155 return II->getValue().match(Query);
156 }
157
158 } // namespace llvm