Skip to content

Commit c782dfe

Browse files
author
git apple-llvm automerger
committed
Merge commit '7063b76b0248' from llvm.org/main into next
2 parents 12ed9a4 + 7063b76 commit c782dfe

File tree

8 files changed

+31
-8
lines changed

8 files changed

+31
-8
lines changed

clang/include/clang/Basic/IdentifierTable.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ enum class ReservedIdentifierStatus {
4848
ContainsDoubleUnderscore,
4949
};
5050

51+
/// Determine whether an identifier is reserved for use as a name at global
52+
/// scope. Such identifiers might be implementation-specific global functions
53+
/// or variables.
54+
inline bool isReservedAtGlobalScope(ReservedIdentifierStatus Status) {
55+
return Status != ReservedIdentifierStatus::NotReserved;
56+
}
57+
58+
/// Determine whether an identifier is reserved in all contexts. Such
59+
/// identifiers might be implementation-specific keywords or macros, for
60+
/// example.
61+
inline bool isReservedInAllContexts(ReservedIdentifierStatus Status) {
62+
return Status != ReservedIdentifierStatus::NotReserved &&
63+
Status != ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope;
64+
}
65+
5166
/// A simple pair of identifier info and location.
5267
using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>;
5368

clang/lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ NamedDecl::isReserved(const LangOptions &LangOpts) const {
10881088
return ReservedIdentifierStatus::NotReserved;
10891089

10901090
ReservedIdentifierStatus Status = II->isReserved(LangOpts);
1091-
if (Status == ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope) {
1091+
if (isReservedAtGlobalScope(Status) && !isReservedInAllContexts(Status)) {
10921092
// Check if we're at TU level or not.
10931093
if (isa<ParmVarDecl>(this) || isTemplateParameter())
10941094
return ReservedIdentifierStatus::NotReserved;

clang/lib/Lex/PPDirectives.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static bool isForModuleBuilding(Module *M, StringRef CurrentModule,
129129

130130
static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
131131
const LangOptions &Lang = PP.getLangOpts();
132-
if (II->isReserved(Lang) != ReservedIdentifierStatus::NotReserved) {
132+
if (isReservedInAllContexts(II->isReserved(Lang))) {
133133
// list from:
134134
// - https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
135135
// - https://docs.microsoft.com/en-us/cpp/c-runtime-library/security-features-in-the-crt?view=msvc-160
@@ -183,7 +183,7 @@ static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
183183
static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
184184
const LangOptions &Lang = PP.getLangOpts();
185185
// Do not warn on keyword undef. It is generally harmless and widely used.
186-
if (II->isReserved(Lang) != ReservedIdentifierStatus::NotReserved)
186+
if (isReservedInAllContexts(II->isReserved(Lang)))
187187
return MD_ReservedMacro;
188188
return MD_NoWarn;
189189
}

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,9 +700,7 @@ ResultBuilder::ShadowMapEntry::end() const {
700700
static bool shouldIgnoreDueToReservedName(const NamedDecl *ND, Sema &SemaRef) {
701701
ReservedIdentifierStatus Status = ND->isReserved(SemaRef.getLangOpts());
702702
// Ignore reserved names for compiler provided decls.
703-
if ((Status != ReservedIdentifierStatus::NotReserved) &&
704-
(Status != ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope) &&
705-
ND->getLocation().isInvalid())
703+
if (isReservedInAllContexts(Status) && ND->getLocation().isInvalid())
706704
return true;
707705

708706
// For system headers ignore only double-underscore names.

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ bool Sema::checkLiteralOperatorId(const CXXScopeSpec &SS,
494494
IdentifierInfo *II = Name.Identifier;
495495
ReservedIdentifierStatus Status = II->isReserved(PP.getLangOpts());
496496
SourceLocation Loc = Name.getEndLoc();
497-
if (Status != ReservedIdentifierStatus::NotReserved &&
497+
if (isReservedInAllContexts(Status) &&
498498
!PP.getSourceManager().isInSystemHeader(Loc)) {
499499
Diag(Loc, diag::warn_reserved_extern_symbol)
500500
<< II << static_cast<int>(Status)

clang/lib/Sema/SemaStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ Sema::ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl,
548548
}
549549

550550
ReservedIdentifierStatus Status = TheDecl->isReserved(getLangOpts());
551-
if (Status != ReservedIdentifierStatus::NotReserved &&
551+
if (isReservedInAllContexts(Status) &&
552552
!Context.getSourceManager().isInSystemHeader(IdentLoc))
553553
Diag(IdentLoc, diag::warn_reserved_extern_symbol)
554554
<< TheDecl << static_cast<int>(Status);

clang/test/Sema/reserved-identifier.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ void foo(unsigned int _Reserved) { // expected-warning {{identifier '_Reserved'
1616
goto __reserved; // expected-warning {{identifier '__reserved' is reserved because it starts with '__'}}
1717
__reserved: // expected-warning {{identifier '__reserved' is reserved because it starts with '__'}}
1818
;
19+
goto _not_reserved;
20+
_not_reserved: ;
1921
}
2022

2123
void foot(unsigned int _not_reserved) {} // no-warning

clang/test/Sema/reserved-identifier.cpp renamed to clang/test/SemaCXX/reserved-identifier.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ long double operator""_SacreBleu(long double) // no-warning
8989
long double sacrebleu = operator"" _SacreBleu(1.2); // expected-warning {{identifier '_SacreBleu' is reserved because it starts with '_' followed by a capital letter}}
9090
long double sangbleu = operator""_SacreBleu(1.2); // no-warning
9191

92+
void operator"" _lowercase(unsigned long long); // no-warning
93+
void operator""_lowercase(unsigned long long); // no-warning
94+
9295
struct _BarbeRouge { // expected-warning {{identifier '_BarbeRouge' is reserved because it starts with '_' followed by a capital letter}}
9396
} p;
9497
struct _BarbeNoire { // expected-warning {{identifier '_BarbeNoire' is reserved because it starts with '_' followed by a capital letter}}
@@ -97,3 +100,8 @@ struct _BarbeNoire { // expected-warning {{identifier '_BarbeNoire' is reserved
97100
struct Any {
98101
friend void _barbegrise(); // expected-warning {{identifier '_barbegrise' is reserved because it starts with '_' at global scope}}
99102
};
103+
104+
#define _not_reserved
105+
#define _Reserved // expected-warning {{macro name is a reserved identifier}}
106+
#undef _not_reserved
107+
#undef _Reserved // expected-warning {{macro name is a reserved identifier}}

0 commit comments

Comments
 (0)