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"
@@ -1755,16 +1762,46 @@ bool ClangImporter::importBridgingHeader(StringRef header, ModuleDecl *adapter,
1755
1762
std::move (sourceBuffer), implicitImport);
1756
1763
}
1757
1764
1758
- std::string ClangImporter::getBridgingHeaderContents (StringRef headerPath,
1759
- off_t &fileSize,
1760
- time_t &fileModTime) {
1765
+ static llvm::Expected<llvm::cas::ObjectRef>
1766
+ setupIncludeTreeInput (clang::CompilerInvocation &invocation,
1767
+ StringRef headerPath, StringRef pchIncludeTree) {
1768
+ auto DB = invocation.getCASOpts ().getOrCreateDatabases ();
1769
+ if (!DB)
1770
+ return DB.takeError ();
1771
+ auto CAS = DB->first ;
1772
+ auto ID = CAS->parseID (pchIncludeTree);
1773
+ if (!ID)
1774
+ return ID.takeError ();
1775
+ auto includeTreeRef = CAS->getReference (*ID);
1776
+ if (!includeTreeRef)
1777
+ return llvm::cas::ObjectStore::createUnknownObjectError (*ID);
1778
+
1779
+ invocation.getFrontendOpts ().Inputs .push_back (clang::FrontendInputFile (
1780
+ *includeTreeRef, headerPath, clang::Language::ObjC));
1781
+
1782
+ return *includeTreeRef;
1783
+ }
1784
+
1785
+ std::string ClangImporter::getBridgingHeaderContents (
1786
+ StringRef headerPath, off_t &fileSize, time_t &fileModTime,
1787
+ StringRef pchIncludeTree) {
1761
1788
auto invocation =
1762
1789
std::make_shared<clang::CompilerInvocation>(*Impl.Invocation );
1763
1790
1764
1791
invocation->getFrontendOpts ().DisableFree = false ;
1765
1792
invocation->getFrontendOpts ().Inputs .clear ();
1766
- invocation->getFrontendOpts ().Inputs .push_back (
1767
- clang::FrontendInputFile (headerPath, clang::Language::ObjC));
1793
+
1794
+ std::optional<llvm::cas::ObjectRef> includeTreeRef;
1795
+ if (pchIncludeTree.empty ())
1796
+ invocation->getFrontendOpts ().Inputs .push_back (
1797
+ clang::FrontendInputFile (headerPath, clang::Language::ObjC));
1798
+ else if (auto err =
1799
+ setupIncludeTreeInput (*invocation, headerPath, pchIncludeTree)
1800
+ .moveInto (includeTreeRef)) {
1801
+ Impl.diagnose ({}, diag::err_rewrite_bridging_header,
1802
+ toString (std::move (err)));
1803
+ return " " ;
1804
+ }
1768
1805
1769
1806
invocation->getPreprocessorOpts ().resetNonModularOptions ();
1770
1807
@@ -1785,18 +1822,36 @@ std::string ClangImporter::getBridgingHeaderContents(StringRef headerPath,
1785
1822
// write to an in-memory buffer.
1786
1823
class RewriteIncludesAction : public clang ::PreprocessorFrontendAction {
1787
1824
raw_ostream &OS;
1825
+ std::optional<llvm::cas::ObjectRef> includeTreeRef;
1788
1826
1789
1827
void ExecuteAction () override {
1790
1828
clang::CompilerInstance &compiler = getCompilerInstance ();
1829
+ // If the input is include tree, setup the IncludeTreePPAction.
1830
+ if (includeTreeRef) {
1831
+ auto IncludeTreeRoot = clang::cas::IncludeTreeRoot::get (
1832
+ compiler.getOrCreateObjectStore (), *includeTreeRef);
1833
+ if (!IncludeTreeRoot)
1834
+ llvm::report_fatal_error (IncludeTreeRoot.takeError ());
1835
+ auto PPCachedAct =
1836
+ clang::createPPActionsFromIncludeTree (*IncludeTreeRoot);
1837
+ if (!PPCachedAct)
1838
+ llvm::report_fatal_error (PPCachedAct.takeError ());
1839
+ compiler.getPreprocessor ().setPPCachedActions (
1840
+ std::move (*PPCachedAct));
1841
+ }
1842
+
1791
1843
clang::RewriteIncludesInInput (compiler.getPreprocessor (), &OS,
1792
1844
compiler.getPreprocessorOutputOpts ());
1793
1845
}
1846
+
1794
1847
public:
1795
- explicit RewriteIncludesAction (raw_ostream &os) : OS(os) {}
1848
+ explicit RewriteIncludesAction (
1849
+ raw_ostream &os, std::optional<llvm::cas::ObjectRef> includeTree)
1850
+ : OS(os), includeTreeRef(includeTree) {}
1796
1851
};
1797
1852
1798
1853
llvm::raw_string_ostream os (result);
1799
- RewriteIncludesAction action (os);
1854
+ RewriteIncludesAction action (os, includeTreeRef );
1800
1855
rewriteInstance.ExecuteAction (action);
1801
1856
});
1802
1857
@@ -2517,6 +2572,7 @@ ClangImporter::Implementation::Implementation(
2517
2572
!ctx.ClangImporterOpts.BridgingHeader.empty()),
2518
2573
DisableOverlayModules(ctx.ClangImporterOpts.DisableOverlayModules),
2519
2574
EnableClangSPI(ctx.ClangImporterOpts.EnableClangSPI),
2575
+ UseClangIncludeTree(ctx.ClangImporterOpts.UseClangIncludeTree),
2520
2576
importSymbolicCXXDecls(
2521
2577
ctx.LangOpts.hasFeature(Feature::ImportSymbolicCXXDecls)),
2522
2578
IsReadingBridgingPCH(false ),
0 commit comments