aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/analysis/EventHandlingErrorReporter.java
blob: 9257a750464abd0d7302fc757fadd96782e7d39f (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2018 The Bazel Authors. 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.analysis;

import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.packages.AbstractRuleErrorConsumer;
import com.google.devtools.build.lib.packages.Attribute;

/**
 * Base class for implementations of {@link
 * com.google.devtools.build.lib.packages.RuleErrorConsumer}.
 *
 * <p>Do not create new implementations of this class - instead, use {@link RuleContext} in Native
 * rule definitions, and {@link com.google.devtools.build.lib.analysis.skylark.SkylarkErrorReporter}
 * in Skylark API definitions. For use in testing, extend {@link AbstractRuleErrorConsumer} instead.
 */
public abstract class EventHandlingErrorReporter extends AbstractRuleErrorConsumer {
  private final String ruleClassNameForLogging;
  private final AnalysisEnvironment env;

  protected EventHandlingErrorReporter(String ruleClassNameForLogging, AnalysisEnvironment env) {
    this.ruleClassNameForLogging = ruleClassNameForLogging;
    this.env = env;
  }

  public void reportError(Location location, String message) {
    env.getEventHandler().handle(Event.error(location, message));
  }

  @Override
  public void ruleError(String message) {
    reportError(getRuleLocation(), prefixRuleMessage(message));
  }

  @Override
  public void attributeError(String attrName, String message) {
    reportError(getAttributeLocation(attrName), completeAttributeMessage(attrName, message));
  }

  @Override
  public boolean hasErrors() {
    return env.hasErrors();
  }

  public void reportWarning(Location location, String message) {
    env.getEventHandler().handle(Event.warn(location, message));
  }

  @Override
  public void ruleWarning(String message) {
    env.getEventHandler().handle(Event.warn(getRuleLocation(), prefixRuleMessage(message)));
  }

  @Override
  public void attributeWarning(String attrName, String message) {
    reportWarning(getAttributeLocation(attrName), completeAttributeMessage(attrName, message));
  }

  private String prefixRuleMessage(String message) {
    return String.format("in %s rule %s: %s", ruleClassNameForLogging, getLabel(), message);
  }

  private String maskInternalAttributeNames(String name) {
    return Attribute.isImplicit(name) ? "(an implicit dependency)" : name;
  }

  /**
   * Prefixes the given message with details about the rule and appends details about the macro that
   * created this rule, if applicable.
   */
  private String completeAttributeMessage(String attrName, String message) {
    // Appends a note to the given message if the offending rule was created by a macro.

    return String.format(
        "in %s attribute of %s rule %s: %s%s",
        maskInternalAttributeNames(attrName),
        ruleClassNameForLogging,
        getLabel(),
        message,
        getMacroMessageAppendix(attrName));
  }

  /** Returns a string describing the macro that created this rule, or an empty string. */
  protected abstract String getMacroMessageAppendix(String attrName);

  protected abstract Label getLabel();

  protected abstract Location getRuleLocation();

  protected abstract Location getAttributeLocation(String attrName);
}