From 66bd5d79891ddb8bf0d31d78c4973f6d20dcb571 Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Wed, 14 Aug 2024 15:11:04 +0200 Subject: [PATCH] [clang-repl] Fix PCH with delayed template parsing (#103028) When instantiating a delayed template, the recorded token stream is passed to `Parser::ParseLateTemplatedFuncDef` which will append the current token "so it doesn't get lost". With incremental extensions enabled, this is `repl_input_end` which subsequently needs support for (de)serialization. --- clang/lib/Serialization/ASTReader.cpp | 1 + clang/lib/Serialization/ASTWriter.cpp | 1 + .../delayed-template-parsing-pch.cpp | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 clang/test/Interpreter/delayed-template-parsing-pch.cpp diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 511e2df7ad323..fa9b815239dbb 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -1887,6 +1887,7 @@ Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record, case tok::annot_pragma_unused: case tok::annot_pragma_openacc: case tok::annot_pragma_openacc_end: + case tok::annot_repl_input_end: break; default: llvm_unreachable("missing deserialization code for annotation token"); diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 1455f8e4145cb..5cfb98c2a1060 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -4734,6 +4734,7 @@ void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) { case tok::annot_pragma_unused: case tok::annot_pragma_openacc: case tok::annot_pragma_openacc_end: + case tok::annot_repl_input_end: break; default: llvm_unreachable("missing serialization code for annotation token"); diff --git a/clang/test/Interpreter/delayed-template-parsing-pch.cpp b/clang/test/Interpreter/delayed-template-parsing-pch.cpp new file mode 100644 index 0000000000000..f3bd4649ed034 --- /dev/null +++ b/clang/test/Interpreter/delayed-template-parsing-pch.cpp @@ -0,0 +1,25 @@ +// Test the setup without incremental extensions first +// RUN: %clang_cc1 -std=c++17 -fdelayed-template-parsing -fpch-instantiate-templates %s -emit-pch -o %t.pch -verify +// RUN: %clang_cc1 -std=c++17 -fdelayed-template-parsing -include-pch %t.pch %s -verify + +// RUN: %clang_cc1 -std=c++17 -fdelayed-template-parsing -fincremental-extensions -fpch-instantiate-templates %s -emit-pch -o %t.incremental.pch -verify +// RUN: %clang_cc1 -std=c++17 -fdelayed-template-parsing -fincremental-extensions -include-pch %t.incremental.pch %s -verify + +// expected-no-diagnostics + +#ifndef PCH +#define PCH + +// Have one template that is instantiated in the PCH (via the passed option +// -fpch-instantiate-templates) and then serialized +template T ft1() { return 0; } +inline int f1() { return ft1(); } + +// Have a second late-parsed template that needs to be deserialized +template T ft2() { return 0; } + +#else + +int f2() { return ft2(); } + +#endif