11
11
#include " clang/AST/ExternalASTSource.h"
12
12
#include " clang/AST/PrettyPrinter.h"
13
13
#include " clang/Basic/Builtins.h"
14
+ #include " clang/Basic/DarwinSDKInfo.h"
14
15
#include " clang/Basic/DiagnosticIDs.h"
15
16
#include " clang/Basic/SourceLocation.h"
16
17
#include " clang/Basic/TargetInfo.h"
39
40
#include " llvm/ExecutionEngine/ExecutionEngine.h"
40
41
#include " llvm/Support/CrashRecoveryContext.h"
41
42
#include " llvm/Support/Debug.h"
43
+ #include " llvm/Support/Error.h"
42
44
#include " llvm/Support/FileSystem.h"
43
45
#include " llvm/Support/TargetSelect.h"
44
46
91
93
#include " lldb/Utility/StringList.h"
92
94
93
95
#include " Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
96
+ #include " Plugins/Platform/MacOSX/PlatformDarwin.h"
97
+ #include " lldb/Utility/XcodeSDK.h"
94
98
95
99
#include < cctype>
96
100
#include < memory>
@@ -279,6 +283,48 @@ class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
279
283
std::string m_output;
280
284
};
281
285
286
+ // / Returns true if the SDK for the specified triple supports
287
+ // / builtin modules in system headers. This is used to decide
288
+ // / whether to pass -fbuiltin-headers-in-system-modules to
289
+ // / the compiler instance when compiling the `std` module.
290
+ // /
291
+ // / \param[in] target The target triple.
292
+ // /
293
+ static llvm::Expected<bool >
294
+ sdkSupportsBuiltinModules (lldb_private::Target &target) {
295
+ auto arch_spec = target.GetArchitecture ();
296
+ auto const &triple = arch_spec.GetTriple ();
297
+ auto module_sp = target.GetExecutableModule ();
298
+ if (!module_sp)
299
+ return llvm::createStringError (" Executable module not found." );
300
+
301
+ // Get SDK path that the target was compiled against.
302
+ auto sdk_or_err = PlatformDarwin::GetSDKPathFromDebugInfo (*module_sp);
303
+ if (!sdk_or_err)
304
+ return sdk_or_err.takeError ();
305
+
306
+ // Use the SDK path from debug-info to find a local matching SDK directory.
307
+ auto sdk_path_or_err =
308
+ HostInfo::GetSDKRoot (HostInfo::SDKOptions{std::move (sdk_or_err->first )});
309
+ if (!sdk_path_or_err)
310
+ return sdk_path_or_err.takeError ();
311
+
312
+ auto VFS = FileSystem::Instance ().GetVirtualFileSystem ();
313
+ if (!VFS)
314
+ return llvm::createStringError (" No virtual filesystem available." );
315
+
316
+ // Extract SDK version from the /path/to/some.sdk/SDKSettings.json
317
+ auto parsed_or_err = clang::parseDarwinSDKInfo (*VFS, *sdk_path_or_err);
318
+ if (!parsed_or_err)
319
+ return parsed_or_err.takeError ();
320
+
321
+ auto maybe_sdk = *parsed_or_err;
322
+ if (!maybe_sdk)
323
+ return llvm::createStringError (" Couldn't find Darwin SDK info." );
324
+
325
+ return XcodeSDK::SDKSupportsBuiltinModules (triple, maybe_sdk->getVersion ());
326
+ }
327
+
282
328
static void SetupModuleHeaderPaths (CompilerInstance *compiler,
283
329
std::vector<std::string> include_directories,
284
330
lldb::TargetSP target_sp) {
@@ -561,7 +607,8 @@ static void SetupLangOpts(CompilerInstance &compiler,
561
607
lang_opts.NoBuiltin = true ;
562
608
}
563
609
564
- static void SetupImportStdModuleLangOpts (CompilerInstance &compiler) {
610
+ static void SetupImportStdModuleLangOpts (CompilerInstance &compiler,
611
+ lldb_private::Target &target) {
565
612
LangOptions &lang_opts = compiler.getLangOpts ();
566
613
lang_opts.Modules = true ;
567
614
// We want to implicitly build modules.
@@ -578,7 +625,7 @@ static void SetupImportStdModuleLangOpts(CompilerInstance &compiler) {
578
625
lang_opts.GNUMode = true ;
579
626
lang_opts.GNUKeywords = true ;
580
627
lang_opts.CPlusPlus11 = true ;
581
- lang_opts.BuiltinHeadersInSystemModules = true ;
628
+ lang_opts.BuiltinHeadersInSystemModules = ! sdkSupportsBuiltinModules (target) ;
582
629
583
630
// The Darwin libc expects this macro to be set.
584
631
lang_opts.GNUCVersion = 40201 ;
@@ -659,7 +706,7 @@ ClangExpressionParser::ClangExpressionParser(
659
706
if (auto *clang_expr = dyn_cast<ClangUserExpression>(&m_expr);
660
707
clang_expr && clang_expr->DidImportCxxModules ()) {
661
708
LLDB_LOG (log, " Adding lang options for importing C++ modules" );
662
- SetupImportStdModuleLangOpts (*m_compiler);
709
+ SetupImportStdModuleLangOpts (*m_compiler, *target_sp );
663
710
SetupModuleHeaderPaths (m_compiler.get (), m_include_directories, target_sp);
664
711
}
665
712
0 commit comments