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