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 //===----------------------------------------------------------------------===//
10 #include <mutex> // std::once
12 #include "lldb/Expression/ClangModulesDeclVendor.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/StreamString.h"
16 #include "lldb/Host/FileSpec.h"
17 #include "lldb/Host/Host.h"
18 #include "lldb/Host/HostInfo.h"
19 #include "lldb/Symbol/CompileUnit.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/LLDBAssert.h"
23 #include "clang/Basic/TargetInfo.h"
24 #include "clang/Frontend/CompilerInstance.h"
25 #include "clang/Frontend/FrontendActions.h"
26 #include "clang/Lex/Preprocessor.h"
27 #include "clang/Parse/Parser.h"
28 #include "clang/Sema/Lookup.h"
29 #include "clang/Serialization/ASTReader.h"
32 using namespace lldb_private;
35 // Any Clang compiler requires a consumer for diagnostics. This one stores them as strings
36 // so we can provide them to the user in case a module failed to load.
37 class StoringDiagnosticConsumer : public clang::DiagnosticConsumer
40 StoringDiagnosticConsumer ();
42 HandleDiagnostic (clang::DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &info);
48 DumpDiagnostics (Stream &error_stream);
50 typedef std::pair<clang::DiagnosticsEngine::Level, std::string> IDAndDiagnostic;
51 std::vector<IDAndDiagnostic> m_diagnostics;
55 // The private implementation of our ClangModulesDeclVendor. Contains all the Clang state required
57 class ClangModulesDeclVendorImpl : public ClangModulesDeclVendor
60 ClangModulesDeclVendorImpl(llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> &diagnostics_engine,
61 llvm::IntrusiveRefCntPtr<clang::CompilerInvocation> &compiler_invocation,
62 std::unique_ptr<clang::CompilerInstance> &&compiler_instance,
63 std::unique_ptr<clang::Parser> &&parser);
66 AddModule(ModulePath &path,
67 ModuleVector *exported_modules,
68 Stream &error_stream) override;
71 AddModulesForCompileUnit(CompileUnit &cu,
72 ModuleVector &exported_modules,
73 Stream &error_stream) override;
76 FindDecls (const ConstString &name,
79 std::vector <clang::NamedDecl*> &decls) override;
82 ForEachMacro(const ModuleVector &modules,
83 std::function<bool (const std::string &)> handler) override;
85 ~ClangModulesDeclVendorImpl();
89 ReportModuleExportsHelper (std::set<ClangModulesDeclVendor::ModuleID> &exports,
90 clang::Module *module);
93 ReportModuleExports (ModuleVector &exports,
94 clang::Module *module);
96 clang::ModuleLoadResult
97 DoGetModule(clang::ModuleIdPath path, bool make_visible);
99 bool m_enabled = false;
101 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> m_diagnostics_engine;
102 llvm::IntrusiveRefCntPtr<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 = 0; // used to give name components fake SourceLocations
107 typedef std::vector<ConstString> ImportedModule;
108 typedef std::map<ImportedModule, clang::Module *> ImportedModuleMap;
109 typedef std::set<ModuleID> ImportedModuleSet;
110 ImportedModuleMap m_imported_modules;
111 ImportedModuleSet m_user_imported_modules;
115 StoringDiagnosticConsumer::StoringDiagnosticConsumer ()
117 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
121 StoringDiagnosticConsumer::HandleDiagnostic (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(IDAndDiagnostic(DiagLevel, std::string(diagnostic_string.data(), diagnostic_string.size())));
131 StoringDiagnosticConsumer::ClearDiagnostics ()
133 m_diagnostics.clear();
137 StoringDiagnosticConsumer::DumpDiagnostics (Stream &error_stream)
139 for (IDAndDiagnostic &diag : m_diagnostics)
144 error_stream.PutCString(diag.second.c_str());
145 error_stream.PutChar('\n');
147 case clang::DiagnosticsEngine::Level::Ignored:
156 static FileSpec g_cached_resource_dir;
158 static std::once_flag g_once_flag;
160 std::call_once(g_once_flag, [](){
161 HostInfo::GetLLDBPath (lldb::ePathTypeClangDir, g_cached_resource_dir);
164 return g_cached_resource_dir;
168 ClangModulesDeclVendor::ClangModulesDeclVendor()
172 ClangModulesDeclVendor::~ClangModulesDeclVendor()
176 ClangModulesDeclVendorImpl::ClangModulesDeclVendorImpl(llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> &diagnostics_engine,
177 llvm::IntrusiveRefCntPtr<clang::CompilerInvocation> &compiler_invocation,
178 std::unique_ptr<clang::CompilerInstance> &&compiler_instance,
179 std::unique_ptr<clang::Parser> &&parser) :
180 ClangModulesDeclVendor(),
181 m_diagnostics_engine(diagnostics_engine),
182 m_compiler_invocation(compiler_invocation),
183 m_compiler_instance(std::move(compiler_instance)),
184 m_parser(std::move(parser)),
190 ClangModulesDeclVendorImpl::ReportModuleExportsHelper (std::set<ClangModulesDeclVendor::ModuleID> &exports,
191 clang::Module *module)
193 if (exports.count(reinterpret_cast<ClangModulesDeclVendor::ModuleID>(module)))
196 exports.insert(reinterpret_cast<ClangModulesDeclVendor::ModuleID>(module));
198 llvm::SmallVector<clang::Module*, 2> sub_exports;
200 module->getExportedModules(sub_exports);
202 for (clang::Module *module : sub_exports)
204 ReportModuleExportsHelper(exports, module);
209 ClangModulesDeclVendorImpl::ReportModuleExports (ClangModulesDeclVendor::ModuleVector &exports,
210 clang::Module *module)
212 std::set<ClangModulesDeclVendor::ModuleID> exports_set;
214 ReportModuleExportsHelper(exports_set, module);
216 for (ModuleID module : exports_set)
218 exports.push_back(module);
223 ClangModulesDeclVendorImpl::AddModule(ModulePath &path,
224 ModuleVector *exported_modules,
225 Stream &error_stream)
229 if (m_compiler_instance->hadModuleLoaderFatalFailure())
231 error_stream.PutCString("error: Couldn't load a module because the module loader is in a fatal state.\n");
235 // Check if we've already imported this module.
237 std::vector<ConstString> imported_module;
239 for (ConstString path_component : path)
241 imported_module.push_back(path_component);
245 ImportedModuleMap::iterator mi = m_imported_modules.find(imported_module);
247 if (mi != m_imported_modules.end())
249 if (exported_modules)
251 ReportModuleExports(*exported_modules, mi->second);
257 if (!m_compiler_instance->getPreprocessor().getHeaderSearchInfo().lookupModule(path[0].GetStringRef()))
259 error_stream.Printf("error: Header search couldn't locate module %s\n", path[0].AsCString());
263 llvm::SmallVector<std::pair<clang::IdentifierInfo *, clang::SourceLocation>, 4> clang_path;
266 clang::SourceManager &source_manager = m_compiler_instance->getASTContext().getSourceManager();
268 for (ConstString path_component : path)
270 clang_path.push_back(std::make_pair(&m_compiler_instance->getASTContext().Idents.get(path_component.GetStringRef()),
271 source_manager.getLocForStartOfFile(source_manager.getMainFileID()).getLocWithOffset(m_source_location_index++)));
275 StoringDiagnosticConsumer *diagnostic_consumer = static_cast<StoringDiagnosticConsumer *>(m_compiler_instance->getDiagnostics().getClient());
277 diagnostic_consumer->ClearDiagnostics();
279 clang::Module *top_level_module = DoGetModule(clang_path.front(), false);
281 if (!top_level_module)
283 diagnostic_consumer->DumpDiagnostics(error_stream);
284 error_stream.Printf("error: Couldn't load top-level module %s\n", path[0].AsCString());
288 clang::Module *submodule = top_level_module;
290 for (size_t ci = 1; ci < path.size(); ++ci)
292 llvm::StringRef component = path[ci].GetStringRef();
293 submodule = submodule->findSubmodule(component.str());
296 diagnostic_consumer->DumpDiagnostics(error_stream);
297 error_stream.Printf("error: Couldn't load submodule %s\n", component.str().c_str());
302 clang::Module *requested_module = DoGetModule(clang_path, true);
304 if (requested_module != nullptr)
306 if (exported_modules)
308 ReportModuleExports(*exported_modules, requested_module);
311 m_imported_modules[imported_module] = requested_module;
323 ClangModulesDeclVendor::LanguageSupportsClangModules (lldb::LanguageType language)
329 // C++ and friends to be added
330 case lldb::LanguageType::eLanguageTypeC:
331 case lldb::LanguageType::eLanguageTypeC11:
332 case lldb::LanguageType::eLanguageTypeC89:
333 case lldb::LanguageType::eLanguageTypeC99:
334 case lldb::LanguageType::eLanguageTypeObjC:
340 ClangModulesDeclVendorImpl::AddModulesForCompileUnit(CompileUnit &cu,
341 ClangModulesDeclVendor::ModuleVector &exported_modules,
342 Stream &error_stream)
344 if (LanguageSupportsClangModules(cu.GetLanguage()))
346 std::vector<ConstString> imported_modules = cu.GetImportedModules();
348 for (ConstString imported_module : imported_modules)
350 std::vector<ConstString> path;
352 path.push_back(imported_module);
354 if (!AddModule(path, &exported_modules, error_stream))
366 // ClangImporter::lookupValue
369 ClangModulesDeclVendorImpl::FindDecls (const ConstString &name,
371 uint32_t max_matches,
372 std::vector <clang::NamedDecl*> &decls)
382 clang::IdentifierInfo &ident = m_compiler_instance->getASTContext().Idents.get(name.GetStringRef());
384 clang::LookupResult lookup_result(m_compiler_instance->getSema(),
385 clang::DeclarationName(&ident),
386 clang::SourceLocation(),
387 clang::Sema::LookupOrdinaryName);
389 m_compiler_instance->getSema().LookupName(lookup_result, m_compiler_instance->getSema().getScopeForContext(m_compiler_instance->getASTContext().getTranslationUnitDecl()));
391 uint32_t num_matches = 0;
393 for (clang::NamedDecl *named_decl : lookup_result)
395 if (num_matches >= max_matches)
398 decls.push_back(named_decl);
406 ClangModulesDeclVendorImpl::ForEachMacro(const ClangModulesDeclVendor::ModuleVector &modules,
407 std::function<bool (const std::string &)> handler)
414 typedef std::map<ModuleID, ssize_t> ModulePriorityMap;
415 ModulePriorityMap module_priorities;
417 ssize_t priority = 0;
419 for (ModuleID module : modules)
421 module_priorities[module] = priority++;
424 if (m_compiler_instance->getPreprocessor().getExternalSource())
426 m_compiler_instance->getPreprocessor().getExternalSource()->ReadDefinedMacros();
429 for (clang::Preprocessor::macro_iterator mi = m_compiler_instance->getPreprocessor().macro_begin(),
430 me = m_compiler_instance->getPreprocessor().macro_end();
434 const clang::IdentifierInfo *ii = nullptr;
437 if (clang::IdentifierInfoLookup *lookup = m_compiler_instance->getPreprocessor().getIdentifierTable().getExternalIdentifierLookup())
439 lookup->get(mi->first->getName());
447 ssize_t found_priority = -1;
448 clang::MacroInfo *macro_info = nullptr;
450 for (clang::ModuleMacro *module_macro : m_compiler_instance->getPreprocessor().getLeafModuleMacros(ii))
452 clang::Module *module = module_macro->getOwningModule();
455 ModulePriorityMap::iterator pi = module_priorities.find(reinterpret_cast<ModuleID>(module));
457 if (pi != module_priorities.end() && pi->second > found_priority)
459 macro_info = module_macro->getMacroInfo();
460 found_priority = pi->second;
464 clang::Module *top_level_module = module->getTopLevelModule();
466 if (top_level_module != module)
468 ModulePriorityMap::iterator pi = module_priorities.find(reinterpret_cast<ModuleID>(top_level_module));
470 if ((pi != module_priorities.end()) && pi->second > found_priority)
472 macro_info = module_macro->getMacroInfo();
473 found_priority = pi->second;
480 std::string macro_expansion = "#define ";
481 macro_expansion.append(mi->first->getName().str().c_str());
484 if (macro_info->isFunctionLike())
486 macro_expansion.append("(");
488 bool first_arg = true;
490 for (clang::MacroInfo::arg_iterator ai = macro_info->arg_begin(),
491 ae = macro_info->arg_end();
497 macro_expansion.append(", ");
504 macro_expansion.append((*ai)->getName().str());
507 if (macro_info->isC99Varargs())
511 macro_expansion.append("...");
515 macro_expansion.append(", ...");
518 else if (macro_info->isGNUVarargs())
520 macro_expansion.append("...");
523 macro_expansion.append(")");
526 macro_expansion.append(" ");
528 bool first_token = true;
530 for (clang::MacroInfo::tokens_iterator ti = macro_info->tokens_begin(),
531 te = macro_info->tokens_end();
537 macro_expansion.append(" ");
546 if (const char *literal_data = ti->getLiteralData())
548 std::string token_str(literal_data, ti->getLength());
549 macro_expansion.append(token_str);
553 bool invalid = false;
554 const char *literal_source = m_compiler_instance->getSourceManager().getCharacterData(ti->getLocation(), &invalid);
558 lldbassert(!"Unhandled token kind");
559 macro_expansion.append("<unknown literal value>");
563 macro_expansion.append(std::string(literal_source, ti->getLength()));
567 else if (const char *punctuator_spelling = clang::tok::getPunctuatorSpelling(ti->getKind()))
569 macro_expansion.append(punctuator_spelling);
571 else if (const char *keyword_spelling = clang::tok::getKeywordSpelling(ti->getKind()))
573 macro_expansion.append(keyword_spelling);
577 switch (ti->getKind())
579 case clang::tok::TokenKind::identifier:
580 macro_expansion.append(ti->getIdentifierInfo()->getName().str());
582 case clang::tok::TokenKind::raw_identifier:
583 macro_expansion.append(ti->getRawIdentifier().str());
585 macro_expansion.append(ti->getName());
591 if (handler(macro_expansion))
600 ClangModulesDeclVendorImpl::~ClangModulesDeclVendorImpl()
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));