Skip to content

[clang-repl] Fix PCH with delayed template parsing #103028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2024

Conversation

hahnjo
Copy link
Member

@hahnjo hahnjo commented Aug 13, 2024

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.

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.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:modules C++20 modules and Clang Header Modules labels Aug 13, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 13, 2024

@llvm/pr-subscribers-clang-modules

@llvm/pr-subscribers-clang

Author: Jonas Hahnfeld (hahnjo)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/103028.diff

3 Files Affected:

  • (modified) clang/lib/Serialization/ASTReader.cpp (+1)
  • (modified) clang/lib/Serialization/ASTWriter.cpp (+1)
  • (added) clang/test/Interpreter/delayed-template-parsing-pch.cpp (+25)
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 511e2df7ad3230..fa9b815239dbb6 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 1455f8e4145cb8..5cfb98c2a1060a 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 00000000000000..f3bd4649ed0345
--- /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 <typename T> T ft1() { return 0; }
+inline int f1() { return ft1<int>(); }
+
+// Have a second late-parsed template that needs to be deserialized
+template <typename T> T ft2() { return 0; }
+
+#else
+
+int f2() { return ft2<int>(); }
+
+#endif

Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks not bad. Let's land it after the CI gets green.

BTW, the delayed template parsing is a deprecated technique. Both clang and MSVC won't enable this after C++20 and we think it is the root of many bugs.

@hahnjo
Copy link
Member Author

hahnjo commented Aug 13, 2024

BTW, the delayed template parsing is a deprecated technique. Both clang and MSVC won't enable this after C++20 and we think it is the root of many bugs.

I agree. It was needed in the past to parse the MSVC stdlib, let's check if we still need it: root-project/root#16222

@hahnjo
Copy link
Member Author

hahnjo commented Aug 14, 2024

BTW, the delayed template parsing is a deprecated technique. Both clang and MSVC won't enable this after C++20 and we think it is the root of many bugs.

I agree. It was needed in the past to parse the MSVC stdlib, let's check if we still need it: root-project/root#16222

Update on this: testing shows that we don't need it anymore, so we turned it off. I guess we still want to merge the fix proposed in this PR regardless? @ChuanqiXu9

@ChuanqiXu9
Copy link
Member

BTW, the delayed template parsing is a deprecated technique. Both clang and MSVC won't enable this after C++20 and we think it is the root of many bugs.

I agree. It was needed in the past to parse the MSVC stdlib, let's check if we still need it: root-project/root#16222

Update on this: testing shows that we don't need it anymore, so we turned it off. I guess we still want to merge the fix proposed in this PR regardless? @ChuanqiXu9

At least it is good for the pre-c++20 codes : )

Copy link
Contributor

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@hahnjo hahnjo merged commit 66bd5d7 into llvm:main Aug 14, 2024
11 checks passed
@hahnjo hahnjo deleted the incremental-delayed-pch branch August 14, 2024 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:modules C++20 modules and Clang Header Modules clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants