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/Frontend/CompilerInstance.h"
14#include "clang/Frontend/FrontendActions.h"
15#include "clang/Frontend/TextDiagnosticPrinter.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Lex/PreprocessorOptions.h"
18#include "clang/Parse/Parser.h"
19#include "clang/Sema/Lookup.h"
20#include "clang/Serialization/ASTReader.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Path.h"
23#include "llvm/Support/Threading.h"
47class StoringDiagnosticConsumer :
public clang::DiagnosticConsumer {
49 StoringDiagnosticConsumer();
51 void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
52 const clang::Diagnostic &info)
override;
54 void ClearDiagnostics();
58 void BeginSourceFile(
const clang::LangOptions &LangOpts,
59 const clang::Preprocessor *PP =
nullptr)
override;
60 void EndSourceFile()
override;
63 bool HandleModuleRemark(
const clang::Diagnostic &info);
64 void SetCurrentModuleProgress(std::string module_name);
66 typedef std::pair<clang::DiagnosticsEngine::Level, std::string>
68 std::vector<IDAndDiagnostic> m_diagnostics;
69 std::unique_ptr<clang::DiagnosticOptions> m_diag_opts;
72 std::unique_ptr<clang::TextDiagnosticPrinter> m_diag_printer;
74 std::unique_ptr<llvm::raw_string_ostream> m_os;
78 std::unique_ptr<Progress> m_current_progress_up;
79 std::vector<std::string> m_module_build_stack;
86 ClangModulesDeclVendorImpl(
87 std::unique_ptr<clang::DiagnosticOptions> diagnostic_options,
88 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
89 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
90 std::unique_ptr<clang::CompilerInstance> compiler_instance,
91 std::unique_ptr<clang::Parser> parser);
93 ~ClangModulesDeclVendorImpl()
override =
default;
95 llvm::Error AddModule(
const SourceModule &module,
96 ModuleVector *exported_modules)
override;
98 llvm::Error AddModulesForCompileUnit(CompileUnit &cu,
99 ModuleVector &exported_modules)
override;
101 uint32_t FindDecls(ConstString name,
bool append, uint32_t max_matches,
102 std::vector<CompilerDecl> &decls)
override;
105 const ModuleVector &modules,
106 std::function<
bool(llvm::StringRef, llvm::StringRef)> handler)
override;
109 typedef llvm::DenseSet<ModuleID> ExportedModuleSet;
110 void ReportModuleExportsHelper(ExportedModuleSet &exports,
111 clang::Module *module);
113 void ReportModuleExports(ModuleVector &exports, clang::Module *module);
115 clang::ModuleLoadResult DoGetModule(clang::ModuleIdPath path,
118 bool m_enabled =
false;
120 std::unique_ptr<clang::DiagnosticOptions> m_diagnostic_options;
121 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> m_diagnostics_engine;
122 std::shared_ptr<clang::CompilerInvocation> m_compiler_invocation;
123 std::unique_ptr<clang::CompilerInstance> m_compiler_instance;
124 std::unique_ptr<clang::Parser> m_parser;
125 size_t m_source_location_index =
128 typedef std::vector<ConstString> ImportedModule;
129 typedef std::map<ImportedModule, clang::Module *> ImportedModuleMap;
130 typedef llvm::DenseSet<ModuleID> ImportedModuleSet;
131 ImportedModuleMap m_imported_modules;
132 ImportedModuleSet m_user_imported_modules;
135 std::shared_ptr<TypeSystemClang> m_ast_context;
139StoringDiagnosticConsumer::StoringDiagnosticConsumer() {
140 m_diag_opts = std::make_unique<clang::DiagnosticOptions>();
141 m_os = std::make_unique<llvm::raw_string_ostream>(m_output);
143 std::make_unique<clang::TextDiagnosticPrinter>(*m_os, *m_diag_opts);
146void StoringDiagnosticConsumer::HandleDiagnostic(
147 clang::DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &info) {
148 if (HandleModuleRemark(info))
153 m_diag_printer->HandleDiagnostic(DiagLevel, info);
156 m_diagnostics.push_back(IDAndDiagnostic(DiagLevel, m_output));
159void StoringDiagnosticConsumer::ClearDiagnostics() { m_diagnostics.clear(); }
161void StoringDiagnosticConsumer::DumpDiagnostics(Stream &error_stream) {
162 for (IDAndDiagnostic &diag : m_diagnostics) {
163 switch (diag.first) {
168 case clang::DiagnosticsEngine::Level::Ignored:
174void StoringDiagnosticConsumer::BeginSourceFile(
175 const clang::LangOptions &LangOpts,
const clang::Preprocessor *PP) {
176 m_diag_printer->BeginSourceFile(LangOpts, PP);
179void StoringDiagnosticConsumer::EndSourceFile() {
180 m_current_progress_up =
nullptr;
181 m_diag_printer->EndSourceFile();
184bool StoringDiagnosticConsumer::HandleModuleRemark(
185 const clang::Diagnostic &info) {
186 Log *log =
GetLog(LLDBLog::Types | LLDBLog::Expressions);
187 switch (info.getID()) {
188 case clang::diag::remark_module_build: {
189 const auto &module_name = info.getArgStdStr(0);
190 SetCurrentModuleProgress(module_name);
191 m_module_build_stack.push_back(module_name);
193 const auto &module_path = info.getArgStdStr(1);
194 LLDB_LOG(log,
"Building Clang module {0} as {1}", module_name, module_path);
197 case clang::diag::remark_module_build_done: {
199 m_module_build_stack.pop_back();
200 if (m_module_build_stack.empty()) {
201 m_current_progress_up =
nullptr;
206 const auto &resumed_module_name = m_module_build_stack.back();
207 SetCurrentModuleProgress(resumed_module_name);
210 const auto &module_name = info.getArgStdStr(0);
211 LLDB_LOG(log,
"Finished building Clang module {0}", module_name);
219void StoringDiagnosticConsumer::SetCurrentModuleProgress(
220 std::string module_name) {
221 if (!m_current_progress_up)
222 m_current_progress_up =
223 std::make_unique<Progress>(
"Building Clang modules");
225 m_current_progress_up->Increment(1, std::move(module_name));
233ClangModulesDeclVendorImpl::ClangModulesDeclVendorImpl(
234 std::unique_ptr<clang::DiagnosticOptions> diagnostic_options,
235 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
236 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
237 std::unique_ptr<clang::CompilerInstance> compiler_instance,
238 std::unique_ptr<clang::Parser> parser)
239 : m_diagnostic_options(std::move(diagnostic_options)),
240 m_diagnostics_engine(std::move(diagnostics_engine)),
241 m_compiler_invocation(std::move(compiler_invocation)),
242 m_compiler_instance(std::move(compiler_instance)),
243 m_parser(std::move(parser)) {
247 std::make_shared<TypeSystemClang>(
"ClangModulesDeclVendor ASTContext",
248 m_compiler_instance->getASTContext());
251void ClangModulesDeclVendorImpl::ReportModuleExportsHelper(
252 ExportedModuleSet &exports, clang::Module *module) {
258 llvm::SmallVector<clang::Module *, 2> sub_exports;
260 module->getExportedModules(sub_exports);
262 for (clang::Module *module : sub_exports)
263 ReportModuleExportsHelper(exports, module);
266void ClangModulesDeclVendorImpl::ReportModuleExports(
268 ExportedModuleSet exports_set;
270 ReportModuleExportsHelper(exports_set, module);
272 for (ModuleID module : exports_set)
273 exports.push_back(module);
277ClangModulesDeclVendorImpl::AddModule(
const SourceModule &module,
278 ModuleVector *exported_modules) {
281 if (m_compiler_instance->hadModuleLoaderFatalFailure())
282 return llvm::createStringError(
283 "couldn't load a module because the module loader is in a fatal state");
287 std::vector<ConstString> imported_module;
290 imported_module.push_back(path_component);
293 ImportedModuleMap::iterator mi = m_imported_modules.find(imported_module);
295 if (mi != m_imported_modules.end()) {
296 if (exported_modules)
297 ReportModuleExports(*exported_modules, mi->second);
298 return llvm::Error::success();
302 clang::HeaderSearch &HS =
303 m_compiler_instance->getPreprocessor().getHeaderSearchInfo();
311 bool is_system_module = (std::distance(path_begin, path_end) >=
312 std::distance(sysroot_begin, sysroot_end)) &&
313 std::equal(sysroot_begin, sysroot_end, path_begin);
315 if (!is_system_module) {
316 bool is_system =
true;
317 bool is_framework =
false;
318 auto dir = HS.getFileMgr().getOptionalDirectoryRef(
321 return llvm::createStringError(
322 "couldn't find module search path directory %s",
325 auto file = HS.lookupModuleMapFile(*dir, is_framework);
327 return llvm::createStringError(
"couldn't find modulemap file in %s",
330 if (HS.parseAndLoadModuleMapFile(*file, is_system))
331 return llvm::createStringError(
332 "failed to parse and load modulemap file in %s",
337 if (!HS.lookupModule(module.
path.front().GetStringRef()))
338 return llvm::createStringError(
"header search couldn't locate module '%s'",
339 module.
path.front().AsCString());
341 llvm::SmallVector<clang::IdentifierLoc, 4> clang_path;
344 clang::SourceManager &source_manager =
345 m_compiler_instance->getASTContext().getSourceManager();
348 clang_path.emplace_back(
349 source_manager.getLocForStartOfFile(source_manager.getMainFileID())
350 .getLocWithOffset(m_source_location_index++),
351 &m_compiler_instance->getASTContext().Idents.get(
356 StoringDiagnosticConsumer *diagnostic_consumer =
357 static_cast<StoringDiagnosticConsumer *
>(
358 m_compiler_instance->getDiagnostics().getClient());
360 diagnostic_consumer->ClearDiagnostics();
362 clang::Module *top_level_module = DoGetModule(clang_path.front(),
false);
364 if (!top_level_module) {
365 lldb_private::StreamString error_stream;
366 diagnostic_consumer->DumpDiagnostics(error_stream);
368 return llvm::createStringError(llvm::formatv(
369 "couldn't load top-level module {0}:\n{1}",
370 module.
path.front().GetStringRef(), error_stream.
GetString()));
373 clang::Module *submodule = top_level_module;
375 for (
auto &component : llvm::ArrayRef<ConstString>(module.
path).drop_front()) {
376 clang::Module *found = submodule->findSubmodule(component.GetStringRef());
378 lldb_private::StreamString error_stream;
379 diagnostic_consumer->DumpDiagnostics(error_stream);
381 return llvm::createStringError(llvm::formatv(
382 "couldn't load submodule '{0}' of module '{1}':\n{2}",
383 component.GetStringRef(), submodule->getFullModuleName(),
393 m_compiler_instance->makeModuleVisible(
394 submodule, clang::Module::NameVisibilityKind::AllVisible,
397 clang::Module *requested_module = DoGetModule(clang_path,
true);
399 if (requested_module !=
nullptr) {
400 if (exported_modules)
401 ReportModuleExports(*exported_modules, requested_module);
403 m_imported_modules[imported_module] = requested_module;
407 return llvm::Error::success();
410 return llvm::createStringError(
411 llvm::formatv(
"unknown error while loading module {0}\n",
412 module.
path.front().GetStringRef()));
434llvm::Error ClangModulesDeclVendorImpl::AddModulesForCompileUnit(
436 if (!LanguageSupportsClangModules(cu.
GetLanguage()))
437 return llvm::Error::success();
439 llvm::Error errors = llvm::Error::success();
442 if (
auto err = AddModule(imported_module, &exported_modules))
443 errors = llvm::joinErrors(std::move(errors), std::move(err));
451ClangModulesDeclVendorImpl::FindDecls(
ConstString name,
bool append,
452 uint32_t max_matches,
453 std::vector<CompilerDecl> &decls) {
460 clang::IdentifierInfo &ident =
461 m_compiler_instance->getASTContext().Idents.get(name.
GetStringRef());
463 clang::LookupResult lookup_result(
464 m_compiler_instance->getSema(), clang::DeclarationName(&ident),
465 clang::SourceLocation(), clang::Sema::LookupOrdinaryName);
467 m_compiler_instance->getSema().LookupName(
469 m_compiler_instance->getSema().getScopeForContext(
470 m_compiler_instance->getASTContext().getTranslationUnitDecl()));
472 uint32_t num_matches = 0;
474 for (clang::NamedDecl *named_decl : lookup_result) {
475 if (num_matches >= max_matches)
478 decls.push_back(m_ast_context->GetCompilerDecl(named_decl));
485void ClangModulesDeclVendorImpl::ForEachMacro(
487 std::function<
bool(llvm::StringRef, llvm::StringRef)> handler) {
491 typedef std::map<ModuleID, ssize_t> ModulePriorityMap;
492 ModulePriorityMap module_priorities;
494 ssize_t priority = 0;
496 for (ModuleID module : modules)
497 module_priorities[module] = priority++;
499 if (m_compiler_instance->getPreprocessor().getExternalSource()) {
500 m_compiler_instance->getPreprocessor()
502 ->ReadDefinedMacros();
505 for (clang::Preprocessor::macro_iterator
506 mi = m_compiler_instance->getPreprocessor().macro_begin(),
507 me = m_compiler_instance->getPreprocessor().macro_end();
509 const clang::IdentifierInfo *ii =
nullptr;
512 if (clang::IdentifierInfoLookup *lookup =
513 m_compiler_instance->getPreprocessor()
514 .getIdentifierTable()
515 .getExternalIdentifierLookup()) {
516 lookup->get(mi->first->getName());
522 ssize_t found_priority = -1;
523 clang::MacroInfo *macro_info =
nullptr;
525 for (clang::ModuleMacro *module_macro :
526 m_compiler_instance->getPreprocessor().getLeafModuleMacros(ii)) {
527 clang::Module *module = module_macro->getOwningModule();
530 ModulePriorityMap::iterator pi =
531 module_priorities.find(
reinterpret_cast<ModuleID
>(module));
533 if (pi != module_priorities.end() && pi->second > found_priority) {
534 macro_info = module_macro->getMacroInfo();
535 found_priority = pi->second;
539 clang::Module *top_level_module =
module->getTopLevelModule();
541 if (top_level_module != module) {
542 ModulePriorityMap::iterator pi = module_priorities.find(
543 reinterpret_cast<ModuleID
>(top_level_module));
545 if ((pi != module_priorities.end()) && pi->second > found_priority) {
546 macro_info = module_macro->getMacroInfo();
547 found_priority = pi->second;
553 std::string macro_expansion =
"#define ";
554 llvm::StringRef macro_identifier = mi->first->getName();
555 macro_expansion.append(macro_identifier.str());
558 if (macro_info->isFunctionLike()) {
559 macro_expansion.append(
"(");
561 bool first_arg =
true;
563 for (
auto pi = macro_info->param_begin(),
564 pe = macro_info->param_end();
567 macro_expansion.append(
", ");
571 macro_expansion.append((*pi)->getName().str());
574 if (macro_info->isC99Varargs()) {
576 macro_expansion.append(
"...");
578 macro_expansion.append(
", ...");
579 }
else if (macro_info->isGNUVarargs())
580 macro_expansion.append(
"...");
582 macro_expansion.append(
")");
585 macro_expansion.append(
" ");
587 bool first_token =
true;
589 for (clang::MacroInfo::const_tokens_iterator
590 ti = macro_info->tokens_begin(),
591 te = macro_info->tokens_end();
594 macro_expansion.append(
" ");
598 if (ti->isLiteral()) {
599 if (
const char *literal_data = ti->getLiteralData()) {
600 std::string token_str(literal_data, ti->getLength());
601 macro_expansion.append(token_str);
603 bool invalid =
false;
604 const char *literal_source =
605 m_compiler_instance->getSourceManager().getCharacterData(
606 ti->getLocation(), &invalid);
610 macro_expansion.append(
"<unknown literal value>");
612 macro_expansion.append(
613 std::string(literal_source, ti->getLength()));
616 }
else if (
const char *punctuator_spelling =
617 clang::tok::getPunctuatorSpelling(ti->getKind())) {
618 macro_expansion.append(punctuator_spelling);
619 }
else if (
const char *keyword_spelling =
620 clang::tok::getKeywordSpelling(ti->getKind())) {
621 macro_expansion.append(keyword_spelling);
623 switch (ti->getKind()) {
624 case clang::tok::TokenKind::identifier:
625 macro_expansion.append(ti->getIdentifierInfo()->getName().str());
627 case clang::tok::TokenKind::raw_identifier:
628 macro_expansion.append(ti->getRawIdentifier().str());
631 macro_expansion.append(ti->getName());
637 if (handler(macro_identifier, macro_expansion)) {
645clang::ModuleLoadResult
646ClangModulesDeclVendorImpl::DoGetModule(clang::ModuleIdPath path,
648 clang::Module::NameVisibilityKind visibility =
649 make_visible ? clang::Module::AllVisible : clang::Module::Hidden;
651 const bool is_inclusion_directive =
false;
653 return m_compiler_instance->loadModule(path.front().getLoc(), path,
654 visibility, is_inclusion_directive);
671 std::vector<std::string> compiler_invocation_arguments = {
674 "-fimplicit-module-maps",
680 "-fmodules-validate-system-headers",
681 "-Werror=non-modular-include-in-framework-module",
682 "-Xclang=-fincremental-extensions",
685 target.
GetPlatform()->AddClangModuleCompilationOptions(
686 &target, compiler_invocation_arguments);
693 llvm::SmallString<128> path;
695 props.GetClangModulesCachePath().GetPath(path);
696 std::string module_cache_argument(
"-fmodules-cache-path=");
697 module_cache_argument.append(std::string(path.str()));
698 compiler_invocation_arguments.push_back(module_cache_argument);
703 for (
size_t spi = 0, spe = module_search_paths.
GetSize(); spi < spe; ++spi) {
706 std::string search_path_argument =
"-I";
707 search_path_argument.append(search_path.
GetPath());
709 compiler_invocation_arguments.push_back(search_path_argument);
716 compiler_invocation_arguments.push_back(
"-resource-dir");
717 compiler_invocation_arguments.push_back(clang_resource_dir.
GetPath());
721 std::vector<const char *> compiler_invocation_argument_cstrs;
722 compiler_invocation_argument_cstrs.reserve(
723 compiler_invocation_arguments.size());
724 for (
const std::string &arg : compiler_invocation_arguments)
725 compiler_invocation_argument_cstrs.push_back(arg.c_str());
727 auto diag_options_up =
728 clang::CreateAndPopulateDiagOpts(compiler_invocation_argument_cstrs);
729 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine =
730 clang::CompilerInstance::createDiagnostics(
732 new StoringDiagnosticConsumer);
735 LLDB_LOG(log,
"ClangModulesDeclVendor's compiler flags {0:$[ ]}",
736 llvm::make_range(compiler_invocation_arguments.begin(),
737 compiler_invocation_arguments.end()));
739 clang::CreateInvocationOptions CIOpts;
740 CIOpts.Diags = diagnostics_engine;
741 std::shared_ptr<clang::CompilerInvocation> invocation =
742 clang::createInvocation(compiler_invocation_argument_cstrs,
748 std::unique_ptr<llvm::MemoryBuffer> source_buffer =
749 llvm::MemoryBuffer::getMemBuffer(
750 "extern int __lldb __attribute__((unavailable));",
754 source_buffer.release());
756 auto instance = std::make_unique<clang::CompilerInstance>(invocation);
760 instance->createFileManager();
761 instance->setDiagnostics(diagnostics_engine);
763 std::unique_ptr<clang::FrontendAction> action(
new clang::SyntaxOnlyAction);
765 instance->setTarget(clang::TargetInfo::CreateTargetInfo(
766 *diagnostics_engine, instance->getInvocation().getTargetOpts()));
768 if (!instance->hasTarget())
771 instance->getTarget().adjust(*diagnostics_engine, instance->getLangOpts(),
774 if (!action->BeginSourceFile(*instance,
775 instance->getFrontendOpts().Inputs[0]))
778 instance->createASTReader();
780 instance->createSema(action->getTranslationUnitKind(),
nullptr);
782 const bool skipFunctionBodies =
false;
783 std::unique_ptr<clang::Parser> parser(
new clang::Parser(
784 instance->getPreprocessor(), instance->getSema(), skipFunctionBodies));
786 instance->getPreprocessor().EnterMainSourceFile();
787 parser->Initialize();
789 clang::Parser::DeclGroupPtrTy parsed;
790 auto ImportState = clang::Sema::ModuleImportState::NotACXX20Module;
791 while (!parser->ParseTopLevelDecl(parsed, ImportState))
794 return new ClangModulesDeclVendorImpl(
795 std::move(diag_options_up), std::move(diagnostics_engine),
796 std::move(invocation), std::move(instance), std::move(parser));
static const char * ModuleImportBufferName
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
static void DumpDiagnostics(void *cookie)
An architecture specification class.
llvm::Triple & GetTriple()
Architecture triple accessor.
static ClangModulesDeclVendor * Create(Target &target)
std::vector< ModuleID > ModuleVector
static bool LanguageSupportsClangModules(lldb::LanguageType language)
Query whether Clang supports modules for a particular language.
~ClangModulesDeclVendor() override
A class that describes a compilation unit.
const std::vector< SourceModule > & GetImportedModules()
Get the compile unit's imported module list.
lldb::LanguageType GetLanguage()
A uniqued constant string class.
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)
const FileSpec & GetFileSpecAtIndex(size_t idx) const
Get file at index.
size_t GetSize() const
Get the number of files in the file list.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
static FileSystem & Instance()
static ModuleListProperties & GetGlobalModuleListProperties()
llvm::StringRef GetString() const
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
FileSpecList GetClangModuleSearchPaths()
lldb::PlatformSP GetPlatform()
const ArchSpec & GetArchitecture() const
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.
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".