Ninja
command_collector.h
Go to the documentation of this file.
1 // Copyright 2024 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NINJA_COMMAND_COLLECTOR_H_
16 #define NINJA_COMMAND_COLLECTOR_H_
17 
18 #include <cassert>
19 #include <unordered_set>
20 #include <vector>
21 
22 #include "graph.h"
23 
24 /// Collects the transitive set of edges that lead into a given set
25 /// of starting nodes. Used to implement the `compdb-targets` tool.
26 ///
27 /// When collecting inputs, the outputs of phony edges are always ignored
28 /// from the result, but are followed by the dependency walk.
29 ///
30 /// Usage is:
31 /// - Create instance.
32 /// - Call CollectFrom() for each root node to collect edges from.
33 /// - Call TakeResult() to retrieve the list of edges.
34 ///
36  void CollectFrom(const Node* node) {
37  assert(node);
38 
39  if (!visited_nodes_.insert(node).second)
40  return;
41 
42  Edge* edge = node->in_edge();
43  if (!edge || !visited_edges_.insert(edge).second)
44  return;
45 
46  for (Node* input_node : edge->inputs_)
47  CollectFrom(input_node);
48 
49  if (!edge->is_phony())
50  in_edges.push_back(edge);
51  }
52 
53  private:
54  std::unordered_set<const Node*> visited_nodes_;
55  std::unordered_set<Edge*> visited_edges_;
56 
57  /// we use a vector to preserve order from requisites to their dependents.
58  /// This may help LSP server performance in languages that support modules,
59  /// but it also ensures that the output of `-t compdb-targets foo` is
60  /// consistent, which is useful in regression tests.
61  public:
62  std::vector<Edge*> in_edges;
63 };
64 
65 #endif // NINJA_COMMAND_COLLECTOR_H_
Collects the transitive set of edges that lead into a given set of starting nodes.
std::unordered_set< const Node * > visited_nodes_
std::vector< Edge * > in_edges
we use a vector to preserve order from requisites to their dependents.
void CollectFrom(const Node *node)
std::unordered_set< Edge * > visited_edges_
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:175
bool is_phony() const
Definition: graph.cc:563
std::vector< Node * > inputs_
Definition: graph.h:216
Information about a node in the dependency graph: the file, whether it's dirty, mtime,...
Definition: graph.h:42
Edge * in_edge() const
Definition: graph.h:100