tidy up, fix a memory leak in Regex::isValid

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82707 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2009-09-24 20:15:51 +00:00
parent 07de8d1acf
commit 48ba9ff3c4
2 changed files with 10 additions and 14 deletions

View File

@ -54,7 +54,6 @@ namespace llvm {
/// Matches. /// Matches.
/// For this feature to be enabled you must construct the regex using /// For this feature to be enabled you must construct the regex using
/// Regex("...", Regex::Sub) constructor. /// Regex("...", Regex::Sub) constructor.
bool match(const StringRef &String, SmallVectorImpl<StringRef> *Matches=0); bool match(const StringRef &String, SmallVectorImpl<StringRef> *Matches=0);
private: private:
struct llvm_regex *preg; struct llvm_regex *preg;

View File

@ -10,15 +10,15 @@
// This file implements a POSIX regular expression matcher. // This file implements a POSIX regular expression matcher.
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Support/Regex.h" #include "llvm/Support/Regex.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "regex_impl.h" #include "regex_impl.h"
#include <string> #include <string>
using namespace llvm; using namespace llvm;
Regex::Regex(const StringRef &regex, unsigned Flags)
{ Regex::Regex(const StringRef &regex, unsigned Flags) {
unsigned flags = 0; unsigned flags = 0;
preg = new struct llvm_regex; preg = new struct llvm_regex;
preg->re_endp = regex.end(); preg->re_endp = regex.end();
@ -35,26 +35,23 @@ Regex::Regex(const StringRef &regex, unsigned Flags)
error = llvm_regcomp(preg, regex.data(), flags|REG_EXTENDED|REG_PEND); error = llvm_regcomp(preg, regex.data(), flags|REG_EXTENDED|REG_PEND);
} }
bool Regex::isValid(std::string &Error) bool Regex::isValid(std::string &Error) {
{
if (!error) if (!error)
return true; return true;
size_t len = llvm_regerror(error, preg, NULL, 0); size_t len = llvm_regerror(error, preg, NULL, 0);
char *errbuff = new char[len];
llvm_regerror(error, preg, errbuff, len); Error.resize(len);
Error.assign(errbuff); llvm_regerror(error, preg, &Error[0], len);
return false; return false;
} }
Regex::~Regex() Regex::~Regex() {
{
llvm_regfree(preg); llvm_regfree(preg);
delete preg; delete preg;
} }
bool Regex::match(const StringRef &String, SmallVectorImpl<StringRef> *Matches) bool Regex::match(const StringRef &String, SmallVectorImpl<StringRef> *Matches){
{
unsigned nmatch = Matches ? preg->re_nsub+1 : 0; unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
if (Matches) { if (Matches) {
@ -81,7 +78,7 @@ bool Regex::match(const StringRef &String, SmallVectorImpl<StringRef> *Matches)
// There was a match. // There was a match.
if (Matches) { // match position requested if (Matches) { // match position requested
for (unsigned i=0;i<nmatch; i++) { for (unsigned i = 0; i != nmatch; ++i) {
if (pm[i].rm_so == -1) { if (pm[i].rm_so == -1) {
// this group didn't match // this group didn't match
Matches->push_back(StringRef()); Matches->push_back(StringRef());