Add regex module

Move regular expression matching into a function in its own module.

No functional change.
This commit is contained in:
Kim Gräsman 2022-04-24 20:13:59 +02:00
parent 6f2b277e6d
commit 0a714265c0
4 changed files with 49 additions and 6 deletions

View File

@ -111,6 +111,7 @@ add_llvm_executable(include-what-you-use
iwyu_output.cc
iwyu_path_util.cc
iwyu_preprocessor.cc
iwyu_regex.cc
iwyu_verrs.cc
)

View File

@ -16,7 +16,6 @@
// not hash_map: it's not as portable and needs hash<string>.
#include <map> // for map, map<>::mapped_type, etc
#include <memory>
#include <regex>
#include <string> // for string, basic_string, etc
#include <system_error> // for error_code
#include <utility> // for pair, make_pair
@ -25,6 +24,7 @@
#include "iwyu_location_util.h"
#include "iwyu_path_util.h"
#include "iwyu_port.h"
#include "iwyu_regex.h"
#include "iwyu_stl_util.h"
#include "iwyu_string_util.h"
#include "iwyu_verrs.h"
@ -1436,17 +1436,16 @@ void IncludePicker::ExpandRegexes() {
for (const string& regex_key : filepath_include_map_regex_keys) {
const vector<MappedInclude>& map_to = filepath_include_map_[regex_key];
// Enclose the regex in ^(...)$ for full match.
std::regex regex(std::string("^(" + regex_key.substr(1) + ")$"));
if (std::regex_match(hdr, regex) &&
!ContainsQuotedInclude(map_to, hdr)) {
std::string regex("^(" + regex_key.substr(1) + ")$");
if (RegexMatch(hdr, regex) && !ContainsQuotedInclude(map_to, hdr)) {
Extend(&filepath_include_map_[hdr], filepath_include_map_[regex_key]);
MarkVisibility(&include_visibility_map_, hdr,
include_visibility_map_[regex_key]);
}
}
for (const string& regex_key : friend_to_headers_map_regex_keys) {
std::regex regex(std::string("^(" + regex_key.substr(1) + ")$"));
if (std::regex_match(hdr, regex)) {
std::string regex("^(" + regex_key.substr(1) + ")$");
if (RegexMatch(hdr, regex)) {
InsertAllInto(friend_to_headers_map_[regex_key],
&friend_to_headers_map_[hdr]);
}

21
iwyu_regex.cc Normal file
View File

@ -0,0 +1,21 @@
//===--- iwyu_regex.cc - iwyu regex implementation ------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "iwyu_regex.h"
#include <regex>
namespace include_what_you_use {
bool RegexMatch(const std::string& str, const std::string& pattern) {
std::regex r(pattern);
return std::regex_match(str, r);
}
} // namespace include_what_you_use

22
iwyu_regex.h Normal file
View File

@ -0,0 +1,22 @@
//===--- iwyu_regex.h - iwyu regex implementation -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef INCLUDE_WHAT_YOU_USE_IWYU_REGEX_H_
#define INCLUDE_WHAT_YOU_USE_IWYU_REGEX_H_
#include <string>
namespace include_what_you_use {
// Returns true if str matches regular expression pattern.
bool RegexMatch(const std::string& str, const std::string& pattern);
} // namespace include_what_you_use
#endif // INCLUDE_WHAT_YOU_USE_IWYU_REGEX_H_