[APFloat] Fixed a bug in method 'fusedMultiplyAdd'.
[oota-llvm.git] / 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 SpecialCaseList *SpecialCaseList::create(StringRef Path, std::string &Error) {
52   if (Path.empty())
53     return new SpecialCaseList();
54   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
55       MemoryBuffer::getFile(Path);
56   if (std::error_code EC = FileOrErr.getError()) {
57     Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
58     return nullptr;
59   }
60   return create(FileOrErr.get().get(), Error);
61 }
62
63 SpecialCaseList *SpecialCaseList::create(
64     const MemoryBuffer *MB, std::string &Error) {
65   std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
66   if (!SCL->parse(MB, Error))
67     return nullptr;
68   return SCL.release();
69 }
70
71 SpecialCaseList *SpecialCaseList::createOrDie(StringRef Path) {
72   std::string Error;
73   if (SpecialCaseList *SCL = create(Path, Error))
74     return SCL;
75   report_fatal_error(Error);
76 }
77
78 bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
79   // Iterate through each line in the blacklist file.
80   SmallVector<StringRef, 16> Lines;
81   SplitString(MB->getBuffer(), Lines, "\n\r");
82   StringMap<StringMap<std::string> > Regexps;
83   assert(Entries.empty() &&
84          "parse() should be called on an empty SpecialCaseList");
85   int LineNo = 1;
86   for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
87        I != E; ++I, ++LineNo) {
88     // Ignore empty lines and lines starting with "#"
89     if (I->empty() || I->startswith("#"))
90       continue;
91     // Get our prefix and unparsed regexp.
92     std::pair<StringRef, StringRef> SplitLine = I->split(":");
93     StringRef Prefix = SplitLine.first;
94     if (SplitLine.second.empty()) {
95       // Missing ':' in the line.
96       Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
97                SplitLine.first + "'").str();
98       return false;
99     }
100
101     std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
102     std::string Regexp = SplitRegexp.first;
103     StringRef Category = SplitRegexp.second;
104
105     // Backwards compatibility.
106     if (Prefix == "global-init") {
107       Prefix = "global";
108       Category = "init";
109     } else if (Prefix == "global-init-type") {
110       Prefix = "type";
111       Category = "init";
112     } else if (Prefix == "global-init-src") {
113       Prefix = "src";
114       Category = "init";
115     }
116
117     // See if we can store Regexp in Strings.
118     if (Regex::isLiteralERE(Regexp)) {
119       Entries[Prefix][Category].Strings.insert(Regexp);
120       continue;
121     }
122
123     // Replace * with .*
124     for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
125          pos += strlen(".*")) {
126       Regexp.replace(pos, strlen("*"), ".*");
127     }
128
129     // Check that the regexp is valid.
130     Regex CheckRE(Regexp);
131     std::string REError;
132     if (!CheckRE.isValid(REError)) {
133       Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
134                SplitLine.second + "': " + REError).str();
135       return false;
136     }
137
138     // Add this regexp into the proper group by its prefix.
139     if (!Regexps[Prefix][Category].empty())
140       Regexps[Prefix][Category] += "|";
141     Regexps[Prefix][Category] += "^" + Regexp + "$";
142   }
143
144   // Iterate through each of the prefixes, and create Regexs for them.
145   for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
146                                                           E = Regexps.end();
147        I != E; ++I) {
148     for (StringMap<std::string>::const_iterator II = I->second.begin(),
149                                                 IE = I->second.end();
150          II != IE; ++II) {
151       Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
152     }
153   }
154   return true;
155 }
156
157 SpecialCaseList::~SpecialCaseList() {}
158
159 bool SpecialCaseList::inSection(StringRef Section, StringRef Query,
160                                 StringRef Category) const {
161   StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
162   if (I == Entries.end()) return false;
163   StringMap<Entry>::const_iterator II = I->second.find(Category);
164   if (II == I->second.end()) return false;
165
166   return II->getValue().match(Query);
167 }
168
169 }  // namespace llvm