Skip to content

Commit 20ee923

Browse files
Merge remote-tracking branch 'origin/main' into wunsafebufferusage
2 parents e650ecd + 23743f5 commit 20ee923

File tree

94 files changed

+2617
-4288
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+2617
-4288
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,7 @@ Designated initializers (N494) C
16521652
Array & element qualification (N2607) C23 C89
16531653
Attributes (N2335) C23 C89
16541654
``#embed`` (N3017) C23 C89, C++
1655+
Octal literals prefixed with ``0o`` or ``0O`` C2y C89, C++
16551656
============================================= ================================ ============= =============
16561657

16571658
Builtin type aliases

clang/docs/ReleaseNotes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ C2y Feature Support
129129
- Implemented `WG14 N3411 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3411.pdf>`_
130130
which allows a source file to not end with a newline character. This is still
131131
reported as a conforming extension in earlier language modes.
132+
- Implemented `WG14 N3353 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3353.htm>_`
133+
which adds the new ``0o`` and ``0O`` ocal literal prefixes and deprecates
134+
octal literals other than ``0`` which do not start with the new prefix. This
135+
feature is exposed in earlier language modes and in C++ as an extension. The
136+
paper also introduced octal and hexadecimal delimited escape sequences (e.g.,
137+
``"\x{12}\o{12}"``) which are also supported as an extension in older C
138+
language modes.
132139

133140
C23 Feature Support
134141
^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def EnumCompare : DiagGroup<"enum-compare", [EnumCompareSwitch,
9292
def DeprecatedAnonEnumEnumConversion : DiagGroup<"deprecated-anon-enum-enum-conversion">;
9393
def DeprecatedEnumEnumConversion : DiagGroup<"deprecated-enum-enum-conversion">;
9494
def DeprecatedEnumFloatConversion : DiagGroup<"deprecated-enum-float-conversion">;
95+
def DeprecatedOctalLiterals : DiagGroup<"deprecated-octal-literals">;
9596
def AnonEnumEnumConversion : DiagGroup<"anon-enum-enum-conversion",
9697
[DeprecatedAnonEnumEnumConversion]>;
9798
def EnumEnumConversion : DiagGroup<"enum-enum-conversion",
@@ -235,7 +236,8 @@ def Deprecated : DiagGroup<"deprecated", [DeprecatedAnonEnumEnumConversion,
235236
DeprecatedVolatile,
236237
DeprecatedWritableStr,
237238
DeprecatedRedundantConstexprStaticDef,
238-
DeprecatedMissingCommaVariadicParam
239+
DeprecatedMissingCommaVariadicParam,
240+
DeprecatedOctalLiterals
239241
]>,
240242
DiagCategory<"Deprecations">;
241243

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ def ext_mathematical_notation : ExtWarn<
148148
InGroup<DiagGroup<"mathematical-notation-identifier-extension">>;
149149

150150
def ext_delimited_escape_sequence : Extension<
151-
"%select{delimited|named}0 escape sequences are a "
152-
"%select{Clang|C++23}1 extension">,
153-
InGroup<DiagGroup<"delimited-escape-sequence-extension">>;
154-
151+
"%select{delimited|named}0 escape sequences are a %select{C++23|C2y|Clang}1 "
152+
"extension">, InGroup<DiagGroup<"delimited-escape-sequence-extension">>;
155153
def warn_cxx23_delimited_escape_sequence : Warning<
156-
"%select{delimited|named}0 escape sequences are "
157-
"incompatible with C++ standards before C++23">,
158-
InGroup<CXXPre23Compat>, DefaultIgnore;
154+
"%select{delimited|named}0 escape sequences are incompatible with C++ "
155+
"standards before C++23">, InGroup<CXXPre23Compat>, DefaultIgnore;
156+
def warn_c2y_delimited_escape_sequence : Warning<
157+
"delimited escape sequences are incompatible with C standards before C2y">,
158+
InGroup<CPre2yCompat>, DefaultIgnore;
159159

160160
def err_delimited_escape_empty : Error<
161161
"delimited escape sequence cannot be empty">;
@@ -256,6 +256,17 @@ def warn_cxx17_hex_literal : Warning<
256256
"hexadecimal floating literals are incompatible with "
257257
"C++ standards before C++17">,
258258
InGroup<CXXPre17CompatPedantic>, DefaultIgnore;
259+
def ext_octal_literal : Extension<
260+
"octal integer literals are a C2y extension">, InGroup<C2y>;
261+
def ext_cpp_octal_literal : Extension<
262+
"octal integer literals are a Clang extension">,
263+
InGroup<DiagGroup<"octal-prefix-extension">>;
264+
def warn_c2y_compat_octal_literal : Warning<
265+
"octal integer literals are incompatible with standards before C2y">,
266+
InGroup<CPre2yCompat>, DefaultIgnore;
267+
def warn_unprefixed_octal_deprecated : Warning<
268+
"octal literals without a '0o' prefix are deprecated">,
269+
InGroup<DeprecatedOctalLiterals>;
259270
def ext_binary_literal : Extension<
260271
"binary integer literals are a C23 extension">, InGroup<C23>;
261272
def warn_c23_compat_binary_literal : Warning<

clang/include/clang/Lex/Lexer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,12 @@ class Lexer : public PreprocessorLexer {
582582
/// sequence.
583583
static bool isNewLineEscaped(const char *BufferStart, const char *Str);
584584

585+
/// Diagnose use of a delimited or named escape sequence.
586+
static void DiagnoseDelimitedOrNamedEscapeSequence(SourceLocation Loc,
587+
bool Named,
588+
const LangOptions &Opts,
589+
DiagnosticsEngine &Diags);
590+
585591
/// Represents a char and the number of bytes parsed to produce it.
586592
struct SizedChar {
587593
char Char;

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5327,7 +5327,19 @@ void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity,
53275327
}
53285328
};
53295329

5330-
switch(SAE->getKind()) {
5330+
auto MangleExtensionBuiltin = [&](const UnaryExprOrTypeTraitExpr *E,
5331+
StringRef Name = {}) {
5332+
if (Name.empty())
5333+
Name = getTraitSpelling(E->getKind());
5334+
mangleVendorType(Name);
5335+
if (SAE->isArgumentType())
5336+
mangleType(SAE->getArgumentType());
5337+
else
5338+
mangleTemplateArgExpr(SAE->getArgumentExpr());
5339+
Out << 'E';
5340+
};
5341+
5342+
switch (SAE->getKind()) {
53315343
case UETT_SizeOf:
53325344
Out << 's';
53335345
MangleAlignofSizeofArg();
@@ -5337,56 +5349,24 @@ void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity,
53375349
// have acted differently since Clang 8, but were previously mangled the
53385350
// same.)
53395351
if (!isCompatibleWith(LangOptions::ClangABI::Ver11)) {
5340-
Out << "u11__alignof__";
5341-
if (SAE->isArgumentType())
5342-
mangleType(SAE->getArgumentType());
5343-
else
5344-
mangleTemplateArgExpr(SAE->getArgumentExpr());
5345-
Out << 'E';
5352+
MangleExtensionBuiltin(SAE, "__alignof__");
53465353
break;
53475354
}
53485355
[[fallthrough]];
53495356
case UETT_AlignOf:
53505357
Out << 'a';
53515358
MangleAlignofSizeofArg();
53525359
break;
5360+
5361+
case UETT_VectorElements:
5362+
case UETT_OpenMPRequiredSimdAlign:
5363+
case UETT_VecStep:
5364+
case UETT_PtrAuthTypeDiscriminator:
53535365
case UETT_DataSizeOf: {
5354-
DiagnosticsEngine &Diags = Context.getDiags();
5355-
unsigned DiagID =
5356-
Diags.getCustomDiagID(DiagnosticsEngine::Error,
5357-
"cannot yet mangle __datasizeof expression");
5358-
Diags.Report(DiagID);
5359-
return;
5360-
}
5361-
case UETT_PtrAuthTypeDiscriminator: {
5362-
DiagnosticsEngine &Diags = Context.getDiags();
5363-
unsigned DiagID = Diags.getCustomDiagID(
5364-
DiagnosticsEngine::Error,
5365-
"cannot yet mangle __builtin_ptrauth_type_discriminator expression");
5366-
Diags.Report(E->getExprLoc(), DiagID);
5367-
return;
5368-
}
5369-
case UETT_VecStep: {
5370-
DiagnosticsEngine &Diags = Context.getDiags();
5371-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
5372-
"cannot yet mangle vec_step expression");
5373-
Diags.Report(DiagID);
5374-
return;
5375-
}
5376-
case UETT_OpenMPRequiredSimdAlign: {
5377-
DiagnosticsEngine &Diags = Context.getDiags();
5378-
unsigned DiagID = Diags.getCustomDiagID(
5379-
DiagnosticsEngine::Error,
5380-
"cannot yet mangle __builtin_omp_required_simd_align expression");
5381-
Diags.Report(DiagID);
5382-
return;
5383-
}
5384-
case UETT_VectorElements: {
53855366
DiagnosticsEngine &Diags = Context.getDiags();
53865367
unsigned DiagID = Diags.getCustomDiagID(
5387-
DiagnosticsEngine::Error,
5388-
"cannot yet mangle __builtin_vectorelements expression");
5389-
Diags.Report(DiagID);
5368+
DiagnosticsEngine::Error, "cannot yet mangle %0 expression");
5369+
Diags.Report(E->getExprLoc(), DiagID) << getTraitSpelling(SAE->getKind());
53905370
return;
53915371
}
53925372
}

clang/lib/Lex/Lexer.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,6 +3405,30 @@ bool Lexer::isCodeCompletionPoint(const char *CurPtr) const {
34053405
return false;
34063406
}
34073407

3408+
void Lexer::DiagnoseDelimitedOrNamedEscapeSequence(SourceLocation Loc,
3409+
bool Named,
3410+
const LangOptions &Opts,
3411+
DiagnosticsEngine &Diags) {
3412+
unsigned DiagId;
3413+
if (Opts.CPlusPlus23)
3414+
DiagId = diag::warn_cxx23_delimited_escape_sequence;
3415+
else if (Opts.C2y && !Named)
3416+
DiagId = diag::warn_c2y_delimited_escape_sequence;
3417+
else
3418+
DiagId = diag::ext_delimited_escape_sequence;
3419+
3420+
// The trailing arguments are only used by the extension warning; either this
3421+
// is a C2y extension or a C++23 extension, unless it's a named escape
3422+
// sequence in C, then it's a Clang extension.
3423+
unsigned Ext;
3424+
if (!Opts.CPlusPlus)
3425+
Ext = Named ? 2 /* Clang extension */ : 1 /* C2y extension */;
3426+
else
3427+
Ext = 0; // C++23 extension
3428+
3429+
Diags.Report(Loc, DiagId) << Named << Ext;
3430+
}
3431+
34083432
std::optional<uint32_t> Lexer::tryReadNumericUCN(const char *&StartPtr,
34093433
const char *SlashLoc,
34103434
Token *Result) {
@@ -3496,12 +3520,10 @@ std::optional<uint32_t> Lexer::tryReadNumericUCN(const char *&StartPtr,
34963520
return std::nullopt;
34973521
}
34983522

3499-
if (Delimited && PP) {
3500-
Diag(SlashLoc, PP->getLangOpts().CPlusPlus23
3501-
? diag::warn_cxx23_delimited_escape_sequence
3502-
: diag::ext_delimited_escape_sequence)
3503-
<< /*delimited*/ 0 << (PP->getLangOpts().CPlusPlus ? 1 : 0);
3504-
}
3523+
if (Delimited && PP)
3524+
DiagnoseDelimitedOrNamedEscapeSequence(getSourceLocation(SlashLoc), false,
3525+
PP->getLangOpts(),
3526+
PP->getDiagnostics());
35053527

35063528
if (Result) {
35073529
Result->setFlag(Token::HasUCN);
@@ -3585,10 +3607,9 @@ std::optional<uint32_t> Lexer::tryReadNamedUCN(const char *&StartPtr,
35853607
}
35863608

35873609
if (Diagnose && Match)
3588-
Diag(SlashLoc, PP->getLangOpts().CPlusPlus23
3589-
? diag::warn_cxx23_delimited_escape_sequence
3590-
: diag::ext_delimited_escape_sequence)
3591-
<< /*named*/ 1 << (PP->getLangOpts().CPlusPlus ? 1 : 0);
3610+
DiagnoseDelimitedOrNamedEscapeSequence(getSourceLocation(SlashLoc), true,
3611+
PP->getLangOpts(),
3612+
PP->getDiagnostics());
35923613

35933614
// If no diagnostic has been emitted yet, likely because we are doing a
35943615
// tentative lexing, we do not want to recover here to make sure the token

clang/lib/Lex/LiteralSupport.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "clang/Lex/Preprocessor.h"
2222
#include "clang/Lex/Token.h"
2323
#include "llvm/ADT/APInt.h"
24+
#include "llvm/ADT/ScopeExit.h"
2425
#include "llvm/ADT/SmallVector.h"
2526
#include "llvm/ADT/StringExtras.h"
2627
#include "llvm/ADT/StringSwitch.h"
@@ -353,10 +354,8 @@ static unsigned ProcessCharEscape(const char *ThisTokBegin,
353354
diag::err_expected)
354355
<< tok::r_brace;
355356
else if (!HadError) {
356-
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
357-
Features.CPlusPlus23 ? diag::warn_cxx23_delimited_escape_sequence
358-
: diag::ext_delimited_escape_sequence)
359-
<< /*delimited*/ 0 << (Features.CPlusPlus ? 1 : 0);
357+
Lexer::DiagnoseDelimitedOrNamedEscapeSequence(Loc, false, Features,
358+
*Diags);
360359
}
361360
}
362361

@@ -709,11 +708,8 @@ static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
709708
diag::warn_ucn_not_valid_in_c89_literal);
710709

711710
if ((IsDelimitedEscapeSequence || IsNamedEscapeSequence) && Diags)
712-
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
713-
Features.CPlusPlus23 ? diag::warn_cxx23_delimited_escape_sequence
714-
: diag::ext_delimited_escape_sequence)
715-
<< (IsNamedEscapeSequence ? 1 : 0) << (Features.CPlusPlus ? 1 : 0);
716-
711+
Lexer::DiagnoseDelimitedOrNamedEscapeSequence(Loc, IsNamedEscapeSequence,
712+
Features, *Diags);
717713
return true;
718714
}
719715

@@ -1423,6 +1419,29 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
14231419
return;
14241420
}
14251421

1422+
// Parse a potential octal literal prefix.
1423+
bool SawOctalPrefix = false;
1424+
if ((c1 == 'O' || c1 == 'o') && (s[1] >= '0' && s[1] <= '7')) {
1425+
unsigned DiagId;
1426+
if (LangOpts.C2y)
1427+
DiagId = diag::warn_c2y_compat_octal_literal;
1428+
else if (LangOpts.CPlusPlus)
1429+
DiagId = diag::ext_cpp_octal_literal;
1430+
else
1431+
DiagId = diag::ext_octal_literal;
1432+
Diags.Report(TokLoc, DiagId);
1433+
++s;
1434+
DigitsBegin = s;
1435+
SawOctalPrefix = true;
1436+
}
1437+
1438+
auto _ = llvm::make_scope_exit([&] {
1439+
// If we still have an octal value but we did not see an octal prefix,
1440+
// diagnose as being an obsolescent feature starting in C2y.
1441+
if (radix == 8 && LangOpts.C2y && !SawOctalPrefix && !hadError)
1442+
Diags.Report(TokLoc, diag::warn_unprefixed_octal_deprecated);
1443+
});
1444+
14261445
// For now, the radix is set to 8. If we discover that we have a
14271446
// floating point constant, the radix will change to 10. Octal floating
14281447
// point constants are not permitted (only decimal and hexadecimal).

0 commit comments

Comments
 (0)