add and document regex support for FileCheck. You can now do stuff like:
[oota-llvm.git] / include / llvm / Support / Regex.h
1 //===-- Regex.h - Regular Expression matcher implementation -*- C++ -*-----===//
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 <string>
15
16 struct llvm_regex;
17
18 namespace llvm {
19   class StringRef;
20   template<typename T> class SmallVectorImpl;
21   
22   class Regex {
23   public:
24     enum {
25       /// Compile with support for subgroup matches, this is just to make
26       /// constructs like Regex("...", 0) more readable as Regex("...", Sub).
27       Sub=0,
28       /// Compile for matching that ignores upper/lower case distinctions.
29       IgnoreCase=1,
30       /// Compile for matching that need only report success or failure,
31       /// not what was matched.
32       NoSub=2,
33       /// Compile for newline-sensitive matching. With this flag '[^' bracket
34       /// expressions and '.' never match newline. A ^ anchor matches the 
35       /// null string after any newline in the string in addition to its normal 
36       /// function, and the $ anchor matches the null string before any 
37       /// newline in the string in addition to its normal function.
38       Newline=4
39     };
40
41     /// Compiles the given POSIX Extended Regular Expression \arg Regex.
42     /// This implementation supports regexes and matching strings with embedded
43     /// NUL characters.
44     Regex(const StringRef &Regex, unsigned Flags=NoSub);
45     ~Regex();
46
47     /// isValid - returns the error encountered during regex compilation, or
48     /// matching, if any.
49     bool isValid(std::string &Error);
50
51     /// matches - Match the regex against a given \arg String.
52     ///
53     /// \param Matches - If given, on a succesful match this will be filled in
54     /// with references to the matched group expressions (inside \arg String),
55     /// the first group is always the entire pattern.
56     /// By default the regex is compiled with NoSub, which disables support for
57     /// Matches.
58     /// For this feature to be enabled you must construct the regex using
59     /// Regex("...", Regex::Sub) constructor.
60     ///
61     /// This returns true on a successful match.
62     bool match(const StringRef &String, SmallVectorImpl<StringRef> *Matches=0);
63   private:
64     struct llvm_regex *preg;
65     int error;
66     bool sub;
67   };
68 }