Author: Haowei Wu Date: 2021-10-11T15:33:04-07:00 New Revision: 1131b1eb3509b47d30a36ea9b42367ab1d7373a2
URL: https://github.com/llvm/llvm-project/commit/1131b1eb3509b47d30a36ea9b42367ab1d7373a2 DIFF: https://github.com/llvm/llvm-project/commit/1131b1eb3509b47d30a36ea9b42367ab1d7373a2.diff LOG: [clang][Fuchsia] Support availability attr on Fuchsia This patch adds support to __attribute__((availability)) annotation for Fuchsia platform. This patch also adds '-ffuchsia-api-level' to allow specify Fuchsia API level from the command line. Differential Revision: https://reviews.llvm.org/D108592 Added: clang/test/Driver/attr-availability-fuchsia.c clang/test/Sema/attr-availability-fuchsia.c Modified: clang/include/clang/Basic/Attr.td clang/include/clang/Basic/DiagnosticSemaKinds.td clang/include/clang/Basic/LangOptions.def clang/include/clang/Driver/Options.td clang/lib/Basic/Targets/OSTargets.h clang/lib/Driver/ToolChains/Clang.cpp clang/lib/Sema/SemaDeclAttr.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index ec7c1bd60ed4c..bd6510677fac0 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -848,6 +848,7 @@ def Availability : InheritableAttr { [{static llvm::StringRef getPrettyPlatformName(llvm::StringRef Platform) { return llvm::StringSwitch<llvm::StringRef>(Platform) .Case("android", "Android") + .Case("fuchsia", "Fuchsia") .Case("ios", "iOS") .Case("macos", "macOS") .Case("tvos", "tvOS") diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 128ad047072e8..c5b3f2c5cca64 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -3554,6 +3554,9 @@ def warn_availability_swift_unavailable_deprecated_only : Warning< InGroup<Availability>; def note_protocol_method : Note< "protocol method is here">; +def warn_availability_fuchsia_unavailable_minor : Warning< + "Fuchsia API Level prohibits specifying a minor or sub-minor version">, + InGroup<Availability>; def warn_unguarded_availability : Warning<"%0 is only available on %1 %2 or newer">, diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index e21d2fcea621e..912fd0ec18961 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -431,6 +431,8 @@ ENUM_LANGOPT(ExtendIntArgs, ExtendArgsKind, 1, ExtendArgsKind::ExtendTo32, "Controls how scalar integer arguments are extended in calls " "to unprototyped and varargs functions") +VALUE_LANGOPT(FuchsiaAPILevel, 32, 0, "Fuchsia API level") + #undef LANGOPT #undef COMPATIBLE_LANGOPT #undef BENIGN_LANGOPT diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 9b0d0dfb7628c..4d422abd03a0e 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3173,6 +3173,9 @@ def mlinker_version_EQ : Joined<["-"], "mlinker-version=">, def mllvm : Separate<["-"], "mllvm">, Flags<[CC1Option,CC1AsOption,CoreOption]>, HelpText<"Additional arguments to forward to LLVM's option processing">, MarshallingInfoStringVector<FrontendOpts<"LLVMArgs">>; +def ffuchsia_api_level_EQ : Joined<["-"], "ffuchsia-api-level=">, + Group<m_Group>, Flags<[CC1Option]>, HelpText<"Set Fuchsia API level">, + MarshallingInfoInt<LangOpts<"FuchsiaAPILevel">>; def mmacosx_version_min_EQ : Joined<["-"], "mmacosx-version-min=">, Group<m_Group>, HelpText<"Set Mac OS X deployment target">; def mmacos_version_min_EQ : Joined<["-"], "mmacos-version-min=">, diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h index 02a0957be5b93..220290c1bacf5 100644 --- a/clang/lib/Basic/Targets/OSTargets.h +++ b/clang/lib/Basic/Targets/OSTargets.h @@ -890,6 +890,9 @@ class LLVM_LIBRARY_VISIBILITY FuchsiaTargetInfo : public OSTargetInfo<Target> { // Required by the libc++ locale support. if (Opts.CPlusPlus) Builder.defineMacro("_GNU_SOURCE"); + Builder.defineMacro("__Fuchsia_API_level__", Twine(Opts.FuchsiaAPILevel)); + this->PlatformName = "fuchsia"; + this->PlatformMinVersion = VersionTuple(Opts.FuchsiaAPILevel); } public: diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index f1771d4fa35d2..7ce6aa289e095 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5481,6 +5481,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, options::OPT_fno_experimental_relative_cxx_abi_vtables); + if (Arg *A = Args.getLastArg(options::OPT_ffuchsia_api_level_EQ)) + A->render(Args, CmdArgs); + // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} // (-ansi is equivalent to -std=c89 or -std=c++98). // diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 07e80901e2193..d49ff4bb2b416 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -2496,6 +2496,15 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { } } + if (II->isStr("fuchsia")) { + Optional<unsigned> Min, Sub; + if ((Min = Introduced.Version.getMinor()) || + (Sub = Introduced.Version.getSubminor())) { + S.Diag(AL.getLoc(), diag::warn_availability_fuchsia_unavailable_minor); + return; + } + } + int PriorityModifier = AL.isPragmaClangAttribute() ? Sema::AP_PragmaClangAttribute : Sema::AP_Explicit; diff --git a/clang/test/Driver/attr-availability-fuchsia.c b/clang/test/Driver/attr-availability-fuchsia.c new file mode 100644 index 0000000000000..5d2b414cb1578 --- /dev/null +++ b/clang/test/Driver/attr-availability-fuchsia.c @@ -0,0 +1,45 @@ +// Test that `-ffuchsia-api-level` is propagated to cc1. +// RUN: %clang -target x86_64-unknown-fuchsia -ffuchsia-api-level=16 -c %s -### 2>&1| FileCheck %s + +// It should also be exposed to non-fuchsia platforms. This is desireable when +// using common Fuchsia headers for building host libraries that also depend on +// the Fuchsia version (such as using a compatible host-side FIDL library that +// talks with a Fuchsia FIDL library of the same version). +// RUN: %clang -target x86_64-unknown-linux-gnu -ffuchsia-api-level=16 -c %s -### 2>&1 | FileCheck %s + +// Check Fuchsia API level macro. +// RUN: %clang -target x86_64-unknown-fuchsia -ffuchsia-api-level=15 -c %s -o %t +// RUN: llvm-readobj --symbols %t | FileCheck %s --check-prefix=CHECK-F15 +// +// RUN: %clang -target x86_64-unknown-fuchsia -ffuchsia-api-level=16 -c %s -o %t +// RUN: llvm-readobj --symbols %t | FileCheck %s --check-prefix=CHECK-F16 + +// Check using a non-integer Fuchsia API level. +// RUN: not %clang -target x86_64-unknown-fuchsia -ffuchsia-api-level=16.0.0 -c %s 2>&1| FileCheck %s --check-prefix=CHECK-ERROR + + +// CHECK: "-ffuchsia-api-level=16" + +// CHECK-F15: Name: f15 + +// CHECK-F16: Name: f16 + +// CHECK-ERROR: error: invalid integral value '16.0.0' in '-ffuchsia-api-level=16.0.0' + +#if __Fuchsia_API_level__ >= 16 +void f16() { + +} +#else +void f15() { + +} +#endif + +int main(int argc, char* argv[]) { +#if __Fuchsia_API_level__ >= 16 + f16(); +#else + f15(); +#endif +} diff --git a/clang/test/Sema/attr-availability-fuchsia.c b/clang/test/Sema/attr-availability-fuchsia.c new file mode 100644 index 0000000000000..b618697cdd6f6 --- /dev/null +++ b/clang/test/Sema/attr-availability-fuchsia.c @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 "-triple" "x86_64-unknown-fuchsia" -ffuchsia-api-level=16 -fsyntax-only -verify %s +// RUN: %clang_cc1 "-triple" "x86_64-unknown-fuchsia" -fsyntax-only %s 2>&1 | FileCheck %s + +// If the version is not specified, we should not get any errors since there +// is no checking (the major version number is zero). +// CHECK-NOT: error: + +void f0(int) __attribute__((availability(fuchsia, introduced = 14, deprecated = 19))); +void f1(int) __attribute__((availability(fuchsia, introduced = 16))); +void f2(int) __attribute__((availability(fuchsia, introduced = 14, deprecated = 16))); // expected-note {{'f2' has been explicitly marked deprecated here}} +void f3(int) __attribute__((availability(fuchsia, introduced = 19))); +void f4(int) __attribute__((availability(fuchsia, introduced = 9, deprecated = 11, obsoleted = 16), availability(ios, introduced = 2.0, deprecated = 3.0))); // expected-note{{explicitly marked unavailable}} +void f5(int) __attribute__((availability(ios, introduced = 3.2), availability(fuchsia, unavailable))); // expected-note{{'f5' has been explicitly marked unavailable here}} +void f6(int) __attribute__((availability(fuchsia, introduced = 16.0))); // expected-warning {{Fuchsia API Level prohibits specifying a minor or sub-minor version}} +void f7(int) __attribute__((availability(fuchsia, introduced = 16.1))); // expected-warning {{Fuchsia API Level prohibits specifying a minor or sub-minor version}} + +void test() { + f0(0); + f1(0); + f2(0); // expected-warning{{'f2' is deprecated: first deprecated in Fuchsia 16}} + f3(0); + f4(0); // expected-error{{f4' is unavailable: obsoleted in Fuchsia 16}} + f5(0); // expected-error{{'f5' is unavailable: not available on Fuchsia}} +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits