Use range-based for loops to iterate over file nodes.

I converted them to non-range-based loops in r226883 and r226893
because at that moment File::parse() may have side effects and
may update the vector that the reference returned from
LinkingContext::nodes().

Now File::parse() is free from side effects. We can use range-based
loops again.

llvm-svn: 231321
This commit is contained in:
Rui Ueyama 2015-03-05 00:07:38 +00:00
parent ab9b179f4f
commit 5d75b6346d
2 changed files with 4 additions and 8 deletions

View File

@ -77,11 +77,8 @@ bool Driver::link(LinkingContext &context, raw_ostream &diagnostics) {
if (context.getNodes().empty()) if (context.getNodes().empty())
return false; return false;
// File::parse may add items to the node list which may invalidate for (std::unique_ptr<Node> &ie : context.getNodes())
// existing iterators. Avoid using iterator to access elements. if (FileNode *node = dyn_cast<FileNode>(ie.get()))
std::vector<std::unique_ptr<Node>> &nodes = context.getNodes();
for (size_t i = 0; i < nodes.size(); ++i)
if (FileNode *node = dyn_cast<FileNode>(nodes[i].get()))
context.getTaskGroup().spawn([node] { node->getFile()->parse(); }); context.getTaskGroup().spawn([node] { node->getFile()->parse(); });
std::vector<std::unique_ptr<File>> internalFiles; std::vector<std::unique_ptr<File>> internalFiles;

View File

@ -805,9 +805,8 @@ parseArgs(int argc, const char **argv, PECOFFLinkingContext &ctx,
// graph. // graph.
static bool hasLibrary(PECOFFLinkingContext &ctx, File *file) { static bool hasLibrary(PECOFFLinkingContext &ctx, File *file) {
StringRef path = file->path(); StringRef path = file->path();
std::vector<std::unique_ptr<Node>> &nodes = ctx.getNodes(); for (std::unique_ptr<Node> &p : ctx.getNodes())
for (size_t i = 0; i < nodes.size(); ++i) if (auto *f = dyn_cast<FileNode>(p.get()))
if (auto *f = dyn_cast<FileNode>(nodes[i].get()))
if (f->getFile()->path() == path) if (f->getFile()->path() == path)
return true; return true;
return false; return false;