diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 6c337801a8435..6468e62889413 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -371,8 +371,12 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, SourceLocation MacroNameLoc = MacroNameTok.getLocation(); if (ShadowFlag) *ShadowFlag = false; - if (!SourceMgr.isInSystemHeader(MacroNameLoc) && - (SourceMgr.getBufferName(MacroNameLoc) != "")) { + // Macro names with reserved identifiers are accepted if built-in or passed + // through the command line (the later may be present if -dD was used to + // generate the preprocessed file). + bool IsBuiltinOrCmd = SourceMgr.isWrittenInBuiltinFile(MacroNameLoc) || + SourceMgr.isWrittenInCommandLineFile(MacroNameLoc); + if (!IsBuiltinOrCmd && !SourceMgr.isInSystemHeader(MacroNameLoc)) { MacroDiag D = MD_NoWarn; if (isDefineUndef == MU_Define) { D = shouldWarnOnMacroDef(*this, II); diff --git a/clang/test/Preprocessor/macro_reserved.i b/clang/test/Preprocessor/macro_reserved.i new file mode 100644 index 0000000000000..c9ed044e4c931 --- /dev/null +++ b/clang/test/Preprocessor/macro_reserved.i @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -x cpp-output %s +#pragma clang diagnostic push +#pragma clang diagnostic warning "-Wreserved-macro-identifier" +# 1 "" 1 +#define __BUILTIN__ +# 2 "" 1 +#define __CMD__ +# 3 "biz.cpp" 1 +#define __SOME_FILE__ // expected-warning {{macro name is a reserved identifier}} +int v; +#pragma clang diagnostic pop