@@ -1093,6 +1093,23 @@ static void ConfigureResourceDirs(swift::CompilerInvocation &invocation,
1093
1093
invocation.setRuntimeResourcePath (resource_dir);
1094
1094
}
1095
1095
1096
+ static void ConfigureModuleCachePath (SwiftASTContext &swift_ast_sp) {
1097
+ std::string moduleCachePath =
1098
+ swift_ast_sp.GetCompilerInvocation ().getClangModuleCachePath ().str ();
1099
+ if (!moduleCachePath.empty ())
1100
+ return ;
1101
+
1102
+ // If the moduleCachePath is not configured, setup a default path location.
1103
+ llvm::SmallString<0 > path;
1104
+ std::error_code ec =
1105
+ llvm::sys::fs::createUniqueDirectory (" ModuleCache" , path);
1106
+ if (!ec)
1107
+ moduleCachePath = std::string (path);
1108
+ else
1109
+ moduleCachePath = " /tmp/lldb-ModuleCache" ;
1110
+ swift_ast_sp.GetCompilerInvocation ().setClangModuleCachePath (moduleCachePath);
1111
+ }
1112
+
1096
1113
static const char *getImportFailureString (swift::serialization::Status status) {
1097
1114
switch (status) {
1098
1115
case swift::serialization::Status::Valid:
@@ -1778,7 +1795,10 @@ static void applyOverrideOptions(std::vector<std::string> &args,
1778
1795
}
1779
1796
1780
1797
void SwiftASTContext::AddExtraClangArgs (
1781
- const std::vector<std::string> &ExtraArgs, StringRef overrideOpts) {
1798
+ const std::vector<std::string> &ExtraArgs,
1799
+ const std::vector<std::string> &module_search_paths,
1800
+ const std::vector<std::pair<std::string, bool >> framework_search_paths,
1801
+ StringRef overrideOpts) {
1782
1802
if (ExtraArgs.empty ())
1783
1803
return ;
1784
1804
@@ -1804,7 +1824,8 @@ void SwiftASTContext::AddExtraClangArgs(
1804
1824
if (importer_options.DirectClangCC1ModuleBuild ) {
1805
1825
if (!fresh_invocation)
1806
1826
importer_options.ExtraArgs .clear ();
1807
- AddExtraClangCC1Args (ExtraArgs, importer_options.ExtraArgs );
1827
+ AddExtraClangCC1Args (ExtraArgs, module_search_paths, framework_search_paths,
1828
+ importer_options.ExtraArgs );
1808
1829
applyOverrideOptions (importer_options.ExtraArgs , overrideOpts);
1809
1830
return ;
1810
1831
}
@@ -1822,17 +1843,43 @@ void SwiftASTContext::AddExtraClangArgs(
1822
1843
}
1823
1844
1824
1845
void SwiftASTContext::AddExtraClangCC1Args (
1825
- const std::vector<std::string> &source, std::vector<std::string> &dest) {
1846
+ const std::vector<std::string> &source,
1847
+ const std::vector<std::string> &module_search_paths,
1848
+ const std::vector<std::pair<std::string, bool >> framework_search_paths,
1849
+ std::vector<std::string> &dest) {
1826
1850
clang::CompilerInvocation invocation;
1851
+ std::vector<std::string> default_paths = {" /usr/include" ,
1852
+ " /user/local/include" };
1827
1853
llvm::SmallVector<const char *> clangArgs;
1828
- clangArgs.reserve (source.size ());
1854
+ clangArgs.reserve (source.size () + module_search_paths.size () * 2 +
1855
+ framework_search_paths.size () * 2 +
1856
+ default_paths.size () * 2 );
1829
1857
llvm::for_each (source, [&](const std::string &Arg) {
1830
1858
// Workaround for the extra driver argument embedded in the swiftmodule by
1831
1859
// some swift compiler version. It always starts with `--target=` and it is
1832
1860
// not a valid cc1 option.
1833
1861
if (!StringRef (Arg).starts_with (" --target=" ))
1834
1862
clangArgs.push_back (Arg.c_str ());
1835
1863
});
1864
+ // Append some search paths from swift invocation so lldb can import
1865
+ // additional clang modules when doing type reconstruction.
1866
+ for (auto &path : module_search_paths) {
1867
+ clangArgs.push_back (" -I" );
1868
+ clangArgs.push_back (path.c_str ());
1869
+ }
1870
+ for (auto &path : default_paths) {
1871
+ llvm::SmallString<128 > search_path (GetPlatformSDKPath ());
1872
+ llvm::sys::path::append (search_path, path);
1873
+ path = std::string (search_path);
1874
+ }
1875
+ for (auto &path : default_paths) {
1876
+ clangArgs.push_back (" -I" );
1877
+ clangArgs.push_back (path.c_str ());
1878
+ }
1879
+ for (auto &path : framework_search_paths) {
1880
+ clangArgs.push_back (" -F" );
1881
+ clangArgs.push_back (path.first .c_str ());
1882
+ }
1836
1883
1837
1884
std::string diags;
1838
1885
llvm::raw_string_ostream os (diags);
@@ -1859,6 +1906,12 @@ void SwiftASTContext::AddExtraClangCC1Args(
1859
1906
// Ignore CAS info inside modules when loading.
1860
1907
invocation.getFrontendOpts ().ModuleLoadIgnoreCAS = true ;
1861
1908
1909
+ // Add options to allow clang importer to do implicit module build.
1910
+ invocation.getLangOpts ().ImplicitModules = true ;
1911
+ invocation.getHeaderSearchOpts ().ImplicitModuleMaps = true ;
1912
+ invocation.getHeaderSearchOpts ().ModuleCachePath =
1913
+ GetCompilerInvocation ().getClangModuleCachePath ().str ();
1914
+
1862
1915
// Remove non-existing modules in a systematic way.
1863
1916
bool module_missing = false ;
1864
1917
auto CheckFileExists = [&](const char *file) {
@@ -1898,7 +1951,7 @@ void SwiftASTContext::AddUserClangArgs(TargetProperties &props) {
1898
1951
std::vector<std::string> user_clang_flags;
1899
1952
for (const auto &arg : args.entries ())
1900
1953
user_clang_flags.push_back (arg.ref ().str ());
1901
- AddExtraClangArgs (user_clang_flags);
1954
+ AddExtraClangArgs (user_clang_flags, {}, {} );
1902
1955
}
1903
1956
1904
1957
// / Turn relative paths in clang options into absolute paths based on
@@ -2471,6 +2524,7 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
2471
2524
HostInfo::GetSwiftResourceDir (triple, swift_ast_sp->GetPlatformSDKPath ());
2472
2525
ConfigureResourceDirs (swift_ast_sp->GetCompilerInvocation (), resource_dir,
2473
2526
triple);
2527
+ ConfigureModuleCachePath (*swift_ast_sp);
2474
2528
2475
2529
swift_ast_sp->SetCompilerInvocationLLDBOverrides ();
2476
2530
@@ -2494,7 +2548,8 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
2494
2548
2495
2549
// Apply the working directory to all relative paths.
2496
2550
StringRef overrideOpts = target ? target->GetSwiftClangOverrideOptions () : " " ;
2497
- swift_ast_sp->AddExtraClangArgs (extra_clang_args, overrideOpts);
2551
+ swift_ast_sp->AddExtraClangArgs (extra_clang_args, module_search_paths,
2552
+ framework_search_paths, overrideOpts);
2498
2553
if (target)
2499
2554
swift_ast_sp->AddUserClangArgs (*target);
2500
2555
else
@@ -2930,6 +2985,7 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
2930
2985
triple, swift_ast_sp->GetPlatformSDKPath ());
2931
2986
ConfigureResourceDirs (swift_ast_sp->GetCompilerInvocation (), resource_dir,
2932
2987
triple);
2988
+ ConfigureModuleCachePath (*swift_ast_sp);
2933
2989
2934
2990
std::vector<swift::PluginSearchOption> plugin_search_options;
2935
2991
std::vector<std::string> module_search_paths;
@@ -2968,7 +3024,8 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
2968
3024
swift_ast_sp->AddDiagnostic (eSeverityError, error);
2969
3025
StringRef override_opts =
2970
3026
target_sp ? target_sp->GetSwiftClangOverrideOptions () : " " ;
2971
- swift_ast_sp->AddExtraClangArgs (extra_clang_args, override_opts);
3027
+ swift_ast_sp->AddExtraClangArgs (extra_clang_args, module_search_paths,
3028
+ framework_search_paths, override_opts);
2972
3029
}
2973
3030
2974
3031
// Now fold any extra options we were passed. This has to be done
@@ -3572,7 +3629,8 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
3572
3629
}
3573
3630
3574
3631
// Create the ClangImporter and determine the Clang module cache path.
3575
- std::string moduleCachePath = " " ;
3632
+ std::string moduleCachePath =
3633
+ GetCompilerInvocation ().getClangModuleCachePath ().str ();
3576
3634
std::unique_ptr<swift::ClangImporter> clang_importer_ap;
3577
3635
auto &clang_importer_options = GetClangImporterOptions ();
3578
3636
if (!m_ast_context_ap->SearchPathOpts .getSDKPath ().empty () ||
@@ -3599,24 +3657,12 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
3599
3657
underlying_error.c_str ());
3600
3658
}
3601
3659
}
3602
- if (clang_importer_ap)
3603
- moduleCachePath = swift::getModuleCachePathFromClang (
3660
+ if (clang_importer_ap) {
3661
+ auto clangModuleCache = swift::getModuleCachePathFromClang (
3604
3662
clang_importer_ap->getClangInstance ());
3605
- }
3606
- }
3607
-
3608
- if (moduleCachePath.empty ()) {
3609
- moduleCachePath = GetClangModulesCacheProperty ();
3610
- // Even though it is initialized to the default Clang location at startup a
3611
- // user could have overwritten it with an empty path.
3612
- if (moduleCachePath.empty ()) {
3613
- llvm::SmallString<0 > path;
3614
- std::error_code ec =
3615
- llvm::sys::fs::createUniqueDirectory (" ModuleCache" , path);
3616
- if (!ec)
3617
- moduleCachePath = std::string (path);
3618
- else
3619
- moduleCachePath = " /tmp/lldb-ModuleCache" ;
3663
+ if (!clangModuleCache.empty ())
3664
+ moduleCachePath = clangModuleCache;
3665
+ }
3620
3666
}
3621
3667
}
3622
3668
LOG_PRINTF (GetLog (LLDBLog::Types), " Using Clang module cache path: %s" ,
0 commit comments