Use enumerators instead of hardcoded integers when processing macro names.

llvm-svn: 220572
This commit is contained in:
Serge Pavlov 2014-10-24 17:31:32 +00:00
parent e9a5f03716
commit d024f528b4
3 changed files with 24 additions and 16 deletions

View File

@ -79,6 +79,13 @@ public:
}
};
/// \brief Context in which macro name is used.
enum MacroUse {
MU_Other = 0, // other than #define or #undef
MU_Define = 1, // macro name specified in #define
MU_Undef = 2 // macro name specified in #undef
};
/// \brief Engages in a tight little dance with the lexer to efficiently
/// preprocess tokens.
///
@ -1360,7 +1367,7 @@ public:
/// followed by EOD. Return true if the token is not a valid on-off-switch.
bool LexOnOffSwitch(tok::OnOffSwitch &OOS);
bool CheckMacroName(Token &MacroNameTok, char isDefineUndef);
bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef);
private:
@ -1404,7 +1411,7 @@ private:
///
/// This emits a diagnostic, sets the token kind to eod,
/// and discards the rest of the macro line if the macro name is invalid.
void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0);
void ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef = MU_Other);
/// The ( starting an argument list of a macro definition has just been read.
/// Lex the rest of the arguments and the closing ), updating \p MI with

View File

@ -100,7 +100,7 @@ void Preprocessor::DiscardUntilEndOfDirective() {
} while (Tmp.isNot(tok::eod));
}
bool Preprocessor::CheckMacroName(Token &MacroNameTok, char isDefineUndef) {
bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef) {
// Missing macro name?
if (MacroNameTok.is(tok::eod))
return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
@ -128,12 +128,12 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, char isDefineUndef) {
MacroNameTok.setIdentifierInfo(II);
}
if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
// Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
return Diag(MacroNameTok, diag::err_defined_macro_name);
}
if (isDefineUndef == 2 && II->hasMacroDefinition() &&
if (isDefineUndef == MU_Undef && 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.
@ -147,17 +147,18 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, char isDefineUndef) {
/// \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) {
/// This sets the token kind to eod and discards the rest of the macro line if
/// the macro name is invalid.
///
/// \param MacroNameTok Token that is expected to be a macro name.
/// \papam isDefineUndef Context in which macro is used.
void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse 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);
CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
setCodeCompletionReached();
LexUnexpandedToken(MacroNameTok);
}
@ -1194,7 +1195,7 @@ void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
/// \brief Handle a #public directive.
void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
Token MacroNameTok;
ReadMacroName(MacroNameTok, 2);
ReadMacroName(MacroNameTok, MU_Undef);
// Error reading macro name? If so, diagnostic already issued.
if (MacroNameTok.is(tok::eod))
@ -1221,7 +1222,7 @@ void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
/// \brief Handle a #private directive.
void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
Token MacroNameTok;
ReadMacroName(MacroNameTok, 2);
ReadMacroName(MacroNameTok, MU_Undef);
// Error reading macro name? If so, diagnostic already issued.
if (MacroNameTok.is(tok::eod))
@ -1897,7 +1898,7 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok,
++NumDefined;
Token MacroNameTok;
ReadMacroName(MacroNameTok, 1);
ReadMacroName(MacroNameTok, MU_Define);
// Error reading macro name? If so, diagnostic already issued.
if (MacroNameTok.is(tok::eod))
@ -2145,7 +2146,7 @@ void Preprocessor::HandleUndefDirective(Token &UndefTok) {
++NumUndefined;
Token MacroNameTok;
ReadMacroName(MacroNameTok, 2);
ReadMacroName(MacroNameTok, MU_Undef);
// Error reading macro name? If so, diagnostic already issued.
if (MacroNameTok.is(tok::eod))

View File

@ -103,7 +103,7 @@ static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
}
// If we don't have a pp-identifier now, this is an error.
if (PP.CheckMacroName(PeekTok, 0))
if (PP.CheckMacroName(PeekTok, MU_Other))
return true;
// Otherwise, we got an identifier, is it defined to something?