Skip to content

Commit 40812df

Browse files
committed
[Clang] Do not warn for serialized builtin or command-line definitions
When using -dD to generte a preprocessed output, the #define directives are preserved. This includes builtin and command line definitions. Before, clang would warn for reserved identifiers for serialzied builtin and command-line definitions. This patch adds an exception to these cases.
1 parent 5a0da33 commit 40812df

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

clang/lib/Lex/PPDirectives.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,12 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
371371
SourceLocation MacroNameLoc = MacroNameTok.getLocation();
372372
if (ShadowFlag)
373373
*ShadowFlag = false;
374-
if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
375-
(SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
374+
// Macro names with reserved identifiers are accepted if built-in or passed
375+
// through the command line (the later may be present if -dD was used to
376+
// generate the preprocessed file).
377+
bool IsBuiltinOrCmd = SourceMgr.isWrittenInBuiltinFile(MacroNameLoc) ||
378+
SourceMgr.isWrittenInCommandLineFile(MacroNameLoc);
379+
if (!IsBuiltinOrCmd && !SourceMgr.isInSystemHeader(MacroNameLoc)) {
376380
MacroDiag D = MD_NoWarn;
377381
if (isDefineUndef == MU_Define) {
378382
D = shouldWarnOnMacroDef(*this, II);

clang/test/Preprocessor/macro_reserved.i

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#pragma clang diagnostic push
33
#pragma clang diagnostic warning "-Wreserved-macro-identifier"
44
# 1 "<built-in>" 1
5-
#define __BUILTIN__ // expected-warning {{macro name is a reserved identifier}}
5+
#define __BUILTIN__
66
# 2 "<command line>" 1
7-
#define __CMD__ // expected-warning {{macro name is a reserved identifier}}
7+
#define __CMD__
88
# 3 "biz.cpp" 1
99
#define __SOME_FILE__ // expected-warning {{macro name is a reserved identifier}}
1010
int v;

0 commit comments

Comments
 (0)