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/Path.h"
26 #include "ClangModulesDeclVendor.h"
28 #include "lldb/Core/Log.h"
29 #include "lldb/Core/StreamString.h"
30 #include "lldb/Host/FileSpec.h"
31 #include "lldb/Host/Host.h"
32 #include "lldb/Host/HostInfo.h"
33 #include "lldb/Symbol/CompileUnit.h"
34 #include "lldb/Target/Target.h"
35 #include "lldb/Utility/LLDBAssert.h"
37 using namespace lldb_private;
40 // Any Clang compiler requires a consumer for diagnostics. This one stores them
42 // so we can provide them to the user in case a module failed to load.
43 class StoringDiagnosticConsumer : public clang::DiagnosticConsumer {
45 StoringDiagnosticConsumer();
47 void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
48 const clang::Diagnostic &info) override;
50 void ClearDiagnostics();
52 void DumpDiagnostics(Stream &error_stream);
55 typedef std::pair<clang::DiagnosticsEngine::Level, std::string>
57 std::vector<IDAndDiagnostic> m_diagnostics;
61 // The private implementation of our ClangModulesDeclVendor. Contains all the
62 // Clang state required
64 class ClangModulesDeclVendorImpl : public ClangModulesDeclVendor {
66 ClangModulesDeclVendorImpl(
67 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
68 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
69 std::unique_ptr<clang::CompilerInstance> compiler_instance,
70 std::unique_ptr<clang::Parser> parser);
72 ~ClangModulesDeclVendorImpl() override = default;
74 bool AddModule(ModulePath &path, ModuleVector *exported_modules,
75 Stream &error_stream) override;
77 bool AddModulesForCompileUnit(CompileUnit &cu, ModuleVector &exported_modules,
78 Stream &error_stream) override;
80 uint32_t FindDecls(const ConstString &name, bool append, uint32_t max_matches,
81 std::vector<clang::NamedDecl *> &decls) override;
83 void ForEachMacro(const ModuleVector &modules,
84 std::function<bool(const std::string &)> handler) override;
88 ReportModuleExportsHelper(std::set<ClangModulesDeclVendor::ModuleID> &exports,
89 clang::Module *module);
91 void ReportModuleExports(ModuleVector &exports, clang::Module *module);
93 clang::ModuleLoadResult DoGetModule(clang::ModuleIdPath path,
96 bool m_enabled = false;
98 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> m_diagnostics_engine;
99 std::shared_ptr<clang::CompilerInvocation> m_compiler_invocation;
100 std::unique_ptr<clang::CompilerInstance> m_compiler_instance;
101 std::unique_ptr<clang::Parser> m_parser;
102 size_t m_source_location_index =
103 0; // used to give name components fake SourceLocations
105 typedef std::vector<ConstString> ImportedModule;
106 typedef std::map<ImportedModule, clang::Module *> ImportedModuleMap;
107 typedef std::set<ModuleID> ImportedModuleSet;
108 ImportedModuleMap m_imported_modules;
109 ImportedModuleSet m_user_imported_modules;
111 } // anonymous namespace
113 StoringDiagnosticConsumer::StoringDiagnosticConsumer() {
114 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
117 void StoringDiagnosticConsumer::HandleDiagnostic(
118 clang::DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &info) {
119 llvm::SmallVector<char, 256> diagnostic_string;
121 info.FormatDiagnostic(diagnostic_string);
123 m_diagnostics.push_back(
124 IDAndDiagnostic(DiagLevel, std::string(diagnostic_string.data(),
125 diagnostic_string.size())));
128 void StoringDiagnosticConsumer::ClearDiagnostics() { m_diagnostics.clear(); }
130 void StoringDiagnosticConsumer::DumpDiagnostics(Stream &error_stream) {
131 for (IDAndDiagnostic &diag : m_diagnostics) {
132 switch (diag.first) {
134 error_stream.PutCString(diag.second);
135 error_stream.PutChar('\n');
137 case clang::DiagnosticsEngine::Level::Ignored:
143 static FileSpec GetResourceDir() {
144 static FileSpec g_cached_resource_dir;
146 static std::once_flag g_once_flag;
148 std::call_once(g_once_flag, []() {
149 HostInfo::GetLLDBPath(lldb::ePathTypeClangDir, g_cached_resource_dir);
152 return g_cached_resource_dir;
155 ClangModulesDeclVendor::ClangModulesDeclVendor() {}
157 ClangModulesDeclVendor::~ClangModulesDeclVendor() {}
159 ClangModulesDeclVendorImpl::ClangModulesDeclVendorImpl(
160 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
161 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
162 std::unique_ptr<clang::CompilerInstance> compiler_instance,
163 std::unique_ptr<clang::Parser> parser)
164 : m_diagnostics_engine(std::move(diagnostics_engine)),
165 m_compiler_invocation(std::move(compiler_invocation)),
166 m_compiler_instance(std::move(compiler_instance)),
167 m_parser(std::move(parser)) {}
169 void ClangModulesDeclVendorImpl::ReportModuleExportsHelper(
170 std::set<ClangModulesDeclVendor::ModuleID> &exports,
171 clang::Module *module) {
172 if (exports.count(reinterpret_cast<ClangModulesDeclVendor::ModuleID>(module)))
175 exports.insert(reinterpret_cast<ClangModulesDeclVendor::ModuleID>(module));
177 llvm::SmallVector<clang::Module *, 2> sub_exports;
179 module->getExportedModules(sub_exports);
181 for (clang::Module *module : sub_exports) {
182 ReportModuleExportsHelper(exports, module);
186 void ClangModulesDeclVendorImpl::ReportModuleExports(
187 ClangModulesDeclVendor::ModuleVector &exports, clang::Module *module) {
188 std::set<ClangModulesDeclVendor::ModuleID> exports_set;
190 ReportModuleExportsHelper(exports_set, module);
192 for (ModuleID module : exports_set) {
193 exports.push_back(module);
197 bool ClangModulesDeclVendorImpl::AddModule(ModulePath &path,
198 ModuleVector *exported_modules,
199 Stream &error_stream) {
202 if (m_compiler_instance->hadModuleLoaderFatalFailure()) {
203 error_stream.PutCString("error: Couldn't load a module because the module "
204 "loader is in a fatal state.\n");
208 // Check if we've already imported this module.
210 std::vector<ConstString> imported_module;
212 for (ConstString path_component : path) {
213 imported_module.push_back(path_component);
217 ImportedModuleMap::iterator mi = m_imported_modules.find(imported_module);
219 if (mi != m_imported_modules.end()) {
220 if (exported_modules) {
221 ReportModuleExports(*exported_modules, mi->second);
227 if (!m_compiler_instance->getPreprocessor()
228 .getHeaderSearchInfo()
229 .lookupModule(path[0].GetStringRef())) {
230 error_stream.Printf("error: Header search couldn't locate module %s\n",
231 path[0].AsCString());
235 llvm::SmallVector<std::pair<clang::IdentifierInfo *, clang::SourceLocation>,
240 clang::SourceManager &source_manager =
241 m_compiler_instance->getASTContext().getSourceManager();
243 for (ConstString path_component : path) {
244 clang_path.push_back(std::make_pair(
245 &m_compiler_instance->getASTContext().Idents.get(
246 path_component.GetStringRef()),
247 source_manager.getLocForStartOfFile(source_manager.getMainFileID())
248 .getLocWithOffset(m_source_location_index++)));
252 StoringDiagnosticConsumer *diagnostic_consumer =
253 static_cast<StoringDiagnosticConsumer *>(
254 m_compiler_instance->getDiagnostics().getClient());
256 diagnostic_consumer->ClearDiagnostics();
258 clang::Module *top_level_module = DoGetModule(clang_path.front(), false);
260 if (!top_level_module) {
261 diagnostic_consumer->DumpDiagnostics(error_stream);
262 error_stream.Printf("error: Couldn't load top-level module %s\n",
263 path[0].AsCString());
267 clang::Module *submodule = top_level_module;
269 for (size_t ci = 1; ci < path.size(); ++ci) {
270 llvm::StringRef component = path[ci].GetStringRef();
271 submodule = submodule->findSubmodule(component.str());
273 diagnostic_consumer->DumpDiagnostics(error_stream);
274 error_stream.Printf("error: Couldn't load submodule %s\n",
275 component.str().c_str());
280 clang::Module *requested_module = DoGetModule(clang_path, true);
282 if (requested_module != nullptr) {
283 if (exported_modules) {
284 ReportModuleExports(*exported_modules, requested_module);
287 m_imported_modules[imported_module] = requested_module;
297 bool ClangModulesDeclVendor::LanguageSupportsClangModules(
298 lldb::LanguageType language) {
302 // C++ and friends to be added
303 case lldb::LanguageType::eLanguageTypeC:
304 case lldb::LanguageType::eLanguageTypeC11:
305 case lldb::LanguageType::eLanguageTypeC89:
306 case lldb::LanguageType::eLanguageTypeC99:
307 case lldb::LanguageType::eLanguageTypeObjC:
312 bool ClangModulesDeclVendorImpl::AddModulesForCompileUnit(
313 CompileUnit &cu, ClangModulesDeclVendor::ModuleVector &exported_modules,
314 Stream &error_stream) {
315 if (LanguageSupportsClangModules(cu.GetLanguage())) {
316 std::vector<ConstString> imported_modules = cu.GetImportedModules();
318 for (ConstString imported_module : imported_modules) {
319 std::vector<ConstString> path;
321 path.push_back(imported_module);
323 if (!AddModule(path, &exported_modules, error_stream)) {
334 // ClangImporter::lookupValue
337 ClangModulesDeclVendorImpl::FindDecls(const ConstString &name, bool append,
338 uint32_t max_matches,
339 std::vector<clang::NamedDecl *> &decls) {
347 clang::IdentifierInfo &ident =
348 m_compiler_instance->getASTContext().Idents.get(name.GetStringRef());
350 clang::LookupResult lookup_result(
351 m_compiler_instance->getSema(), clang::DeclarationName(&ident),
352 clang::SourceLocation(), clang::Sema::LookupOrdinaryName);
354 m_compiler_instance->getSema().LookupName(
356 m_compiler_instance->getSema().getScopeForContext(
357 m_compiler_instance->getASTContext().getTranslationUnitDecl()));
359 uint32_t num_matches = 0;
361 for (clang::NamedDecl *named_decl : lookup_result) {
362 if (num_matches >= max_matches)
365 decls.push_back(named_decl);
372 void ClangModulesDeclVendorImpl::ForEachMacro(
373 const ClangModulesDeclVendor::ModuleVector &modules,
374 std::function<bool(const std::string &)> handler) {
379 typedef std::map<ModuleID, ssize_t> ModulePriorityMap;
380 ModulePriorityMap module_priorities;
382 ssize_t priority = 0;
384 for (ModuleID module : modules) {
385 module_priorities[module] = priority++;
388 if (m_compiler_instance->getPreprocessor().getExternalSource()) {
389 m_compiler_instance->getPreprocessor()
391 ->ReadDefinedMacros();
394 for (clang::Preprocessor::macro_iterator
395 mi = m_compiler_instance->getPreprocessor().macro_begin(),
396 me = m_compiler_instance->getPreprocessor().macro_end();
398 const clang::IdentifierInfo *ii = nullptr;
401 if (clang::IdentifierInfoLookup *lookup =
402 m_compiler_instance->getPreprocessor()
403 .getIdentifierTable()
404 .getExternalIdentifierLookup()) {
405 lookup->get(mi->first->getName());
412 ssize_t found_priority = -1;
413 clang::MacroInfo *macro_info = nullptr;
415 for (clang::ModuleMacro *module_macro :
416 m_compiler_instance->getPreprocessor().getLeafModuleMacros(ii)) {
417 clang::Module *module = module_macro->getOwningModule();
420 ModulePriorityMap::iterator pi =
421 module_priorities.find(reinterpret_cast<ModuleID>(module));
423 if (pi != module_priorities.end() && pi->second > found_priority) {
424 macro_info = module_macro->getMacroInfo();
425 found_priority = pi->second;
429 clang::Module *top_level_module = module->getTopLevelModule();
431 if (top_level_module != module) {
432 ModulePriorityMap::iterator pi = module_priorities.find(
433 reinterpret_cast<ModuleID>(top_level_module));
435 if ((pi != module_priorities.end()) && pi->second > found_priority) {
436 macro_info = module_macro->getMacroInfo();
437 found_priority = pi->second;
443 std::string macro_expansion = "#define ";
444 macro_expansion.append(mi->first->getName().str());
447 if (macro_info->isFunctionLike()) {
448 macro_expansion.append("(");
450 bool first_arg = true;
452 for (clang::MacroInfo::arg_iterator ai = macro_info->arg_begin(),
453 ae = macro_info->arg_end();
456 macro_expansion.append(", ");
461 macro_expansion.append((*ai)->getName().str());
464 if (macro_info->isC99Varargs()) {
466 macro_expansion.append("...");
468 macro_expansion.append(", ...");
470 } else if (macro_info->isGNUVarargs()) {
471 macro_expansion.append("...");
474 macro_expansion.append(")");
477 macro_expansion.append(" ");
479 bool first_token = true;
481 for (clang::MacroInfo::tokens_iterator ti = macro_info->tokens_begin(),
482 te = macro_info->tokens_end();
485 macro_expansion.append(" ");
490 if (ti->isLiteral()) {
491 if (const char *literal_data = ti->getLiteralData()) {
492 std::string token_str(literal_data, ti->getLength());
493 macro_expansion.append(token_str);
495 bool invalid = false;
496 const char *literal_source =
497 m_compiler_instance->getSourceManager().getCharacterData(
498 ti->getLocation(), &invalid);
501 lldbassert(0 && "Unhandled token kind");
502 macro_expansion.append("<unknown literal value>");
504 macro_expansion.append(
505 std::string(literal_source, ti->getLength()));
508 } else if (const char *punctuator_spelling =
509 clang::tok::getPunctuatorSpelling(ti->getKind())) {
510 macro_expansion.append(punctuator_spelling);
511 } else if (const char *keyword_spelling =
512 clang::tok::getKeywordSpelling(ti->getKind())) {
513 macro_expansion.append(keyword_spelling);
515 switch (ti->getKind()) {
516 case clang::tok::TokenKind::identifier:
517 macro_expansion.append(ti->getIdentifierInfo()->getName().str());
519 case clang::tok::TokenKind::raw_identifier:
520 macro_expansion.append(ti->getRawIdentifier().str());
523 macro_expansion.append(ti->getName());
529 if (handler(macro_expansion)) {
537 clang::ModuleLoadResult
538 ClangModulesDeclVendorImpl::DoGetModule(clang::ModuleIdPath path,
540 clang::Module::NameVisibilityKind visibility =
541 make_visible ? clang::Module::AllVisible : clang::Module::Hidden;
543 const bool is_inclusion_directive = false;
545 return m_compiler_instance->loadModule(path.front().second, path, visibility,
546 is_inclusion_directive);
549 static const char *ModuleImportBufferName = "LLDBModulesMemoryBuffer";
551 lldb_private::ClangModulesDeclVendor *
552 ClangModulesDeclVendor::Create(Target &target) {
553 // FIXME we should insure programmatically that the expression parser's
554 // compiler and the modules runtime's
555 // compiler are both initialized in the same way – preferably by the same
558 if (!target.GetPlatform()->SupportsModules())
561 const ArchSpec &arch = target.GetArchitecture();
563 std::vector<std::string> compiler_invocation_arguments = {
566 "-fimplicit-module-maps",
571 arch.GetTriple().str(),
572 "-fmodules-validate-system-headers",
573 "-Werror=non-modular-include-in-framework-module"};
575 target.GetPlatform()->AddClangModuleCompilationOptions(
576 &target, compiler_invocation_arguments);
578 compiler_invocation_arguments.push_back(ModuleImportBufferName);
580 // Add additional search paths with { "-I", path } or { "-F", path } here.
583 llvm::SmallString<128> DefaultModuleCache;
584 const bool erased_on_reboot = false;
585 llvm::sys::path::system_temp_directory(erased_on_reboot,
587 llvm::sys::path::append(DefaultModuleCache, "org.llvm.clang");
588 llvm::sys::path::append(DefaultModuleCache, "ModuleCache");
589 std::string module_cache_argument("-fmodules-cache-path=");
590 module_cache_argument.append(DefaultModuleCache.str().str());
591 compiler_invocation_arguments.push_back(module_cache_argument);
594 FileSpecList &module_search_paths = target.GetClangModuleSearchPaths();
596 for (size_t spi = 0, spe = module_search_paths.GetSize(); spi < spe; ++spi) {
597 const FileSpec &search_path = module_search_paths.GetFileSpecAtIndex(spi);
599 std::string search_path_argument = "-I";
600 search_path_argument.append(search_path.GetPath());
602 compiler_invocation_arguments.push_back(search_path_argument);
606 FileSpec clang_resource_dir = GetResourceDir();
608 if (clang_resource_dir.IsDirectory()) {
609 compiler_invocation_arguments.push_back("-resource-dir");
610 compiler_invocation_arguments.push_back(clang_resource_dir.GetPath());
614 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine =
615 clang::CompilerInstance::createDiagnostics(new clang::DiagnosticOptions,
616 new StoringDiagnosticConsumer);
618 std::vector<const char *> compiler_invocation_argument_cstrs;
620 for (const std::string &arg : compiler_invocation_arguments) {
621 compiler_invocation_argument_cstrs.push_back(arg.c_str());
624 std::shared_ptr<clang::CompilerInvocation> invocation =
625 clang::createInvocationFromCommandLine(compiler_invocation_argument_cstrs,
631 std::unique_ptr<llvm::MemoryBuffer> source_buffer =
632 llvm::MemoryBuffer::getMemBuffer(
633 "extern int __lldb __attribute__((unavailable));",
634 ModuleImportBufferName);
636 invocation->getPreprocessorOpts().addRemappedFile(ModuleImportBufferName,
637 source_buffer.release());
639 std::unique_ptr<clang::CompilerInstance> instance(
640 new clang::CompilerInstance);
642 instance->setDiagnostics(diagnostics_engine.get());
643 instance->setInvocation(invocation);
645 std::unique_ptr<clang::FrontendAction> action(new clang::SyntaxOnlyAction);
647 instance->setTarget(clang::TargetInfo::CreateTargetInfo(
648 *diagnostics_engine, instance->getInvocation().TargetOpts));
650 if (!instance->hasTarget())
653 instance->getTarget().adjust(instance->getLangOpts());
655 if (!action->BeginSourceFile(*instance,
656 instance->getFrontendOpts().Inputs[0]))
659 instance->getPreprocessor().enableIncrementalProcessing();
661 instance->createModuleManager();
663 instance->createSema(action->getTranslationUnitKind(), nullptr);
665 const bool skipFunctionBodies = false;
666 std::unique_ptr<clang::Parser> parser(new clang::Parser(
667 instance->getPreprocessor(), instance->getSema(), skipFunctionBodies));
669 instance->getPreprocessor().EnterMainSourceFile();
670 parser->Initialize();
672 clang::Parser::DeclGroupPtrTy parsed;
674 while (!parser->ParseTopLevelDecl(parsed))
677 return new ClangModulesDeclVendorImpl(std::move(diagnostics_engine),
678 std::move(invocation),
679 std::move(instance), std::move(parser));