File: errorprone.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (284 lines) | stat: -rwxr-xr-x 10,631 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python3
#
# Copyright 2025 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Run Error Prone."""

import argparse
import sys

import compile_java
from util import server_utils

# Add a check here to cause the suggested fix to be applied while compiling.
# Use this when trying to enable more checks.
# BE SURE TO BUILD WITH --offline
ERRORPRONE_CHECKS_TO_APPLY = []

# Checks to disable in tests.
TESTONLY_ERRORPRONE_WARNINGS_TO_DISABLE = [
    # Can hurt readability to enforce this on test classes.
    'FieldCanBeStatic',
    # These are allowed in tests.
    'NoStreams',
    # Too much effort to enable.
    'UnusedVariable',
]

# Full list of checks: https://errorprone.info/bugpatterns
ERRORPRONE_WARNINGS_TO_DISABLE = [
    'InlineMeInliner',
    'InlineMeSuggester',
    # High priority to enable:
    'HidingField',
    'AlreadyChecked',
    'DirectInvocationOnMock',
    'MockNotUsedInProduction',
    'PatternMatchingInstanceof',
    'AssignmentExpression',
    'RuleNotRun',
    # High priority to enable in non-tests:
    'JdkObsolete',
    'ReturnValueIgnored',
    'StaticAssignmentInConstructor',
    # These are all for Javadoc, which we don't really care about.
    # vvv
    'InvalidBlockTag',
    'InvalidParam',
    'InvalidLink',
    'InvalidInlineTag',
    'MalformedInlineTag',
    'MissingSummary',
    'NotJavadoc',
    'UnescapedEntity',
    'UnrecognisedJavadocTag',
    # ^^^
    'MutablePublicArray',
    'NonCanonicalType',
    'DoNotClaimAnnotations',
    'JavaUtilDate',
    'IdentityHashMapUsage',
    'StaticMockMember',
    # Triggers in tests where this is useful to do.
    'StaticAssignmentOfThrowable',
    # TODO(crbug.com/41384349): Follow steps in bug.
    'CatchAndPrintStackTrace',
    # TODO(crbug.com/41364806): Follow steps in bug.
    'TypeParameterUnusedInFormals',
    # Android platform default is always UTF-8.
    # https://developer.android.com/reference/java/nio/charset/Charset.html#defaultCharset()
    'DefaultCharset',
    # There are lots of times when we just want to post a task.
    'FutureReturnValueIgnored',
    # Just false positives in our code.
    'ThreadJoinLoop',
    # Low priority corner cases with String.split.
    # Linking Guava and using Splitter was rejected
    # in the https://chromium-review.googlesource.com/c/chromium/src/+/871630.
    'StringSplitter',
    # Preferred to use another method since it propagates exceptions better.
    'ClassNewInstance',
    # Results in false positives.
    'ThreadLocalUsage',
    # Low priority.
    'EqualsHashCode',
    # Not necessary for tests.
    'OverrideThrowableToString',
    # Not that useful.
    'UnsafeReflectiveConstructionCast',
    # Not that useful.
    'MixedMutabilityReturnType',
    # Nice to have.
    'EqualsGetClass',
    # A lot of false-positives from CharSequence.equals().
    'UndefinedEquals',
    # Dagger generated code triggers this.
    'SameNameButDifferent',
    # Does not apply to Android because it assumes no desugaring.
    'UnnecessaryLambda',
    # Nice to have.
    'EmptyCatch',
    # Nice to have.
    'BadImport',
    # Nice to have.
    'UseCorrectAssertInTests',
    # Must be off since we are now passing in annotation processor generated
    # code as a source jar (deduplicating work with turbine).
    'RefersToDaggerCodegen',
    # We already have presubmit checks for this. We don't want it to fail
    # local compiles.
    'RemoveUnusedImports',
    # Only has false positives (would not want to enable this).
    'UnicodeEscape',
    # A lot of existing violations. e.g. Should return List and not ArrayList
    'NonApiType',
    # Nice to have.
    'StringCharset',
    # Nice to have.
    'StringConcatToTextBlock',
    # Nice to have.
    'StringCaseLocaleUsage',
    # Low priority.
    'RedundantControlFlow',
    # Low priority.
    'StatementSwitchToExpressionSwitch',
    # Assigning to fields marked as @Mock or @Spy. Suggested fix is to delete
    # assignments, which would break tests in many cases.
    'UnnecessaryAssignment',
]

# Full list of checks: https://errorprone.info/bugpatterns
# Only those marked as "experimental" need to be listed here in order to be
# enabled.
ERRORPRONE_WARNINGS_TO_ENABLE = [
    'BinderIdentityRestoredDangerously',
    'EmptyIf',
    'EqualsBrokenForNull',
    'FieldCanBeFinal',
    'FieldCanBeLocal',
    'FieldCanBeStatic',
    'InvalidThrows',
    'LongLiteralLowerCaseSuffix',
    'MultiVariableDeclaration',
    'RedundantOverride',
    'StaticQualifiedUsingExpression',
    'TimeUnitMismatch',
    'UnnecessaryStaticImport',
    'UseBinds',
    'WildcardImport',
    'NoStreams',
]


def main():
  parser = argparse.ArgumentParser()
  parser.add_argument('--skip-build-server',
                      action='store_true',
                      help='Avoid using the build server.')
  parser.add_argument('--use-build-server',
                      action='store_true',
                      help='Always use the build server.')
  parser.add_argument('--testonly',
                      action='store_true',
                      help='Disable some Error Prone checks')
  parser.add_argument('--enable-nullaway',
                      action='store_true',
                      help='Enable NullAway (requires --enable-errorprone)')
  parser.add_argument('--stamp',
                      required=True,
                      help='Path of output .stamp file')
  options, compile_java_argv = parser.parse_known_args()

  compile_java_argv += ['--jar-path', options.stamp]

  # Use the build server for errorprone runs.
  if not options.skip_build_server and (server_utils.MaybeRunCommand(
      name=options.stamp,
      argv=sys.argv,
      stamp_file=options.stamp,
      use_build_server=options.use_build_server)):
    compile_java.main(compile_java_argv, write_depfile_only=True)
    return

  # All errorprone args are passed space-separated in a single arg.
  errorprone_flags = ['-Xplugin:ErrorProne']

  if options.enable_nullaway:
    # See: https://github.com/uber/NullAway/wiki/Configuration
    # Check nullability only for classes marked with @NullMarked (this is our
    # migration story).
    errorprone_flags += ['-XepOpt:NullAway:OnlyNullMarked']
    errorprone_flags += [
        '-XepOpt:NullAway:CustomContractAnnotations='
        'org.chromium.build.annotations.Contract,'
        'org.chromium.support_lib_boundary.util.Contract'
    ]
    # TODO(agrieve): Re-enable once this is fixed:
    #     https://github.com/uber/NullAway/issues/1104
    # errorprone_flags += ['-XepOpt:NullAway:CheckContracts=true']

    # Make it a warning to use assumeNonNull() with a @NonNull.
    errorprone_flags += [('-XepOpt:NullAway:CastToNonNullMethod='
                          'org.chromium.build.NullUtil.assumeNonNull')]
    # Detect "assert foo != null" as a null check.
    errorprone_flags += ['-XepOpt:NullAway:AssertsEnabled=true']
    # Do not ignore @Nullable & @NonNull in non-@NullMarked classes.
    errorprone_flags += [
        '-XepOpt:NullAway:AcknowledgeRestrictiveAnnotations=true'
    ]
    # Treat @RecentlyNullable the same as @Nullable.
    errorprone_flags += ['-XepOpt:Nullaway:AcknowledgeAndroidRecent=true']
    # Enable experimental checking of @Nullable generics.
    # https://github.com/uber/NullAway/wiki/JSpecify-Support
    errorprone_flags += ['-XepOpt:NullAway:JSpecifyMode=true']
    # Treat these the same as constructors.
    # These are in addition to the default list in "DEFAULT_KNOWN_INITIALIZERS":
    # https://github.com/uber/NullAway/blob/d5cb4f1190a96045d85b92c6d119e4595840cc8a/nullaway/src/main/java/com/uber/nullaway/ErrorProneCLIFlagsConfig.java#L128
    init_methods = [
        'android.app.backup.BackupAgent.onCreate',
        'android.content.ContentProvider.attachInfo',
        'android.content.ContentProvider.onCreate',
        'android.content.ContextWrapper.attachBaseContext',
        'androidx.preference.PreferenceFragmentCompat.onCreatePreferences',
    ]
    errorprone_flags += [
        '-XepOpt:NullAway:KnownInitializers=' + ','.join(init_methods)
    ]

  # Make everything a warning so that when treat_warnings_as_errors is false,
  # they do not fail the build.
  errorprone_flags += ['-XepAllErrorsAsWarnings']
  # Don't check generated files (those tagged with @Generated).
  errorprone_flags += ['-XepDisableWarningsInGeneratedCode']
  errorprone_flags.extend('-Xep:{}:OFF'.format(x)
                          for x in ERRORPRONE_WARNINGS_TO_DISABLE)
  errorprone_flags.extend('-Xep:{}:WARN'.format(x)
                          for x in ERRORPRONE_WARNINGS_TO_ENABLE)
  if options.testonly:
    errorprone_flags.extend('-Xep:{}:OFF'.format(x)
                            for x in TESTONLY_ERRORPRONE_WARNINGS_TO_DISABLE)

  if ERRORPRONE_CHECKS_TO_APPLY:
    to_apply = list(ERRORPRONE_CHECKS_TO_APPLY)
    if options.testonly:
      to_apply = [
          x for x in to_apply
          if x not in TESTONLY_ERRORPRONE_WARNINGS_TO_DISABLE
      ]
    errorprone_flags += [
        '-XepPatchLocation:IN_PLACE', '-XepPatchChecks:,' + ','.join(to_apply)
    ]

  # These are required to use JDK 16, and are taken directly from
  # https://errorprone.info/docs/installation
  javac_args = [
      '-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED',
      '-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED',
      '-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED',
      '-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED',
      '-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
      '-J--add-exports=jdk.compiler/com.sun.tools.javac.processing='
      'ALL-UNNAMED',
      '-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
      '-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
      '-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
      '-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED',
  ]

  javac_args += ['-XDcompilePolicy=simple', ' '.join(errorprone_flags)]

  javac_args += ['-XDshould-stop.ifError=FLOW']
  # This flag quits errorprone after checks and before code generation, since
  # we do not need errorprone outputs, this speeds up errorprone by 4 seconds
  # for chrome_java.
  if not ERRORPRONE_CHECKS_TO_APPLY:
    javac_args += ['-XDshould-stop.ifNoError=FLOW']

  compile_java.main(compile_java_argv,
                    extra_javac_args=javac_args,
                    use_errorprone=True)


if __name__ == '__main__':
  main()