File tree Expand file tree Collapse file tree 4 files changed +15
-22
lines changed Expand file tree Collapse file tree 4 files changed +15
-22
lines changed Original file line number Diff line number Diff line change @@ -155,7 +155,7 @@ C Language Changes
155155 diagnoses implicit conversion from ``void * `` to another pointer type as
156156 being incompatible with C++. (#GH17792)
157157- Added ``-Wc++-keyword ``, grouped under ``-Wc++-compat ``, which diagnoses when
158- a C++ keyword is used as an identifier in C. Partially addresses #GH21898.
158+ a C++ keyword is used as an identifier in C. ( #GH21898)
159159- Added ``-Wc++-hidden-decl ``, grouped under ``-Wc++-compat ``, which diagnoses
160160 use of tag types which are visible in C but not visible in C++ due to scoping
161161 rules. e.g.,
Original file line number Diff line number Diff line change @@ -287,7 +287,12 @@ static void AddKeyword(StringRef Keyword,
287287 // Don't add this keyword if disabled in this language and isn't otherwise
288288 // special.
289289 if (AddResult == KS_Disabled) {
290- if (IsKeywordInCpp (Flags))
290+ // We do not consider any identifiers to be C++ keywords when in
291+ // Objective-C because @ effectively introduces a custom grammar where C++
292+ // keywords can be used (and similar for selectors). We could enable this
293+ // for Objective-C, but it would require more logic to ensure we do not
294+ // issue compatibility diagnostics in these cases.
295+ if (!LangOpts.ObjC && IsKeywordInCpp (Flags))
291296 MarkIdentifierAsKeywordInCpp (Table, Keyword);
292297 return ;
293298 }
Original file line number Diff line number Diff line change @@ -836,23 +836,8 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) {
836836
837837 // If this identifier would be a keyword in C++, diagnose as a compatibility
838838 // issue.
839- if (II.IsKeywordInCPlusPlus () && !DisableMacroExpansion) {
840- // Objective-C is special in that something like @class is two tokens,
841- // where 'class' is an identifier. We don't want to diagnose @class as
842- // using an identifier reserved in C++, but we do still want to diagnose
843- // something like 'int class;'. In Objective-C, @ basically introduces an
844- // entirely new grammar, so no identier should be flagged as reserved.
845- bool Diagnose = true ;
846- if (getLangOpts ().ObjC ) {
847- std::optional<Token> PrevTok =
848- Lexer::findPreviousToken (Identifier.getLocation (), getSourceManager (),
849- getLangOpts (), /* IncludeComments=*/ false );
850- Diagnose = !PrevTok || !PrevTok->is (tok::at);
851- }
852-
853- if (Diagnose)
854- Diag (Identifier, diag::warn_pp_identifier_is_cpp_keyword) << &II;
855- }
839+ if (II.IsKeywordInCPlusPlus () && !DisableMacroExpansion)
840+ Diag (Identifier, diag::warn_pp_identifier_is_cpp_keyword) << &II;
856841
857842 // If this is an extension token, diagnose its use.
858843 // We avoid diagnosing tokens that originate from macro definitions.
Original file line number Diff line number Diff line change 11// RUN: %clang_cc1 -fsyntax-only -verify -Wc++-keyword %s
2+ // expected-no-diagnostics
23
3- // Show that @class is not diagnosed as use of a C++ keyword in Objective-C,
4- // but that other uses of class are diagnosed.
4+ // Show that we do not diagnose any C++ keywords in Objective-C.
55
66@class Foo; // Okay, Objective-C @ keyword, not a regular identifier
77
8- int class = 12 ; // expected-warning {{identifier 'class' conflicts with a C++ keyword}}
8+ // FIXME: it would be nice to diagnose this, but it is intentionally allowed
9+ // due to @ and selectors allowing C++ keywords in ways that are supposed to be
10+ // contextually compatible with C++.
11+ int class = 12 ;
912
You can’t perform that action at this time.
0 commit comments