Skip to content

Commit 6a2fe6d

Browse files
committed
[clang] Add an ABI_EXTENSION concept that is testable under -pedantic-errors
This is a targeted change to allow us to specify that an extension test should return true even if -pedantic-errors is enabled. In the longer term we may want to consider changing the behavior of -pedantic-errors to not gate __has_extension checks, and become more in line with `-Wpedantic -Werror`.
1 parent f9b9e9b commit 6a2fe6d

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

clang/include/clang/Basic/Features.def

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
//
2525
//===----------------------------------------------------------------------===//
2626

27-
#if !defined(FEATURE) && !defined(EXTENSION)
28-
# error Define either the FEATURE or EXTENSION macro to handle features
27+
#if !defined(FEATURE) && !defined(EXTENSION) && !defined(ABI_EXTENSION)
28+
# error Define either the FEATURE, EXTENSION, or ABI_EXTENSION macro to handle features
2929
#endif
3030

3131
#ifndef FEATURE
@@ -36,6 +36,10 @@
3636
#define EXTENSION(Name, Predicate)
3737
#endif
3838

39+
#ifndef ABI_EXTENSION
40+
#define ABI_EXTENSION(Name, Predicate)
41+
#endif
42+
3943
FEATURE(speculative_load_hardening, LangOpts.SpeculativeLoadHardening)
4044
FEATURE(address_sanitizer,
4145
LangOpts.Sanitize.hasOneOf(SanitizerKind::Address |
@@ -148,7 +152,7 @@ FEATURE(thread_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Thread))
148152
FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
149153
FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
150154
FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics)
151-
EXTENSION(ptrauth_qualifier, LangOpts.PointerAuthIntrinsics)
155+
ABI_EXTENSION(ptrauth_qualifier, LangOpts.PointerAuthIntrinsics)
152156
FEATURE(ptrauth_calls, LangOpts.PointerAuthCalls)
153157
FEATURE(ptrauth_returns, LangOpts.PointerAuthReturns)
154158
FEATURE(ptrauth_vtable_pointer_address_discrimination, LangOpts.PointerAuthVTPtrAddressDiscrimination)
@@ -385,3 +389,4 @@ EXTENSION(cxx_type_aware_allocators, true)
385389

386390
#undef EXTENSION
387391
#undef FEATURE
392+
#undef ABI_EXTENSION

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,11 +1183,15 @@ void DumpCompilerOptionsAction::ExecuteAction() {
11831183
OS << "\n\"extensions\" : [\n";
11841184
{
11851185
llvm::SmallString<128> Str;
1186-
#define EXTENSION(Name, Predicate) \
1186+
#define EXTENSION_OPTION(Name, Predicate) \
11871187
("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
11881188
.toVector(Str);
1189+
#define EXTENSION(Name, Predicate) EXTENSION_OPTION(Name, Predicate)
1190+
#define ABI_EXTENSION(Name, Predicate) EXTENSION_OPTION(Name, Predicate)
11891191
#include "clang/Basic/Features.def"
11901192
#undef EXTENSION
1193+
#undef ABI_EXTENSION
1194+
#undef EXTENSION_OPTION
11911195
// Remove the newline and comma from the last entry to ensure this remains
11921196
// valid JSON.
11931197
OS << Str.substr(0, Str.size() - 2);

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,19 +1109,26 @@ static bool HasExtension(const Preprocessor &PP, StringRef Extension) {
11091109
if (HasFeature(PP, Extension))
11101110
return true;
11111111

1112+
// Normalize the extension name, __foo__ becomes foo.
1113+
if (Extension.starts_with("__") && Extension.ends_with("__") &&
1114+
Extension.size() >= 4)
1115+
Extension = Extension.substr(2, Extension.size() - 4);
1116+
1117+
const LangOptions &LangOpts = PP.getLangOpts();
1118+
#define ABI_EXTENSION(Name, Predicate) .Case(#Name, Predicate)
1119+
bool IsABIExtension = llvm::StringSwitch<bool>(Extension)
1120+
#include "clang/Basic/Features.def"
1121+
.Default(false);
1122+
#undef ABI_EXTENSION
1123+
if (IsABIExtension)
1124+
return true;
1125+
11121126
// If the use of an extension results in an error diagnostic, extensions are
11131127
// effectively unavailable, so just return false here.
11141128
if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
11151129
diag::Severity::Error)
11161130
return false;
11171131

1118-
const LangOptions &LangOpts = PP.getLangOpts();
1119-
1120-
// Normalize the extension name, __foo__ becomes foo.
1121-
if (Extension.starts_with("__") && Extension.ends_with("__") &&
1122-
Extension.size() >= 4)
1123-
Extension = Extension.substr(2, Extension.size() - 4);
1124-
11251132
// Because we inherit the feature list from HasFeature, this string switch
11261133
// must be less restrictive than HasFeature's.
11271134
#define EXTENSION(Name, Predicate) .Case(#Name, Predicate)

clang/test/Sema/ptrauth-qualifier.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -triple arm64-apple-ios -std=c23 -fsyntax-only -verify -fptrauth-intrinsics %s
22
// RUN: %clang_cc1 -triple aarch64-linux-gnu -std=c23 -fsyntax-only -verify -fptrauth-intrinsics %s
3+
// RUN: %clang_cc1 -triple aarch64-linux-gnu -std=c23 -fsyntax-only -verify -fptrauth-intrinsics -pedantic-errors -Wno-c2y-extensions %s
34

45
#if !__has_extension(ptrauth_qualifier)
56
// This error means that the __ptrauth qualifier availability test says that it

0 commit comments

Comments
 (0)