5555#include " clang/Basic/DiagnosticOptions.h"
5656#include " clang/Basic/FileEntry.h"
5757#include " clang/Basic/IdentifierTable.h"
58+ #include " clang/Basic/LangStandard.h"
5859#include " clang/Basic/Module.h"
5960#include " clang/Basic/TargetInfo.h"
6061#include " clang/Basic/Version.h"
6162#include " clang/CAS/CASOptions.h"
63+ #include " clang/CAS/IncludeTree.h"
6264#include " clang/CodeGen/ObjectFilePCHContainerOperations.h"
6365#include " clang/Frontend/CompilerInvocation.h"
6466#include " clang/Frontend/FrontendActions.h"
67+ #include " clang/Frontend/IncludeTreePPActions.h"
6568#include " clang/Frontend/TextDiagnosticPrinter.h"
6669#include " clang/Frontend/Utils.h"
6770#include " clang/Index/IndexingAction.h"
8083#include " llvm/ADT/STLExtras.h"
8184#include " llvm/ADT/SmallVector.h"
8285#include " llvm/ADT/StringExtras.h"
86+ #include " llvm/CAS/CASReference.h"
87+ #include " llvm/CAS/ObjectStore.h"
8388#include " llvm/Support/CrashRecoveryContext.h"
89+ #include " llvm/Support/Error.h"
90+ #include " llvm/Support/ErrorHandling.h"
8491#include " llvm/Support/FileCollector.h"
8592#include " llvm/Support/FileSystem.h"
8693#include " llvm/Support/Memory.h"
@@ -1776,16 +1783,46 @@ bool ClangImporter::importBridgingHeader(StringRef header, ModuleDecl *adapter,
17761783 std::move (sourceBuffer), implicitImport);
17771784}
17781785
1779- std::string ClangImporter::getBridgingHeaderContents (StringRef headerPath,
1780- off_t &fileSize,
1781- time_t &fileModTime) {
1786+ static llvm::Expected<llvm::cas::ObjectRef>
1787+ setupIncludeTreeInput (clang::CompilerInvocation &invocation,
1788+ StringRef headerPath, StringRef pchIncludeTree) {
1789+ auto DB = invocation.getCASOpts ().getOrCreateDatabases ();
1790+ if (!DB)
1791+ return DB.takeError ();
1792+ auto CAS = DB->first ;
1793+ auto ID = CAS->parseID (pchIncludeTree);
1794+ if (!ID)
1795+ return ID.takeError ();
1796+ auto includeTreeRef = CAS->getReference (*ID);
1797+ if (!includeTreeRef)
1798+ return llvm::cas::ObjectStore::createUnknownObjectError (*ID);
1799+
1800+ invocation.getFrontendOpts ().Inputs .push_back (clang::FrontendInputFile (
1801+ *includeTreeRef, headerPath, clang::Language::ObjC));
1802+
1803+ return *includeTreeRef;
1804+ }
1805+
1806+ std::string ClangImporter::getBridgingHeaderContents (
1807+ StringRef headerPath, off_t &fileSize, time_t &fileModTime,
1808+ StringRef pchIncludeTree) {
17821809 auto invocation =
17831810 std::make_shared<clang::CompilerInvocation>(*Impl.Invocation );
17841811
17851812 invocation->getFrontendOpts ().DisableFree = false ;
17861813 invocation->getFrontendOpts ().Inputs .clear ();
1787- invocation->getFrontendOpts ().Inputs .push_back (
1788- clang::FrontendInputFile (headerPath, clang::Language::ObjC));
1814+
1815+ std::optional<llvm::cas::ObjectRef> includeTreeRef;
1816+ if (pchIncludeTree.empty ())
1817+ invocation->getFrontendOpts ().Inputs .push_back (
1818+ clang::FrontendInputFile (headerPath, clang::Language::ObjC));
1819+ else if (auto err =
1820+ setupIncludeTreeInput (*invocation, headerPath, pchIncludeTree)
1821+ .moveInto (includeTreeRef)) {
1822+ Impl.diagnose ({}, diag::err_rewrite_bridging_header,
1823+ toString (std::move (err)));
1824+ return " " ;
1825+ }
17891826
17901827 invocation->getPreprocessorOpts ().resetNonModularOptions ();
17911828
@@ -1806,18 +1843,36 @@ std::string ClangImporter::getBridgingHeaderContents(StringRef headerPath,
18061843 // write to an in-memory buffer.
18071844 class RewriteIncludesAction : public clang ::PreprocessorFrontendAction {
18081845 raw_ostream &OS;
1846+ std::optional<llvm::cas::ObjectRef> includeTreeRef;
18091847
18101848 void ExecuteAction () override {
18111849 clang::CompilerInstance &compiler = getCompilerInstance ();
1850+ // If the input is include tree, setup the IncludeTreePPAction.
1851+ if (includeTreeRef) {
1852+ auto IncludeTreeRoot = clang::cas::IncludeTreeRoot::get (
1853+ compiler.getOrCreateObjectStore (), *includeTreeRef);
1854+ if (!IncludeTreeRoot)
1855+ llvm::report_fatal_error (IncludeTreeRoot.takeError ());
1856+ auto PPCachedAct =
1857+ clang::createPPActionsFromIncludeTree (*IncludeTreeRoot);
1858+ if (!PPCachedAct)
1859+ llvm::report_fatal_error (PPCachedAct.takeError ());
1860+ compiler.getPreprocessor ().setPPCachedActions (
1861+ std::move (*PPCachedAct));
1862+ }
1863+
18121864 clang::RewriteIncludesInInput (compiler.getPreprocessor (), &OS,
18131865 compiler.getPreprocessorOutputOpts ());
18141866 }
1867+
18151868 public:
1816- explicit RewriteIncludesAction (raw_ostream &os) : OS(os) {}
1869+ explicit RewriteIncludesAction (
1870+ raw_ostream &os, std::optional<llvm::cas::ObjectRef> includeTree)
1871+ : OS(os), includeTreeRef(includeTree) {}
18171872 };
18181873
18191874 llvm::raw_string_ostream os (result);
1820- RewriteIncludesAction action (os);
1875+ RewriteIncludesAction action (os, includeTreeRef );
18211876 rewriteInstance.ExecuteAction (action);
18221877 });
18231878
@@ -2553,6 +2608,7 @@ ClangImporter::Implementation::Implementation(
25532608 !ctx.ClangImporterOpts.BridgingHeader.empty()),
25542609 DisableOverlayModules(ctx.ClangImporterOpts.DisableOverlayModules),
25552610 EnableClangSPI(ctx.ClangImporterOpts.EnableClangSPI),
2611+ UseClangIncludeTree(ctx.ClangImporterOpts.UseClangIncludeTree),
25562612 importSymbolicCXXDecls(
25572613 ctx.LangOpts.hasFeature(Feature::ImportSymbolicCXXDecls)),
25582614 IsReadingBridgingPCH(false ),
0 commit comments