summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Bytheway <jbytheway@gmail.com>2019-08-14 10:47:43 -0400
committerKim Gräsman <kim.grasman@gmail.com>2019-09-01 16:41:53 +0200
commit0bb8e70bb19b3887cc8c843cd691b03f065313dc (patch)
tree26eea39b1f566fb1ab1670dc28fc67fbcc30e24e
parent20edac9449d5261dca71a77dde10bfa82573c751 (diff)
Refactor includes as a candidate for themselves
It's important that GetCandidateHeadersForFilepath can return the path itself as a candidate for accessing a specified path. This used to work by GetPublicValues returning the provided key as one of the possibilities for accessing that key. However, the key is not always a quoted include, it can also be a symbol. So, to prevent GetPublicValues returning a symbol, this required a workaround where all symbols had to be marked private. We can simplify all of this by moving the relevant logic out of GetPublicValues and into GetCandidateHeadersForFilepath. There was already a case in GetCandidateHeadersForFilepath where the path itself was added to the candidates, so we simply add an additional circumstance in which that happens. This simplifies both GetPublicValues and AddSymbolMapping.
-rw-r--r--iwyu_include_picker.cc18
-rw-r--r--iwyu_include_picker.h4
2 files changed, 11 insertions, 11 deletions
diff --git a/iwyu_include_picker.cc b/iwyu_include_picker.cc
index a656c94..4aa2bd9 100644
--- a/iwyu_include_picker.cc
+++ b/iwyu_include_picker.cc
@@ -1156,9 +1156,6 @@ void IncludePicker::AddSymbolMapping(const string& map_from,
IncludeVisibility to_visibility) {
symbol_include_map_[map_from].push_back(map_to);
- // Symbol-names are always marked as private (or GetPublicValues()
- // will self-map them, below).
- MarkVisibility(&include_visibility_map_, map_from, kPrivate);
MarkVisibility(&include_visibility_map_, map_to.quoted_include,
to_visibility);
}
@@ -1302,8 +1299,6 @@ vector<MappedInclude> IncludePicker::GetPublicValues(
if (!values || values->empty())
return retval;
- 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(include_visibility_map_, value.quoted_include, kPublic)
@@ -1341,9 +1336,12 @@ vector<MappedInclude> IncludePicker::GetCandidateHeadersForFilepath(
filepath, MakeAbsolutePath(GetParentPath(including_filepath)));
vector<MappedInclude> retval =
GetPublicValues(filepath_include_map_, quoted_header);
- if (retval.empty()) {
- // the filepath isn't in include_map, so just quote and return it.
- retval.push_back(MappedInclude(quoted_header, filepath));
+ // We also need to consider the header itself. Make that an option if it's
+ // public or there's no other option.
+ MappedInclude default_header(quoted_header, filepath);
+ if (retval.empty() || GetVisibility(quoted_header, kPublic) == kPublic) {
+ // Insert at front so it's the preferred option
+ retval.insert(retval.begin(), default_header);
}
return retval;
}
@@ -1589,9 +1587,9 @@ IncludeVisibility IncludePicker::ParseVisibility(
}
IncludeVisibility IncludePicker::GetVisibility(
- const string& quoted_include) const {
+ const string& quoted_include, IncludeVisibility default_value) const {
return GetOrDefault(
- include_visibility_map_, quoted_include, kUnusedVisibility);
+ include_visibility_map_, quoted_include, default_value);
}
} // namespace include_what_you_use
diff --git a/iwyu_include_picker.h b/iwyu_include_picker.h
index 6249f2a..905a559 100644
--- a/iwyu_include_picker.h
+++ b/iwyu_include_picker.h
@@ -211,7 +211,9 @@ class IncludePicker {
// Return the visibility of a given quoted_include if known, else
// kUnusedVisibility.
- IncludeVisibility GetVisibility(const string& quoted_include) const;
+ IncludeVisibility GetVisibility(
+ const string& quoted_include,
+ IncludeVisibility default_value = kUnusedVisibility) const;
// For the given key, return the vector of values associated with
// that key, or an empty vector if the key does not exist in the