Index: clang/include/clang/Basic/DiagnosticGroups.td =================================================================== --- clang/include/clang/Basic/DiagnosticGroups.td +++ clang/include/clang/Basic/DiagnosticGroups.td @@ -773,6 +773,9 @@ def DuplicateArgDecl : DiagGroup<"duplicate-method-arg">; def SignedEnumBitfield : DiagGroup<"signed-enum-bitfield">; + +def ReservedIdentifier : DiagGroup<"reserved-identifier", [ReservedIdAsMacro]>; + // Unreachable code warning groups. // // The goal is make -Wunreachable-code on by default, in -Wall, or at Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -372,6 +372,10 @@ "%select{used|required to be captured for this use}1">, InGroup, DefaultIgnore; +def warn_reserved_identifier: Warning< + "%0 is a reserved identifier">, + InGroup, DefaultIgnore; + def warn_parameter_size: Warning< "%0 is a large (%1 bytes) pass-by-value argument; " "pass it by reference instead ?">, InGroup; Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -2572,6 +2572,8 @@ SourceLocation Less, SourceLocation Greater); + void warnOnReservedIdentifier(const NamedDecl *D); + Decl *ActOnDeclarator(Scope *S, Declarator &D); NamedDecl *HandleDeclarator(Scope *S, Declarator &D, @@ -4102,6 +4104,7 @@ bool AllowStringTemplate, bool DiagnoseMissing, StringLiteral *StringLit = nullptr); bool isKnownName(StringRef name); + bool IsIdentifierReserved(IdentifierInfo *II); /// Status of the function emission on the CUDA/HIP/OpenMP host/device attrs. enum class FunctionEmissionStatus { Index: clang/lib/Sema/Sema.cpp =================================================================== --- clang/lib/Sema/Sema.cpp +++ clang/lib/Sema/Sema.cpp @@ -2401,6 +2401,45 @@ return Ident___float128; } +bool Sema::IsIdentifierReserved(IdentifierInfo *II) { + + if (!II) + return false; + + if (getLangOpts().ObjC) + return false; + + StringRef Name = II->getName(); + // '_' is a reserved identifier, but it's use is so common (e.g. to store + // ignored values) that we don't warn on it. + if (Name.size() <= 1) + return false; + + // [lex.name] p3 + if (Name[0] == '_') { + + // Perform a lookup at TUScope. If it succeeds, we're at global scope and a + // single '_' is enough to be reserved. + LookupResult IdentifierLookup(*this, II, SourceLocation(), + Sema::LookupAnyName, + Sema::ForExternalRedeclaration); + IdentifierLookup.suppressDiagnostics(); + if (LookupName(IdentifierLookup, TUScope)) + return true; + + // Each name that begins with an underscore followed by an uppercase letter + // or another underscore is reserved. + if (Name[1] == '_' || ('A' <= Name[1] && Name[1] <= 'Z')) + return true; + } + + // Each name that contains a double underscore (__) is reserved. + if (getLangOpts().CPlusPlus && Name.contains("__")) + return true; + + return false; +} + void Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD, CapturedRegionKind K, unsigned OpenMPCaptureLevel) { Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -5544,6 +5544,12 @@ return false; } +void Sema::warnOnReservedIdentifier(const NamedDecl *D) { + if (!Context.getSourceManager().isInSystemHeader(D->getLocation()) && + IsIdentifierReserved(D->getIdentifier())) + Diag(D->getLocation(), diag::warn_reserved_identifier) << D; +} + Decl *Sema::ActOnDeclarator(Scope *S, Declarator &D) { D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration); Decl *Dcl = HandleDeclarator(S, D, MultiTemplateParamsArg()); @@ -5889,6 +5895,9 @@ if (isInOpenMPDeclareTargetContext()) checkDeclIsAllowedInOpenMPTarget(nullptr, New); + if (D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_extern) + warnOnReservedIdentifier(New); + return New; } @@ -13628,6 +13637,8 @@ if (getLangOpts().OpenCL) deduceOpenCLAddressSpace(New); + warnOnReservedIdentifier(New); + return New; } @@ -16282,6 +16293,8 @@ } else if (SkipBody && SkipBody->ShouldSkip) { return SkipBody->Previous; } else { + if (TUK == TUK_Definition) + warnOnReservedIdentifier(New); return New; } } @@ -17094,6 +17107,8 @@ i != end; ++i) { FieldDecl *FD = cast(*i); + warnOnReservedIdentifier(FD); + // Get the type for the field. const Type *FDTy = FD->getType().getTypePtr(); @@ -17819,6 +17834,8 @@ ActOnDocumentableDecl(New); + warnOnReservedIdentifier(New); + return New; } Index: clang/lib/Sema/SemaDeclCXX.cpp =================================================================== --- clang/lib/Sema/SemaDeclCXX.cpp +++ clang/lib/Sema/SemaDeclCXX.cpp @@ -10015,8 +10015,9 @@ DeclContext *LookupDC = dyn_cast(D); if (DeclaratorDecl *DD = dyn_cast(D)) { - for (unsigned i = 0; i < DD->getNumTemplateParameterLists(); ++i) + for (unsigned i = 0; i < DD->getNumTemplateParameterLists(); ++i) { ParameterLists.push_back(DD->getTemplateParameterList(i)); + } if (FunctionDecl *FD = dyn_cast(D)) { if (FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) @@ -10988,6 +10989,9 @@ // for the namespace has the declarations that showed up in that particular // namespace definition. PushDeclContext(NamespcScope, Namespc); + + warnOnReservedIdentifier(Namespc); + return Namespc; } @@ -12662,6 +12666,9 @@ PushOnScopeChains(NewND, S); ActOnDocumentableDecl(NewND); + + warnOnReservedIdentifier(NewND); + return NewND; } Index: clang/lib/Sema/SemaStmt.cpp =================================================================== --- clang/lib/Sema/SemaStmt.cpp +++ clang/lib/Sema/SemaStmt.cpp @@ -541,6 +541,10 @@ return SubStmt; } + if (!Context.getSourceManager().isInSystemHeader(IdentLoc) && + IsIdentifierReserved(TheDecl->getIdentifier())) + Diag(IdentLoc, diag::warn_reserved_identifier) << TheDecl; + // Otherwise, things are good. Fill in the declaration and return it. LabelStmt *LS = new (Context) LabelStmt(IdentLoc, TheDecl, SubStmt); TheDecl->setStmt(LS); Index: clang/lib/Sema/SemaTemplate.cpp =================================================================== --- clang/lib/Sema/SemaTemplate.cpp +++ clang/lib/Sema/SemaTemplate.cpp @@ -1677,6 +1677,9 @@ if (ExportLoc.isValid()) Diag(ExportLoc, diag::warn_template_export_unsupported); + for (NamedDecl *P : Params) + warnOnReservedIdentifier(P); + return TemplateParameterList::Create( Context, TemplateLoc, LAngleLoc, llvm::makeArrayRef(Params.data(), Params.size()), @@ -8686,6 +8689,7 @@ } ActOnDocumentableDecl(NewDecl); + warnOnReservedIdentifier(NewDecl); PushOnScopeChains(NewDecl, S); return NewDecl; } Index: clang/test/Sema/reserved-identifier.c =================================================================== --- /dev/null +++ clang/test/Sema/reserved-identifier.c @@ -0,0 +1,54 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -Wreserved-identifier -Wno-visibility %s + +#define __oof foo__ // expected-warning {{macro name is a reserved identifier}} + +int foo__bar() { return 0; } // no-warning +static int _bar() { return 0; } // expected-warning {{'_bar' is a reserved identifier}} +static int _Bar() { return 0; } // expected-warning {{'_Bar' is a reserved identifier}} +int _foo() { return 0; } // expected-warning {{'_foo' is a reserved identifier}} + +// This one is explicitly skipped by -Wreserved-identifier +void *_; // no-warning + +void foo(unsigned int _Reserved) { // expected-warning {{'_Reserved' is a reserved identifier}} + unsigned int __1 = // expected-warning {{'__1' is a reserved identifier}} + _Reserved; // no-warning + goto __reserved; +__reserved: // expected-warning {{'__reserved' is a reserved identifier}} + ; +} + +enum __menu { // expected-warning {{'__menu' is a reserved identifier}} + __some, // expected-warning {{'__some' is a reserved identifier}} + _Other, // expected-warning {{'_Other' is a reserved identifier}} + _other // expected-warning {{'_other' is a reserved identifier}} +}; + +struct __babar { // expected-warning {{'__babar' is a reserved identifier}} +}; + +// No warning for forward declaration, they may come from uncontrolled source. +struct _Zebulon; // no-warning + +typedef struct { + int _Field; // expected-warning {{'_Field' is a reserved identifier}} + int _field; // expected-warning {{'_field' is a reserved identifier}} +} _Typedef; // expected-warning {{'_Typedef' is a reserved identifier}} + +int foobar() { + return foo__bar(); // no-warning +} + +struct _reserved { // expected-warning {{'_reserved' is a reserved identifier}} + int a; +} cunf(void) { + return (struct _reserved){1}; +} + +// FIXME: According to clang declaration context layering, _preserved belongs to +// the translation unit, so we emit a warning. It's unclear that's what the +// standard mandate, but it's such a corner case we can live with it. +void func(struct _preserved { int a; } r) {} // expected-warning {{'_preserved' is a reserved identifier}} + +// No warn for extern declaration, as they may be controlled by a third-party. +extern char *_strdup(const char *); // no-warning Index: clang/test/Sema/reserved-identifier.cpp =================================================================== --- /dev/null +++ clang/test/Sema/reserved-identifier.cpp @@ -0,0 +1,80 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify -Wreserved-identifier %s + +int foo__bar() { return 0; } // expected-warning {{'foo__bar' is a reserved identifier}} +static int _bar() { return 0; } // expected-warning {{'_bar' is a reserved identifier}} +static int _Bar() { return 0; } // expected-warning {{'_Bar' is a reserved identifier}} +int _barbouille() { return 0; } // expected-warning {{'_barbouille' is a reserved identifier}} + +void foo(unsigned int _Reserved) { // expected-warning {{'_Reserved' is a reserved identifier}} + unsigned int __1 = // expected-warning {{'__1' is a reserved identifier}} + _Reserved; // no-warning +} + +// This one is explicitly skipped by -Wreserved-identifier +void *_; // no-warning + +template constexpr bool __toucan = true; // expected-warning {{'__toucan' is a reserved identifier}} + +template +concept _Barbotine = __toucan; // expected-warning {{'_Barbotine' is a reserved identifier}} + +template // expected-warning {{'__' is a reserved identifier}} +struct BarbeNoire {}; + +template // expected-warning {{'__' is a reserved identifier}} +void BarbeRousse() {} + +namespace _Barbidur { // expected-warning {{'_Barbidur' is a reserved identifier}} + +struct __barbidou {}; // expected-warning {{'__barbidou' is a reserved identifier}} +struct _barbidou {}; // no-warning + +int __barbouille; // expected-warning {{'__barbouille' is a reserved identifier}} +int _barbouille; // expected-warning {{'_barbouille' is a reserved identifier}} + +int __babar() { return 0; } // expected-warning {{'__babar' is a reserved identifier}} +int _babar() { return 0; } // no-warning + +} // namespace _Barbidur + +class __barbapapa { // expected-warning {{'__barbapapa' is a reserved identifier}} + void _barbabelle() {} // no-warning + int _Barbalala; // expected-warning {{'_Barbalala' is a reserved identifier}} +}; + +enum class __menu { // expected-warning {{'__menu' is a reserved identifier}} + __some, // expected-warning {{'__some' is a reserved identifier}} + _Other, // expected-warning {{'_Other' is a reserved identifier}} + _other // no-warning +}; + +enum _Menu { // expected-warning {{'_Menu' is a reserved identifier}} + _OtheR_, // expected-warning {{'_OtheR_' is a reserved identifier}} + _other_ // expected-warning {{'_other_' is a reserved identifier}} +}; + +enum { + __some, // expected-warning {{'__some' is a reserved identifier}} + _Other, // expected-warning {{'_Other' is a reserved identifier}} + _other // expected-warning {{'_other' is a reserved identifier}} +}; + +static union { + int _barbeFleurie; // no-warning +}; + +using _Barbamama = __barbapapa; // expected-warning {{'_Barbamama' is a reserved identifier}} + +int foobar() { + return foo__bar(); // no-warning +} + +namespace { +// we skip this one because it's not at top-level. +int _barbatruc; // expected-warning {{'_barbatruc' is a reserved identifier}} +} + +long double operator"" _BarbeBleue(long double) // no-warning +{ + return 0.; +}