LLDB mainline
ClangModulesDeclVendor.cpp
Go to the documentation of this file.
1//===-- ClangModulesDeclVendor.cpp ----------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Basic/Diagnostic.h"
10#include "clang/Basic/DiagnosticFrontend.h"
11#include "clang/Basic/DiagnosticSerialization.h"
12#include "clang/Basic/IdentifierTable.h"
13#include "clang/Basic/TargetInfo.h"
14#include "clang/Driver/CreateInvocationFromArgs.h"
15#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Frontend/FrontendActions.h"
17#include "clang/Frontend/TextDiagnosticPrinter.h"
18#include "clang/Lex/Preprocessor.h"
19#include "clang/Lex/PreprocessorOptions.h"
20#include "clang/Parse/Parser.h"
21#include "clang/Sema/Lookup.h"
22#include "clang/Serialization/ASTReader.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/ErrorExtras.h"
25#include "llvm/Support/Path.h"
26#include "llvm/Support/Threading.h"
27
28#include "ClangHost.h"
30
33#include "lldb/Core/Progress.h"
36#include "lldb/Target/Target.h"
40#include "lldb/Utility/Log.h"
41
42#include <memory>
43
44using namespace lldb_private;
45
46namespace {
47/// Any Clang compiler requires a consumer for diagnostics. This one stores
48/// them as strings so we can provide them to the user in case a module failed
49/// to load.
50class StoringDiagnosticConsumer : public clang::DiagnosticConsumer {
51public:
52 StoringDiagnosticConsumer();
53
54 void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
55 const clang::Diagnostic &info) override;
56
57 void ClearDiagnostics();
58
59 void DumpDiagnostics(Stream &error_stream);
60
61 void BeginSourceFile(const clang::LangOptions &LangOpts,
62 const clang::Preprocessor *PP = nullptr) override;
63 void EndSourceFile() override;
64
65private:
66 bool HandleModuleRemark(const clang::Diagnostic &info);
67 void SetCurrentModuleProgress(std::string module_name);
68
69 typedef std::pair<clang::DiagnosticsEngine::Level, std::string>
70 IDAndDiagnostic;
71 std::vector<IDAndDiagnostic> m_diagnostics;
72 std::unique_ptr<clang::DiagnosticOptions> m_diag_opts;
73 /// Output string filled by m_os. Will be reused for different diagnostics.
74 std::string m_output;
75 /// Output stream of m_diag_printer.
76 std::unique_ptr<llvm::raw_string_ostream> m_os;
77 /// The DiagnosticPrinter used for creating the full diagnostic messages
78 /// that are stored in m_diagnostics.
79 std::unique_ptr<clang::TextDiagnosticPrinter> m_diag_printer;
80 /// A Progress with explicitly managed lifetime.
81 std::unique_ptr<Progress> m_current_progress_up;
82 std::vector<std::string> m_module_build_stack;
83};
84
85/// The private implementation of our ClangModulesDeclVendor. Contains all the
86/// Clang state required to load modules.
87class ClangModulesDeclVendorImpl : public ClangModulesDeclVendor {
88public:
89 ClangModulesDeclVendorImpl(
90 std::unique_ptr<clang::DiagnosticOptions> diagnostic_options,
91 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
92 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
93 std::unique_ptr<clang::CompilerInstance> compiler_instance,
94 std::unique_ptr<clang::Parser> parser);
95
96 ~ClangModulesDeclVendorImpl() override = default;
97
98 llvm::Error AddModule(const SourceModule &module,
99 ModuleVector *exported_modules) override;
100
101 llvm::Error AddModulesForCompileUnit(CompileUnit &cu,
102 ModuleVector &exported_modules) override;
103
104 uint32_t FindDecls(ConstString name, bool append, uint32_t max_matches,
105 std::vector<CompilerDecl> &decls) override;
106
107 void ForEachMacro(
108 const ModuleVector &modules,
109 std::function<bool(llvm::StringRef, llvm::StringRef)> handler) override;
110
111private:
112 typedef llvm::DenseSet<ModuleID> ExportedModuleSet;
113 void ReportModuleExportsHelper(ExportedModuleSet &exports,
114 clang::Module *module);
115
116 void ReportModuleExports(ModuleVector &exports, clang::Module *module);
117
118 clang::ModuleLoadResult DoGetModule(clang::ModuleIdPath path,
119 bool make_visible);
120
121 bool m_enabled = false;
122
123 std::unique_ptr<clang::DiagnosticOptions> m_diagnostic_options;
124 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> m_diagnostics_engine;
125 std::shared_ptr<clang::CompilerInvocation> m_compiler_invocation;
126 std::unique_ptr<clang::CompilerInstance> m_compiler_instance;
127 std::unique_ptr<clang::Parser> m_parser;
128 size_t m_source_location_index =
129 0; // used to give name components fake SourceLocations
130
131 typedef std::vector<ConstString> ImportedModule;
132 typedef std::map<ImportedModule, clang::Module *> ImportedModuleMap;
133 typedef llvm::DenseSet<ModuleID> ImportedModuleSet;
134 ImportedModuleMap m_imported_modules;
135 ImportedModuleSet m_user_imported_modules;
136 // We assume that every ASTContext has an TypeSystemClang, so we also store
137 // a custom TypeSystemClang for our internal ASTContext.
138 std::shared_ptr<TypeSystemClang> m_ast_context;
139};
140} // anonymous namespace
141
142StoringDiagnosticConsumer::StoringDiagnosticConsumer() {
143 m_diag_opts = std::make_unique<clang::DiagnosticOptions>();
144 m_os = std::make_unique<llvm::raw_string_ostream>(m_output);
145 m_diag_printer =
146 std::make_unique<clang::TextDiagnosticPrinter>(*m_os, *m_diag_opts);
147}
148
149void StoringDiagnosticConsumer::HandleDiagnostic(
150 clang::DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &info) {
151 if (HandleModuleRemark(info))
152 return;
153
154 // Print the diagnostic to m_output.
155 m_output.clear();
156 m_diag_printer->HandleDiagnostic(DiagLevel, info);
157
158 // Store the diagnostic for later.
159 m_diagnostics.push_back(IDAndDiagnostic(DiagLevel, m_output));
160}
161
162void StoringDiagnosticConsumer::ClearDiagnostics() { m_diagnostics.clear(); }
163
164void StoringDiagnosticConsumer::DumpDiagnostics(Stream &error_stream) {
165 for (IDAndDiagnostic &diag : m_diagnostics) {
166 switch (diag.first) {
167 default:
168 error_stream.PutCString(diag.second);
169 error_stream.PutChar('\n');
170 break;
171 case clang::DiagnosticsEngine::Level::Ignored:
172 break;
173 }
174 }
175}
176
177void StoringDiagnosticConsumer::BeginSourceFile(
178 const clang::LangOptions &LangOpts, const clang::Preprocessor *PP) {
179 m_diag_printer->BeginSourceFile(LangOpts, PP);
180}
181
182void StoringDiagnosticConsumer::EndSourceFile() {
183 m_current_progress_up = nullptr;
184 m_diag_printer->EndSourceFile();
185}
186
187bool StoringDiagnosticConsumer::HandleModuleRemark(
188 const clang::Diagnostic &info) {
189 Log *log = GetLog(LLDBLog::Types | LLDBLog::Expressions);
190 switch (info.getID()) {
191 case clang::diag::remark_module_build: {
192 const auto &module_name = info.getArgStdStr(0);
193 SetCurrentModuleProgress(module_name);
194 m_module_build_stack.push_back(module_name);
195
196 const auto &module_path = info.getArgStdStr(1);
197 LLDB_LOG(log, "Building Clang module {0} as {1}", module_name, module_path);
198 return true;
199 }
200 case clang::diag::remark_module_build_done: {
201 // The current module is done.
202 m_module_build_stack.pop_back();
203 if (m_module_build_stack.empty()) {
204 m_current_progress_up = nullptr;
205 } else {
206 // When the just completed module began building, a module that depends on
207 // it ("module A") was effectively paused. Update the progress to re-show
208 // "module A" as continuing to be built.
209 const auto &resumed_module_name = m_module_build_stack.back();
210 SetCurrentModuleProgress(resumed_module_name);
211 }
212
213 const auto &module_name = info.getArgStdStr(0);
214 LLDB_LOG(log, "Finished building Clang module {0}", module_name);
215 return true;
216 }
217 case clang::diag::remark_module_import: {
218 const auto &module_name = info.getArgStdStr(0);
219 const auto &module_path = info.getArgStdStr(1);
220 LLDB_LOG(log, "Importing Clang module {0} from {1}", module_name,
221 module_path);
222 return true;
223 }
224
225 default:
226 return false;
227 }
228}
229
230void StoringDiagnosticConsumer::SetCurrentModuleProgress(
231 std::string module_name) {
232 if (!m_current_progress_up)
233 m_current_progress_up =
234 std::make_unique<Progress>("Building Clang modules");
235
236 m_current_progress_up->Increment(1, std::move(module_name));
237}
238
241
243
244ClangModulesDeclVendorImpl::ClangModulesDeclVendorImpl(
245 std::unique_ptr<clang::DiagnosticOptions> diagnostic_options,
246 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
247 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
248 std::unique_ptr<clang::CompilerInstance> compiler_instance,
249 std::unique_ptr<clang::Parser> parser)
250 : m_diagnostic_options(std::move(diagnostic_options)),
251 m_diagnostics_engine(std::move(diagnostics_engine)),
252 m_compiler_invocation(std::move(compiler_invocation)),
253 m_compiler_instance(std::move(compiler_instance)),
254 m_parser(std::move(parser)) {
255
256 // Initialize our TypeSystemClang.
257 m_ast_context =
258 std::make_shared<TypeSystemClang>("ClangModulesDeclVendor ASTContext",
259 m_compiler_instance->getASTContext());
260}
261
262void ClangModulesDeclVendorImpl::ReportModuleExportsHelper(
263 ExportedModuleSet &exports, clang::Module *module) {
264 if (exports.count(reinterpret_cast<ClangModulesDeclVendor::ModuleID>(module)))
265 return;
266
267 exports.insert(reinterpret_cast<ClangModulesDeclVendor::ModuleID>(module));
268
269 llvm::SmallVector<clang::Module *, 2> sub_exports;
270
271 module->getExportedModules(sub_exports);
272
273 for (clang::Module *module : sub_exports)
274 ReportModuleExportsHelper(exports, module);
275}
276
277void ClangModulesDeclVendorImpl::ReportModuleExports(
278 ClangModulesDeclVendor::ModuleVector &exports, clang::Module *module) {
279 ExportedModuleSet exports_set;
280
281 ReportModuleExportsHelper(exports_set, module);
282
283 for (ModuleID module : exports_set)
284 exports.push_back(module);
285}
286
287llvm::Error
288ClangModulesDeclVendorImpl::AddModule(const SourceModule &module,
289 ModuleVector *exported_modules) {
290 // Fail early.
291
292 if (m_compiler_instance->hadModuleLoaderFatalFailure())
293 return llvm::createStringError(
294 "couldn't load a module because the module loader is in a fatal state");
295
296 // Check if we've already imported this module.
297
298 std::vector<ConstString> imported_module;
299
300 for (ConstString path_component : module.path)
301 imported_module.push_back(path_component);
302
303 {
304 ImportedModuleMap::iterator mi = m_imported_modules.find(imported_module);
305
306 if (mi != m_imported_modules.end()) {
307 if (exported_modules)
308 ReportModuleExports(*exported_modules, mi->second);
309 return llvm::Error::success();
310 }
311 }
312
313 clang::HeaderSearch &HS =
314 m_compiler_instance->getPreprocessor().getHeaderSearchInfo();
315
316 if (module.search_path) {
317 auto path_begin = llvm::sys::path::begin(module.search_path.GetStringRef());
318 auto path_end = llvm::sys::path::end(module.search_path.GetStringRef());
319 auto sysroot_begin = llvm::sys::path::begin(module.sysroot.GetStringRef());
320 auto sysroot_end = llvm::sys::path::end(module.sysroot.GetStringRef());
321 // FIXME: Use C++14 std::equal(it, it, it, it) variant once it's available.
322 bool is_system_module = (std::distance(path_begin, path_end) >=
323 std::distance(sysroot_begin, sysroot_end)) &&
324 std::equal(sysroot_begin, sysroot_end, path_begin);
325 // No need to inject search paths to modules in the sysroot.
326 if (!is_system_module) {
327 bool is_system = true;
328 bool is_framework = false;
329 auto dir = HS.getFileMgr().getOptionalDirectoryRef(
330 module.search_path.GetStringRef());
331 if (!dir)
332 return llvm::createStringError(
333 "couldn't find module search path directory %s",
334 module.search_path.GetCString());
335
336 auto file = HS.lookupModuleMapFile(*dir, is_framework);
337 if (!file)
338 return llvm::createStringError("couldn't find modulemap file in %s",
339 module.search_path.GetCString());
340
341 if (HS.parseAndLoadModuleMapFile(*file, is_system,
342 /*ImplicitlyDiscovered=*/false))
343 return llvm::createStringError(
344 "failed to parse and load modulemap file in %s",
345 module.search_path.GetCString());
346 }
347 }
348
349 if (!HS.lookupModule(module.path.front().GetStringRef()))
350 return llvm::createStringErrorV(
351 "header search couldn't locate module '{0}'", module.path.front());
352
353 llvm::SmallVector<clang::IdentifierLoc, 4> clang_path;
354
355 {
356 clang::SourceManager &source_manager =
357 m_compiler_instance->getASTContext().getSourceManager();
358
359 for (ConstString path_component : module.path) {
360 clang_path.emplace_back(
361 source_manager.getLocForStartOfFile(source_manager.getMainFileID())
362 .getLocWithOffset(m_source_location_index++),
363 &m_compiler_instance->getASTContext().Idents.get(
364 path_component.GetStringRef()));
365 }
366 }
367
368 StoringDiagnosticConsumer *diagnostic_consumer =
369 static_cast<StoringDiagnosticConsumer *>(
370 m_compiler_instance->getDiagnostics().getClient());
371
372 diagnostic_consumer->ClearDiagnostics();
373
374 clang::Module *top_level_module = DoGetModule(clang_path.front(), false);
375
376 if (!top_level_module) {
377 lldb_private::StreamString error_stream;
378 diagnostic_consumer->DumpDiagnostics(error_stream);
379
380 return llvm::createStringErrorV("couldn't load top-level module {0}:\n{1}",
381 module.path.front().GetStringRef(),
382 error_stream.GetString());
383 }
384
385 clang::Module *submodule = top_level_module;
386
387 for (auto &component : llvm::ArrayRef<ConstString>(module.path).drop_front()) {
388 clang::Module *found = submodule->findSubmodule(component.GetStringRef());
389 if (!found) {
390 lldb_private::StreamString error_stream;
391 diagnostic_consumer->DumpDiagnostics(error_stream);
392
393 return llvm::createStringErrorV(
394 "couldn't load submodule '{0}' of module '{1}':\n{2}",
395 component.GetStringRef(), submodule->getFullModuleName(),
396 error_stream.GetString());
397 }
398
399 submodule = found;
400 }
401
402 // If we didn't make the submodule visible here, Clang wouldn't allow LLDB to
403 // pick any of the decls in the submodules during C++ name lookup.
404 if (submodule)
405 m_compiler_instance->makeModuleVisible(
406 submodule, clang::Module::NameVisibilityKind::AllVisible,
407 /*ImportLoc=*/{});
408
409 clang::Module *requested_module = DoGetModule(clang_path, true);
410
411 if (requested_module != nullptr) {
412 if (exported_modules)
413 ReportModuleExports(*exported_modules, requested_module);
414
415 m_imported_modules[imported_module] = requested_module;
416
417 m_enabled = true;
418
419 return llvm::Error::success();
420 }
421
422 return llvm::createStringErrorV("unknown error while loading module {0}\n",
423 module.path.front().GetStringRef());
424}
425
444
445llvm::Error ClangModulesDeclVendorImpl::AddModulesForCompileUnit(
446 CompileUnit &cu, ClangModulesDeclVendor::ModuleVector &exported_modules) {
447 if (!LanguageSupportsClangModules(cu.GetLanguage()))
448 return llvm::Error::success();
449
450 llvm::Error errors = llvm::Error::success();
451
452 for (auto &imported_module : cu.GetImportedModules())
453 if (auto err = AddModule(imported_module, &exported_modules))
454 errors = llvm::joinErrors(std::move(errors), std::move(err));
455
456 return errors;
457}
458
459// ClangImporter::lookupValue
460
461uint32_t
462ClangModulesDeclVendorImpl::FindDecls(ConstString name, bool append,
463 uint32_t max_matches,
464 std::vector<CompilerDecl> &decls) {
465 if (!m_enabled)
466 return 0;
467
468 if (!append)
469 decls.clear();
470
471 clang::IdentifierInfo &ident =
472 m_compiler_instance->getASTContext().Idents.get(name.GetStringRef());
473
474 clang::LookupResult lookup_result(
475 m_compiler_instance->getSema(), clang::DeclarationName(&ident),
476 clang::SourceLocation(), clang::Sema::LookupOrdinaryName);
477
478 m_compiler_instance->getSema().LookupName(
479 lookup_result,
480 m_compiler_instance->getSema().getScopeForContext(
481 m_compiler_instance->getASTContext().getTranslationUnitDecl()));
482
483 uint32_t num_matches = 0;
484
485 for (clang::NamedDecl *named_decl : lookup_result) {
486 if (num_matches >= max_matches)
487 return num_matches;
488
489 decls.push_back(m_ast_context->GetCompilerDecl(named_decl));
490 ++num_matches;
491 }
492
493 return num_matches;
494}
495
496void ClangModulesDeclVendorImpl::ForEachMacro(
498 std::function<bool(llvm::StringRef, llvm::StringRef)> handler) {
499 if (!m_enabled)
500 return;
501
502 typedef std::map<ModuleID, ssize_t> ModulePriorityMap;
503 ModulePriorityMap module_priorities;
504
505 ssize_t priority = 0;
506
507 for (ModuleID module : modules)
508 module_priorities[module] = priority++;
509
510 if (m_compiler_instance->getPreprocessor().getExternalSource()) {
511 m_compiler_instance->getPreprocessor()
512 .getExternalSource()
513 ->ReadDefinedMacros();
514 }
515
516 for (clang::Preprocessor::macro_iterator
517 mi = m_compiler_instance->getPreprocessor().macro_begin(),
518 me = m_compiler_instance->getPreprocessor().macro_end();
519 mi != me; ++mi) {
520 const clang::IdentifierInfo *ii = nullptr;
521
522 {
523 if (clang::IdentifierInfoLookup *lookup =
524 m_compiler_instance->getPreprocessor()
525 .getIdentifierTable()
526 .getExternalIdentifierLookup()) {
527 lookup->get(mi->first->getName());
528 }
529 if (!ii)
530 ii = mi->first;
531 }
532
533 ssize_t found_priority = -1;
534 clang::MacroInfo *macro_info = nullptr;
535
536 for (clang::ModuleMacro *module_macro :
537 m_compiler_instance->getPreprocessor().getLeafModuleMacros(ii)) {
538 clang::Module *module = module_macro->getOwningModule();
539
540 {
541 ModulePriorityMap::iterator pi =
542 module_priorities.find(reinterpret_cast<ModuleID>(module));
543
544 if (pi != module_priorities.end() && pi->second > found_priority) {
545 macro_info = module_macro->getMacroInfo();
546 found_priority = pi->second;
547 }
548 }
549
550 clang::Module *top_level_module = module->getTopLevelModule();
551
552 if (top_level_module != module) {
553 ModulePriorityMap::iterator pi = module_priorities.find(
554 reinterpret_cast<ModuleID>(top_level_module));
555
556 if ((pi != module_priorities.end()) && pi->second > found_priority) {
557 macro_info = module_macro->getMacroInfo();
558 found_priority = pi->second;
559 }
560 }
561 }
562
563 if (macro_info) {
564 std::string macro_expansion = "#define ";
565 llvm::StringRef macro_identifier = mi->first->getName();
566 macro_expansion.append(macro_identifier.str());
567
568 {
569 if (macro_info->isFunctionLike()) {
570 macro_expansion.append("(");
571
572 bool first_arg = true;
573
574 for (auto pi = macro_info->param_begin(),
575 pe = macro_info->param_end();
576 pi != pe; ++pi) {
577 if (!first_arg)
578 macro_expansion.append(", ");
579 else
580 first_arg = false;
581
582 macro_expansion.append((*pi)->getName().str());
583 }
584
585 if (macro_info->isC99Varargs()) {
586 if (first_arg)
587 macro_expansion.append("...");
588 else
589 macro_expansion.append(", ...");
590 } else if (macro_info->isGNUVarargs())
591 macro_expansion.append("...");
592
593 macro_expansion.append(")");
594 }
595
596 macro_expansion.append(" ");
597
598 bool first_token = true;
599
600 for (clang::MacroInfo::const_tokens_iterator
601 ti = macro_info->tokens_begin(),
602 te = macro_info->tokens_end();
603 ti != te; ++ti) {
604 if (!first_token)
605 macro_expansion.append(" ");
606 else
607 first_token = false;
608
609 if (ti->isLiteral()) {
610 if (const char *literal_data = ti->getLiteralData()) {
611 std::string token_str(literal_data, ti->getLength());
612 macro_expansion.append(token_str);
613 } else {
614 bool invalid = false;
615 const char *literal_source =
616 m_compiler_instance->getSourceManager().getCharacterData(
617 ti->getLocation(), &invalid);
618
619 if (invalid) {
620 lldbassert(0 && "Unhandled token kind");
621 macro_expansion.append("<unknown literal value>");
622 } else {
623 macro_expansion.append(
624 std::string(literal_source, ti->getLength()));
625 }
626 }
627 } else if (const char *punctuator_spelling =
628 clang::tok::getPunctuatorSpelling(ti->getKind())) {
629 macro_expansion.append(punctuator_spelling);
630 } else if (const char *keyword_spelling =
631 clang::tok::getKeywordSpelling(ti->getKind())) {
632 macro_expansion.append(keyword_spelling);
633 } else {
634 switch (ti->getKind()) {
635 case clang::tok::TokenKind::identifier:
636 macro_expansion.append(ti->getIdentifierInfo()->getName().str());
637 break;
638 case clang::tok::TokenKind::raw_identifier:
639 macro_expansion.append(ti->getRawIdentifier().str());
640 break;
641 default:
642 macro_expansion.append(ti->getName());
643 break;
644 }
645 }
646 }
647
648 if (handler(macro_identifier, macro_expansion)) {
649 return;
650 }
651 }
652 }
653 }
654}
655
656clang::ModuleLoadResult
657ClangModulesDeclVendorImpl::DoGetModule(clang::ModuleIdPath path,
658 bool make_visible) {
659 clang::Module::NameVisibilityKind visibility =
660 make_visible ? clang::Module::AllVisible : clang::Module::Hidden;
661
662 const bool is_inclusion_directive = false;
663
664 return m_compiler_instance->loadModule(path.front().getLoc(), path,
665 visibility, is_inclusion_directive);
666}
667
668static const char *ModuleImportBufferName = "LLDBModulesMemoryBuffer";
669
672 // FIXME we should insure programmatically that the expression parser's
673 // compiler and the modules runtime's
674 // compiler are both initialized in the same way – preferably by the same
675 // code.
676
677 if (!target.GetPlatform()->SupportsModules())
678 return nullptr;
679
680 const ArchSpec &arch = target.GetArchitecture();
681
682 std::vector<std::string> compiler_invocation_arguments = {
683 "clang",
684 "-fmodules",
685 "-fimplicit-module-maps",
686 "-fcxx-modules",
687 "-fsyntax-only",
688 "-femit-all-decls",
689 "-target",
690 arch.GetTriple().str(),
691 "-fmodules-validate-system-headers",
692 "-Werror=non-modular-include-in-framework-module",
693 "-Xclang=-fincremental-extensions",
694 "-Rmodule-import",
695 "-Rmodule-build"};
696
697 target.GetPlatform()->AddClangModuleCompilationOptions(
698 &target, compiler_invocation_arguments);
699
700 compiler_invocation_arguments.push_back(ModuleImportBufferName);
701
702 // Add additional search paths with { "-I", path } or { "-F", path } here.
703
704 {
705 llvm::SmallString<128> path;
706 const auto &props = ModuleList::GetGlobalModuleListProperties();
707 props.GetClangModulesCachePath().GetPath(path);
708 std::string module_cache_argument("-fmodules-cache-path=");
709 module_cache_argument.append(std::string(path.str()));
710 compiler_invocation_arguments.push_back(module_cache_argument);
711 }
712
713 FileSpecList module_search_paths = target.GetClangModuleSearchPaths();
714
715 for (size_t spi = 0, spe = module_search_paths.GetSize(); spi < spe; ++spi) {
716 const FileSpec &search_path = module_search_paths.GetFileSpecAtIndex(spi);
717
718 std::string search_path_argument = "-I";
719 search_path_argument.append(search_path.GetPath());
720
721 compiler_invocation_arguments.push_back(search_path_argument);
722 }
723
724 {
725 FileSpec clang_resource_dir = GetClangResourceDir();
726
727 if (FileSystem::Instance().IsDirectory(clang_resource_dir.GetPath())) {
728 compiler_invocation_arguments.push_back("-resource-dir");
729 compiler_invocation_arguments.push_back(clang_resource_dir.GetPath());
730 }
731 }
732
733 std::vector<const char *> compiler_invocation_argument_cstrs;
734 compiler_invocation_argument_cstrs.reserve(
735 compiler_invocation_arguments.size());
736 for (const std::string &arg : compiler_invocation_arguments)
737 compiler_invocation_argument_cstrs.push_back(arg.c_str());
738
739 auto diag_options_up =
740 clang::CreateAndPopulateDiagOpts(compiler_invocation_argument_cstrs);
741 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine =
742 clang::CompilerInstance::createDiagnostics(
743 *FileSystem::Instance().GetVirtualFileSystem(), *diag_options_up,
744 new StoringDiagnosticConsumer);
745
747 LLDB_LOG(log, "ClangModulesDeclVendor's compiler flags {0:$[ ]}",
748 llvm::make_range(compiler_invocation_arguments.begin(),
749 compiler_invocation_arguments.end()));
750
751 clang::CreateInvocationOptions CIOpts;
752 CIOpts.Diags = diagnostics_engine;
753 std::shared_ptr<clang::CompilerInvocation> invocation =
754 clang::createInvocation(compiler_invocation_argument_cstrs,
755 std::move(CIOpts));
756
757 if (!invocation)
758 return nullptr;
759
760 std::unique_ptr<llvm::MemoryBuffer> source_buffer =
761 llvm::MemoryBuffer::getMemBuffer(
762 "extern int __lldb __attribute__((unavailable));",
764
765 invocation->getPreprocessorOpts().addRemappedFile(ModuleImportBufferName,
766 source_buffer.release());
767
768 auto instance = std::make_unique<clang::CompilerInstance>(invocation);
769
770 // Make sure clang uses the same VFS as LLDB.
771 instance->setVirtualFileSystem(FileSystem::Instance().GetVirtualFileSystem());
772 instance->createFileManager();
773 instance->setDiagnostics(diagnostics_engine);
774
775 std::unique_ptr<clang::FrontendAction> action(new clang::SyntaxOnlyAction);
776
777 instance->setTarget(clang::TargetInfo::CreateTargetInfo(
778 *diagnostics_engine, instance->getInvocation().getTargetOpts()));
779
780 if (!instance->hasTarget())
781 return nullptr;
782
783 instance->getTarget().adjust(*diagnostics_engine, instance->getLangOpts(),
784 /*AuxTarget=*/nullptr);
785
786 if (!action->BeginSourceFile(*instance,
787 instance->getFrontendOpts().Inputs[0]))
788 return nullptr;
789
790 instance->createASTReader();
791
792 instance->createSema(action->getTranslationUnitKind(), nullptr);
793
794 const bool skipFunctionBodies = false;
795 std::unique_ptr<clang::Parser> parser(new clang::Parser(
796 instance->getPreprocessor(), instance->getSema(), skipFunctionBodies));
797
798 instance->getPreprocessor().EnterMainSourceFile();
799 parser->Initialize();
800
801 clang::Parser::DeclGroupPtrTy parsed;
802 auto ImportState = clang::Sema::ModuleImportState::NotACXX20Module;
803 while (!parser->ParseTopLevelDecl(parsed, ImportState))
804 ;
805
806 return new ClangModulesDeclVendorImpl(
807 std::move(diag_options_up), std::move(diagnostics_engine),
808 std::move(invocation), std::move(instance), std::move(parser));
809}
static const char * ModuleImportBufferName
#define lldbassert(x)
Definition LLDBAssert.h:16
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:364
static void DumpDiagnostics(void *cookie)
An architecture specification class.
Definition ArchSpec.h:32
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:460
static ClangModulesDeclVendor * Create(Target &target)
static bool LanguageSupportsClangModules(lldb::LanguageType language)
Query whether Clang supports modules for a particular language.
A class that describes a compilation unit.
Definition CompileUnit.h:43
const std::vector< SourceModule > & GetImportedModules()
Get the compile unit's imported module list.
lldb::LanguageType GetLanguage()
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const char * GetCString() const
Get the string value as a C string.
DeclVendor(DeclVendorKind kind)
Definition DeclVendor.h:28
A file collection class.
const FileSpec & GetFileSpecAtIndex(size_t idx) const
Get file at index.
size_t GetSize() const
Get the number of files in the file list.
A file utility class.
Definition FileSpec.h:57
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
static FileSystem & Instance()
static ModuleListProperties & GetGlobalModuleListProperties()
llvm::StringRef GetString() const
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
size_t PutChar(char ch)
Definition Stream.cpp:131
FileSpecList GetClangModuleSearchPaths()
Definition Target.cpp:5447
lldb::PlatformSP GetPlatform()
Definition Target.h:1972
const ArchSpec & GetArchitecture() const
Definition Target.h:1282
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
FileSpec GetClangResourceDir()
LanguageType
Programming language type.
@ eLanguageTypeC_plus_plus_14
ISO C++:2014.
@ eLanguageTypeC11
ISO C:2011.
@ eLanguageTypeC99
ISO C:1999.
@ eLanguageTypeC_plus_plus_03
ISO C++:2003.
@ eLanguageTypeObjC_plus_plus
Objective-C++.
@ eLanguageTypeC_plus_plus_11
ISO C++:2011.
@ eLanguageTypeC89
ISO C:1989.
@ eLanguageTypeC
Non-standardized C, such as K&R.
@ eLanguageTypeObjC
Objective-C.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
Information needed to import a source-language module.
std::vector< ConstString > path
Something like "Module.Submodule".