aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/JavacOptions.java
blob: 9e6494c10debb9cb4fd71d406193ba6493bbc412 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2014 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.buildjar.javac;

import com.google.auto.value.AutoValue;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Preprocess javac -Xlint options. We also need to make the different versions of javac treat
 * -Xlint options uniformly.
 *
 * <p>Some versions of javac now process the -Xlint options without allowing later options to
 * override earlier ones on the command line. For example, {@code -Xlint:All -Xlint:None} results in
 * all warnings being enabled.
 *
 * <p>This class preprocesses the -Xlint options within the javac options to achieve a command line
 * that is sensitive to ordering. That is, with this preprocessing step, {@code -Xlint:all
 * -Xlint:none} results in no warnings being enabled.
 */
public final class JavacOptions {

  /** Returns an immutable list containing all the non-Bazel specific Javac flags. */
  public static ImmutableList<String> removeBazelSpecificFlags(String[] javacopts) {
    return removeBazelSpecificFlags(Arrays.asList(javacopts));
  }

  /** Returns an immutable list containing all the non-Bazel specific Javac flags. */
  public static ImmutableList<String> removeBazelSpecificFlags(Iterable<String> javacopts) {
    return filterJavacopts(javacopts).standardJavacopts();
  }

  /** A collection of javac flags, divided into Bazel-specific and standard options. */
  @AutoValue
  public abstract static class FilteredJavacopts {
    /** Bazel-specific javac flags, e.g. Error Prone's -Xep: flags. */
    public abstract ImmutableList<String> bazelJavacopts();

    /** Standard javac flags. */
    public abstract ImmutableList<String> standardJavacopts();

    /** Creates a {@link FilteredJavacopts}. */
    public static FilteredJavacopts create(
        ImmutableList<String> bazelJavacopts, ImmutableList<String> standardJavacopts) {
      return new AutoValue_JavacOptions_FilteredJavacopts(bazelJavacopts, standardJavacopts);
    }
  }

  /** Filters a list of javac flags into Bazel-specific and standard flags. */
  public static FilteredJavacopts filterJavacopts(Iterable<String> javacopts) {
    ImmutableList.Builder<String> bazelJavacopts = ImmutableList.builder();
    ImmutableList.Builder<String> standardJavacopts = ImmutableList.builder();
    for (String opt : javacopts) {
      if (isBazelSpecificFlag(opt)) {
        bazelJavacopts.add(opt);
      } else {
        standardJavacopts.add(opt);
      }
    }
    return FilteredJavacopts.create(bazelJavacopts.build(), standardJavacopts.build());
  }

  private static boolean isBazelSpecificFlag(String opt) {
    return opt.startsWith("-Werror:")
        || opt.startsWith("-Xep")
        // TODO(b/36228287): delete this once the migration to -XepDisableAllChecks is complete
        || opt.equals("-extra_checks")
        || opt.startsWith("-extra_checks:");
  }

  /**
   * Interface to define an option normalizer. For instance, to group all -Xlint: option into one
   * place.
   *
   * <p>All normalizers used by the JavacOptions class will be started by calling the {@link
   * #start()} method when starting the parsing of a list of option. For each option, the first
   * option normalized whose {@link #processOption(String)} method returns true stops its parsing
   * and the option is supposed to be added at the end to the normalized list of option with the
   * {@link #normalize(List)} method. Options not handled by a normalizer will be returned as such
   * in the normalized option list.
   */
  public static interface JavacOptionNormalizer {
    /**
     * Process an option and return true if the option was handled by this normalizer. {@code
     * remaining} provides an iterator to any remaining options so normalizers that process
     * non-nullary options can also process the options' arguments.
     */
    boolean processOption(String option, Iterator<String> remaining);

    /**
     * Add the normalized versions of the options handled by {@link #processOption(String)} to the
     * {@code normalized} list
     */
    void normalize(List<String> normalized);
  }

  /**
   * Parse an option that starts with {@code -Xlint:} into a bunch of xlintopts. We silently drop
   * xlintopts that would disable any warnings that we turn into errors by default (treating them
   * like invalid xlintopts). It also parse -nowarn option as -Xlint:none.
   */
  public static final class XlintOptionNormalizer implements JavacOptionNormalizer {

    private static final Joiner COMMA_MINUS_JOINER = Joiner.on(",-");
    private static final Joiner COMMA_JOINER = Joiner.on(",");

    /**
     * This type models a starting selection from which lint options can be added or removed. E.g.,
     * {@code -Xlint} indicates we start with the set of recommended checks enabled, and {@code
     * -Xlint:none} means we start without any checks enabled.
     */
    private static enum BasisXlintSelection {
      /** {@code -Xlint:none} */
      None,
      /** {@code -Xlint:all} */
      All,
      /** {@code -Xlint} */
      Recommended,
      /** Nothing specified; default} */
      Empty
    }

    private final ImmutableList<String> enforcedXlints;
    private final Set<String> xlintPlus;
    private final Set<String> xlintMinus = new LinkedHashSet<>();
    private BasisXlintSelection xlintBasis = BasisXlintSelection.Empty;

    public XlintOptionNormalizer() {
      this(ImmutableList.<String>of());
    }

    public XlintOptionNormalizer(ImmutableList<String> enforcedXlints) {
      this.enforcedXlints = enforcedXlints;
      xlintPlus = new LinkedHashSet<>(enforcedXlints);
      resetBasisTo(BasisXlintSelection.Empty);
    }

    @Override
    public boolean processOption(String option, Iterator<String> remaining) {
      if (option.equals("-nowarn")) {
        // It is equivalent to -Xlint:none
        resetBasisTo(BasisXlintSelection.None);
        return true;
      } else if (option.equals("-Xlint")) {
        resetBasisTo(BasisXlintSelection.Recommended);
        return true;
      } else if (option.startsWith("-Xlint")) {
        for (String arg : option.substring("-Xlint:".length()).split(",", -1)) {
          if (arg.equals("all") || arg.isEmpty()) {
            resetBasisTo(BasisXlintSelection.All);
          } else if (arg.equals("none")) {
            resetBasisTo(BasisXlintSelection.None);
          } else if (arg.startsWith("-")) {
            arg = arg.substring("-".length());
            if (!enforcedXlints.contains(arg)) {
              xlintPlus.remove(arg);
              if (xlintBasis != BasisXlintSelection.None) {
                xlintMinus.add(arg);
              }
            }
          } else { // not a '-' prefix
            xlintMinus.remove(arg);
            if (xlintBasis != BasisXlintSelection.All) {
              xlintPlus.add(arg);
            }
          }
        }
        return true;
      }
      return false;
    }

    @Override
    public void normalize(List<String> normalized) {
      switch (xlintBasis) {
        case Recommended:
          normalized.add("-Xlint");
          break;
        case All:
          normalized.add("-Xlint:all");
          break;
        case None:
          if (xlintPlus.isEmpty()) {
            /*
             * This should never happen with warnings as errors. The plus set should always contain
             * at least the warnings in warningsAsErrors.
             */
            normalized.add("-Xlint:none");
          }
          break;
        default:
          break;
      }
      if (xlintBasis != BasisXlintSelection.All && !xlintPlus.isEmpty()) {
        normalized.add("-Xlint:" + COMMA_JOINER.join(xlintPlus));
      }
      if (xlintBasis != BasisXlintSelection.None && !xlintMinus.isEmpty()) {
        normalized.add("-Xlint:-" + COMMA_MINUS_JOINER.join(xlintMinus));
      }
    }

    private void resetBasisTo(BasisXlintSelection selection) {
      xlintBasis = selection;
      xlintPlus.clear();
      xlintMinus.clear();
      if (selection != BasisXlintSelection.All) {
        xlintPlus.addAll(enforcedXlints);
      }
    }
  }

  /**
   * Normalizer for {@code -source}, {@code -target}, and {@code --release} options. If both {@code
   * -source} and {@code --release} are specified, {@code --release} wins.
   */
  public static class ReleaseOptionNormalizer implements JavacOptionNormalizer {

    private String source;
    private String target;
    private String release;

    @Override
    public boolean processOption(String option, Iterator<String> remaining) {
      switch (option) {
        case "-source":
          if (remaining.hasNext()) {
            source = remaining.next();
            release = null;
          }
          return true;
        case "-target":
          if (remaining.hasNext()) {
            target = remaining.next();
            release = null;
          }
          return true;
        case "--release":
          if (remaining.hasNext()) {
            release = remaining.next();
            source = null;
            target = null;
          }
          return true;
        default: // fall out
      }
      if (option.startsWith("--release=")) {
        release = option.substring("--release=".length());
        source = null;
        target = null;
        return true;
      }
      return false;
    }

    @Override
    public void normalize(List<String> normalized) {
      if (release != null) {
        normalized.add("--release");
        normalized.add(release);
      } else {
        if (source != null) {
          normalized.add("-source");
          normalized.add(source);
        }
        if (target != null) {
          normalized.add("-target");
          normalized.add(target);
        }
      }
    }
  }

  /**
   * Outputs a reasonably normalized javac option list.
   *
   * @param javacopts the raw javac option list to cleanup
   * @param normalizers the list of normalizers to apply
   * @return a new cleaned up javac option list
   */
  public static List<String> normalizeOptionsWithNormalizers(
      List<String> javacopts, JavacOptionNormalizer... normalizers) {
    List<String> normalized = new ArrayList<>();

    Iterator<String> it = javacopts.iterator();
    while (it.hasNext()) {
      String opt = it.next();
      boolean found = false;
      for (JavacOptionNormalizer normalizer : normalizers) {
        if (normalizer.processOption(opt, it)) {
          found = true;
          break;
        }
      }
      if (!found) {
        normalized.add(opt);
      }
    }

    for (JavacOptionNormalizer normalizer : normalizers) {
      normalizer.normalize(normalized);
    }
    return normalized;
  }

  /**
   * A wrapper around {@ref #normalizeOptionsWithNormalizers(List, JavacOptionNormalizer...)} to use
   * {@link XlintOptionNormalizer} as default normalizer.
   *
   * <p>The -Xlint option list has up to one each of a -Xlint* basis flag followed by a
   * -Xlint:xxx,yyy,zzz add flag followed by a -Xlint:-xxx,-yyy,-zzz minus flag.
   */
  public static List<String> normalizeOptions(List<String> javacopts) {
    return normalizeOptionsWithNormalizers(
        javacopts, new XlintOptionNormalizer(), new ReleaseOptionNormalizer());
  }
}