Skip to content

Commit b1263d7

Browse files
committed
[clang] Remove fixed point arithmetic error
Prior to this, clang would always report ``` compile with '-ffixed-point' to enable fixed point types ``` whenever it sees `_Accum`, `_Fract`, or `_Sat` when fixed point arithmetic is not enabled. This can break existing code that uses these as variable names and doesn't use fixed point arithmetic like in some microsoft headers (llvm#67750 (comment)). Fixed point should not raise this error for these cases, so this removes the error altogether and defaults to the usual error clang gives where it can see these keywords as either unknown types or regular variables.
1 parent 5425925 commit b1263d7

File tree

6 files changed

+42
-46
lines changed

6 files changed

+42
-46
lines changed

clang/include/clang/Basic/DiagnosticCommonKinds.td

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,6 @@ def ext_clang_diagnose_if : Extension<"'diagnose_if' is a clang extension">,
260260
InGroup<GccCompat>;
261261
def err_too_large_for_fixed_point : Error<
262262
"this value is too large for this fixed point type">;
263-
def err_fixed_point_not_enabled : Error<"compile with "
264-
"'-ffixed-point' to enable fixed point types">;
265263
def err_unimplemented_conversion_with_fixed_point_type : Error<
266264
"conversion between fixed point and %0 is not yet supported">;
267265

clang/include/clang/Basic/TokenKinds.def

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ PUNCTUATOR(caretcaret, "^^")
284284
// HALFSUPPORT - This is a keyword if 'half' is a built-in type
285285
// WCHARSUPPORT - This is a keyword if 'wchar_t' is a built-in type
286286
// CHAR8SUPPORT - This is a keyword if 'char8_t' is a built-in type
287+
// KEYFIXEDPOINT - This is a keyword according to the N1169 fixed point
288+
// extension.
287289
//
288290
KEYWORD(auto , KEYALL)
289291
KEYWORD(break , KEYALL)
@@ -423,9 +425,9 @@ C23_KEYWORD(typeof , KEYGNU)
423425
C23_KEYWORD(typeof_unqual , 0)
424426

425427
// ISO/IEC JTC1 SC22 WG14 N1169 Extension
426-
KEYWORD(_Accum , KEYNOCXX)
427-
KEYWORD(_Fract , KEYNOCXX)
428-
KEYWORD(_Sat , KEYNOCXX)
428+
KEYWORD(_Accum , KEYFIXEDPOINT)
429+
KEYWORD(_Fract , KEYFIXEDPOINT)
430+
KEYWORD(_Sat , KEYFIXEDPOINT)
429431

430432
// GNU Extensions (in impl-reserved namespace)
431433
KEYWORD(_Decimal32 , KEYALL)

clang/lib/Basic/IdentifierTable.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ namespace {
108108
KEYSYCL = 0x800000,
109109
KEYCUDA = 0x1000000,
110110
KEYHLSL = 0x2000000,
111-
KEYMAX = KEYHLSL, // The maximum key
111+
KEYFIXEDPOINT = 0x4000000,
112+
KEYMAX = KEYFIXEDPOINT, // The maximum key
112113
KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX20,
113114
KEYALL = (KEYMAX | (KEYMAX-1)) & ~KEYNOMS18 &
114115
~KEYNOOPENCL // KEYNOMS18 and KEYNOOPENCL are used to exclude.
@@ -210,6 +211,8 @@ static KeywordStatus getKeywordStatusHelper(const LangOptions &LangOpts,
210211
case KEYNOMS18:
211212
// The disable behavior for this is handled in getKeywordStatus.
212213
return KS_Unknown;
214+
case KEYFIXEDPOINT:
215+
return LangOpts.FixedPoint ? KS_Enabled : KS_Disabled;
213216
default:
214217
llvm_unreachable("Unknown KeywordStatus flag");
215218
}

clang/lib/Parse/ParseDecl.cpp

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,17 +3288,6 @@ Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
32883288
return false;
32893289
}
32903290

3291-
// Choose the apprpriate diagnostic error for why fixed point types are
3292-
// disabled, set the previous specifier, and mark as invalid.
3293-
static void SetupFixedPointError(const LangOptions &LangOpts,
3294-
const char *&PrevSpec, unsigned &DiagID,
3295-
bool &isInvalid) {
3296-
assert(!LangOpts.FixedPoint);
3297-
DiagID = diag::err_fixed_point_not_enabled;
3298-
PrevSpec = ""; // Not used by diagnostic
3299-
isInvalid = true;
3300-
}
3301-
33023291
/// ParseDeclarationSpecifiers
33033292
/// declaration-specifiers: [C99 6.7]
33043293
/// storage-class-specifier declaration-specifiers[opt]
@@ -4275,27 +4264,24 @@ void Parser::ParseDeclarationSpecifiers(
42754264
DiagID, Policy);
42764265
break;
42774266
case tok::kw__Accum:
4278-
if (!getLangOpts().FixedPoint) {
4279-
SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
4280-
} else {
4281-
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec,
4282-
DiagID, Policy);
4283-
}
4267+
assert(getLangOpts().FixedPoint &&
4268+
"This keyword is only used when fixed point types are enabled "
4269+
"with `-ffixed-point`");
4270+
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec, DiagID,
4271+
Policy);
42844272
break;
42854273
case tok::kw__Fract:
4286-
if (!getLangOpts().FixedPoint) {
4287-
SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
4288-
} else {
4289-
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec,
4290-
DiagID, Policy);
4291-
}
4274+
assert(getLangOpts().FixedPoint &&
4275+
"This keyword is only used when fixed point types are enabled "
4276+
"with `-ffixed-point`");
4277+
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec, DiagID,
4278+
Policy);
42924279
break;
42934280
case tok::kw__Sat:
4294-
if (!getLangOpts().FixedPoint) {
4295-
SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
4296-
} else {
4297-
isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID);
4298-
}
4281+
assert(getLangOpts().FixedPoint &&
4282+
"This keyword is only used when fixed point types are enabled "
4283+
"with `-ffixed-point`");
4284+
isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID);
42994285
break;
43004286
case tok::kw___float128:
43014287
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -x c -verify %s
2+
// RUN: %clang_cc1 -x c -verify %s -ffixed-point -DFIXED_POINT=1
3+
4+
int _Accum;
5+
6+
#ifdef FIXED_POINT
7+
// expected-error@4{{cannot combine with previous 'int' declaration specifier}}
8+
// expected-warning@4{{declaration does not declare anything}}
9+
#else
10+
// expected-no-diagnostics
11+
#endif

clang/test/Frontend/fixed_point_not_enabled.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
// RUN: %clang_cc1 -x c -verify %s
22

33
// Primary fixed point types
4-
signed short _Accum s_short_accum; // expected-error{{compile with '-ffixed-point' to enable fixed point types}}
5-
signed _Accum s_accum; // expected-error{{compile with '-ffixed-point' to enable fixed point types}}
6-
signed long _Accum s_long_accum; // expected-error{{compile with '-ffixed-point' to enable fixed point types}}
7-
unsigned short _Accum u_short_accum; // expected-error{{compile with '-ffixed-point' to enable fixed point types}}
8-
unsigned _Accum u_accum; // expected-error{{compile with '-ffixed-point' to enable fixed point types}}
9-
unsigned long _Accum u_long_accum; // expected-error{{compile with '-ffixed-point' to enable fixed point types}}
10-
11-
// Aliased fixed point types
12-
short _Accum short_accum; // expected-error{{compile with '-ffixed-point' to enable fixed point types}}
13-
_Accum accum; // expected-error{{compile with '-ffixed-point' to enable fixed point types}}
14-
// expected-error@-1{{type specifier missing, defaults to 'int'}}
15-
long _Accum long_accum; // expected-error{{compile with '-ffixed-point' to enable fixed point types}}
4+
// Without `-ffixed-point`, these keywords are now treated as typedef'd types or identifiers.
5+
_Accum accum; // expected-error{{unknown type name '_Accum'}}
6+
_Fract fract; // expected-error{{unknown type name '_Fract'}}
7+
_Sat _Accum sat_accum; // expected-error{{unknown type name '_Sat'}}
8+
// expected-error@-1{{expected ';' after top level declarator}}
9+
signed _Accum signed_accum; // expected-error{{expected ';' after top level declarator}}
10+
signed _Fract signed_fract; // expected-error{{expected ';' after top level declarator}}
11+
signed _Sat _Accum signed_sat_accum; // expected-error{{expected ';' after top level declarator}}
1612

1713
// Cannot use fixed point suffixes
1814
int accum_int = 10k; // expected-error{{invalid suffix 'k' on integer constant}}

0 commit comments

Comments
 (0)