Skip to content

Commit fe35a7e

Browse files
committed
Merge commit '85c810060e1a' from llvm.org/main into next
2 parents c7c3003 + 85c8100 commit fe35a7e

File tree

11 files changed

+341
-12
lines changed

11 files changed

+341
-12
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ C Language Changes
154154
- Added ``-Wimplicit-void-ptr-cast``, grouped under ``-Wc++-compat``, which
155155
diagnoses implicit conversion from ``void *`` to another pointer type as
156156
being incompatible with C++. (#GH17792)
157+
- Added ``-Wc++-keyword``, grouped under ``-Wc++-compat``, which diagnoses when
158+
a C++ keyword is used as an identifier in C. (#GH21898)
157159
- Added ``-Wc++-hidden-decl``, grouped under ``-Wc++-compat``, which diagnoses
158160
use of tag types which are visible in C but not visible in C++ due to scoping
159161
rules. e.g.,
@@ -482,6 +484,8 @@ Improvements to Clang's diagnostics
482484
- ``-Winitializer-overrides`` and ``-Wreorder-init-list`` are now grouped under
483485
the ``-Wc99-designator`` diagnostic group, as they also are about the
484486
behavior of the C99 feature as it was introduced into C++20. Fixes #GH47037
487+
- ``-Wreserved-identifier`` now fires on reserved parameter names in a function
488+
declaration which is not a definition.
485489

486490
Improvements to Clang's time-trace
487491
----------------------------------

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def C99Compat : DiagGroup<"c99-compat">;
157157
def C23Compat : DiagGroup<"c23-compat">;
158158
def : DiagGroup<"c2x-compat", [C23Compat]>;
159159

160+
def CppKeywordInC : DiagGroup<"c++-keyword">;
160161
def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;
161162
def InitStringTooLongMissingNonString :
162163
DiagGroup<"unterminated-string-initialization">;
@@ -178,9 +179,9 @@ def ImplicitIntToEnumCast : DiagGroup<"implicit-int-enum-cast",
178179
[ImplicitEnumEnumCast]>;
179180
def TentativeDefnCompat : DiagGroup<"tentative-definition-compat">;
180181
def CXXCompat: DiagGroup<"c++-compat", [ImplicitVoidPtrCast, DefaultConstInit,
181-
ImplicitIntToEnumCast, HiddenCppDecl,
182-
InitStringTooLongForCpp,
183-
TentativeDefnCompat,
182+
ImplicitIntToEnumCast, CppKeywordInC,
183+
HiddenCppDecl, TentativeDefnCompat,
184+
InitStringTooLongForCpp,
184185
DuplicateDeclSpecifier]>;
185186

186187
def ExternCCompat : DiagGroup<"extern-c-compat">;

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ def warn_pp_macro_is_reserved_attribute_id : Warning<
421421
def warn_pp_objc_macro_redef_ignored : Warning<
422422
"ignoring redefinition of Objective-C qualifier macro">,
423423
InGroup<DiagGroup<"objc-macro-redefinition">>;
424+
def warn_pp_identifier_is_cpp_keyword : Warning<
425+
"identifier %0 conflicts with a C++ keyword">,
426+
InGroup<CppKeywordInC>, DefaultIgnore;
424427

425428
def pp_invalid_string_literal : Warning<
426429
"invalid string literal, ignoring final '\\'">;

clang/include/clang/Basic/IdentifierTable.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo {
195195
LLVM_PREFERRED_TYPE(bool)
196196
unsigned IsFinal : 1;
197197

198-
// 22 bits left in a 64-bit word.
198+
// True if this identifier would be a keyword in C++ mode.
199+
LLVM_PREFERRED_TYPE(bool)
200+
unsigned IsKeywordInCpp : 1;
201+
202+
// 21 bits left in a 64-bit word.
199203

200204
// Managed by the language front-end.
201205
void *FETokenInfo = nullptr;
@@ -212,7 +216,7 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo {
212216
IsFromAST(false), ChangedAfterLoad(false), FEChangedAfterLoad(false),
213217
RevertedTokenID(false), OutOfDate(false), IsModulesImport(false),
214218
IsMangledOpenMPVariantName(false), IsDeprecatedMacro(false),
215-
IsRestrictExpansion(false), IsFinal(false) {}
219+
IsRestrictExpansion(false), IsFinal(false), IsKeywordInCpp(false) {}
216220

217221
public:
218222
IdentifierInfo(const IdentifierInfo &) = delete;
@@ -444,6 +448,10 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo {
444448
}
445449
bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; }
446450

451+
/// Return true if this identifier would be a keyword in C++ mode.
452+
bool IsKeywordInCPlusPlus() const { return IsKeywordInCpp; }
453+
void setIsKeywordInCPlusPlus(bool Val = true) { IsKeywordInCpp = Val; }
454+
447455
/// Return true if this token is a keyword in the specified language.
448456
bool isKeyword(const LangOptions &LangOpts) const;
449457

@@ -462,6 +470,7 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo {
462470
/// If this returns false, we know that HandleIdentifier will not affect
463471
/// the token.
464472
bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; }
473+
void setHandleIdentifierCase(bool Val = true) { NeedsHandleIdentifier = Val; }
465474

466475
/// Return true if the identifier in its current state was loaded
467476
/// from an AST file.

clang/include/clang/Basic/TokenKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ ANNOTATION(embed)
10531053
#undef TYPE_TRAIT_2
10541054
#undef TYPE_TRAIT_1
10551055
#undef TYPE_TRAIT
1056+
#undef MODULES_KEYWORD
10561057
#undef CXX20_KEYWORD
10571058
#undef CXX11_KEYWORD
10581059
#undef KEYWORD

clang/lib/Basic/IdentifierTable.cpp

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,32 @@ static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
260260
return CurStatus;
261261
}
262262

263+
static bool IsKeywordInCpp(unsigned Flags) {
264+
while (Flags != 0) {
265+
unsigned CurFlag = Flags & ~(Flags - 1);
266+
Flags = Flags & ~CurFlag;
267+
switch (static_cast<TokenKey>(CurFlag)) {
268+
case KEYCXX:
269+
case KEYCXX11:
270+
case KEYCXX20:
271+
case BOOLSUPPORT:
272+
case WCHARSUPPORT:
273+
case CHAR8SUPPORT:
274+
return true;
275+
default:
276+
break; // Go to the next flag, try again.
277+
}
278+
}
279+
return false;
280+
}
281+
282+
static void MarkIdentifierAsKeywordInCpp(IdentifierTable &Table,
283+
StringRef Name) {
284+
IdentifierInfo &II = Table.get(Name, tok::identifier);
285+
II.setIsKeywordInCPlusPlus();
286+
II.setHandleIdentifierCase();
287+
}
288+
263289
/// AddKeyword - This method is used to associate a token ID with specific
264290
/// identifiers because they are language keywords. This causes the lexer to
265291
/// automatically map matching identifiers to specialized token codes.
@@ -268,8 +294,18 @@ static void AddKeyword(StringRef Keyword,
268294
const LangOptions &LangOpts, IdentifierTable &Table) {
269295
KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
270296

271-
// Don't add this keyword if disabled in this language.
272-
if (AddResult == KS_Disabled) return;
297+
// Don't add this keyword if disabled in this language and isn't otherwise
298+
// special.
299+
if (AddResult == KS_Disabled) {
300+
// We do not consider any identifiers to be C++ keywords when in
301+
// Objective-C because @ effectively introduces a custom grammar where C++
302+
// keywords can be used (and similar for selectors). We could enable this
303+
// for Objective-C, but it would require more logic to ensure we do not
304+
// issue compatibility diagnostics in these cases.
305+
if (!LangOpts.ObjC && IsKeywordInCpp(Flags))
306+
MarkIdentifierAsKeywordInCpp(Table, Keyword);
307+
return;
308+
}
273309

274310
IdentifierInfo &Info =
275311
Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode);
@@ -314,9 +350,11 @@ void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
314350
#define ALIAS(NAME, TOK, FLAGS) \
315351
AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
316352
FLAGS, LangOpts, *this);
317-
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
318-
if (LangOpts.CXXOperatorNames) \
319-
AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
353+
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
354+
if (LangOpts.CXXOperatorNames) \
355+
AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this); \
356+
else \
357+
MarkIdentifierAsKeywordInCpp(*this, StringRef(#NAME));
320358
#define OBJC_AT_KEYWORD(NAME) \
321359
if (LangOpts.ObjC) \
322360
AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);

clang/lib/Lex/Preprocessor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,11 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) {
840840
II.setIsFutureCompatKeyword(false);
841841
}
842842

843+
// If this identifier would be a keyword in C++, diagnose as a compatibility
844+
// issue.
845+
if (II.IsKeywordInCPlusPlus() && !DisableMacroExpansion)
846+
Diag(Identifier, diag::warn_pp_identifier_is_cpp_keyword) << &II;
847+
843848
// If this is an extension token, diagnose its use.
844849
// We avoid diagnosing tokens that originate from macro definitions.
845850
// FIXME: This warning is disabled in cases where it shouldn't be,

clang/lib/Sema/SemaDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@
6161
#include "clang/Sema/SemaWasm.h"
6262
#include "clang/Sema/Template.h"
6363
#include "llvm/ADT/STLForwardCompat.h"
64+
#include "llvm/ADT/SmallPtrSet.h"
6465
#include "llvm/ADT/SmallString.h"
6566
#include "llvm/ADT/StringExtras.h"
67+
#include "llvm/ADT/StringSwitch.h"
6668
#include "llvm/Support/SaveAndRestore.h"
6769
#include "llvm/TargetParser/Triple.h"
6870
#include <algorithm>
@@ -11390,6 +11392,15 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
1139011392
}
1139111393
/* TO_UPSTREAM(BoundsSafety) OFF*/
1139211394

11395+
// If this declarator is a declaration and not a definition, its parameters
11396+
// will not be pushed onto a scope chain. That means we will not issue any
11397+
// reserved identifier warnings for the declaration, but we will for the
11398+
// definition. Handle those here.
11399+
if (!D.isFunctionDefinition()) {
11400+
for (const ParmVarDecl *PVD : Params)
11401+
warnOnReservedIdentifier(PVD);
11402+
}
11403+
1139311404
if (D.getDeclSpec().isNoreturnSpecified())
1139411405
NewFD->addAttr(
1139511406
C11NoReturnAttr::Create(Context, D.getDeclSpec().getNoreturnSpecLoc()));

clang/test/OpenMP/assumes_messages.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,14 @@
5959
#pragma omp begin assumes ext // expected-warning {{valid begin assumes clauses start with 'ext_', 'absent', 'contains', 'holds', 'no_openmp', 'no_openmp_routines', 'no_openmp_constructs', 'no_parallelism'; token will be ignored}}
6060
#pragma omp end assumes
6161

62-
#pragma omp assumes ext_123(not allowed) // expected-warning {{'ext_123' clause should not be followed by arguments; tokens will be ignored}} expected-note {{the ignored tokens spans until here}}
63-
#pragma omp begin assumes ext_123(not allowed) // expected-warning {{'ext_123' clause should not be followed by arguments; tokens will be ignored}} expected-note {{the ignored tokens spans until here}}
62+
// FIXME: We should be getting an expected note about where the span of ignored
63+
// tokens ends. However, error recovery ends up lexing the 'not' token,
64+
// emitting a (silenced) diagnostic about use of a C++ keyword in C, and the
65+
// note gets associated with *that* (silenced) diagnostic. This is an existing
66+
// issue that also happens with error recovery of reserved identifiers or
67+
// extension tokens, but is unfortunate nonetheless.
68+
#pragma omp assumes ext_123(not allowed) // expected-warning {{'ext_123' clause should not be followed by arguments; tokens will be ignored}}
69+
#pragma omp begin assumes ext_123(not allowed) // expected-warning {{'ext_123' clause should not be followed by arguments; tokens will be ignored}}
6470
#pragma omp end assumes
6571

6672
#pragma omp end assumes // expected-error {{'#pragma omp end assumes' with no matching '#pragma omp begin assumes'}}

0 commit comments

Comments
 (0)