check-flagged-apis: add list subcommand
Add a new subcommand to list all flagged APIs and corresponding flags.
This provides an overview of what flagged APIs exist in the Android
tree.
Bug: 345207706
Test: build/tools/check-flagged-apis/check-flagged-apis.sh list
Test: atest check-flagged-apis-test
Flag: EXEMPT host side tool
Change-Id: Icc224f3787480353baabbd3946f36f003f35db59
diff --git a/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt b/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt
index 4696efd..1125d39 100644
--- a/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt
+++ b/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt
@@ -210,6 +210,40 @@
}
}
+class ListCommand :
+ CliktCommand(
+ help =
+ """
+List all flagged APIs and corresponding flags.
+
+The output format is "<fully-qualified-name-of-flag> <state-of-flag> <API>", one line per API.
+
+The output can be post-processed by e.g. piping it to grep to filter out only enabled APIs, or all APIs guarded by a given flag.
+""") {
+ private val apiSignaturePath by
+ option(ARG_API_SIGNATURE)
+ .help(ARG_API_SIGNATURE_HELP)
+ .path(mustExist = true, canBeDir = false, mustBeReadable = true)
+ .required()
+ private val flagValuesPath by
+ option(ARG_FLAG_VALUES)
+ .help(ARG_FLAG_VALUES_HELP)
+ .path(mustExist = true, canBeDir = false, mustBeReadable = true)
+ .required()
+
+ override fun run() {
+ val flaggedSymbols =
+ apiSignaturePath.toFile().inputStream().use {
+ parseApiSignature(apiSignaturePath.toString(), it)
+ }
+ val flags = flagValuesPath.toFile().inputStream().use { parseFlagValues(it) }
+ val output = listFlaggedApis(flaggedSymbols, flags)
+ if (output.isNotEmpty()) {
+ println(output.joinToString("\n"))
+ }
+ }
+}
+
internal fun parseApiSignature(path: String, input: InputStream): Set<Pair<Symbol, Flag>> {
val output = mutableSetOf<Pair<Symbol, Flag>>()
val visitor =
@@ -460,4 +494,35 @@
return errors
}
-fun main(args: Array<String>) = MainCommand().subcommands(CheckCommand()).main(args)
+/**
+ * Collect all known info about all @FlaggedApi annotated APIs.
+ *
+ * Each API will be represented as a String, on the format
+ * <pre>
+ * <fully-qualified-name-of-flag< <state-of-flag< <API<
+ * </pre>
+ *
+ * @param flaggedSymbolsInSource the set of symbols that are flagged in the source code
+ * @param flags the set of flags and their values
+ * @return a list of Strings encoding API data using the format described above, sorted
+ * alphabetically
+ */
+internal fun listFlaggedApis(
+ flaggedSymbolsInSource: Set<Pair<Symbol, Flag>>,
+ flags: Map<Flag, Boolean>
+): List<String> {
+ val output = mutableListOf<String>()
+ for ((symbol, flag) in flaggedSymbolsInSource) {
+ val flagState =
+ when (flags.get(flag)) {
+ true -> "ENABLED"
+ false -> "DISABLED"
+ null -> "UNKNOWN"
+ }
+ output.add("$flag $flagState ${symbol.toPrettyString()}")
+ }
+ output.sort()
+ return output
+}
+
+fun main(args: Array<String>) = MainCommand().subcommands(CheckCommand(), ListCommand()).main(args)