diff options
Diffstat (limited to 'clang/lib/Lex/PPDirectives.cpp')
-rw-r--r-- | clang/lib/Lex/PPDirectives.cpp | 88 |
1 files changed, 48 insertions, 40 deletions
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index ba213a878ad..5f80f4487fb 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -129,70 +129,78 @@ void Preprocessor::DiscardUntilEndOfDirective() { } while (Tmp.isNot(tok::eod)); } -/// \brief Lex and validate a macro name, which occurs after a -/// \#define or \#undef. -/// -/// This sets the token kind to eod and discards the rest -/// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if -/// this is due to a a \#define, 2 if \#undef directive, 0 if it is something -/// else (e.g. \#ifdef). -void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { - // Read the token, don't allow macro expansion on it. - LexUnexpandedToken(MacroNameTok); - - if (MacroNameTok.is(tok::code_completion)) { - if (CodeComplete) - CodeComplete->CodeCompleteMacroName(isDefineUndef == 1); - setCodeCompletionReached(); - LexUnexpandedToken(MacroNameTok); - } - +bool Preprocessor::CheckMacroName(Token &MacroNameTok, char isDefineUndef) { // Missing macro name? - if (MacroNameTok.is(tok::eod)) { - Diag(MacroNameTok, diag::err_pp_missing_macro_name); - return; - } + if (MacroNameTok.is(tok::eod)) + return Diag(MacroNameTok, diag::err_pp_missing_macro_name); IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); if (!II) { bool Invalid = false; std::string Spelling = getSpelling(MacroNameTok, &Invalid); if (Invalid) - return; + return Diag(MacroNameTok, diag::err_pp_macro_not_identifier); const IdentifierInfo &Info = Identifiers.get(Spelling); // Allow #defining |and| and friends in microsoft mode. if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MSVCCompat) { MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling)); - return; + return false; } if (Info.isCPlusPlusOperatorKeyword()) // C++ 2.5p2: Alternative tokens behave the same as its primary token // except for their spellings. - Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; - else - Diag(MacroNameTok, diag::err_pp_macro_not_identifier); - // Fall through on error. - } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { + return Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) + << Spelling; + + return Diag(MacroNameTok, diag::err_pp_macro_not_identifier); + } + + if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4. - Diag(MacroNameTok, diag::err_defined_macro_name); - } else if (isDefineUndef == 2 && II->hasMacroDefinition() && - getMacroInfo(II)->isBuiltinMacro()) { + return Diag(MacroNameTok, diag::err_defined_macro_name); + } + + if (isDefineUndef == 2 && II->hasMacroDefinition() && + getMacroInfo(II)->isBuiltinMacro()) { // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 // and C++ [cpp.predefined]p4], but allow it as an extension. Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro); - return; - } else { - // Okay, we got a good identifier node. Return it. - return; } - // Invalid macro name, read and discard the rest of the line. Then set the - // token kind to tok::eod. - MacroNameTok.setKind(tok::eod); - return DiscardUntilEndOfDirective(); + // Okay, we got a good identifier. + return false; +} + +/// \brief Lex and validate a macro name, which occurs after a +/// \#define or \#undef. +/// +/// This sets the token kind to eod and discards the rest +/// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if +/// this is due to a a \#define, 2 if \#undef directive, 0 if it is something +/// else (e.g. \#ifdef). +void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { + // Read the token, don't allow macro expansion on it. + LexUnexpandedToken(MacroNameTok); + + if (MacroNameTok.is(tok::code_completion)) { + if (CodeComplete) + CodeComplete->CodeCompleteMacroName(isDefineUndef == 1); + setCodeCompletionReached(); + LexUnexpandedToken(MacroNameTok); + } + + if (!CheckMacroName(MacroNameTok, isDefineUndef)) + return; + + // Invalid macro name, read and discard the rest of the line and set the + // token kind to tok::eod if necessary. + if (MacroNameTok.isNot(tok::eod)) { + MacroNameTok.setKind(tok::eod); + DiscardUntilEndOfDirective(); + } } /// \brief Ensure that the next token is a tok::eod token. |