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