2012-11-25 22:09:37 +00:00
|
|
|
//===--- iwyu_include_picker.cc - map to canonical #includes for iwyu -----===//
|
2011-02-04 22:28:15 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "iwyu_include_picker.h"
|
|
|
|
|
2011-05-24 00:07:01 +01:00
|
|
|
#include <stddef.h> // for size_t
|
2011-05-04 19:30:53 +01:00
|
|
|
#include <algorithm> // for find
|
|
|
|
// TODO(wan): make sure IWYU doesn't suggest <iterator>.
|
|
|
|
#include <iterator> // for find
|
|
|
|
// not hash_map: it's not as portable and needs hash<string>.
|
2011-05-24 00:07:01 +01:00
|
|
|
#include <map> // for map, map<>::mapped_type, etc
|
2011-12-01 02:30:18 +00:00
|
|
|
#include <ostream>
|
2011-05-04 19:30:53 +01:00
|
|
|
#include <string> // for string, basic_string, etc
|
|
|
|
#include <utility> // for pair, make_pair
|
2011-05-24 00:07:01 +01:00
|
|
|
#include <vector> // for vector, vector<>::iterator
|
2011-05-04 19:29:59 +01:00
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
#include "iwyu_path_util.h"
|
|
|
|
#include "iwyu_stl_util.h"
|
|
|
|
#include "iwyu_string_util.h"
|
2011-12-01 02:30:18 +00:00
|
|
|
#include "iwyu_verrs.h"
|
2011-05-04 19:30:53 +01:00
|
|
|
#include "port.h" // for CHECK_
|
2012-10-14 23:39:30 +01:00
|
|
|
|
2011-05-13 00:10:38 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-10-14 23:39:30 +01:00
|
|
|
#include "llvm/Support/Casting.h"
|
2012-11-25 21:26:06 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2011-05-13 00:10:38 +01:00
|
|
|
#include "llvm/Support/Regex.h"
|
2012-10-14 23:39:30 +01:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/YAMLParser.h"
|
2011-12-01 02:30:18 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-10-14 23:39:30 +01:00
|
|
|
#include "llvm/Support/system_error.h"
|
2011-02-04 22:28:15 +00:00
|
|
|
|
2011-04-27 00:00:56 +01:00
|
|
|
using std::find;
|
2011-05-04 19:17:55 +01:00
|
|
|
using std::make_pair;
|
2012-10-14 23:39:30 +01:00
|
|
|
using std::map;
|
2011-02-04 22:28:15 +00:00
|
|
|
using std::pair;
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
2012-10-14 23:39:30 +01:00
|
|
|
using llvm::MemoryBuffer;
|
|
|
|
using llvm::OwningPtr;
|
|
|
|
using llvm::SourceMgr;
|
|
|
|
using llvm::errs;
|
|
|
|
using llvm::yaml::MappingNode;
|
|
|
|
using llvm::yaml::Node;
|
|
|
|
using llvm::yaml::ScalarNode;
|
|
|
|
using llvm::yaml::SequenceNode;
|
|
|
|
using llvm::yaml::Stream;
|
|
|
|
using llvm::yaml::document_iterator;
|
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
namespace include_what_you_use {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2011-05-13 00:10:38 +01:00
|
|
|
// Returns true if str is a valid quoted filepath pattern (i.e. either
|
|
|
|
// a quoted filepath or "@" followed by a regex for matching a quoted
|
|
|
|
// filepath).
|
|
|
|
bool IsQuotedFilepathPattern(const string& str) {
|
|
|
|
return IsQuotedInclude(str) || StartsWith(str, "@");
|
|
|
|
}
|
|
|
|
|
2011-04-27 00:08:06 +01:00
|
|
|
// Given a vector of nodes, augment each node with its children, as
|
|
|
|
// defined by m: nodes[i] is replaced by nodes[i] + m[nodes[i]],
|
|
|
|
// ignoring duplicates. The input vector is modified in place.
|
|
|
|
void ExpandOnce(const IncludePicker::IncludeMap& m, vector<string>* nodes) {
|
|
|
|
vector<string> nodes_and_children;
|
|
|
|
set<string> seen_nodes_and_children;
|
|
|
|
for (Each<string> node(nodes); !node.AtEnd(); ++node) {
|
|
|
|
// First insert the node itself, then all its kids.
|
|
|
|
if (!ContainsKey(seen_nodes_and_children, *node)) {
|
|
|
|
nodes_and_children.push_back(*node);
|
|
|
|
seen_nodes_and_children.insert(*node);
|
|
|
|
}
|
|
|
|
if (const vector<string>* children = FindInMap(&m, *node)) {
|
|
|
|
for (Each<string> child(children); !child.AtEnd(); ++child) {
|
|
|
|
if (!ContainsKey(seen_nodes_and_children, *child)) {
|
|
|
|
nodes_and_children.push_back(*child);
|
|
|
|
seen_nodes_and_children.insert(*child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodes->swap(nodes_and_children); // modify nodes in-place
|
|
|
|
}
|
|
|
|
|
|
|
|
enum TransitiveStatus { kUnused = 0, kCalculating, kDone };
|
|
|
|
|
|
|
|
// If the filename-map maps a.h to b.h, and also b.h to c.h, then
|
2011-02-19 02:32:52 +00:00
|
|
|
// there's a transitive mapping of a.h to c.h. We want to add that
|
|
|
|
// into the filepath map as well, to make lookups easier. We do this
|
|
|
|
// by doing a depth-first search for a single mapping, recursing
|
|
|
|
// whenever the value is itself a key in the map, and putting the
|
|
|
|
// results in a vector of all values seen.
|
2011-04-27 00:08:06 +01:00
|
|
|
// NOTE: This function updates values seen in filename_map, but
|
|
|
|
// does not invalidate any filename_map iterators.
|
|
|
|
void MakeNodeTransitive(IncludePicker::IncludeMap* filename_map,
|
|
|
|
map<string, TransitiveStatus>* seen_nodes,
|
2011-10-17 21:32:27 +01:00
|
|
|
vector<string>* node_stack, // used for debugging
|
2011-04-27 00:08:06 +01:00
|
|
|
const string& key) {
|
|
|
|
// If we've already calculated this node's transitive closure, we're done.
|
|
|
|
const TransitiveStatus status = (*seen_nodes)[key];
|
|
|
|
if (status == kCalculating) { // means there's a cycle in the mapping
|
|
|
|
// third-party code sometimes has #include cycles (*cough* boost
|
|
|
|
// *cough*). Because we add many implicit third-party mappings,
|
|
|
|
// we may add a cycle without meaning to. The best we can do is
|
We already ignored cycles-in-includes in third-party files.
Ignore them for files in /internal/ as well, which suffer from
the same potential problem.
We could decide to complain on such cycles in /internal/ as
well, since this is our code so we can fix these cyclic
includes. But that requires adding new infrastructure, to add
a new kind of iwyu complaint, and I don't think this is
important enough to warrant. I'd rather the cycle get broken
just by the normal include cleanup that iwyu does.
Implementation-wise, I would have preferred to add a bit to
each include-mapping we see, to indicate whether it's one we
should complain if we see a cycle, or not. But that was a
pretty invasive change to the data structures that are complex
enough already, so I didn't. I think this code is already
hard enough to understand without adding another layer of
abstraction necessary to make such a bit easy to access. I
could be convinced otherwise.
R=dsturtevant
DELTA=65 (63 added, 0 deleted, 2 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=2651
2011-07-20 20:04:34 +01:00
|
|
|
// to ignore the mapping that causes the cycle. Same with code
|
|
|
|
// in /internal/. We could CHECK-fail in such a case, but it's
|
|
|
|
// probably better to just keep going.
|
|
|
|
if (StartsWith(key, "\"third_party/") ||
|
|
|
|
key.find("internal/") != string::npos) {
|
|
|
|
VERRS(4) << "Ignoring a cyclical mapping involving " << key << "\n";
|
2011-04-27 00:08:06 +01:00
|
|
|
return;
|
We already ignored cycles-in-includes in third-party files.
Ignore them for files in /internal/ as well, which suffer from
the same potential problem.
We could decide to complain on such cycles in /internal/ as
well, since this is our code so we can fix these cyclic
includes. But that requires adding new infrastructure, to add
a new kind of iwyu complaint, and I don't think this is
important enough to warrant. I'd rather the cycle get broken
just by the normal include cleanup that iwyu does.
Implementation-wise, I would have preferred to add a bit to
each include-mapping we see, to indicate whether it's one we
should complain if we see a cycle, or not. But that was a
pretty invasive change to the data structures that are complex
enough already, so I didn't. I think this code is already
hard enough to understand without adding another layer of
abstraction necessary to make such a bit easy to access. I
could be convinced otherwise.
R=dsturtevant
DELTA=65 (63 added, 0 deleted, 2 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=2651
2011-07-20 20:04:34 +01:00
|
|
|
}
|
2011-04-27 00:08:06 +01:00
|
|
|
}
|
2011-10-17 21:32:27 +01:00
|
|
|
if (status == kCalculating) {
|
|
|
|
VERRS(0) << "Cycle in include-mapping:\n";
|
|
|
|
for (size_t i = 0; i < node_stack->size(); ++i)
|
|
|
|
VERRS(0) << " " << (*node_stack)[i] << " ->\n";
|
|
|
|
VERRS(0) << " " << key << "\n";
|
|
|
|
CHECK_(false && "Cycle in include-mapping"); // cycle is a fatal error
|
|
|
|
}
|
2011-04-27 00:08:06 +01:00
|
|
|
if (status == kDone)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IncludePicker::IncludeMap::iterator node = filename_map->find(key);
|
|
|
|
if (node == filename_map->end()) {
|
|
|
|
(*seen_nodes)[key] = kDone;
|
2011-02-19 02:32:52 +00:00
|
|
|
return;
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 00:08:06 +01:00
|
|
|
// Keep track of node->second as we update it, to avoid duplicates.
|
|
|
|
(*seen_nodes)[key] = kCalculating;
|
|
|
|
for (Each<string> child(&node->second); !child.AtEnd(); ++child) {
|
2011-10-17 21:32:27 +01:00
|
|
|
node_stack->push_back(*child);
|
|
|
|
MakeNodeTransitive(filename_map, seen_nodes, node_stack, *child);
|
|
|
|
node_stack->pop_back();
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
2011-04-27 00:08:06 +01:00
|
|
|
(*seen_nodes)[key] = kDone;
|
|
|
|
|
|
|
|
// Our transitive closure is just the union of the closure of our
|
|
|
|
// children. This routine replaces our value with this closure,
|
|
|
|
// by replacing each of our values with its values. Since our
|
|
|
|
// values have already been made transitive, that is a closure.
|
|
|
|
ExpandOnce(*filename_map, &node->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates the values in filename_map based on its transitive mappings.
|
|
|
|
void MakeMapTransitive(IncludePicker::IncludeMap* filename_map) {
|
|
|
|
// Insert keys of filename_map here once we know their value is
|
|
|
|
// the complete transitive closure.
|
|
|
|
map<string, TransitiveStatus> seen_nodes;
|
2011-10-17 21:32:27 +01:00
|
|
|
vector<string> node_stack;
|
2011-04-27 00:08:06 +01:00
|
|
|
for (Each<string, vector<string> > it(filename_map); !it.AtEnd(); ++it)
|
2011-10-17 21:32:27 +01:00
|
|
|
MakeNodeTransitive(filename_map, &seen_nodes, &node_stack, it->first);
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
2012-10-14 23:39:30 +01:00
|
|
|
// Get a scalar value from a YAML node.
|
|
|
|
// Returns empty string if it's not of type ScalarNode.
|
|
|
|
string GetScalarValue(Node* node) {
|
|
|
|
ScalarNode* scalar = llvm::dyn_cast<ScalarNode>(node);
|
|
|
|
if (scalar == NULL)
|
|
|
|
return string();
|
|
|
|
|
|
|
|
llvm::SmallString<8> storage;
|
|
|
|
return scalar->getValue(storage).str();
|
|
|
|
}
|
2011-02-04 22:28:15 +00:00
|
|
|
|
2012-10-14 23:39:30 +01:00
|
|
|
// Get a sequence value from a YAML node.
|
|
|
|
// Returns empty vector if it's not of type SequenceNode.
|
|
|
|
vector<string> GetSequenceValue(Node* node) {
|
|
|
|
vector<string> result;
|
|
|
|
|
|
|
|
SequenceNode* sequence = llvm::dyn_cast<SequenceNode>(node);
|
|
|
|
if (sequence != NULL) {
|
|
|
|
for (SequenceNode::iterator it = sequence->begin();
|
|
|
|
it != sequence->end(); ++it) {
|
|
|
|
result.push_back(GetScalarValue(&*it));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2011-02-19 02:32:52 +00:00
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
IncludePicker::IncludePicker()
|
2011-02-19 02:32:52 +00:00
|
|
|
: symbol_include_map_(),
|
|
|
|
filepath_include_map_(),
|
2011-03-04 00:00:14 +00:00
|
|
|
filepath_visibility_map_(),
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
quoted_includes_to_quoted_includers_(),
|
2011-02-11 23:08:41 +00:00
|
|
|
has_called_finalize_added_include_lines_(false) {
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 00:10:38 +01:00
|
|
|
void IncludePicker::MarkVisibility(
|
|
|
|
const string& quoted_filepath_pattern,
|
|
|
|
IncludePicker::Visibility vis) {
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(!has_called_finalize_added_include_lines_ && "Can't mutate anymore");
|
2011-03-04 00:00:14 +00:00
|
|
|
|
|
|
|
// insert() leaves any old value alone, and only inserts if the key is new.
|
2011-05-13 00:10:38 +01:00
|
|
|
filepath_visibility_map_.insert(make_pair(quoted_filepath_pattern, vis));
|
2011-09-20 16:20:25 +01:00
|
|
|
CHECK_(filepath_visibility_map_[quoted_filepath_pattern] == vis)
|
|
|
|
<< " Same file seen with two different visibilities: "
|
|
|
|
<< quoted_filepath_pattern
|
|
|
|
<< " Old vis: "
|
|
|
|
<< filepath_visibility_map_[quoted_filepath_pattern]
|
|
|
|
<< " New vis: "
|
|
|
|
<< vis;
|
2011-03-04 00:00:14 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 02:32:52 +00:00
|
|
|
// AddDirectInclude lets us use some hard-coded rules to add filepath
|
|
|
|
// mappings at runtime. It includes, for instance, mappings from
|
|
|
|
// 'project/internal/foo.h' to 'project/public/foo_public.h' in google
|
|
|
|
// code (Google hides private headers in /internal/, much like glibc
|
|
|
|
// hides them in /bits/.)
|
|
|
|
void IncludePicker::AddDirectInclude(const string& includer_filepath,
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
const string& includee_filepath,
|
|
|
|
const string& quoted_include_as_typed) {
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(!has_called_finalize_added_include_lines_ && "Can't mutate anymore");
|
2011-02-19 02:32:52 +00:00
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
// Note: the includer may be a .cc file, which is unnecessary to add
|
|
|
|
// to our map, but harmless.
|
2011-02-19 02:32:52 +00:00
|
|
|
const string quoted_includer = ConvertToQuotedInclude(includer_filepath);
|
For some reason I was telling the include-picker about
includes-as-written rather than the actual file-path of the
included file. Since the include-picker deals with actual
file-paths (of the decls), this made no sense, and indeed we
were seeing when code depended on a search path, we weren't
finding the proper include-mapping for it.
For instance, python .h files has
#include "dictobject.h"
rather than
#include "third_party/python2_4_3/gcc-3.4-glibc-2.11.1-grte-k8-linux-python2.6-opt/include/python2.6/dictobject.h
Thus, while we had code that correctly mapped
third_party/python2_4_3 to <Python.h>, it wasn't firing on the
above code because the include-picker saw it as just
"dictobject.h". This is now fixed by using the actual
file-path.
While testing, I discovered the test-file was often calling
AddDirectInclude() improperly (with extra "'s). It didn't
happen to matter, but I cleaned it up.
R=wan,dsturtevant
DELTA=43 (5 added, 2 deleted, 36 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1574
2011-04-27 00:04:37 +01:00
|
|
|
const string quoted_includee = ConvertToQuotedInclude(includee_filepath);
|
|
|
|
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
quoted_includes_to_quoted_includers_[quoted_includee].insert(quoted_includer);
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
const pair<string, string> key(includer_filepath, includee_filepath);
|
2011-05-26 00:01:16 +01:00
|
|
|
includer_and_includee_to_include_as_typed_[key] = quoted_include_as_typed;
|
2011-04-06 21:07:58 +01:00
|
|
|
|
2011-05-04 19:31:58 +01:00
|
|
|
// Mark the clang fake-file "<built-in>" as private, so we never try
|
|
|
|
// to map anything to it.
|
|
|
|
if (includer_filepath == "<built-in>")
|
|
|
|
MarkIncludeAsPrivate("\"<built-in>\"");
|
|
|
|
|
2011-04-06 21:07:58 +01:00
|
|
|
// Automatically mark files in foo/internal/bar as private, and map them.
|
2011-05-13 00:10:38 +01:00
|
|
|
// Then say that everyone else in foo/.* is a friend, who is allowed to
|
Fix two bugs in one!
First, the 'friend' pragma wasn't working at all, due to some
confusion about what needed to be a quoted include and what
needed to be a filepath. I've cleaned all that up, updated
the comments, and added some tests to catch that situation.
Second, I moved the handling of /internal/ to a bespoke scheme
to using the normal 'friend' scheme -- basically, all internal
files automatically are friends with everyone else in the same
'package'. This fixes a bug that caused us to map some
#include to <built-in>, when the include-chain was:
<built-in> -> foo/internal/bar.cc -> foo/internal/baz.h
Before this change, both foo/internal/bar.cc and
foo/internal/baz.h were considered private, since they are in
/internal/, so the #include of baz.h was mapped to the only
non-private "file" in the chain, which is <built-in>. After
this change, foo/internal/bar.cc is still considered private,
but it doesn't matter since it's also considered a friend of
foo/internal/baz.h, and thus allowed to #include it.
R=wan,dsturtevant
DELTA=131 (83 added, 22 deleted, 26 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1728
2011-05-04 19:27:47 +01:00
|
|
|
// include the otherwise-private header.
|
|
|
|
const size_t internal_pos = quoted_includee.find("internal/");
|
|
|
|
if (internal_pos != string::npos &&
|
|
|
|
(internal_pos == 0 || quoted_includee[internal_pos - 1] == '/')) {
|
For some reason I was telling the include-picker about
includes-as-written rather than the actual file-path of the
included file. Since the include-picker deals with actual
file-paths (of the decls), this made no sense, and indeed we
were seeing when code depended on a search path, we weren't
finding the proper include-mapping for it.
For instance, python .h files has
#include "dictobject.h"
rather than
#include "third_party/python2_4_3/gcc-3.4-glibc-2.11.1-grte-k8-linux-python2.6-opt/include/python2.6/dictobject.h
Thus, while we had code that correctly mapped
third_party/python2_4_3 to <Python.h>, it wasn't firing on the
above code because the include-picker saw it as just
"dictobject.h". This is now fixed by using the actual
file-path.
While testing, I discovered the test-file was often calling
AddDirectInclude() improperly (with extra "'s). It didn't
happen to matter, but I cleaned it up.
R=wan,dsturtevant
DELTA=43 (5 added, 2 deleted, 36 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1574
2011-04-27 00:04:37 +01:00
|
|
|
MarkIncludeAsPrivate(quoted_includee);
|
2011-05-13 00:10:38 +01:00
|
|
|
// The second argument here is a regex for matching a quoted
|
|
|
|
// filepath. We get the opening quote from quoted_includee, and
|
|
|
|
// the closing quote as part of the .*.
|
|
|
|
AddFriendRegex(includee_filepath,
|
|
|
|
quoted_includee.substr(0, internal_pos) + ".*");
|
For some reason I was telling the include-picker about
includes-as-written rather than the actual file-path of the
included file. Since the include-picker deals with actual
file-paths (of the decls), this made no sense, and indeed we
were seeing when code depended on a search path, we weren't
finding the proper include-mapping for it.
For instance, python .h files has
#include "dictobject.h"
rather than
#include "third_party/python2_4_3/gcc-3.4-glibc-2.11.1-grte-k8-linux-python2.6-opt/include/python2.6/dictobject.h
Thus, while we had code that correctly mapped
third_party/python2_4_3 to <Python.h>, it wasn't firing on the
above code because the include-picker saw it as just
"dictobject.h". This is now fixed by using the actual
file-path.
While testing, I discovered the test-file was often calling
AddDirectInclude() improperly (with extra "'s). It didn't
happen to matter, but I cleaned it up.
R=wan,dsturtevant
DELTA=43 (5 added, 2 deleted, 36 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1574
2011-04-27 00:04:37 +01:00
|
|
|
AddMapping(quoted_includee, quoted_includer);
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
2011-04-06 21:07:58 +01:00
|
|
|
|
|
|
|
// Automatically mark <asm-FOO/bar.h> as private, and map to <asm/bar.h>.
|
For some reason I was telling the include-picker about
includes-as-written rather than the actual file-path of the
included file. Since the include-picker deals with actual
file-paths (of the decls), this made no sense, and indeed we
were seeing when code depended on a search path, we weren't
finding the proper include-mapping for it.
For instance, python .h files has
#include "dictobject.h"
rather than
#include "third_party/python2_4_3/gcc-3.4-glibc-2.11.1-grte-k8-linux-python2.6-opt/include/python2.6/dictobject.h
Thus, while we had code that correctly mapped
third_party/python2_4_3 to <Python.h>, it wasn't firing on the
above code because the include-picker saw it as just
"dictobject.h". This is now fixed by using the actual
file-path.
While testing, I discovered the test-file was often calling
AddDirectInclude() improperly (with extra "'s). It didn't
happen to matter, but I cleaned it up.
R=wan,dsturtevant
DELTA=43 (5 added, 2 deleted, 36 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1574
2011-04-27 00:04:37 +01:00
|
|
|
if (StartsWith(quoted_includee, "<asm-")) {
|
|
|
|
MarkIncludeAsPrivate(quoted_includee);
|
|
|
|
string public_header = quoted_includee;
|
2011-04-06 21:07:58 +01:00
|
|
|
StripPast(&public_header, "/"); // read past "asm-whatever/"
|
|
|
|
public_header = "<asm/" + public_header; // now it's <asm/something.h>
|
For some reason I was telling the include-picker about
includes-as-written rather than the actual file-path of the
included file. Since the include-picker deals with actual
file-paths (of the decls), this made no sense, and indeed we
were seeing when code depended on a search path, we weren't
finding the proper include-mapping for it.
For instance, python .h files has
#include "dictobject.h"
rather than
#include "third_party/python2_4_3/gcc-3.4-glibc-2.11.1-grte-k8-linux-python2.6-opt/include/python2.6/dictobject.h
Thus, while we had code that correctly mapped
third_party/python2_4_3 to <Python.h>, it wasn't firing on the
above code because the include-picker saw it as just
"dictobject.h". This is now fixed by using the actual
file-path.
While testing, I discovered the test-file was often calling
AddDirectInclude() improperly (with extra "'s). It didn't
happen to matter, but I cleaned it up.
R=wan,dsturtevant
DELTA=43 (5 added, 2 deleted, 36 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1574
2011-04-27 00:04:37 +01:00
|
|
|
AddMapping(quoted_includee, public_header);
|
2011-04-06 21:07:58 +01:00
|
|
|
}
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
2012-11-25 21:26:06 +00:00
|
|
|
void IncludePicker::AddMappingFileSearchPath(const string& path) {
|
|
|
|
string absolute_path = MakeAbsolutePath(path);
|
|
|
|
if (std::find(mapping_file_search_path_.begin(),
|
|
|
|
mapping_file_search_path_.end(),
|
|
|
|
absolute_path) == mapping_file_search_path_.end()) {
|
|
|
|
VERRS(6) << "Adding mapping file search path: " << absolute_path << "\n";
|
|
|
|
mapping_file_search_path_.push_back(absolute_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-04 00:00:14 +00:00
|
|
|
void IncludePicker::AddMapping(const string& map_from, const string& map_to) {
|
2011-05-24 00:06:34 +01:00
|
|
|
VERRS(4) << "Adding mapping from " << map_from << " to " << map_to << "\n";
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(!has_called_finalize_added_include_lines_ && "Can't mutate anymore");
|
2011-05-13 00:10:38 +01:00
|
|
|
CHECK_(IsQuotedFilepathPattern(map_from)
|
|
|
|
&& "All map keys must be quoted filepaths or @ followed by regex");
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(IsQuotedInclude(map_to) && "All map values must be quoted includes");
|
2011-03-04 00:00:14 +00:00
|
|
|
filepath_include_map_[map_from].push_back(map_to);
|
|
|
|
}
|
|
|
|
|
2012-10-14 23:39:30 +01:00
|
|
|
void IncludePicker::AddIncludeMapping(const string& map_from,
|
|
|
|
IncludePicker::Visibility from_visibility,
|
|
|
|
const string& map_to,
|
|
|
|
IncludePicker::Visibility to_visibility) {
|
|
|
|
AddMapping(map_from, map_to);
|
|
|
|
MarkVisibility(map_from, from_visibility);
|
|
|
|
MarkVisibility(map_to, to_visibility);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncludePicker::AddSymbolMapping(const string& map_from,
|
|
|
|
IncludePicker::Visibility from_visibility,
|
|
|
|
const string& map_to,
|
|
|
|
IncludePicker::Visibility to_visibility) {
|
|
|
|
CHECK_(IsQuotedInclude(map_to) && "Map values must be quoted includes");
|
|
|
|
symbol_include_map_[map_from].push_back(map_to);
|
|
|
|
|
|
|
|
// Symbol-names are always marked as private (or GetPublicValues()
|
|
|
|
// will self-map them, below).
|
|
|
|
MarkVisibility(map_from, kPrivate);
|
|
|
|
MarkVisibility(map_to, to_visibility);
|
|
|
|
}
|
|
|
|
|
2011-05-13 00:10:38 +01:00
|
|
|
void IncludePicker::MarkIncludeAsPrivate(const string& quoted_filepath_pattern) {
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(!has_called_finalize_added_include_lines_ && "Can't mutate anymore");
|
2011-05-13 00:10:38 +01:00
|
|
|
CHECK_(IsQuotedFilepathPattern(quoted_filepath_pattern)
|
|
|
|
&& "MIAP takes a quoted filepath pattern");
|
|
|
|
MarkVisibility(quoted_filepath_pattern, kPrivate);
|
2011-02-19 02:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 00:10:38 +01:00
|
|
|
void IncludePicker::AddFriendRegex(const string& includee,
|
|
|
|
const string& friend_regex) {
|
|
|
|
friend_to_headers_map_["@" + friend_regex].insert(includee);
|
2011-05-04 19:17:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2011-05-13 00:10:38 +01:00
|
|
|
// Given a map keyed by quoted filepath patterns, return a vector
|
|
|
|
// containing the @-regexes among the keys.
|
|
|
|
template <typename MapType>
|
|
|
|
vector<string> ExtractKeysMarkedAsRegexes(const MapType& m) {
|
|
|
|
vector<string> regex_keys;
|
2011-05-04 19:17:55 +01:00
|
|
|
for (Each<typename MapType::value_type> it(&m); !it.AtEnd(); ++it) {
|
2011-05-13 00:10:38 +01:00
|
|
|
if (StartsWith(it->first, "@"))
|
|
|
|
regex_keys.push_back(it->first);
|
2011-05-04 19:17:55 +01:00
|
|
|
}
|
2011-05-13 00:10:38 +01:00
|
|
|
return regex_keys;
|
2011-05-04 19:17:55 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2011-05-13 00:10:38 +01:00
|
|
|
// Expands the regex keys in filepath_include_map_ and
|
|
|
|
// friend_to_headers_map_ by matching them against all source files
|
|
|
|
// seen by iwyu. For each include that matches the regex, we add it
|
|
|
|
// to the map by copying the regex entry and replacing the key with
|
|
|
|
// the seen #include.
|
|
|
|
void IncludePicker::ExpandRegexes() {
|
|
|
|
// First, get the regex keys.
|
|
|
|
const vector<string> filepath_include_map_regex_keys =
|
|
|
|
ExtractKeysMarkedAsRegexes(filepath_include_map_);
|
|
|
|
const vector<string> friend_to_headers_map_regex_keys =
|
|
|
|
ExtractKeysMarkedAsRegexes(friend_to_headers_map_);
|
2011-02-19 02:32:52 +00:00
|
|
|
|
2011-05-13 00:10:38 +01:00
|
|
|
// Then, go through all #includes to see if they match the regexes,
|
|
|
|
// discarding the identity mappings. TODO(wan): to improve
|
|
|
|
// performance, don't construct more than one Regex object for each
|
|
|
|
// element in the above vectors.
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
for (Each<string, set<string> > incmap("ed_includes_to_quoted_includers_);
|
|
|
|
!incmap.AtEnd(); ++incmap) {
|
|
|
|
const string& hdr = incmap->first;
|
2011-05-13 00:10:38 +01:00
|
|
|
for (Each<string> it(&filepath_include_map_regex_keys); !it.AtEnd(); ++it) {
|
|
|
|
const string& regex_key = *it;
|
|
|
|
const vector<string>& map_to = filepath_include_map_[regex_key];
|
|
|
|
// Enclose the regex in ^(...)$ for full match.
|
|
|
|
llvm::Regex regex(std::string("^(" + regex_key.substr(1) + ")$"));
|
|
|
|
if (regex.match(hdr.c_str(), NULL) && !ContainsValue(map_to, hdr)) {
|
|
|
|
Extend(&filepath_include_map_[hdr], filepath_include_map_[regex_key]);
|
|
|
|
MarkVisibility(hdr, filepath_visibility_map_[regex_key]);
|
2011-02-19 02:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-13 00:10:38 +01:00
|
|
|
for (Each<string> it(&friend_to_headers_map_regex_keys);
|
2011-05-04 19:17:55 +01:00
|
|
|
!it.AtEnd(); ++it) {
|
2011-05-13 00:10:38 +01:00
|
|
|
const string& regex_key = *it;
|
|
|
|
llvm::Regex regex(std::string("^(" + regex_key.substr(1) + ")$"));
|
|
|
|
if (regex.match(hdr.c_str(), NULL)) {
|
|
|
|
InsertAllInto(friend_to_headers_map_[regex_key],
|
2011-05-04 19:17:55 +01:00
|
|
|
&friend_to_headers_map_[hdr]);
|
|
|
|
}
|
|
|
|
}
|
2011-02-19 02:32:52 +00:00
|
|
|
}
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
// We treat third-party code specially, since it's difficult to add
|
|
|
|
// iwyu pragmas to code we don't own. Basically, what we do is trust
|
|
|
|
// the code authors when it comes to third-party code: if they
|
|
|
|
// #include x.h to get symbols from y.h, then assume that's how the
|
|
|
|
// third-party authors wanted it. This boils down to the following
|
|
|
|
// rules:
|
2011-09-22 16:39:43 +01:00
|
|
|
// 1) If there's already a mapping for third_party/y.h, do not
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
// add any implicit maps for it.
|
|
|
|
// 2) if not_third_party/x.{h,cc} #includes third_party/y.h,
|
|
|
|
// assume y.h is supposed to be included directly, and do not
|
|
|
|
// add any implicit maps for it.
|
|
|
|
// 3) Otherwise, if third_party/x.h #includes third_party/y.h,
|
2011-09-22 16:39:43 +01:00
|
|
|
// add a mapping from y.h to x.h. Unless y.h already has
|
|
|
|
// a hard-coded visibility set, make y.h private. This
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
// means iwyu will never suggest adding y.h.
|
|
|
|
void IncludePicker::AddImplicitThirdPartyMappings() {
|
2011-05-24 00:06:34 +01:00
|
|
|
set<string> third_party_headers_with_explicit_mappings;
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
for (Each<IncludeMap::value_type>
|
|
|
|
it(&filepath_include_map_); !it.AtEnd(); ++it) {
|
2011-05-24 00:06:34 +01:00
|
|
|
if (IsThirdPartyFile(it->first))
|
|
|
|
third_party_headers_with_explicit_mappings.insert(it->first);
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set<string> headers_included_from_non_third_party;
|
|
|
|
for (Each<string, set<string> >
|
|
|
|
it("ed_includes_to_quoted_includers_); !it.AtEnd(); ++it) {
|
|
|
|
for (Each<string> includer(&it->second); !includer.AtEnd(); ++includer) {
|
2011-05-24 00:06:34 +01:00
|
|
|
if (!IsThirdPartyFile(*includer)) {
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
headers_included_from_non_third_party.insert(it->first);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Each<string, set<string> >
|
|
|
|
it("ed_includes_to_quoted_includers_); !it.AtEnd(); ++it) {
|
2011-05-24 00:06:34 +01:00
|
|
|
const string& includee = it->first;
|
2011-09-22 16:39:43 +01:00
|
|
|
if (!IsThirdPartyFile(includee) ||
|
|
|
|
ContainsKey(third_party_headers_with_explicit_mappings, includee) ||
|
2011-05-24 00:06:34 +01:00
|
|
|
ContainsKey(headers_included_from_non_third_party, includee)) {
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (Each<string> includer(&it->second); !includer.AtEnd(); ++includer) {
|
2011-05-24 00:06:34 +01:00
|
|
|
// From the 'if' statement above, we already know that includee
|
|
|
|
// is not included from non-third-party code.
|
|
|
|
CHECK_(IsThirdPartyFile(*includer) && "Why not nixed!");
|
2011-09-22 16:39:43 +01:00
|
|
|
CHECK_(IsThirdPartyFile(includee) && "Why not nixed!");
|
2011-05-24 00:06:34 +01:00
|
|
|
AddMapping(includee, *includer);
|
2011-09-22 16:39:43 +01:00
|
|
|
if (GetVisibility(includee) == kUnusedVisibility) {
|
|
|
|
MarkIncludeAsPrivate(includee);
|
|
|
|
}
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-19 02:32:52 +00:00
|
|
|
// Handle work that's best done after we've seen all the mappings
|
|
|
|
// (including dynamically-added ones) and all the include files.
|
2011-05-13 00:10:38 +01:00
|
|
|
// For instance, we can now expand all the regexes we've seen in
|
2011-02-19 02:32:52 +00:00
|
|
|
// the mapping-keys, since we have the full list of #includes to
|
|
|
|
// match them again. We also transitively-close the maps.
|
2011-02-04 22:28:15 +00:00
|
|
|
void IncludePicker::FinalizeAddedIncludes() {
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(!has_called_finalize_added_include_lines_ && "Can't call FAI twice");
|
2011-02-04 22:28:15 +00:00
|
|
|
|
2011-05-13 00:10:38 +01:00
|
|
|
// The map keys may be regular expressions. Match those to seen #includes now.
|
|
|
|
ExpandRegexes();
|
2011-02-04 22:28:15 +00:00
|
|
|
|
Add special support for third-party code, to make it less
likely we'll suggest adding an internal third-party header.
Basically, we stop trying to do include-what-you-use fixes on
third-party code.
More precisely, we make an 'implicit' judgment on which
third-party headers are public and which are private, based on
what existing code (in this translation unit) #includes. We
marked all unincluded files as private, which means iwyu will
never suggest adding a new third-party file as an #include.
Insted, it will suggest some already-included third-party file
that gets the needed file transitively.
Since it's not really practical for us to fix third-party code
to have better #include hygiene, or even to mark up third-party
code with iwyu pragmas, we need to do something similar to
this. We could just manually update iwyu_include_picker's
third_party_map with rules for every third party package we
have, but that's expensive. This is much cheaper, with the
downside that we may miss some potential include-what-you-use
opportunities in third-party code (going from a more-generic
third-party include to a less-generic one). I think that's a
low cost.
R=dsturtevant
DELTA=113 (104 added, 0 deleted, 9 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1577
2011-04-27 00:06:16 +01:00
|
|
|
// We treat third-party code specially, since it's difficult to add
|
|
|
|
// iwyu pragmas to code we don't own.
|
|
|
|
AddImplicitThirdPartyMappings();
|
|
|
|
|
2011-02-19 02:32:52 +00:00
|
|
|
// If a.h maps to b.h maps to c.h, we'd like an entry from a.h to c.h too.
|
2011-04-27 00:08:06 +01:00
|
|
|
MakeMapTransitive(&filepath_include_map_);
|
|
|
|
// Now that filepath_include_map_ is transitively closed, it's an
|
|
|
|
// easy task to get the values of symbol_include_map_ closed too.
|
|
|
|
// We can't use Each<>() because we need a non-const iterator.
|
|
|
|
for (IncludePicker::IncludeMap::iterator it = symbol_include_map_.begin();
|
|
|
|
it != symbol_include_map_.end(); ++it) {
|
|
|
|
ExpandOnce(filepath_include_map_, &it->second);
|
|
|
|
}
|
2011-02-04 22:28:15 +00:00
|
|
|
|
2011-02-19 02:32:52 +00:00
|
|
|
has_called_finalize_added_include_lines_ = true;
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 00:00:14 +00:00
|
|
|
// 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 map.
|
|
|
|
// *However*, we filter out all values that have private visibility
|
|
|
|
// before returning the vector. *Also*, if the key is public in
|
|
|
|
// the map, we insert the key as the first of the returned values,
|
|
|
|
// this is an implicit "self-map."
|
|
|
|
vector<string> IncludePicker::GetPublicValues(
|
|
|
|
const IncludePicker::IncludeMap& m, const string& key) const {
|
2011-05-13 00:10:38 +01:00
|
|
|
CHECK_(!StartsWith(key, "@"));
|
2011-03-04 00:00:14 +00:00
|
|
|
vector<string> retval;
|
|
|
|
const vector<string>* values = FindInMap(&m, key);
|
|
|
|
if (!values || values->empty())
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
if (GetOrDefault(filepath_visibility_map_, key, kPublic) == kPublic)
|
|
|
|
retval.push_back(key); // we can map to ourself!
|
|
|
|
for (Each<string> it(values); !it.AtEnd(); ++it) {
|
2011-05-13 00:10:38 +01:00
|
|
|
CHECK_(!StartsWith(*it, "@"));
|
2011-03-04 00:00:14 +00:00
|
|
|
if (GetOrDefault(filepath_visibility_map_, *it, kPublic) == kPublic)
|
|
|
|
retval.push_back(*it);
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-05-26 00:01:16 +01:00
|
|
|
string IncludePicker::MaybeGetIncludeNameAsWritten(
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
const string& includer_filepath, const string& includee_filepath) const {
|
|
|
|
const pair<string, string> key(includer_filepath, includee_filepath);
|
|
|
|
// I want to use GetOrDefault here, but it has trouble deducing tpl args.
|
2011-05-26 00:01:16 +01:00
|
|
|
const string* value = FindInMap(&includer_and_includee_to_include_as_typed_,
|
|
|
|
key);
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
return value ? *value : "";
|
|
|
|
}
|
|
|
|
|
2012-11-25 21:26:06 +00:00
|
|
|
error_code IncludePicker::TryReadMappingFile(
|
|
|
|
const string& filename,
|
|
|
|
OwningPtr<MemoryBuffer>& buffer) const {
|
|
|
|
string absolute_path;
|
|
|
|
if (IsAbsolutePath(filename)) {
|
|
|
|
VERRS(5) << "Absolute mapping filename: " << filename << ".\n";
|
|
|
|
absolute_path = filename;
|
|
|
|
} else {
|
|
|
|
VERRS(5) << "Relative mapping filename: " << filename << ". "
|
|
|
|
<< "Scanning search path.\n";
|
|
|
|
// Scan search path
|
|
|
|
for (Each<string> it(&mapping_file_search_path_); !it.AtEnd(); ++it) {
|
|
|
|
string candidate = MakeAbsolutePath(*it, filename);
|
|
|
|
if (llvm::sys::fs::exists(candidate)) {
|
|
|
|
absolute_path = candidate;
|
|
|
|
VERRS(5) << "Found mapping file: " << candidate << ".\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error_code error = MemoryBuffer::getFile(absolute_path, buffer);
|
|
|
|
VERRS(5) << "Opened mapping file: " << filename << "? "
|
|
|
|
<< error.message() << "\n";
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
The rule that files like 'foo/internal/bar.h' should always be
treated as private headers, and mapped to the nearest
including public header, was too strict. The basic problem:
what if 'foo/internal/baz.h' tried to include
'foo/internal/bar.h'? It should be able to. In some cases,
'foo/internal/bar.h' isn't even included from any non-internal
file, and we end up suggesting to #include <built-in> (the
only non-private include in the include-chain).
I fixed this up by adding a new function for mapping private
headers to public, that takes into account who is doing the
including. If foo/x/y/z is including foo/internal/a/b/c, we
don't say foo/internal/a/b/c is private in this context. But
if joe/otherproject tries to include foo/internal/a/b/c, then
we *do* say foo/internal/a/b/c is private, and map it to its
closest public header.
I also took out unnecessary code that marked includer-files
that are '/internal/' as private, not just included-files. We
should never need to mark includers as private; if the
includer-file is itself included in turn, we'll have ample
opportunity to mark it private then. Otherwise, we run the
risk of a file being marked private, with nobody including it
that we can map to.
To better match the new semantics that files aren't
intrinsically public or private but it depends on the context,
I renamed GetPublicHeader* to GetCandidateHeader*,
R=wan,dsturtevant
DELTA=179 (84 added, 2 deleted, 93 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1590
2011-04-27 00:12:07 +01:00
|
|
|
vector<string> IncludePicker::GetCandidateHeadersForSymbol(
|
2011-02-04 22:28:15 +00:00
|
|
|
const string& symbol) const {
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(has_called_finalize_added_include_lines_ && "Must finalize includes");
|
2011-02-19 02:32:52 +00:00
|
|
|
return GetPublicValues(symbol_include_map_, symbol);
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
The rule that files like 'foo/internal/bar.h' should always be
treated as private headers, and mapped to the nearest
including public header, was too strict. The basic problem:
what if 'foo/internal/baz.h' tried to include
'foo/internal/bar.h'? It should be able to. In some cases,
'foo/internal/bar.h' isn't even included from any non-internal
file, and we end up suggesting to #include <built-in> (the
only non-private include in the include-chain).
I fixed this up by adding a new function for mapping private
headers to public, that takes into account who is doing the
including. If foo/x/y/z is including foo/internal/a/b/c, we
don't say foo/internal/a/b/c is private in this context. But
if joe/otherproject tries to include foo/internal/a/b/c, then
we *do* say foo/internal/a/b/c is private, and map it to its
closest public header.
I also took out unnecessary code that marked includer-files
that are '/internal/' as private, not just included-files. We
should never need to mark includers as private; if the
includer-file is itself included in turn, we'll have ample
opportunity to mark it private then. Otherwise, we run the
risk of a file being marked private, with nobody including it
that we can map to.
To better match the new semantics that files aren't
intrinsically public or private but it depends on the context,
I renamed GetPublicHeader* to GetCandidateHeader*,
R=wan,dsturtevant
DELTA=179 (84 added, 2 deleted, 93 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1590
2011-04-27 00:12:07 +01:00
|
|
|
vector<string> IncludePicker::GetCandidateHeadersForFilepath(
|
2011-02-19 02:32:52 +00:00
|
|
|
const string& filepath) const {
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(has_called_finalize_added_include_lines_ && "Must finalize includes");
|
2011-02-19 02:32:52 +00:00
|
|
|
const string quoted_header = ConvertToQuotedInclude(filepath);
|
|
|
|
vector<string> retval = GetPublicValues(filepath_include_map_, quoted_header);
|
2011-02-04 22:28:15 +00:00
|
|
|
if (retval.empty()) {
|
2011-02-19 02:32:52 +00:00
|
|
|
// the filepath isn't in include_map, so just quote and return it.
|
|
|
|
retval.push_back(quoted_header);
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
Fix two bugs in one!
First, the 'friend' pragma wasn't working at all, due to some
confusion about what needed to be a quoted include and what
needed to be a filepath. I've cleaned all that up, updated
the comments, and added some tests to catch that situation.
Second, I moved the handling of /internal/ to a bespoke scheme
to using the normal 'friend' scheme -- basically, all internal
files automatically are friends with everyone else in the same
'package'. This fixes a bug that caused us to map some
#include to <built-in>, when the include-chain was:
<built-in> -> foo/internal/bar.cc -> foo/internal/baz.h
Before this change, both foo/internal/bar.cc and
foo/internal/baz.h were considered private, since they are in
/internal/, so the #include of baz.h was mapped to the only
non-private "file" in the chain, which is <built-in>. After
this change, foo/internal/bar.cc is still considered private,
but it doesn't matter since it's also considered a friend of
foo/internal/baz.h, and thus allowed to #include it.
R=wan,dsturtevant
DELTA=131 (83 added, 22 deleted, 26 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1728
2011-05-04 19:27:47 +01:00
|
|
|
// Except for the case that the includer is a 'friend' of the includee
|
|
|
|
// (via an '// IWYU pragma: friend XXX'), the same as
|
|
|
|
// GetCandidateHeadersForFilepath.
|
The rule that files like 'foo/internal/bar.h' should always be
treated as private headers, and mapped to the nearest
including public header, was too strict. The basic problem:
what if 'foo/internal/baz.h' tried to include
'foo/internal/bar.h'? It should be able to. In some cases,
'foo/internal/bar.h' isn't even included from any non-internal
file, and we end up suggesting to #include <built-in> (the
only non-private include in the include-chain).
I fixed this up by adding a new function for mapping private
headers to public, that takes into account who is doing the
including. If foo/x/y/z is including foo/internal/a/b/c, we
don't say foo/internal/a/b/c is private in this context. But
if joe/otherproject tries to include foo/internal/a/b/c, then
we *do* say foo/internal/a/b/c is private, and map it to its
closest public header.
I also took out unnecessary code that marked includer-files
that are '/internal/' as private, not just included-files. We
should never need to mark includers as private; if the
includer-file is itself included in turn, we'll have ample
opportunity to mark it private then. Otherwise, we run the
risk of a file being marked private, with nobody including it
that we can map to.
To better match the new semantics that files aren't
intrinsically public or private but it depends on the context,
I renamed GetPublicHeader* to GetCandidateHeader*,
R=wan,dsturtevant
DELTA=179 (84 added, 2 deleted, 93 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1590
2011-04-27 00:12:07 +01:00
|
|
|
vector<string> IncludePicker::GetCandidateHeadersForFilepathIncludedFrom(
|
|
|
|
const string& included_filepath, const string& including_filepath) const {
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
vector<string> retval;
|
Fix two bugs in one!
First, the 'friend' pragma wasn't working at all, due to some
confusion about what needed to be a quoted include and what
needed to be a filepath. I've cleaned all that up, updated
the comments, and added some tests to catch that situation.
Second, I moved the handling of /internal/ to a bespoke scheme
to using the normal 'friend' scheme -- basically, all internal
files automatically are friends with everyone else in the same
'package'. This fixes a bug that caused us to map some
#include to <built-in>, when the include-chain was:
<built-in> -> foo/internal/bar.cc -> foo/internal/baz.h
Before this change, both foo/internal/bar.cc and
foo/internal/baz.h were considered private, since they are in
/internal/, so the #include of baz.h was mapped to the only
non-private "file" in the chain, which is <built-in>. After
this change, foo/internal/bar.cc is still considered private,
but it doesn't matter since it's also considered a friend of
foo/internal/baz.h, and thus allowed to #include it.
R=wan,dsturtevant
DELTA=131 (83 added, 22 deleted, 26 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1728
2011-05-04 19:27:47 +01:00
|
|
|
const string quoted_includer = ConvertToQuotedInclude(including_filepath);
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
const string quoted_includee = ConvertToQuotedInclude(included_filepath);
|
2011-05-04 19:17:55 +01:00
|
|
|
const set<string>* headers_with_includer_as_friend =
|
Fix two bugs in one!
First, the 'friend' pragma wasn't working at all, due to some
confusion about what needed to be a quoted include and what
needed to be a filepath. I've cleaned all that up, updated
the comments, and added some tests to catch that situation.
Second, I moved the handling of /internal/ to a bespoke scheme
to using the normal 'friend' scheme -- basically, all internal
files automatically are friends with everyone else in the same
'package'. This fixes a bug that caused us to map some
#include to <built-in>, when the include-chain was:
<built-in> -> foo/internal/bar.cc -> foo/internal/baz.h
Before this change, both foo/internal/bar.cc and
foo/internal/baz.h were considered private, since they are in
/internal/, so the #include of baz.h was mapped to the only
non-private "file" in the chain, which is <built-in>. After
this change, foo/internal/bar.cc is still considered private,
but it doesn't matter since it's also considered a friend of
foo/internal/baz.h, and thus allowed to #include it.
R=wan,dsturtevant
DELTA=131 (83 added, 22 deleted, 26 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1728
2011-05-04 19:27:47 +01:00
|
|
|
FindInMap(&friend_to_headers_map_, quoted_includer);
|
2011-05-04 19:17:55 +01:00
|
|
|
if (headers_with_includer_as_friend != NULL &&
|
|
|
|
ContainsKey(*headers_with_includer_as_friend, included_filepath)) {
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
retval.push_back(quoted_includee);
|
|
|
|
} else {
|
|
|
|
retval = GetCandidateHeadersForFilepath(included_filepath);
|
2011-05-24 00:07:27 +01:00
|
|
|
if (retval.size() == 1) {
|
|
|
|
const string& quoted_header = retval[0];
|
|
|
|
if (GetVisibility(quoted_header) == IncludePicker::kPrivate) {
|
|
|
|
VERRS(0) << "Warning: "
|
|
|
|
<< "No public header found to replace the private header "
|
|
|
|
<< quoted_header << "\n";
|
|
|
|
}
|
|
|
|
}
|
2011-05-04 19:17:55 +01:00
|
|
|
}
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
|
|
|
|
// We'll have called ConvertToQuotedInclude on members of retval,
|
|
|
|
// but sometimes we can do better -- if included_filepath is in
|
|
|
|
// retval, the iwyu-preprocessor may have stored the quoted-include
|
|
|
|
// as typed in including_filepath. This is better to use than
|
|
|
|
// ConvertToQuotedInclude because it avoids trouble when the same
|
2011-05-26 00:01:16 +01:00
|
|
|
// file is accessible via different include search-paths, or is
|
|
|
|
// accessed via a symlink.
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
const string& quoted_include_as_typed
|
2011-05-26 00:01:16 +01:00
|
|
|
= MaybeGetIncludeNameAsWritten(including_filepath, included_filepath);
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
if (!quoted_include_as_typed.empty()) {
|
2011-05-26 00:01:16 +01:00
|
|
|
vector<string>::iterator it = std::find(retval.begin(), retval.end(),
|
|
|
|
quoted_includee);
|
|
|
|
if (it != retval.end())
|
|
|
|
*it = quoted_include_as_typed;
|
When keeping an #include, prefer the include-name as typed,
rather than the one clang gives us. Normally they're the
same, but can be different when the #include could be accessed
via different paths, or via symlinks (for instance, if
we #include "a/b/c.h" and compile with "-I. -Ia -Ia/b", then
we could say #include "a/b/c.h", #include "b/c.h", or #include
"c.h"). clang will, as I understand it, pick one of these
three forms arbitrarily for FileEntry::getName. We store the
name as it was actually typed in the source, and prefer it.
Obviously, the above only works for includes that already
exist. If we suggest a new include, we will fall back on
whatever clang gives us, which is an arbitrary name (in
practice, the first form seen).
R=klimek
DELTA=161 (100 added, 1 deleted, 60 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1851
2011-05-13 00:10:15 +01:00
|
|
|
}
|
|
|
|
return retval;
|
The rule that files like 'foo/internal/bar.h' should always be
treated as private headers, and mapped to the nearest
including public header, was too strict. The basic problem:
what if 'foo/internal/baz.h' tried to include
'foo/internal/bar.h'? It should be able to. In some cases,
'foo/internal/bar.h' isn't even included from any non-internal
file, and we end up suggesting to #include <built-in> (the
only non-private include in the include-chain).
I fixed this up by adding a new function for mapping private
headers to public, that takes into account who is doing the
including. If foo/x/y/z is including foo/internal/a/b/c, we
don't say foo/internal/a/b/c is private in this context. But
if joe/otherproject tries to include foo/internal/a/b/c, then
we *do* say foo/internal/a/b/c is private, and map it to its
closest public header.
I also took out unnecessary code that marked includer-files
that are '/internal/' as private, not just included-files. We
should never need to mark includers as private; if the
includer-file is itself included in turn, we'll have ample
opportunity to mark it private then. Otherwise, we run the
risk of a file being marked private, with nobody including it
that we can map to.
To better match the new semantics that files aren't
intrinsically public or private but it depends on the context,
I renamed GetPublicHeader* to GetCandidateHeader*,
R=wan,dsturtevant
DELTA=179 (84 added, 2 deleted, 93 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1590
2011-04-27 00:12:07 +01:00
|
|
|
}
|
|
|
|
|
2011-02-19 02:32:52 +00:00
|
|
|
bool IncludePicker::HasMapping(const string& map_from_filepath,
|
|
|
|
const string& map_to_filepath) const {
|
2011-03-18 02:51:02 +00:00
|
|
|
CHECK_(has_called_finalize_added_include_lines_ && "Must finalize includes");
|
2011-02-19 02:32:52 +00:00
|
|
|
const string quoted_from = ConvertToQuotedInclude(map_from_filepath);
|
|
|
|
const string quoted_to = ConvertToQuotedInclude(map_to_filepath);
|
The rule that files like 'foo/internal/bar.h' should always be
treated as private headers, and mapped to the nearest
including public header, was too strict. The basic problem:
what if 'foo/internal/baz.h' tried to include
'foo/internal/bar.h'? It should be able to. In some cases,
'foo/internal/bar.h' isn't even included from any non-internal
file, and we end up suggesting to #include <built-in> (the
only non-private include in the include-chain).
I fixed this up by adding a new function for mapping private
headers to public, that takes into account who is doing the
including. If foo/x/y/z is including foo/internal/a/b/c, we
don't say foo/internal/a/b/c is private in this context. But
if joe/otherproject tries to include foo/internal/a/b/c, then
we *do* say foo/internal/a/b/c is private, and map it to its
closest public header.
I also took out unnecessary code that marked includer-files
that are '/internal/' as private, not just included-files. We
should never need to mark includers as private; if the
includer-file is itself included in turn, we'll have ample
opportunity to mark it private then. Otherwise, we run the
risk of a file being marked private, with nobody including it
that we can map to.
To better match the new semantics that files aren't
intrinsically public or private but it depends on the context,
I renamed GetPublicHeader* to GetCandidateHeader*,
R=wan,dsturtevant
DELTA=179 (84 added, 2 deleted, 93 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1590
2011-04-27 00:12:07 +01:00
|
|
|
// We can't use GetCandidateHeadersForFilepath since includer might be private
|
2011-03-04 00:00:14 +00:00
|
|
|
const vector<string>* all_mappers = FindInMap(&filepath_include_map_,
|
|
|
|
quoted_from);
|
2011-02-19 02:32:52 +00:00
|
|
|
if (all_mappers) {
|
2011-03-04 00:00:14 +00:00
|
|
|
for (Each<string> it(all_mappers); !it.AtEnd(); ++it) {
|
|
|
|
if (*it == quoted_to)
|
2011-02-19 02:32:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
2011-02-19 02:32:52 +00:00
|
|
|
return quoted_to == quoted_from; // indentity mapping, why not?
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
2012-10-14 23:39:30 +01:00
|
|
|
// Parses a YAML/JSON file containing mapping directives of various types:
|
|
|
|
// symbol - symbol name -> quoted include
|
|
|
|
// include - private quoted include -> public quoted include
|
|
|
|
// ref - include mechanism for mapping files, to allow project-specific
|
|
|
|
// groupings
|
|
|
|
// We use this to maintain mappings externally, to make it easier
|
|
|
|
// to update/adjust to local circumstances.
|
|
|
|
void IncludePicker::AddMappingsFromFile(const string& filename) {
|
|
|
|
OwningPtr<MemoryBuffer> buffer;
|
2012-11-25 21:26:06 +00:00
|
|
|
error_code error = TryReadMappingFile(filename, buffer);
|
|
|
|
if (error) {
|
2012-10-14 23:39:30 +01:00
|
|
|
errs() << "Cannot open mapping file '" << filename << "': "
|
2012-11-25 21:26:06 +00:00
|
|
|
<< error.message() << ".\n";
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceMgr source_manager;
|
2012-12-02 14:11:05 +00:00
|
|
|
Stream json_stream(buffer.take(), source_manager);
|
2012-10-14 23:39:30 +01:00
|
|
|
|
|
|
|
document_iterator stream_begin = json_stream.begin();
|
|
|
|
if (stream_begin == json_stream.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get root sequence.
|
|
|
|
Node* root = stream_begin->getRoot();
|
|
|
|
SequenceNode *array = llvm::dyn_cast<SequenceNode>(root);
|
|
|
|
if (array == NULL) {
|
2012-12-02 14:11:05 +00:00
|
|
|
json_stream.printError(root, "Root element must be an array.");
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (SequenceNode::iterator it = array->begin(); it != array->end(); ++it) {
|
|
|
|
Node& current_node = *it;
|
|
|
|
|
|
|
|
// Every item must be a JSON object ("mapping" in YAML terms.)
|
|
|
|
MappingNode* mapping = llvm::dyn_cast<MappingNode>(¤t_node);
|
|
|
|
if (mapping == NULL) {
|
2012-12-02 14:11:05 +00:00
|
|
|
json_stream.printError(¤t_node,
|
|
|
|
"Mapping directives must be objects.");
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (MappingNode::iterator it = mapping->begin();
|
|
|
|
it != mapping->end(); ++it) {
|
|
|
|
// General form is { directive: <data> }.
|
|
|
|
const string directive = GetScalarValue(it->getKey());
|
|
|
|
|
|
|
|
if (directive == "symbol") {
|
|
|
|
// Symbol mapping.
|
|
|
|
vector<string> mapping = GetSequenceValue(it->getValue());
|
|
|
|
if (mapping.size() != 4) {
|
2012-12-02 14:11:05 +00:00
|
|
|
json_stream.printError(¤t_node,
|
2012-10-14 23:39:30 +01:00
|
|
|
"Symbol mapping expects a value on the form "
|
2012-12-02 14:11:05 +00:00
|
|
|
"'[from, visibility, to, visibility]'.");
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Visibility from_visibility = ParseVisibility(mapping[1]);
|
|
|
|
if (from_visibility == kUnusedVisibility) {
|
2012-12-02 14:11:05 +00:00
|
|
|
json_stream.printError(¤t_node,
|
|
|
|
"Unknown visibility '" + mapping[1] + "'.");
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Visibility to_visibility = ParseVisibility(mapping[3]);
|
|
|
|
if (to_visibility == kUnusedVisibility) {
|
2012-12-02 14:11:05 +00:00
|
|
|
json_stream.printError(¤t_node,
|
|
|
|
"Unknown visibility '" + mapping[3] + "'.");
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddSymbolMapping(
|
|
|
|
mapping[0],
|
|
|
|
from_visibility,
|
|
|
|
mapping[2],
|
|
|
|
to_visibility);
|
|
|
|
} else if (directive == "include") {
|
|
|
|
// Include mapping.
|
|
|
|
vector<string> mapping = GetSequenceValue(it->getValue());
|
|
|
|
if (mapping.size() != 4) {
|
2012-12-02 14:11:05 +00:00
|
|
|
json_stream.printError(¤t_node,
|
2012-10-14 23:39:30 +01:00
|
|
|
"Include mapping expects a value on the form "
|
2012-12-02 14:11:05 +00:00
|
|
|
"'[from, visibility, to, visibility]'.");
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Visibility from_visibility = ParseVisibility(mapping[1]);
|
|
|
|
if (from_visibility == kUnusedVisibility) {
|
2012-12-02 14:11:05 +00:00
|
|
|
json_stream.printError(¤t_node,
|
|
|
|
"Unknown visibility '" + mapping[1] + "'.");
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Visibility to_visibility = ParseVisibility(mapping[3]);
|
|
|
|
if (to_visibility == kUnusedVisibility) {
|
2012-12-02 14:11:05 +00:00
|
|
|
json_stream.printError(¤t_node,
|
|
|
|
"Unknown visibility '" + mapping[3] + "'.");
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddIncludeMapping(
|
|
|
|
mapping[0],
|
|
|
|
from_visibility,
|
|
|
|
mapping[2],
|
|
|
|
to_visibility);
|
|
|
|
} else if (directive == "ref") {
|
2012-11-25 21:26:06 +00:00
|
|
|
// Mapping ref.
|
2012-10-14 23:39:30 +01:00
|
|
|
string ref_file = GetScalarValue(it->getValue());
|
|
|
|
if (ref_file.empty()) {
|
2012-12-02 14:11:05 +00:00
|
|
|
json_stream.printError(¤t_node,
|
|
|
|
"Mapping ref expects a single filename value.");
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-25 21:26:06 +00:00
|
|
|
// Add the path of the file we're currently processing
|
|
|
|
// to the search path. Allows refs to be relative to referrer.
|
|
|
|
AddMappingFileSearchPath(GetParentPath(filename));
|
|
|
|
|
|
|
|
// Recurse.
|
2012-10-14 23:39:30 +01:00
|
|
|
AddMappingsFromFile(ref_file);
|
|
|
|
} else {
|
2012-12-02 14:11:05 +00:00
|
|
|
json_stream.printError(¤t_node,
|
|
|
|
"Unknown directive '" + directive + "'.");
|
2012-10-14 23:39:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IncludePicker::Visibility IncludePicker::ParseVisibility(
|
|
|
|
const string& visibility) const {
|
|
|
|
if (visibility == "private")
|
|
|
|
return kPrivate;
|
|
|
|
else if (visibility == "public")
|
|
|
|
return kPublic;
|
|
|
|
|
|
|
|
return kUnusedVisibility;
|
|
|
|
}
|
|
|
|
|
2011-05-24 00:07:27 +01:00
|
|
|
IncludePicker::Visibility IncludePicker::GetVisibility(
|
|
|
|
const string& quoted_include) const {
|
|
|
|
return GetOrDefault(
|
|
|
|
filepath_visibility_map_, quoted_include, kUnusedVisibility);
|
|
|
|
}
|
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
} // namespace include_what_you_use
|