} 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.
/// EvaluateDefined - Process a 'defined(sym)' expression.
static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
bool ValueLive, Preprocessor &PP) {
- IdentifierInfo *II;
SourceLocation beginLoc(PeekTok.getLocation());
Result.setBegin(beginLoc);
PP.setCodeCompletionReached();
PP.LexUnexpandedNonComment(PeekTok);
}
-
+
// If we don't have a pp-identifier now, this is an error.
- if ((II = PeekTok.getIdentifierInfo()) == nullptr) {
- PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
+ if (PP.CheckMacroName(PeekTok, 0))
return true;
- }
// Otherwise, we got an identifier, is it defined to something?
+ IdentifierInfo *II = PeekTok.getIdentifierInfo();
Result.Val = II->hasMacroDefinition();
Result.Val.setIsUnsigned(false); // Result is signed intmax_t.