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