OSDN Git Service

add and document regex support for FileCheck. You can now do stuff like:
[android-x86/external-llvm.git] / lib / Support / Regex.cpp
1 //===-- Regex.cpp - Regular Expression matcher implementation -------------===//
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 file implements a POSIX regular expression matcher.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Regex.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "regex_impl.h"
19 #include <string>
20 using namespace llvm;
21
22 Regex::Regex(const StringRef &regex, unsigned Flags) {
23   unsigned flags = 0;
24   preg = new llvm_regex();
25   preg->re_endp = regex.end();
26   if (Flags & IgnoreCase) 
27     flags |= REG_ICASE;
28   if (Flags & NoSub) {
29     flags |= REG_NOSUB;
30     sub = false;
31   } else {
32     sub = true;
33   }
34   if (Flags & Newline)
35     flags |= REG_NEWLINE;
36   error = llvm_regcomp(preg, regex.data(), flags|REG_EXTENDED|REG_PEND);
37 }
38
39 bool Regex::isValid(std::string &Error) {
40   if (!error)
41     return true;
42
43   size_t len = llvm_regerror(error, preg, NULL, 0);
44   
45   Error.resize(len);
46   llvm_regerror(error, preg, &Error[0], len);
47   return false;
48 }
49
50 Regex::~Regex() {
51   llvm_regfree(preg);
52   delete preg;
53 }
54
55 bool Regex::match(const StringRef &String, SmallVectorImpl<StringRef> *Matches){
56   unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
57
58   if (Matches) {
59     assert(sub && "Substring matching requested but pattern compiled without");
60     Matches->clear();
61   }
62
63   // pmatch needs to have at least one element.
64   SmallVector<llvm_regmatch_t, 8> pm;
65   pm.resize(nmatch > 0 ? nmatch : 1);
66   pm[0].rm_so = 0;
67   pm[0].rm_eo = String.size();
68
69   int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
70
71   if (rc == REG_NOMATCH)
72     return false;
73   if (rc != 0) {
74     // regexec can fail due to invalid pattern or running out of memory.
75     error = rc;
76     return false;
77   }
78
79   // There was a match.
80
81   if (Matches) { // match position requested
82     for (unsigned i = 0; i != nmatch; ++i) {
83       if (pm[i].rm_so == -1) {
84         // this group didn't match
85         Matches->push_back(StringRef());
86         continue;
87       }
88       assert(pm[i].rm_eo > pm[i].rm_so);
89       Matches->push_back(StringRef(String.data()+pm[i].rm_so,
90                                    pm[i].rm_eo-pm[i].rm_so));
91     }
92   }
93
94   return true;
95 }