diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -848,6 +848,7 @@ [{static llvm::StringRef getPrettyPlatformName(llvm::StringRef Platform) { return llvm::StringSwitch(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 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -3543,6 +3543,9 @@ InGroup; def note_protocol_method : Note< "protocol method is here">; +def warn_availability_fuchsia_unavailable_minor : Warning< + "Fuchsia versions only support 'major', not '.minor[.subminor]'">, + InGroup; 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 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -428,6 +428,8 @@ "Controls how scalar integer arguments are extended in calls " "to unprototyped and varargs functions") +VALUE_LANGOPT(FuchsiaVersion, 32, 0, "Fuchsia Version") + #undef LANGOPT #undef COMPATIBLE_LANGOPT #undef BENIGN_LANGOPT diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3136,6 +3136,9 @@ def mllvm : Separate<["-"], "mllvm">, Flags<[CC1Option,CC1AsOption,CoreOption]>, HelpText<"Additional arguments to forward to LLVM's option processing">, MarshallingInfoStringVector>; +def mfuchsia_version_EQ : Joined<["-"], "mfuchsia-version=">, + Group, Flags<[CC1Option]>, HelpText<"Set Fuchsia version target">, + MarshallingInfoInt>; def mmacosx_version_min_EQ : Joined<["-"], "mmacosx-version-min=">, 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 --- a/clang/lib/Basic/Targets/OSTargets.h +++ b/clang/lib/Basic/Targets/OSTargets.h @@ -885,6 +885,9 @@ // Required by the libc++ locale support. if (Opts.CPlusPlus) Builder.defineMacro("_GNU_SOURCE"); + + this->PlatformName = "fuchsia"; + this->PlatformMinVersion = VersionTuple(Opts.FuchsiaVersion); } public: diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5462,6 +5462,9 @@ 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_mfuchsia_version_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 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -2488,6 +2488,15 @@ } } + if (II->isStr("fuchsia")) { + Optional 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/Frontend/attr-availability-fuchsia.c b/clang/test/Frontend/attr-availability-fuchsia.c new file mode 100644 --- /dev/null +++ b/clang/test/Frontend/attr-availability-fuchsia.c @@ -0,0 +1,10 @@ +// Test that `-mfuchsia-version` is propagated to cc1. +// RUN: %clang -target x86_64-unknown-fuchsia -mfuchsia-version=16 -c %s -### |& 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 -mfuchsia-version=16 -c %s -### |& FileCheck %s +// +// CHECK: "-mfuchsia-version=16" diff --git a/clang/test/Sema/attr-availability-fuchsia.c b/clang/test/Sema/attr-availability-fuchsia.c new file mode 100644 --- /dev/null +++ b/clang/test/Sema/attr-availability-fuchsia.c @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 "-triple" "x86_64-unknown-fuchsia" -mfuchsia-version=16 -fsyntax-only -verify %s +// RUN: %clang_cc1 "-triple" "x86_64-unknown-fuchsia" -fsyntax-only %s |& \ +// RUN: 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 versions only support 'major', not '.minor[.subminor]}} +void f7(int) __attribute__((availability(fuchsia, introduced = 16.1))); // expected-warning {{Fuchsia versions only support 'major', not '.minor[.subminor]}} + +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}} +}