diff options
author | John Bytheway <jbytheway@gmail.com> | 2019-08-13 10:41:08 -0400 |
---|---|---|
committer | Kim Gräsman <kim.grasman@gmail.com> | 2019-09-01 16:41:53 +0200 |
commit | 20edac9449d5261dca71a77dde10bfa82573c751 (patch) | |
tree | 8ea14bed4ec0b17470ed180e16051c7d7c7c383e | |
parent | 151371887144b9140b2541fd381562f8ae1d977e (diff) |
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.
-rw-r--r-- | iwyu_include_picker.cc | 35 | ||||
-rw-r--r-- | iwyu_include_picker.h | 13 |
2 files changed, 28 insertions, 20 deletions
diff --git a/iwyu_include_picker.cc b/iwyu_include_picker.cc index d950d9b..a656c94 100644 --- a/iwyu_include_picker.cc +++ b/iwyu_include_picker.cc @@ -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 diff --git a/iwyu_include_picker.h b/iwyu_include_picker.h index 49f02ab..6249f2a 100644 --- a/iwyu_include_picker.h +++ b/iwyu_include_picker.h @@ -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. |