Add a pre-compiled header in clang compiler instance

I am working on using the clang API to compile a C++ program programmatically. The following program can serve as an example:

// main.cpp
#include "helper.h"
int main() {
   return 0;
// helper.cpp
void helper() {
// helper.h
void helper();

Originally, I read helper.h and main.cpp into separate strings, header and code, where there was not #include directive in main.cpp. Then I invoked the clang compiler instance to compile header + code and emit the IR. However, the header file could be huge and I want to cache a pre-compile helper.h to improve the performance.

Since I want to separate the compilation, and main.cpp depends on helper.h, I must introduce #include "helper.h" to ensure main.cpp` can be compiled. I can do the following in command line clang:

clang helper.h -emit-pch -o helper.pch
// use the pre-compiled pch file
clang -include-pch helper.pch main.cpp -o main

I figured out that I can use a clang::frontend::GeneratePCHAction to compile the header string and produce a .pch file. However, I have no idea how can I include this .pch file when I perform compilation on code string with a clang compiler instance. The only function that looks workable in the document is createPCHExternalASTSource, but I can’t find any example on how to use it and how it exactly works.

Also, I wonder if there is a way that I don’t need to write .pch to disk, but instead can keep it in memory, so that I can add it to the clang compiler instance without the overhead of writing to disk.

Any comment and suggestion can be extremely helpful. Thank you so much!