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/Parse/Parser.h"
20 #include "clang/Sema/Lookup.h"
21 #include "clang/Serialization/ASTReader.h"
24 #include "ClangModulesDeclVendor.h"
26 #include "lldb/Core/Log.h"
27 #include "lldb/Core/StreamString.h"
28 #include "lldb/Host/FileSpec.h"
29 #include "lldb/Host/Host.h"
30 #include "lldb/Host/HostInfo.h"
31 #include "lldb/Symbol/CompileUnit.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Utility/LLDBAssert.h"
35 using namespace lldb_private;
38 // Any Clang compiler requires a consumer for diagnostics. This one stores them as strings
39 // so we can provide them to the user in case a module failed to load.
40 class StoringDiagnosticConsumer : public clang::DiagnosticConsumer
43 StoringDiagnosticConsumer ();
46 HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
47 const clang::Diagnostic &info) override;
53 DumpDiagnostics (Stream &error_stream);
56 typedef std::pair<clang::DiagnosticsEngine::Level, std::string> IDAndDiagnostic;
57 std::vector<IDAndDiagnostic> m_diagnostics;
61 // The private implementation of our ClangModulesDeclVendor. Contains all the Clang state required
63 class ClangModulesDeclVendorImpl : public ClangModulesDeclVendor
66 ClangModulesDeclVendorImpl(llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> &diagnostics_engine,
67 llvm::IntrusiveRefCntPtr<clang::CompilerInvocation> &compiler_invocation,
68 std::unique_ptr<clang::CompilerInstance> &&compiler_instance,
69 std::unique_ptr<clang::Parser> &&parser);
71 ~ClangModulesDeclVendorImpl() override = default;
74 AddModule(ModulePath &path,
75 ModuleVector *exported_modules,
76 Stream &error_stream) override;
79 AddModulesForCompileUnit(CompileUnit &cu,
80 ModuleVector &exported_modules,
81 Stream &error_stream) override;
84 FindDecls(const ConstString &name,
87 std::vector <clang::NamedDecl*> &decls) override;
90 ForEachMacro(const ModuleVector &modules,
91 std::function<bool (const std::string &)> handler) override;
95 ReportModuleExportsHelper (std::set<ClangModulesDeclVendor::ModuleID> &exports,
96 clang::Module *module);
99 ReportModuleExports (ModuleVector &exports,
100 clang::Module *module);
102 clang::ModuleLoadResult
103 DoGetModule(clang::ModuleIdPath path, bool make_visible);
105 bool m_enabled = false;
107 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> m_diagnostics_engine;
108 llvm::IntrusiveRefCntPtr<clang::CompilerInvocation> m_compiler_invocation;
109 std::unique_ptr<clang::CompilerInstance> m_compiler_instance;
110 std::unique_ptr<clang::Parser> m_parser;
111 size_t m_source_location_index = 0; // used to give name components fake SourceLocations
113 typedef std::vector<ConstString> ImportedModule;
114 typedef std::map<ImportedModule, clang::Module *> ImportedModuleMap;
115 typedef std::set<ModuleID> ImportedModuleSet;
116 ImportedModuleMap m_imported_modules;
117 ImportedModuleSet m_user_imported_modules;
119 } // anonymous namespace
121 StoringDiagnosticConsumer::StoringDiagnosticConsumer ()
123 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
127 StoringDiagnosticConsumer::HandleDiagnostic (clang::DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &info)
129 llvm::SmallVector<char, 256> diagnostic_string;
131 info.FormatDiagnostic(diagnostic_string);
133 m_diagnostics.push_back(IDAndDiagnostic(DiagLevel, std::string(diagnostic_string.data(), diagnostic_string.size())));
137 StoringDiagnosticConsumer::ClearDiagnostics ()
139 m_diagnostics.clear();
143 StoringDiagnosticConsumer::DumpDiagnostics (Stream &error_stream)
145 for (IDAndDiagnostic &diag : m_diagnostics)
150 error_stream.PutCString(diag.second.c_str());
151 error_stream.PutChar('\n');
153 case clang::DiagnosticsEngine::Level::Ignored:
162 static FileSpec g_cached_resource_dir;
164 static std::once_flag g_once_flag;
166 std::call_once(g_once_flag, [](){
167 HostInfo::GetLLDBPath (lldb::ePathTypeClangDir, g_cached_resource_dir);
170 return g_cached_resource_dir;
173 ClangModulesDeclVendor::ClangModulesDeclVendor()
177 ClangModulesDeclVendor::~ClangModulesDeclVendor()
181 ClangModulesDeclVendorImpl::ClangModulesDeclVendorImpl(llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> &diagnostics_engine,
182 llvm::IntrusiveRefCntPtr<clang::CompilerInvocation> &compiler_invocation,
183 std::unique_ptr<clang::CompilerInstance> &&compiler_instance,
184 std::unique_ptr<clang::Parser> &&parser) :
185 ClangModulesDeclVendor(),
186 m_diagnostics_engine(diagnostics_engine),
187 m_compiler_invocation(compiler_invocation),
188 m_compiler_instance(std::move(compiler_instance)),
189 m_parser(std::move(parser)),
195 ClangModulesDeclVendorImpl::ReportModuleExportsHelper (std::set<ClangModulesDeclVendor::ModuleID> &exports,
196 clang::Module *module)
198 if (exports.count(reinterpret_cast<ClangModulesDeclVendor::ModuleID>(module)))
201 exports.insert(reinterpret_cast<ClangModulesDeclVendor::ModuleID>(module));
203 llvm::SmallVector<clang::Module*, 2> sub_exports;
205 module->getExportedModules(sub_exports);
207 for (clang::Module *module : sub_exports)
209 ReportModuleExportsHelper(exports, module);
214 ClangModulesDeclVendorImpl::ReportModuleExports (ClangModulesDeclVendor::ModuleVector &exports,
215 clang::Module *module)
217 std::set<ClangModulesDeclVendor::ModuleID> exports_set;
219 ReportModuleExportsHelper(exports_set, module);
221 for (ModuleID module : exports_set)
223 exports.push_back(module);
228 ClangModulesDeclVendorImpl::AddModule(ModulePath &path,
229 ModuleVector *exported_modules,
230 Stream &error_stream)
234 if (m_compiler_instance->hadModuleLoaderFatalFailure())
236 error_stream.PutCString("error: Couldn't load a module because the module loader is in a fatal state.\n");
240 // Check if we've already imported this module.
242 std::vector<ConstString> imported_module;
244 for (ConstString path_component : path)
246 imported_module.push_back(path_component);
250 ImportedModuleMap::iterator mi = m_imported_modules.find(imported_module);
252 if (mi != m_imported_modules.end())
254 if (exported_modules)
256 ReportModuleExports(*exported_modules, mi->second);
262 if (!m_compiler_instance->getPreprocessor().getHeaderSearchInfo().lookupModule(path[0].GetStringRef()))
264 error_stream.Printf("error: Header search couldn't locate module %s\n", path[0].AsCString());
268 llvm::SmallVector<std::pair<clang::IdentifierInfo *, clang::SourceLocation>, 4> clang_path;
271 clang::SourceManager &source_manager = m_compiler_instance->getASTContext().getSourceManager();
273 for (ConstString path_component : path)
275 clang_path.push_back(std::make_pair(&m_compiler_instance->getASTContext().Idents.get(path_component.GetStringRef()),
276 source_manager.getLocForStartOfFile(source_manager.getMainFileID()).getLocWithOffset(m_source_location_index++)));
280 StoringDiagnosticConsumer *diagnostic_consumer = static_cast<StoringDiagnosticConsumer *>(m_compiler_instance->getDiagnostics().getClient());
282 diagnostic_consumer->ClearDiagnostics();
284 clang::Module *top_level_module = DoGetModule(clang_path.front(), false);
286 if (!top_level_module)
288 diagnostic_consumer->DumpDiagnostics(error_stream);
289 error_stream.Printf("error: Couldn't load top-level module %s\n", path[0].AsCString());
293 clang::Module *submodule = top_level_module;
295 for (size_t ci = 1; ci < path.size(); ++ci)
297 llvm::StringRef component = path[ci].GetStringRef();
298 submodule = submodule->findSubmodule(component.str());
301 diagnostic_consumer->DumpDiagnostics(error_stream);
302 error_stream.Printf("error: Couldn't load submodule %s\n", component.str().c_str());
307 clang::Module *requested_module = DoGetModule(clang_path, true);
309 if (requested_module != nullptr)
311 if (exported_modules)
313 ReportModuleExports(*exported_modules, requested_module);
316 m_imported_modules[imported_module] = requested_module;
327 ClangModulesDeclVendor::LanguageSupportsClangModules (lldb::LanguageType language)
333 // C++ and friends to be added
334 case lldb::LanguageType::eLanguageTypeC:
335 case lldb::LanguageType::eLanguageTypeC11:
336 case lldb::LanguageType::eLanguageTypeC89:
337 case lldb::LanguageType::eLanguageTypeC99:
338 case lldb::LanguageType::eLanguageTypeObjC:
344 ClangModulesDeclVendorImpl::AddModulesForCompileUnit(CompileUnit &cu,
345 ClangModulesDeclVendor::ModuleVector &exported_modules,
346 Stream &error_stream)
348 if (LanguageSupportsClangModules(cu.GetLanguage()))
350 std::vector<ConstString> imported_modules = cu.GetImportedModules();
352 for (ConstString imported_module : imported_modules)
354 std::vector<ConstString> path;
356 path.push_back(imported_module);
358 if (!AddModule(path, &exported_modules, error_stream))
370 // ClangImporter::lookupValue
373 ClangModulesDeclVendorImpl::FindDecls (const ConstString &name,
375 uint32_t max_matches,
376 std::vector <clang::NamedDecl*> &decls)
386 clang::IdentifierInfo &ident = m_compiler_instance->getASTContext().Idents.get(name.GetStringRef());
388 clang::LookupResult lookup_result(m_compiler_instance->getSema(),
389 clang::DeclarationName(&ident),
390 clang::SourceLocation(),
391 clang::Sema::LookupOrdinaryName);
393 m_compiler_instance->getSema().LookupName(lookup_result, m_compiler_instance->getSema().getScopeForContext(m_compiler_instance->getASTContext().getTranslationUnitDecl()));
395 uint32_t num_matches = 0;
397 for (clang::NamedDecl *named_decl : lookup_result)
399 if (num_matches >= max_matches)
402 decls.push_back(named_decl);
410 ClangModulesDeclVendorImpl::ForEachMacro(const ClangModulesDeclVendor::ModuleVector &modules,
411 std::function<bool (const std::string &)> handler)
418 typedef std::map<ModuleID, ssize_t> ModulePriorityMap;
419 ModulePriorityMap module_priorities;
421 ssize_t priority = 0;
423 for (ModuleID module : modules)
425 module_priorities[module] = priority++;
428 if (m_compiler_instance->getPreprocessor().getExternalSource())
430 m_compiler_instance->getPreprocessor().getExternalSource()->ReadDefinedMacros();
433 for (clang::Preprocessor::macro_iterator mi = m_compiler_instance->getPreprocessor().macro_begin(),
434 me = m_compiler_instance->getPreprocessor().macro_end();
438 const clang::IdentifierInfo *ii = nullptr;
441 if (clang::IdentifierInfoLookup *lookup = m_compiler_instance->getPreprocessor().getIdentifierTable().getExternalIdentifierLookup())
443 lookup->get(mi->first->getName());
451 ssize_t found_priority = -1;
452 clang::MacroInfo *macro_info = nullptr;
454 for (clang::ModuleMacro *module_macro : m_compiler_instance->getPreprocessor().getLeafModuleMacros(ii))
456 clang::Module *module = module_macro->getOwningModule();
459 ModulePriorityMap::iterator pi = module_priorities.find(reinterpret_cast<ModuleID>(module));
461 if (pi != module_priorities.end() && pi->second > found_priority)
463 macro_info = module_macro->getMacroInfo();
464 found_priority = pi->second;
468 clang::Module *top_level_module = module->getTopLevelModule();
470 if (top_level_module != module)
472 ModulePriorityMap::iterator pi = module_priorities.find(reinterpret_cast<ModuleID>(top_level_module));
474 if ((pi != module_priorities.end()) && pi->second > found_priority)
476 macro_info = module_macro->getMacroInfo();
477 found_priority = pi->second;
484 std::string macro_expansion = "#define ";
485 macro_expansion.append(mi->first->getName().str().c_str());
488 if (macro_info->isFunctionLike())
490 macro_expansion.append("(");
492 bool first_arg = true;
494 for (clang::MacroInfo::arg_iterator ai = macro_info->arg_begin(),
495 ae = macro_info->arg_end();
501 macro_expansion.append(", ");
508 macro_expansion.append((*ai)->getName().str());
511 if (macro_info->isC99Varargs())
515 macro_expansion.append("...");
519 macro_expansion.append(", ...");
522 else if (macro_info->isGNUVarargs())
524 macro_expansion.append("...");
527 macro_expansion.append(")");
530 macro_expansion.append(" ");
532 bool first_token = true;
534 for (clang::MacroInfo::tokens_iterator ti = macro_info->tokens_begin(),
535 te = macro_info->tokens_end();
541 macro_expansion.append(" ");
550 if (const char *literal_data = ti->getLiteralData())
552 std::string token_str(literal_data, ti->getLength());
553 macro_expansion.append(token_str);
557 bool invalid = false;
558 const char *literal_source = m_compiler_instance->getSourceManager().getCharacterData(ti->getLocation(), &invalid);
562 lldbassert(!"Unhandled token kind");
563 macro_expansion.append("<unknown literal value>");
567 macro_expansion.append(std::string(literal_source, ti->getLength()));
571 else if (const char *punctuator_spelling = clang::tok::getPunctuatorSpelling(ti->getKind()))
573 macro_expansion.append(punctuator_spelling);
575 else if (const char *keyword_spelling = clang::tok::getKeywordSpelling(ti->getKind()))
577 macro_expansion.append(keyword_spelling);
581 switch (ti->getKind())
583 case clang::tok::TokenKind::identifier:
584 macro_expansion.append(ti->getIdentifierInfo()->getName().str());
586 case clang::tok::TokenKind::raw_identifier:
587 macro_expansion.append(ti->getRawIdentifier().str());
589 macro_expansion.append(ti->getName());
595 if (handler(macro_expansion))
604 clang::ModuleLoadResult
605 ClangModulesDeclVendorImpl::DoGetModule(clang::ModuleIdPath path,
608 clang::Module::NameVisibilityKind visibility = make_visible ? clang::Module::AllVisible : clang::Module::Hidden;
610 const bool is_inclusion_directive = false;
612 return m_compiler_instance->loadModule(path.front().second, path, visibility, is_inclusion_directive);
615 static const char *ModuleImportBufferName = "LLDBModulesMemoryBuffer";
617 lldb_private::ClangModulesDeclVendor *
618 ClangModulesDeclVendor::Create(Target &target)
620 // FIXME we should insure programmatically that the expression parser's compiler and the modules runtime's
621 // compiler are both initialized in the same way – preferably by the same code.
623 if (!target.GetPlatform()->SupportsModules())
626 const ArchSpec &arch = target.GetArchitecture();
628 std::vector<std::string> compiler_invocation_arguments =
634 "-target", arch.GetTriple().str(),
635 "-fmodules-validate-system-headers",
636 "-Werror=non-modular-include-in-framework-module"
639 target.GetPlatform()->AddClangModuleCompilationOptions(&target, compiler_invocation_arguments);
641 compiler_invocation_arguments.push_back(ModuleImportBufferName);
643 // Add additional search paths with { "-I", path } or { "-F", path } here.
646 llvm::SmallString<128> DefaultModuleCache;
647 const bool erased_on_reboot = false;
648 llvm::sys::path::system_temp_directory(erased_on_reboot, DefaultModuleCache);
649 llvm::sys::path::append(DefaultModuleCache, "org.llvm.clang");
650 llvm::sys::path::append(DefaultModuleCache, "ModuleCache");
651 std::string module_cache_argument("-fmodules-cache-path=");
652 module_cache_argument.append(DefaultModuleCache.str().str());
653 compiler_invocation_arguments.push_back(module_cache_argument);
656 FileSpecList &module_search_paths = target.GetClangModuleSearchPaths();
658 for (size_t spi = 0, spe = module_search_paths.GetSize(); spi < spe; ++spi)
660 const FileSpec &search_path = module_search_paths.GetFileSpecAtIndex(spi);
662 std::string search_path_argument = "-I";
663 search_path_argument.append(search_path.GetPath());
665 compiler_invocation_arguments.push_back(search_path_argument);
669 FileSpec clang_resource_dir = GetResourceDir();
671 if (clang_resource_dir.IsDirectory())
673 compiler_invocation_arguments.push_back("-resource-dir");
674 compiler_invocation_arguments.push_back(clang_resource_dir.GetPath());
678 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine = clang::CompilerInstance::createDiagnostics(new clang::DiagnosticOptions,
679 new StoringDiagnosticConsumer);
681 std::vector<const char *> compiler_invocation_argument_cstrs;
683 for (const std::string &arg : compiler_invocation_arguments) {
684 compiler_invocation_argument_cstrs.push_back(arg.c_str());
687 llvm::IntrusiveRefCntPtr<clang::CompilerInvocation> invocation(clang::createInvocationFromCommandLine(compiler_invocation_argument_cstrs, diagnostics_engine));
692 std::unique_ptr<llvm::MemoryBuffer> source_buffer = llvm::MemoryBuffer::getMemBuffer("extern int __lldb __attribute__((unavailable));",
693 ModuleImportBufferName);
695 invocation->getPreprocessorOpts().addRemappedFile(ModuleImportBufferName, source_buffer.release());
697 std::unique_ptr<clang::CompilerInstance> instance(new clang::CompilerInstance);
699 instance->setDiagnostics(diagnostics_engine.get());
700 instance->setInvocation(invocation.get());
702 std::unique_ptr<clang::FrontendAction> action(new clang::SyntaxOnlyAction);
704 instance->setTarget(clang::TargetInfo::CreateTargetInfo(*diagnostics_engine, instance->getInvocation().TargetOpts));
706 if (!instance->hasTarget())
709 instance->getTarget().adjust(instance->getLangOpts());
711 if (!action->BeginSourceFile(*instance, instance->getFrontendOpts().Inputs[0]))
714 instance->getPreprocessor().enableIncrementalProcessing();
716 instance->createModuleManager();
718 instance->createSema(action->getTranslationUnitKind(), nullptr);
720 const bool skipFunctionBodies = false;
721 std::unique_ptr<clang::Parser> parser(new clang::Parser(instance->getPreprocessor(), instance->getSema(), skipFunctionBodies));
723 instance->getPreprocessor().EnterMainSourceFile();
724 parser->Initialize();
726 clang::Parser::DeclGroupPtrTy parsed;
728 while (!parser->ParseTopLevelDecl(parsed));
730 return new ClangModulesDeclVendorImpl (diagnostics_engine, invocation, std::move(instance), std::move(parser));