Coverage Report

Created: 2020-06-26 05:44

/home/arjun/llvm-project/llvm/include/llvm/Support/Regex.h
Line
Count
Source (jump to first uncovered line)
1
//===-- Regex.h - Regular Expression matcher implementation -*- C++ -*-----===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements a POSIX regular expression matcher.  Both Basic and
10
// Extended POSIX regular expressions (ERE) are supported.  EREs were extended
11
// to support backreferences in matches.
12
// This implementation also supports matching strings with embedded NUL chars.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#ifndef LLVM_SUPPORT_REGEX_H
17
#define LLVM_SUPPORT_REGEX_H
18
19
#include <string>
20
21
struct llvm_regex;
22
23
namespace llvm {
24
  class StringRef;
25
  template<typename T> class SmallVectorImpl;
26
27
  class Regex {
28
  public:
29
    enum {
30
      NoFlags=0,
31
      /// Compile for matching that ignores upper/lower case distinctions.
32
      IgnoreCase=1,
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=2,
39
      /// By default, the POSIX extended regular expression (ERE) syntax is
40
      /// assumed. Pass this flag to turn on basic regular expressions (BRE)
41
      /// instead.
42
      BasicRegex=4
43
    };
44
45
    Regex();
46
    /// Compiles the given regular expression \p Regex.
47
    ///
48
    /// \param Regex - referenced string is no longer needed after this
49
    /// constructor does finish.  Only its compiled form is kept stored.
50
    Regex(StringRef Regex, unsigned Flags = NoFlags);
51
    Regex(const Regex &) = delete;
52
0
    Regex &operator=(Regex regex) {
53
0
      std::swap(preg, regex.preg);
54
0
      std::swap(error, regex.error);
55
0
      return *this;
56
0
    }
57
    Regex(Regex &&regex);
58
    ~Regex();
59
60
    /// isValid - returns the error encountered during regex compilation, if
61
    /// any.
62
    bool isValid(std::string &Error) const;
63
0
    bool isValid() const { return !error; }
64
65
    /// getNumMatches - In a valid regex, return the number of parenthesized
66
    /// matches it contains.  The number filled in by match will include this
67
    /// many entries plus one for the whole regex (as element 0).
68
    unsigned getNumMatches() const;
69
70
    /// matches - Match the regex against a given \p String.
71
    ///
72
    /// \param Matches - If given, on a successful match this will be filled in
73
    /// with references to the matched group expressions (inside \p String),
74
    /// the first group is always the entire pattern.
75
    ///
76
    /// \param Error - If non-null, any errors in the matching will be recorded
77
    /// as a non-empty string. If there is no error, it will be an empty string.
78
    ///
79
    /// This returns true on a successful match.
80
    bool match(StringRef String, SmallVectorImpl<StringRef> *Matches = nullptr,
81
               std::string *Error = nullptr) const;
82
83
    /// sub - Return the result of replacing the first match of the regex in
84
    /// \p String with the \p Repl string. Backreferences like "\0" in the
85
    /// replacement string are replaced with the appropriate match substring.
86
    ///
87
    /// Note that the replacement string has backslash escaping performed on
88
    /// it. Invalid backreferences are ignored (replaced by empty strings).
89
    ///
90
    /// \param Error If non-null, any errors in the substitution (invalid
91
    /// backreferences, trailing backslashes) will be recorded as a non-empty
92
    /// string. If there is no error, it will be an empty string.
93
    std::string sub(StringRef Repl, StringRef String,
94
                    std::string *Error = nullptr) const;
95
96
    /// If this function returns true, ^Str$ is an extended regular
97
    /// expression that matches Str and only Str.
98
    static bool isLiteralERE(StringRef Str);
99
100
    /// Turn String into a regex by escaping its special characters.
101
    static std::string escape(StringRef String);
102
103
  private:
104
    struct llvm_regex *preg;
105
    int error;
106
  };
107
}
108
109
#endif // LLVM_SUPPORT_REGEX_H