API availability should be checked using __builtin_available
This change turns -Wunguarded-availability option which enforces calls
to APIs which are introduced after the min_sdk_version of the
compliation unit to be guarded with __builtin_available check. For
example, let's assume that we have foo() which was introduced with API
level 30.
void foo() __INTRODUCED_IN(30);
Then if foo() is called for a module whose min_sdk_version is less than
30, say 26, it should be called as below:
if (__builtin_available(android 30, *)) {
foo();
} else {
// fallback impl
}
For modules whose min_sdk_version is >=30, the guard is not required.
Bug: 150860940
Bug: 134795810
Test: m
Change-Id: I084148b9a051350626a37cb394daa4398b7332d5
diff --git a/cc/config/clang.go b/cc/config/clang.go
index 3282958..35dd10f 100644
--- a/cc/config/clang.go
+++ b/cc/config/clang.go
@@ -141,6 +141,13 @@
// Warnings from clang-10
// Nested and array designated initialization is nice to have.
"-Wno-c99-designator",
+
+ // Calls to the APIs that are newer than the min sdk version of the caller should be
+ // guarded with __builtin_available.
+ "-Wunguarded-availability",
+ // This macro allows the bionic versioning.h to indirectly determine whether the
+ // option -Wunguarded-availability is on or not.
+ "-D__ANDROID_UNGUARDED_AVAILABILITY__",
}, " "))
pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{