aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/actions/ActionGraphVisitor.java
blob: 4ddda4c02783ed675f23b577f3128a08af375384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.actions;

/**
 * An abstract visitor for the action graph.  Specializes {@link BipartiteVisitor} for artifacts and
 * actions, and takes care of visiting the complete transitive closure.
 */
public abstract class ActionGraphVisitor extends BipartiteVisitor<Action, Artifact> {

  private final ActionGraph actionGraph;

  public ActionGraphVisitor(ActionGraph actionGraph) {
    this.actionGraph = actionGraph;
  }

  /**
   * Called for all artifacts in the visitation.  Hook for subclasses. 
   *
   * @param artifact
   */
  protected void visitArtifact(Artifact artifact) {}

  /**
   * Called for all actions in the visitation.  Hook for subclasses.
   *
   * @param action
   */
  protected void visitAction(Action action) {}

  /**
   * Whether the given action should be visited. If this returns false, the visitation stops here,
   * so the dependencies of this action are also not visited.
   *
   * @param action  
   */
  protected boolean shouldVisit(Action action) {
    return true;
  }

  /**
   * Whether the given artifact should be visited. If this returns false, the visitation stops here,
   * so dependencies of this artifact (if it is a generated one) are also not visited.
   *
   * @param artifact
   */
  protected boolean shouldVisit(Artifact artifact) {
    return true;
  }

  @SuppressWarnings("unused")
  protected final void visitArtifacts(Iterable<Artifact> artifacts) {
    for (Artifact artifact : artifacts) {
      visitArtifact(artifact);
    }
  }

  @Override protected void white(Artifact artifact) {
    Action action = actionGraph.getGeneratingAction(artifact);
    visitArtifact(artifact);
    if (action != null && shouldVisit(action)) {
      visitBlackNode(action);
    }
  }

  @Override protected void black(Action action) {
    visitAction(action);
    for (Artifact input : action.getInputs()) {
      if (shouldVisit(input)) {
        visitWhiteNode(input);
      }
    }
  }
}