const char *Word;
} PreprocessorDirs[] = {
{ tgtok::Ifdef, "ifdef" },
+ { tgtok::Ifndef, "ifndef" },
{ tgtok::Else, "else" },
{ tgtok::Endif, "endif" },
{ tgtok::Define, "define" }
PrintFatalError("lexPreprocessor() called for unknown "
"preprocessor directive");
- if (Kind == tgtok::Ifdef) {
+ if (Kind == tgtok::Ifdef || Kind == tgtok::Ifndef) {
StringRef MacroName = prepLexMacroName();
+ StringRef IfTokName = Kind == tgtok::Ifdef ? "#ifdef" : "#ifndef";
if (MacroName.empty())
- return ReturnError(TokStart, "Expected macro name after #ifdef");
+ return ReturnError(TokStart, "Expected macro name after " + IfTokName);
bool MacroIsDefined = DefinedMacros.count(MacroName) != 0;
+ // Canonicalize ifndef to ifdef equivalent
+ if (Kind == tgtok::Ifndef) {
+ MacroIsDefined = !MacroIsDefined;
+ Kind = tgtok::Ifdef;
+ }
+
// Regardless of whether we are processing tokens or not,
// we put the #ifdef control on stack.
PrepIncludeStack.back()->push_back(
{Kind, MacroIsDefined, SMLoc::getFromPointer(TokStart)});
if (!prepSkipDirectiveEnd())
- return ReturnError(CurPtr,
- "Only comments are supported after #ifdef NAME");
+ return ReturnError(CurPtr, "Only comments are supported after " +
+ IfTokName + " NAME");
// If we were not processing tokens before this #ifdef,
// then just return back to the lines skipping code.
// Check if this #else is correct before calling prepSkipDirectiveEnd(),
// which will move CurPtr away from the beginning of #else.
if (PrepIncludeStack.back()->empty())
- return ReturnError(TokStart, "#else without #ifdef");
+ return ReturnError(TokStart, "#else without #ifdef or #ifndef");
PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();