55
55
#include " clang/Basic/DiagnosticOptions.h"
56
56
#include " clang/Basic/FileEntry.h"
57
57
#include " clang/Basic/IdentifierTable.h"
58
+ #include " clang/Basic/LangStandard.h"
58
59
#include " clang/Basic/Module.h"
59
60
#include " clang/Basic/TargetInfo.h"
60
61
#include " clang/Basic/Version.h"
61
62
#include " clang/CAS/CASOptions.h"
63
+ #include " clang/CAS/IncludeTree.h"
62
64
#include " clang/CodeGen/ObjectFilePCHContainerOperations.h"
63
65
#include " clang/Frontend/CompilerInvocation.h"
64
66
#include " clang/Frontend/FrontendActions.h"
67
+ #include " clang/Frontend/IncludeTreePPActions.h"
65
68
#include " clang/Frontend/TextDiagnosticPrinter.h"
66
69
#include " clang/Frontend/Utils.h"
67
70
#include " clang/Index/IndexingAction.h"
80
83
#include " llvm/ADT/STLExtras.h"
81
84
#include " llvm/ADT/SmallVector.h"
82
85
#include " llvm/ADT/StringExtras.h"
86
+ #include " llvm/CAS/CASReference.h"
87
+ #include " llvm/CAS/ObjectStore.h"
83
88
#include " llvm/Support/CrashRecoveryContext.h"
89
+ #include " llvm/Support/Error.h"
90
+ #include " llvm/Support/ErrorHandling.h"
84
91
#include " llvm/Support/FileCollector.h"
85
92
#include " llvm/Support/FileSystem.h"
86
93
#include " llvm/Support/Memory.h"
@@ -1776,16 +1783,46 @@ bool ClangImporter::importBridgingHeader(StringRef header, ModuleDecl *adapter,
1776
1783
std::move (sourceBuffer), implicitImport);
1777
1784
}
1778
1785
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) {
1782
1809
auto invocation =
1783
1810
std::make_shared<clang::CompilerInvocation>(*Impl.Invocation );
1784
1811
1785
1812
invocation->getFrontendOpts ().DisableFree = false ;
1786
1813
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
+ }
1789
1826
1790
1827
invocation->getPreprocessorOpts ().resetNonModularOptions ();
1791
1828
@@ -1806,18 +1843,36 @@ std::string ClangImporter::getBridgingHeaderContents(StringRef headerPath,
1806
1843
// write to an in-memory buffer.
1807
1844
class RewriteIncludesAction : public clang ::PreprocessorFrontendAction {
1808
1845
raw_ostream &OS;
1846
+ std::optional<llvm::cas::ObjectRef> includeTreeRef;
1809
1847
1810
1848
void ExecuteAction () override {
1811
1849
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
+
1812
1864
clang::RewriteIncludesInInput (compiler.getPreprocessor (), &OS,
1813
1865
compiler.getPreprocessorOutputOpts ());
1814
1866
}
1867
+
1815
1868
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) {}
1817
1872
};
1818
1873
1819
1874
llvm::raw_string_ostream os (result);
1820
- RewriteIncludesAction action (os);
1875
+ RewriteIncludesAction action (os, includeTreeRef );
1821
1876
rewriteInstance.ExecuteAction (action);
1822
1877
});
1823
1878
@@ -2553,6 +2608,7 @@ ClangImporter::Implementation::Implementation(
2553
2608
!ctx.ClangImporterOpts.BridgingHeader.empty()),
2554
2609
DisableOverlayModules(ctx.ClangImporterOpts.DisableOverlayModules),
2555
2610
EnableClangSPI(ctx.ClangImporterOpts.EnableClangSPI),
2611
+ UseClangIncludeTree(ctx.ClangImporterOpts.UseClangIncludeTree),
2556
2612
importSymbolicCXXDecls(
2557
2613
ctx.LangOpts.hasFeature(Feature::ImportSymbolicCXXDecls)),
2558
2614
IsReadingBridgingPCH(false ),
0 commit comments