blob: ca591bd4a1b1c17b89ddc6ce6219c8a41ca88265 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
adonovanec1cdc92020-08-07 08:15:51 -070014package com.google.devtools.build.lib.analysis;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010015
tomlua155b532017-11-08 20:12:47 +010016import com.google.common.base.Preconditions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import com.google.common.collect.ImmutableList;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import java.util.ArrayList;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019import java.util.Collection;
Ulf Adams07dba942015-03-05 14:47:37 +000020import java.util.Collections;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021import java.util.List;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022import javax.annotation.Nullable;
23
24/**
25 * Checked exception for analysis-time errors, which can store the errors for later reporting.
26 *
27 * <p>It's more robust for a method to throw this exception than expecting a
28 * {@link RuleErrorConsumer} object (which may be null).
29 */
30public final class AnalysisIssues extends Exception {
31
32 /**
33 * An error entry.
34 *
35 * <p>{@link AnalysisIssues} can accumulate multiple of these, and report all of them at once.
36 */
37 public static final class Entry {
38 private final String attribute;
39 private final String messageTemplate;
40 private final Object[] arguments;
41
42 private Entry(@Nullable String attribute, String messageTemplate, Object... arguments) {
43 this.attribute = attribute;
44 this.messageTemplate = messageTemplate;
45 this.arguments = arguments;
46 }
47
48 private void reportTo(RuleErrorConsumer errors) {
49 String msg = String.format(messageTemplate, arguments);
50 if (attribute == null) {
51 errors.ruleError(msg);
52 } else {
53 errors.attributeError(attribute, msg);
54 }
55 }
56
Laszlo Csomorfbdfa612016-01-22 10:03:19 +000057 private void reportTo(StringBuilder sb) {
58 String msg = String.format(messageTemplate, arguments);
59 if (attribute == null) {
60 sb.append("ERROR: ").append(msg);
61 } else {
62 sb.append("ERROR: in attribute \"").append(attribute).append("\": ").append(msg);
63 }
64 }
65
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010066 @Override
67 public String toString() {
68 if (attribute == null) {
69 return String.format("ERROR: " + messageTemplate, arguments);
70 } else {
71 List<Object> args = new ArrayList<>();
72 args.add(attribute);
Ulf Adams07dba942015-03-05 14:47:37 +000073 Collections.addAll(args, arguments);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010074 return String.format("ERROR in '%s': " + messageTemplate, args.toArray());
75 }
76 }
77 }
78
79 private final ImmutableList<Entry> entries;
80
81 public AnalysisIssues(Entry entry) {
82 this.entries = ImmutableList.of(Preconditions.checkNotNull(entry));
83 }
84
85 public AnalysisIssues(Collection<Entry> entries) {
86 this.entries = ImmutableList.copyOf(Preconditions.checkNotNull(entries));
87 }
88
89 /**
90 * Creates a attribute error entry that will be added to a {@link AnalysisIssues} later.
91 */
92 public static Entry attributeError(String attribute, String messageTemplate,
93 Object... arguments) {
94 return new Entry(attribute, messageTemplate, arguments);
95 }
96
97 public static Entry ruleError(String messageTemplate, Object... arguments) {
98 return new Entry(null, messageTemplate, arguments);
99 }
100
101 /**
102 * Report all accumulated errors and warnings to the given consumer object.
103 */
104 public void reportTo(RuleErrorConsumer errors) {
105 Preconditions.checkNotNull(errors);
106 for (Entry e : entries) {
107 e.reportTo(errors);
108 }
109 }
110
Laszlo Csomorfbdfa612016-01-22 10:03:19 +0000111 @Nullable
112 private String asString() {
113 if (entries == null) {
114 return null;
115 }
116
117 StringBuilder sb = new StringBuilder();
118 for (Entry e : entries) {
119 e.reportTo(sb);
120 }
121 return sb.toString();
122 }
123
124 @Override
125 public String getMessage() {
126 return asString();
127 }
128
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100129 @Override
130 public String toString() {
Laszlo Csomorfbdfa612016-01-22 10:03:19 +0000131 String s = asString();
132 return s == null ? "" : s;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100133 }
134}