diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -3023,6 +3023,10 @@ IdentifierInfo *IfNDefMacro = nullptr; const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro); const bool ConditionalTrue = DER.Conditional; + // Lexer might become invalid if we hit code completion point while evulating + // expression. + if (!CurPPLexer) + return; // If this condition is equivalent to #ifndef X, and if this is the first // directive seen, handle it for the multiple-include optimization. diff --git a/clang/test/CodeCompletion/crash-if-directive.cpp b/clang/test/CodeCompletion/crash-if-directive.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CodeCompletion/crash-if-directive.cpp @@ -0,0 +1,3 @@ +#define FOO(X) X + 1 +#if FOO( +// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:2:9 %s | FileCheck %s --allow-empty