1 //===-- ClangModulesDeclVendor.cpp ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 // Other libraries and framework includes
15 #include "clang/Basic/TargetInfo.h"
16 #include "clang/Frontend/CompilerInstance.h"
17 #include "clang/Frontend/FrontendActions.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/Support/FileSystem.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/Threading.h"
28 #include "ClangModulesDeclVendor.h"
30 #include "lldb/Host/Host.h"
31 #include "lldb/Host/HostInfo.h"
32 #include "lldb/Symbol/CompileUnit.h"
33 #include "lldb/Target/Target.h"
34 #include "lldb/Utility/FileSpec.h"
35 #include "lldb/Utility/LLDBAssert.h"
36 #include "lldb/Utility/Log.h"
37 #include "lldb/Utility/StreamString.h"
39 using namespace lldb_private;
42 // Any Clang compiler requires a consumer for diagnostics. This one stores them
44 // so we can provide them to the user in case a module failed to load.
45 class StoringDiagnosticConsumer : public clang::DiagnosticConsumer {
47 StoringDiagnosticConsumer();
49 void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
50 const clang::Diagnostic &info) override;
52 void ClearDiagnostics();
54 void DumpDiagnostics(Stream &error_stream);
57 typedef std::pair<clang::DiagnosticsEngine::Level, std::string>
59 std::vector<IDAndDiagnostic> m_diagnostics;
63 // The private implementation of our ClangModulesDeclVendor. Contains all the
64 // Clang state required
66 class ClangModulesDeclVendorImpl : public ClangModulesDeclVendor {
68 ClangModulesDeclVendorImpl(
69 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
70 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
71 std::unique_ptr<clang::CompilerInstance> compiler_instance,
72 std::unique_ptr<clang::Parser> parser);
74 ~ClangModulesDeclVendorImpl() override = default;
76 bool AddModule(ModulePath &path, ModuleVector *exported_modules,
77 Stream &error_stream) override;
79 bool AddModulesForCompileUnit(CompileUnit &cu, ModuleVector &exported_modules,
80 Stream &error_stream) override;
82 uint32_t FindDecls(const ConstString &name, bool append, uint32_t max_matches,
83 std::vector<clang::NamedDecl *> &decls) override;
85 void ForEachMacro(const ModuleVector &modules,
86 std::function<bool(const std::string &)> handler) override;
88 clang::ExternalASTMerger::ImporterSource GetImporterSource() override;
91 ReportModuleExportsHelper(std::set<ClangModulesDeclVendor::ModuleID> &exports,
92 clang::Module *module);
94 void ReportModuleExports(ModuleVector &exports, clang::Module *module);
96 clang::ModuleLoadResult DoGetModule(clang::ModuleIdPath path,
99 bool m_enabled = false;
101 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> m_diagnostics_engine;
102 std::shared_ptr<clang::CompilerInvocation> m_compiler_invocation;
103 std::unique_ptr<clang::CompilerInstance> m_compiler_instance;
104 std::unique_ptr<clang::Parser> m_parser;
105 size_t m_source_location_index =
106 0; // used to give name components fake SourceLocations
108 typedef std::vector<ConstString> ImportedModule;
109 typedef std::map<ImportedModule, clang::Module *> ImportedModuleMap;
110 typedef std::set<ModuleID> ImportedModuleSet;
111 ImportedModuleMap m_imported_modules;
112 ImportedModuleSet m_user_imported_modules;
113 const clang::ExternalASTMerger::OriginMap m_origin_map;
115 } // anonymous namespace
117 StoringDiagnosticConsumer::StoringDiagnosticConsumer() {
118 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
121 void StoringDiagnosticConsumer::HandleDiagnostic(
122 clang::DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &info) {
123 llvm::SmallVector<char, 256> diagnostic_string;
125 info.FormatDiagnostic(diagnostic_string);
127 m_diagnostics.push_back(
128 IDAndDiagnostic(DiagLevel, std::string(diagnostic_string.data(),
129 diagnostic_string.size())));
132 void StoringDiagnosticConsumer::ClearDiagnostics() { m_diagnostics.clear(); }
134 void StoringDiagnosticConsumer::DumpDiagnostics(Stream &error_stream) {
135 for (IDAndDiagnostic &diag : m_diagnostics) {
136 switch (diag.first) {
138 error_stream.PutCString(diag.second);
139 error_stream.PutChar('\n');
141 case clang::DiagnosticsEngine::Level::Ignored:
147 static FileSpec GetResourceDir() {
148 static FileSpec g_cached_resource_dir;
150 static llvm::once_flag g_once_flag;
152 llvm::call_once(g_once_flag, []() {
153 HostInfo::GetLLDBPath(lldb::ePathTypeClangDir, g_cached_resource_dir);
156 return g_cached_resource_dir;
159 ClangModulesDeclVendor::ClangModulesDeclVendor() {}
161 ClangModulesDeclVendor::~ClangModulesDeclVendor() {}
163 ClangModulesDeclVendorImpl::ClangModulesDeclVendorImpl(
164 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
165 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
166 std::unique_ptr<clang::CompilerInstance> compiler_instance,
167 std::unique_ptr<clang::Parser> parser)
168 : m_diagnostics_engine(std::move(diagnostics_engine)),
169 m_compiler_invocation(std::move(compiler_invocation)),
170 m_compiler_instance(std::move(compiler_instance)),
171 m_parser(std::move(parser)) {}
173 void ClangModulesDeclVendorImpl::ReportModuleExportsHelper(
174 std::set<ClangModulesDeclVendor::ModuleID> &exports,
175 clang::Module *module) {
176 if (exports.count(reinterpret_cast<ClangModulesDeclVendor::ModuleID>(module)))
179 exports.insert(reinterpret_cast<ClangModulesDeclVendor::ModuleID>(module));
181 llvm::SmallVector<clang::Module *, 2> sub_exports;
183 module->getExportedModules(sub_exports);
185 for (clang::Module *module : sub_exports) {
186 ReportModuleExportsHelper(exports, module);
190 void ClangModulesDeclVendorImpl::ReportModuleExports(
191 ClangModulesDeclVendor::ModuleVector &exports, clang::Module *module) {
192 std::set<ClangModulesDeclVendor::ModuleID> exports_set;
194 ReportModuleExportsHelper(exports_set, module);
196 for (ModuleID module : exports_set) {
197 exports.push_back(module);
201 bool ClangModulesDeclVendorImpl::AddModule(ModulePath &path,
202 ModuleVector *exported_modules,
203 Stream &error_stream) {
206 if (m_compiler_instance->hadModuleLoaderFatalFailure()) {
207 error_stream.PutCString("error: Couldn't load a module because the module "
208 "loader is in a fatal state.\n");
212 // Check if we've already imported this module.
214 std::vector<ConstString> imported_module;
216 for (ConstString path_component : path) {
217 imported_module.push_back(path_component);
221 ImportedModuleMap::iterator mi = m_imported_modules.find(imported_module);
223 if (mi != m_imported_modules.end()) {
224 if (exported_modules) {
225 ReportModuleExports(*exported_modules, mi->second);
231 if (!m_compiler_instance->getPreprocessor()
232 .getHeaderSearchInfo()
233 .lookupModule(path[0].GetStringRef())) {
234 error_stream.Printf("error: Header search couldn't locate module %s\n",
235 path[0].AsCString());
239 llvm::SmallVector<std::pair<clang::IdentifierInfo *, clang::SourceLocation>,
244 clang::SourceManager &source_manager =
245 m_compiler_instance->getASTContext().getSourceManager();
247 for (ConstString path_component : path) {
248 clang_path.push_back(std::make_pair(
249 &m_compiler_instance->getASTContext().Idents.get(
250 path_component.GetStringRef()),
251 source_manager.getLocForStartOfFile(source_manager.getMainFileID())
252 .getLocWithOffset(m_source_location_index++)));
256 StoringDiagnosticConsumer *diagnostic_consumer =
257 static_cast<StoringDiagnosticConsumer *>(
258 m_compiler_instance->getDiagnostics().getClient());
260 diagnostic_consumer->ClearDiagnostics();
262 clang::Module *top_level_module = DoGetModule(clang_path.front(), false);
264 if (!top_level_module) {
265 diagnostic_consumer->DumpDiagnostics(error_stream);
266 error_stream.Printf("error: Couldn't load top-level module %s\n",
267 path[0].AsCString());
271 clang::Module *submodule = top_level_module;
273 for (size_t ci = 1; ci < path.size(); ++ci) {
274 llvm::StringRef component = path[ci].GetStringRef();
275 submodule = submodule->findSubmodule(component.str());
277 diagnostic_consumer->DumpDiagnostics(error_stream);
278 error_stream.Printf("error: Couldn't load submodule %s\n",
279 component.str().c_str());
284 clang::Module *requested_module = DoGetModule(clang_path, true);
286 if (requested_module != nullptr) {
287 if (exported_modules) {
288 ReportModuleExports(*exported_modules, requested_module);
291 m_imported_modules[imported_module] = requested_module;
301 bool ClangModulesDeclVendor::LanguageSupportsClangModules(
302 lldb::LanguageType language) {
306 // C++ and friends to be added
307 case lldb::LanguageType::eLanguageTypeC:
308 case lldb::LanguageType::eLanguageTypeC11:
309 case lldb::LanguageType::eLanguageTypeC89:
310 case lldb::LanguageType::eLanguageTypeC99:
311 case lldb::LanguageType::eLanguageTypeObjC:
316 bool ClangModulesDeclVendorImpl::AddModulesForCompileUnit(
317 CompileUnit &cu, ClangModulesDeclVendor::ModuleVector &exported_modules,
318 Stream &error_stream) {
319 if (LanguageSupportsClangModules(cu.GetLanguage())) {
320 std::vector<ConstString> imported_modules = cu.GetImportedModules();
322 for (ConstString imported_module : imported_modules) {
323 std::vector<ConstString> path;
325 path.push_back(imported_module);
327 if (!AddModule(path, &exported_modules, error_stream)) {
338 // ClangImporter::lookupValue
341 ClangModulesDeclVendorImpl::FindDecls(const ConstString &name, bool append,
342 uint32_t max_matches,
343 std::vector<clang::NamedDecl *> &decls) {
351 clang::IdentifierInfo &ident =
352 m_compiler_instance->getASTContext().Idents.get(name.GetStringRef());
354 clang::LookupResult lookup_result(
355 m_compiler_instance->getSema(), clang::DeclarationName(&ident),
356 clang::SourceLocation(), clang::Sema::LookupOrdinaryName);
358 m_compiler_instance->getSema().LookupName(
360 m_compiler_instance->getSema().getScopeForContext(
361 m_compiler_instance->getASTContext().getTranslationUnitDecl()));
363 uint32_t num_matches = 0;
365 for (clang::NamedDecl *named_decl : lookup_result) {
366 if (num_matches >= max_matches)
369 decls.push_back(named_decl);
376 void ClangModulesDeclVendorImpl::ForEachMacro(
377 const ClangModulesDeclVendor::ModuleVector &modules,
378 std::function<bool(const std::string &)> handler) {
383 typedef std::map<ModuleID, ssize_t> ModulePriorityMap;
384 ModulePriorityMap module_priorities;
386 ssize_t priority = 0;
388 for (ModuleID module : modules) {
389 module_priorities[module] = priority++;
392 if (m_compiler_instance->getPreprocessor().getExternalSource()) {
393 m_compiler_instance->getPreprocessor()
395 ->ReadDefinedMacros();
398 for (clang::Preprocessor::macro_iterator
399 mi = m_compiler_instance->getPreprocessor().macro_begin(),
400 me = m_compiler_instance->getPreprocessor().macro_end();
402 const clang::IdentifierInfo *ii = nullptr;
405 if (clang::IdentifierInfoLookup *lookup =
406 m_compiler_instance->getPreprocessor()
407 .getIdentifierTable()
408 .getExternalIdentifierLookup()) {
409 lookup->get(mi->first->getName());
416 ssize_t found_priority = -1;
417 clang::MacroInfo *macro_info = nullptr;
419 for (clang::ModuleMacro *module_macro :
420 m_compiler_instance->getPreprocessor().getLeafModuleMacros(ii)) {
421 clang::Module *module = module_macro->getOwningModule();
424 ModulePriorityMap::iterator pi =
425 module_priorities.find(reinterpret_cast<ModuleID>(module));
427 if (pi != module_priorities.end() && pi->second > found_priority) {
428 macro_info = module_macro->getMacroInfo();
429 found_priority = pi->second;
433 clang::Module *top_level_module = module->getTopLevelModule();
435 if (top_level_module != module) {
436 ModulePriorityMap::iterator pi = module_priorities.find(
437 reinterpret_cast<ModuleID>(top_level_module));
439 if ((pi != module_priorities.end()) && pi->second > found_priority) {
440 macro_info = module_macro->getMacroInfo();
441 found_priority = pi->second;
447 std::string macro_expansion = "#define ";
448 macro_expansion.append(mi->first->getName().str());
451 if (macro_info->isFunctionLike()) {
452 macro_expansion.append("(");
454 bool first_arg = true;
456 for (auto pi = macro_info->param_begin(),
457 pe = macro_info->param_end();
460 macro_expansion.append(", ");
465 macro_expansion.append((*pi)->getName().str());
468 if (macro_info->isC99Varargs()) {
470 macro_expansion.append("...");
472 macro_expansion.append(", ...");
474 } else if (macro_info->isGNUVarargs()) {
475 macro_expansion.append("...");
478 macro_expansion.append(")");
481 macro_expansion.append(" ");
483 bool first_token = true;
485 for (clang::MacroInfo::tokens_iterator ti = macro_info->tokens_begin(),
486 te = macro_info->tokens_end();
489 macro_expansion.append(" ");
494 if (ti->isLiteral()) {
495 if (const char *literal_data = ti->getLiteralData()) {
496 std::string token_str(literal_data, ti->getLength());
497 macro_expansion.append(token_str);
499 bool invalid = false;
500 const char *literal_source =
501 m_compiler_instance->getSourceManager().getCharacterData(
502 ti->getLocation(), &invalid);
505 lldbassert(0 && "Unhandled token kind");
506 macro_expansion.append("<unknown literal value>");
508 macro_expansion.append(
509 std::string(literal_source, ti->getLength()));
512 } else if (const char *punctuator_spelling =
513 clang::tok::getPunctuatorSpelling(ti->getKind())) {
514 macro_expansion.append(punctuator_spelling);
515 } else if (const char *keyword_spelling =
516 clang::tok::getKeywordSpelling(ti->getKind())) {
517 macro_expansion.append(keyword_spelling);
519 switch (ti->getKind()) {
520 case clang::tok::TokenKind::identifier:
521 macro_expansion.append(ti->getIdentifierInfo()->getName().str());
523 case clang::tok::TokenKind::raw_identifier:
524 macro_expansion.append(ti->getRawIdentifier().str());
527 macro_expansion.append(ti->getName());
533 if (handler(macro_expansion)) {
541 clang::ModuleLoadResult
542 ClangModulesDeclVendorImpl::DoGetModule(clang::ModuleIdPath path,
544 clang::Module::NameVisibilityKind visibility =
545 make_visible ? clang::Module::AllVisible : clang::Module::Hidden;
547 const bool is_inclusion_directive = false;
549 return m_compiler_instance->loadModule(path.front().second, path, visibility,
550 is_inclusion_directive);
553 clang::ExternalASTMerger::ImporterSource
554 ClangModulesDeclVendorImpl::GetImporterSource() {
555 return {m_compiler_instance->getASTContext(),
556 m_compiler_instance->getFileManager(), m_origin_map};
559 static const char *ModuleImportBufferName = "LLDBModulesMemoryBuffer";
561 lldb_private::ClangModulesDeclVendor *
562 ClangModulesDeclVendor::Create(Target &target) {
563 // FIXME we should insure programmatically that the expression parser's
564 // compiler and the modules runtime's
565 // compiler are both initialized in the same way – preferably by the same
568 if (!target.GetPlatform()->SupportsModules())
571 const ArchSpec &arch = target.GetArchitecture();
573 std::vector<std::string> compiler_invocation_arguments = {
576 "-fimplicit-module-maps",
581 arch.GetTriple().str(),
582 "-fmodules-validate-system-headers",
583 "-Werror=non-modular-include-in-framework-module"};
585 target.GetPlatform()->AddClangModuleCompilationOptions(
586 &target, compiler_invocation_arguments);
588 compiler_invocation_arguments.push_back(ModuleImportBufferName);
590 // Add additional search paths with { "-I", path } or { "-F", path } here.
593 llvm::SmallString<128> DefaultModuleCache;
594 const bool erased_on_reboot = false;
595 llvm::sys::path::system_temp_directory(erased_on_reboot,
597 llvm::sys::path::append(DefaultModuleCache, "org.llvm.clang");
598 llvm::sys::path::append(DefaultModuleCache, "ModuleCache");
599 std::string module_cache_argument("-fmodules-cache-path=");
600 module_cache_argument.append(DefaultModuleCache.str().str());
601 compiler_invocation_arguments.push_back(module_cache_argument);
604 FileSpecList &module_search_paths = target.GetClangModuleSearchPaths();
606 for (size_t spi = 0, spe = module_search_paths.GetSize(); spi < spe; ++spi) {
607 const FileSpec &search_path = module_search_paths.GetFileSpecAtIndex(spi);
609 std::string search_path_argument = "-I";
610 search_path_argument.append(search_path.GetPath());
612 compiler_invocation_arguments.push_back(search_path_argument);
616 FileSpec clang_resource_dir = GetResourceDir();
618 if (llvm::sys::fs::is_directory(clang_resource_dir.GetPath())) {
619 compiler_invocation_arguments.push_back("-resource-dir");
620 compiler_invocation_arguments.push_back(clang_resource_dir.GetPath());
624 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine =
625 clang::CompilerInstance::createDiagnostics(new clang::DiagnosticOptions,
626 new StoringDiagnosticConsumer);
628 std::vector<const char *> compiler_invocation_argument_cstrs;
630 for (const std::string &arg : compiler_invocation_arguments) {
631 compiler_invocation_argument_cstrs.push_back(arg.c_str());
634 std::shared_ptr<clang::CompilerInvocation> invocation =
635 clang::createInvocationFromCommandLine(compiler_invocation_argument_cstrs,
641 std::unique_ptr<llvm::MemoryBuffer> source_buffer =
642 llvm::MemoryBuffer::getMemBuffer(
643 "extern int __lldb __attribute__((unavailable));",
644 ModuleImportBufferName);
646 invocation->getPreprocessorOpts().addRemappedFile(ModuleImportBufferName,
647 source_buffer.release());
649 std::unique_ptr<clang::CompilerInstance> instance(
650 new clang::CompilerInstance);
652 instance->setDiagnostics(diagnostics_engine.get());
653 instance->setInvocation(invocation);
655 std::unique_ptr<clang::FrontendAction> action(new clang::SyntaxOnlyAction);
657 instance->setTarget(clang::TargetInfo::CreateTargetInfo(
658 *diagnostics_engine, instance->getInvocation().TargetOpts));
660 if (!instance->hasTarget())
663 instance->getTarget().adjust(instance->getLangOpts());
665 if (!action->BeginSourceFile(*instance,
666 instance->getFrontendOpts().Inputs[0]))
669 instance->getPreprocessor().enableIncrementalProcessing();
671 instance->createModuleManager();
673 instance->createSema(action->getTranslationUnitKind(), nullptr);
675 const bool skipFunctionBodies = false;
676 std::unique_ptr<clang::Parser> parser(new clang::Parser(
677 instance->getPreprocessor(), instance->getSema(), skipFunctionBodies));
679 instance->getPreprocessor().EnterMainSourceFile();
680 parser->Initialize();
682 clang::Parser::DeclGroupPtrTy parsed;
684 while (!parser->ParseTopLevelDecl(parsed))
687 return new ClangModulesDeclVendorImpl(std::move(diagnostics_engine),
688 std::move(invocation),
689 std::move(instance), std::move(parser));