Refactor Visibility map code

Generalize IncludePicker::MarkVisibility to take a visibility map as an
argument, and rename filepath_visibility_map to include_visibility_map_.

This is in preparation for adding a new visibility map which is keyed by
paths rather than quoted includes.
This commit is contained in:
John Bytheway 2019-08-13 10:41:08 -04:00 committed by Kim Gräsman
parent 1513718871
commit 20edac9449
2 changed files with 28 additions and 20 deletions

View File

@ -1067,17 +1067,17 @@ void IncludePicker::AddDefaultMappings() {
IWYU_ARRAYSIZE(stdlib_cpp_public_headers));
}
void IncludePicker::MarkVisibility(const string& quoted_filepath_pattern,
void IncludePicker::MarkVisibility(VisibilityMap* map,
const string& key,
IncludeVisibility visibility) {
CHECK_(!has_called_finalize_added_include_lines_ && "Can't mutate anymore");
// insert() leaves any old value alone, and only inserts if the key is new.
filepath_visibility_map_.insert(
make_pair(quoted_filepath_pattern, visibility));
CHECK_(filepath_visibility_map_[quoted_filepath_pattern] == visibility)
map->insert(make_pair(key, visibility));
CHECK_((*map)[key] == visibility)
<< " Same file seen with two different visibilities: "
<< quoted_filepath_pattern
<< " Old vis: " << filepath_visibility_map_[quoted_filepath_pattern]
<< key
<< " Old vis: " << (*map)[key]
<< " New vis: " << visibility;
}
@ -1146,8 +1146,9 @@ void IncludePicker::AddIncludeMapping(const string& map_from,
const MappedInclude& map_to,
IncludeVisibility to_visibility) {
AddMapping(map_from, map_to);
MarkVisibility(map_from, from_visibility);
MarkVisibility(map_to.quoted_include, to_visibility);
MarkVisibility(&include_visibility_map_, map_from, from_visibility);
MarkVisibility(&include_visibility_map_, map_to.quoted_include,
to_visibility);
}
void IncludePicker::AddSymbolMapping(const string& map_from,
@ -1157,8 +1158,9 @@ void IncludePicker::AddSymbolMapping(const string& map_from,
// Symbol-names are always marked as private (or GetPublicValues()
// will self-map them, below).
MarkVisibility(map_from, kPrivate);
MarkVisibility(map_to.quoted_include, to_visibility);
MarkVisibility(&include_visibility_map_, map_from, kPrivate);
MarkVisibility(&include_visibility_map_, map_to.quoted_include,
to_visibility);
}
void IncludePicker::AddIncludeMappings(const IncludeMapEntry* entries,
@ -1181,7 +1183,7 @@ void IncludePicker::AddSymbolMappings(const IncludeMapEntry* entries,
void IncludePicker::AddPublicIncludes(const char** includes, size_t count) {
for (size_t i = 0; i < count; ++i) {
const char* include = includes[i];
MarkVisibility(include, kPublic);
MarkVisibility(&include_visibility_map_, include, kPublic);
}
}
@ -1190,7 +1192,7 @@ void IncludePicker::MarkIncludeAsPrivate(
CHECK_(!has_called_finalize_added_include_lines_ && "Can't mutate anymore");
CHECK_(IsQuotedFilepathPattern(quoted_filepath_pattern)
&& "MIAP takes a quoted filepath pattern");
MarkVisibility(quoted_filepath_pattern, kPrivate);
MarkVisibility(&include_visibility_map_, quoted_filepath_pattern, kPrivate);
}
void IncludePicker::AddFriendRegex(const string& includee_filepath,
@ -1249,7 +1251,8 @@ void IncludePicker::ExpandRegexes() {
llvm::Regex regex(std::string("^(" + regex_key.substr(1) + ")$"));
if (regex.match(hdr, nullptr) && !ContainsQuotedInclude(map_to, hdr)) {
Extend(&filepath_include_map_[hdr], filepath_include_map_[regex_key]);
MarkVisibility(hdr, filepath_visibility_map_[regex_key]);
MarkVisibility(&include_visibility_map_, hdr,
include_visibility_map_[regex_key]);
}
}
for (const string& regex_key : friend_to_headers_map_regex_keys) {
@ -1299,11 +1302,11 @@ vector<MappedInclude> IncludePicker::GetPublicValues(
if (!values || values->empty())
return retval;
if (GetOrDefault(filepath_visibility_map_, key, kPublic) == kPublic)
if (GetOrDefault(include_visibility_map_, key, kPublic) == kPublic)
retval.push_back(MappedInclude(key)); // we can map to ourself!
for (const MappedInclude& value : *values) {
CHECK_(!StartsWith(value.quoted_include, "@"));
if (GetOrDefault(filepath_visibility_map_, value.quoted_include, kPublic)
if (GetOrDefault(include_visibility_map_, value.quoted_include, kPublic)
== kPublic)
retval.push_back(value);
}
@ -1588,7 +1591,7 @@ IncludeVisibility IncludePicker::ParseVisibility(
IncludeVisibility IncludePicker::GetVisibility(
const string& quoted_include) const {
return GetOrDefault(
filepath_visibility_map_, quoted_include, kUnusedVisibility);
include_visibility_map_, quoted_include, kUnusedVisibility);
}
} // namespace include_what_you_use

View File

@ -84,6 +84,11 @@ class IncludePicker {
// lists of candidate public headers to include for symbol or quoted include.
typedef map<string, vector<MappedInclude>> IncludeMap;
// Used to track visibility as specified either in mapping files or via
// pragmas. The keys are quoted includes. The values are the
// visibility of the respective files.
typedef map<string, IncludeVisibility> VisibilityMap;
explicit IncludePicker(bool no_default_mappings);
// ----- Routines to dynamically modify the include-picker
@ -101,7 +106,7 @@ class IncludePicker {
// Indicate that the given quoted include should be considered
// a "private" include. If possible, we use the include-picker
// mappings to map such includes to public (not-private) includs.
// mappings to map such includes to public (not-private) includes.
void MarkIncludeAsPrivate(const string& quoted_include);
// Add this to say that "any file whose name matches the
@ -196,8 +201,8 @@ class IncludePicker {
// seen by iwyu.
void ExpandRegexes();
// Adds an entry to filepath_visibility_map_, with error checking.
void MarkVisibility(const string& quoted_filepath_pattern,
// Adds an entry to the given VisibilityMap, with error checking.
void MarkVisibility(VisibilityMap* map, const string& key,
IncludeVisibility visibility);
// Parse visibility from a string. Returns kUnusedVisibility if
@ -233,7 +238,7 @@ class IncludePicker {
// A map of all quoted-includes to whether they're public or private.
// Quoted-includes that are not present in this map are assumed public.
map<string, IncludeVisibility> filepath_visibility_map_;
VisibilityMap include_visibility_map_;
// All the includes we've seen so far, to help with globbing and
// other dynamic mapping. For each file, we list who #includes it.