Author: Kadir Cetinkaya Date: 2021-05-10T11:24:27+02:00 New Revision: 761f3d16753ecea625173729dd8c53df022cd4ab
URL: https://github.com/llvm/llvm-project/commit/761f3d16753ecea625173729dd8c53df022cd4ab DIFF: https://github.com/llvm/llvm-project/commit/761f3d16753ecea625173729dd8c53df022cd4ab.diff LOG: [clang][PreProcessor] Cutoff parsing after hitting completion point This fixes a crash caused by Lexers being invalidated at code completion points in https://github.com/llvm/llvm-project/blob/main/clang/lib/Lex/PPLexerChange.cpp#L520. Differential Revision: https://reviews.llvm.org/D102069 Added: clang/test/CodeCompletion/crash-if-directive.cpp Modified: clang/lib/Lex/PPDirectives.cpp Removed: ################################################################################ diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index a771b7c5d122d..d5cb46f75aec3 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -595,6 +595,9 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc, CurPPLexer->LexingRawMode = false; IdentifierInfo *IfNDefMacro = nullptr; DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro); + // Stop if Lexer became invalid after hitting code completion token. + if (!CurPPLexer) + return; const bool CondValue = DER.Conditional; CurPPLexer->LexingRawMode = true; if (Callbacks) { @@ -3023,6 +3026,10 @@ void Preprocessor::HandleIfDirective(Token &IfToken, IdentifierInfo *IfNDefMacro = nullptr; const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro); const bool ConditionalTrue = DER.Conditional; + // Lexer might become invalid if we hit code completion point while evaluating + // 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 index 0000000000000..0878c5c871e0c --- /dev/null +++ b/clang/test/CodeCompletion/crash-if-directive.cpp @@ -0,0 +1,6 @@ +#define FOO(X) X +#if FOO( +#elif FOO( +#endif +// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:2:9 %s +// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:3:11 %s _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits