27
27
namespace llvm {
28
28
29
29
bool SpecialCaseList::Matcher::insert (std::string Regexp,
30
+ unsigned LineNumber,
30
31
std::string &REError) {
31
32
if (Regexp.empty ()) {
32
33
REError = " Supplied regexp was blank" ;
33
34
return false ;
34
35
}
35
36
36
37
if (Regex::isLiteralERE (Regexp)) {
37
- Strings. insert ( Regexp) ;
38
+ Strings[ Regexp] = LineNumber ;
38
39
return true ;
39
40
}
40
41
Trigrams.insert (Regexp);
@@ -45,34 +46,30 @@ bool SpecialCaseList::Matcher::insert(std::string Regexp,
45
46
Regexp.replace (pos, strlen (" *" ), " .*" );
46
47
}
47
48
49
+ Regexp = (Twine (" ^(" ) + StringRef (Regexp) + " )$" ).str ();
50
+
48
51
// Check that the regexp is valid.
49
52
Regex CheckRE (Regexp);
50
53
if (!CheckRE.isValid (REError))
51
54
return false ;
52
55
53
- if (!UncompiledRegEx.empty ())
54
- UncompiledRegEx += " |" ;
55
- UncompiledRegEx += " ^(" + Regexp + " )$" ;
56
+ RegExes.emplace_back (
57
+ std::make_pair (make_unique<Regex>(std::move (CheckRE)), LineNumber));
56
58
return true ;
57
59
}
58
60
59
- void SpecialCaseList::Matcher::compile () {
60
- if (!UncompiledRegEx.empty ()) {
61
- RegEx.reset (new Regex (UncompiledRegEx));
62
- UncompiledRegEx.clear ();
63
- }
64
- }
65
-
66
- bool SpecialCaseList::Matcher::match (StringRef Query) const {
67
- if (Strings.count (Query))
68
- return true ;
61
+ unsigned SpecialCaseList::Matcher::match (StringRef Query) const {
62
+ auto It = Strings.find (Query);
63
+ if (It != Strings.end ())
64
+ return It->second ;
69
65
if (Trigrams.isDefinitelyOut (Query))
70
66
return false ;
71
- return RegEx && RegEx->match (Query);
67
+ for (auto & RegExKV : RegExes)
68
+ if (RegExKV.first ->match (Query))
69
+ return RegExKV.second ;
70
+ return 0 ;
72
71
}
73
72
74
- SpecialCaseList::SpecialCaseList () : Sections(), IsCompiled(false ) {}
75
-
76
73
std::unique_ptr<SpecialCaseList>
77
74
SpecialCaseList::create (const std::vector<std::string> &Paths,
78
75
std::string &Error) {
@@ -114,7 +111,6 @@ bool SpecialCaseList::createInternal(const std::vector<std::string> &Paths,
114
111
return false ;
115
112
}
116
113
}
117
- compile ();
118
114
return true ;
119
115
}
120
116
@@ -123,7 +119,6 @@ bool SpecialCaseList::createInternal(const MemoryBuffer *MB,
123
119
StringMap<size_t > Sections;
124
120
if (!parse (MB, Sections, Error))
125
121
return false ;
126
- compile ();
127
122
return true ;
128
123
}
129
124
@@ -132,11 +127,13 @@ bool SpecialCaseList::parse(const MemoryBuffer *MB,
132
127
std::string &Error) {
133
128
// Iterate through each line in the blacklist file.
134
129
SmallVector<StringRef, 16 > Lines;
135
- SplitString ( MB->getBuffer (), Lines, " \n\r " );
130
+ MB->getBuffer (). split ( Lines, ' \n ' );
136
131
137
- int LineNo = 1 ;
132
+ unsigned LineNo = 1 ;
138
133
StringRef Section = " *" ;
134
+
139
135
for (auto I = Lines.begin (), E = Lines.end (); I != E; ++I, ++LineNo) {
136
+ *I = I->trim ();
140
137
// Ignore empty lines and lines starting with "#"
141
138
if (I->empty () || I->startswith (" #" ))
142
139
continue ;
@@ -181,19 +178,18 @@ bool SpecialCaseList::parse(const MemoryBuffer *MB,
181
178
if (SectionsMap.find (Section) == SectionsMap.end ()) {
182
179
std::unique_ptr<Matcher> M = make_unique<Matcher>();
183
180
std::string REError;
184
- if (!M->insert (Section, REError)) {
181
+ if (!M->insert (Section, LineNo, REError)) {
185
182
Error = (Twine (" malformed section " ) + Section + " : '" + REError).str ();
186
183
return false ;
187
184
}
188
- M->compile ();
189
185
190
186
SectionsMap[Section] = Sections.size ();
191
187
Sections.emplace_back (std::move (M));
192
188
}
193
189
194
190
auto &Entry = Sections[SectionsMap[Section]].Entries [Prefix][Category];
195
191
std::string REError;
196
- if (!Entry.insert (std::move (Regexp), REError)) {
192
+ if (!Entry.insert (std::move (Regexp), LineNo, REError)) {
197
193
Error = (Twine (" malformed regex in line " ) + Twine (LineNo) + " : '" +
198
194
SplitLine.second + " ': " + REError).str ();
199
195
return false ;
@@ -202,38 +198,33 @@ bool SpecialCaseList::parse(const MemoryBuffer *MB,
202
198
return true ;
203
199
}
204
200
205
- void SpecialCaseList::compile () {
206
- assert (!IsCompiled && " compile() should only be called once" );
207
- // Iterate through every section compiling regular expressions for every query
208
- // and creating Section entries.
209
- for (auto &Section : Sections)
210
- for (auto &Prefix : Section.Entries )
211
- for (auto &Category : Prefix.getValue ())
212
- Category.getValue ().compile ();
213
-
214
- IsCompiled = true ;
215
- }
216
-
217
201
SpecialCaseList::~SpecialCaseList () {}
218
202
219
203
bool SpecialCaseList::inSection (StringRef Section, StringRef Prefix,
220
204
StringRef Query, StringRef Category) const {
221
- assert (IsCompiled && " SpecialCaseList::compile() was not called!" );
205
+ return inSectionBlame (Section, Prefix, Query, Category);
206
+ }
222
207
208
+ unsigned SpecialCaseList::inSectionBlame (StringRef Section, StringRef Prefix,
209
+ StringRef Query,
210
+ StringRef Category) const {
223
211
for (auto &SectionIter : Sections)
224
- if (SectionIter.SectionMatcher ->match (Section) &&
225
- inSection (SectionIter.Entries , Prefix, Query, Category))
226
- return true ;
227
-
228
- return false ;
212
+ if (SectionIter.SectionMatcher ->match (Section)) {
213
+ unsigned Blame =
214
+ inSectionBlame (SectionIter.Entries , Prefix, Query, Category);
215
+ if (Blame)
216
+ return Blame;
217
+ }
218
+ return 0 ;
229
219
}
230
220
231
- bool SpecialCaseList::inSection (const SectionEntries &Entries, StringRef Prefix,
232
- StringRef Query, StringRef Category) const {
221
+ unsigned SpecialCaseList::inSectionBlame (const SectionEntries &Entries,
222
+ StringRef Prefix, StringRef Query,
223
+ StringRef Category) const {
233
224
SectionEntries::const_iterator I = Entries.find (Prefix);
234
- if (I == Entries.end ()) return false ;
225
+ if (I == Entries.end ()) return 0 ;
235
226
StringMap<Matcher>::const_iterator II = I->second .find (Category);
236
- if (II == I->second .end ()) return false ;
227
+ if (II == I->second .end ()) return 0 ;
237
228
238
229
return II->getValue ().match (Query);
239
230
}
0 commit comments