1*6777b538SAndroid Build Coastguard Worker# Static Analysis 2*6777b538SAndroid Build Coastguard Worker 3*6777b538SAndroid Build Coastguard WorkerWe use several tools for static analysis in chromium. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker[TOC] 6*6777b538SAndroid Build Coastguard Worker 7*6777b538SAndroid Build Coastguard Worker## [Android Lint](lint.md) 8*6777b538SAndroid Build Coastguard Worker* Runs as part of normal compilation. 9*6777b538SAndroid Build Coastguard Worker* Controlled by GN arg: `disable_android_lint` (or `android_static_analysis`). 10*6777b538SAndroid Build Coastguard Worker* [Useful checks include](https://googlesamples.github.io/android-custom-lint-rules/checks/index.md.html): 11*6777b538SAndroid Build Coastguard Worker * `NewApi` (ensureing `Build.VERSION.SDK_INT` checks are in place). 12*6777b538SAndroid Build Coastguard Worker* A list of disabled checks is found [within `lint.py`]. 13*6777b538SAndroid Build Coastguard Worker * and [`lint-baseline.xml`] files contain individual suppressions. 14*6777b538SAndroid Build Coastguard Worker* Custom lint checks [are possible], but we don't have any. 15*6777b538SAndroid Build Coastguard Worker* Checks run on the entire codebase, not only on changed lines. 16*6777b538SAndroid Build Coastguard Worker* Does not run when `chromium_code = false` (e.g. for `//third_party`). 17*6777b538SAndroid Build Coastguard Worker 18*6777b538SAndroid Build Coastguard Worker[are possible]: https://googlesamples.github.io/android-custom-lint-rules/api-guide.md.html 19*6777b538SAndroid Build Coastguard Worker[within `lint.py`]: https://source.chromium.org/chromium/chromium/src/+/main:build/android/gyp/lint.py;l=25 20*6777b538SAndroid Build Coastguard Worker[`lint-baseline.xml`]: https://source.chromium.org/search?q=f:lint-baseline.xml%20-f:third_party 21*6777b538SAndroid Build Coastguard Worker 22*6777b538SAndroid Build Coastguard Worker## [ErrorProne](https://errorprone.info/) 23*6777b538SAndroid Build Coastguard Worker* Runs as part of normal compilation. 24*6777b538SAndroid Build Coastguard Worker* Controlled by GN arg: `use_errorprone_java_compiler` (or 25*6777b538SAndroid Build Coastguard Worker `android_static_analysis`). 26*6777b538SAndroid Build Coastguard Worker* [Useful checks include](https://errorprone.info/bugpatterns): 27*6777b538SAndroid Build Coastguard Worker * Enforcement of `@GuardedBy`, `@CheckReturnValue`, and `@DoNotMock`. 28*6777b538SAndroid Build Coastguard Worker * Enforcement of `/* paramName= */` comments. 29*6777b538SAndroid Build Coastguard Worker* A list of enabled / disabled checks is found [within `compile_java.py`](https://cs.chromium.org/chromium/src/build/android/gyp/compile_java.py?l=30) 30*6777b538SAndroid Build Coastguard Worker * Many checks are currently disabled because there is work involved in fixing 31*6777b538SAndroid Build Coastguard Worker violations they introduce. Please help! 32*6777b538SAndroid Build Coastguard Worker* Chrome has [a few custom checks]: 33*6777b538SAndroid Build Coastguard Worker* Checks run on the entire codebase, not only on changed lines. 34*6777b538SAndroid Build Coastguard Worker* Does not run when `chromium_code = false` (e.g. for `//third_party`). 35*6777b538SAndroid Build Coastguard Worker 36*6777b538SAndroid Build Coastguard Worker[a few custom checks]: /tools/android/errorprone_plugin/src/org/chromium/tools/errorprone/plugin/ 37*6777b538SAndroid Build Coastguard Worker 38*6777b538SAndroid Build Coastguard Worker## [Checkstyle](https://checkstyle.sourceforge.io/) 39*6777b538SAndroid Build Coastguard Worker* Mainly used for checking Java formatting & style. 40*6777b538SAndroid Build Coastguard Worker * E.g.: Unused imports and naming conventions. 41*6777b538SAndroid Build Coastguard Worker* Allows custom checks to be added via XML. Here [is ours]. 42*6777b538SAndroid Build Coastguard Worker* Preferred over adding checks via `PRESUBMIT.py` because the tool understands 43*6777b538SAndroid Build Coastguard Worker `@SuppressWarnings` annotations. 44*6777b538SAndroid Build Coastguard Worker* Runs only on changed lines as a part of `PRESUBMIT.py`. 45*6777b538SAndroid Build Coastguard Worker 46*6777b538SAndroid Build Coastguard Worker[is ours]: /tools/android/checkstyle/chromium-style-5.0.xml 47*6777b538SAndroid Build Coastguard Worker 48*6777b538SAndroid Build Coastguard Worker## [PRESUBMIT.py](/PRESUBMIT.py): 49*6777b538SAndroid Build Coastguard Worker* Checks for banned patterns via `_BANNED_JAVA_FUNCTIONS`. 50*6777b538SAndroid Build Coastguard Worker * (These should likely be moved to checkstyle). 51*6777b538SAndroid Build Coastguard Worker* Checks for a random set of things in `ChecksAndroidSpecificOnUpload()`. 52*6777b538SAndroid Build Coastguard Worker * Including running Checkstyle. 53*6777b538SAndroid Build Coastguard Worker* Runs only on changed lines. 54*6777b538SAndroid Build Coastguard Worker 55*6777b538SAndroid Build Coastguard Worker## [Bytecode Processor](/build/android/bytecode/) 56*6777b538SAndroid Build Coastguard Worker* Runs as part of normal compilation. 57*6777b538SAndroid Build Coastguard Worker* Controlled by GN arg: `android_static_analysis`. 58*6777b538SAndroid Build Coastguard Worker* Performs a single check: 59*6777b538SAndroid Build Coastguard Worker * Enforces that targets do not rely on indirect dependencies to populate 60*6777b538SAndroid Build Coastguard Worker their classpath. 61*6777b538SAndroid Build Coastguard Worker * In other words: that `deps` are not missing any entries. 62