9#include "clang/Basic/Diagnostic.h"
10#include "clang/Basic/DiagnosticFrontend.h"
11#include "clang/Basic/DiagnosticSerialization.h"
12#include "clang/Basic/IdentifierTable.h"
13#include "clang/Basic/TargetInfo.h"
14#include "clang/Driver/CreateInvocationFromArgs.h"
15#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Frontend/FrontendActions.h"
17#include "clang/Frontend/TextDiagnosticPrinter.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/ADT/StringRef.h"
24#include "llvm/Support/ErrorExtras.h"
25#include "llvm/Support/Path.h"
26#include "llvm/Support/Threading.h"
50class StoringDiagnosticConsumer :
public clang::DiagnosticConsumer {
52 StoringDiagnosticConsumer();
54 void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
55 const clang::Diagnostic &info)
override;
57 void ClearDiagnostics();
61 void BeginSourceFile(
const clang::LangOptions &LangOpts,
62 const clang::Preprocessor *PP =
nullptr)
override;
63 void EndSourceFile()
override;
66 bool HandleModuleRemark(
const clang::Diagnostic &info);
67 void SetCurrentModuleProgress(std::string module_name);
69 typedef std::pair<clang::DiagnosticsEngine::Level, std::string>
71 std::vector<IDAndDiagnostic> m_diagnostics;
72 std::unique_ptr<clang::DiagnosticOptions> m_diag_opts;
76 std::unique_ptr<llvm::raw_string_ostream> m_os;
79 std::unique_ptr<clang::TextDiagnosticPrinter> m_diag_printer;
81 std::unique_ptr<Progress> m_current_progress_up;
82 std::vector<std::string> m_module_build_stack;
89 ClangModulesDeclVendorImpl(
90 std::unique_ptr<clang::DiagnosticOptions> diagnostic_options,
91 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
92 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
93 std::unique_ptr<clang::CompilerInstance> compiler_instance,
94 std::unique_ptr<clang::Parser> parser);
96 ~ClangModulesDeclVendorImpl()
override =
default;
98 llvm::Error AddModule(
const SourceModule &module,
99 ModuleVector *exported_modules)
override;
101 llvm::Error AddModulesForCompileUnit(CompileUnit &cu,
102 ModuleVector &exported_modules)
override;
104 uint32_t FindDecls(ConstString name,
bool append, uint32_t max_matches,
105 std::vector<CompilerDecl> &decls)
override;
108 const ModuleVector &modules,
109 std::function<
bool(llvm::StringRef, llvm::StringRef)> handler)
override;
112 typedef llvm::DenseSet<ModuleID> ExportedModuleSet;
113 void ReportModuleExportsHelper(ExportedModuleSet &exports,
114 clang::Module *module);
116 void ReportModuleExports(ModuleVector &exports, clang::Module *module);
118 clang::ModuleLoadResult DoGetModule(clang::ModuleIdPath path,
121 bool m_enabled =
false;
123 std::unique_ptr<clang::DiagnosticOptions> m_diagnostic_options;
124 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> m_diagnostics_engine;
125 std::shared_ptr<clang::CompilerInvocation> m_compiler_invocation;
126 std::unique_ptr<clang::CompilerInstance> m_compiler_instance;
127 std::unique_ptr<clang::Parser> m_parser;
128 size_t m_source_location_index =
131 typedef std::vector<ConstString> ImportedModule;
132 typedef std::map<ImportedModule, clang::Module *> ImportedModuleMap;
133 typedef llvm::DenseSet<ModuleID> ImportedModuleSet;
134 ImportedModuleMap m_imported_modules;
135 ImportedModuleSet m_user_imported_modules;
138 std::shared_ptr<TypeSystemClang> m_ast_context;
142StoringDiagnosticConsumer::StoringDiagnosticConsumer() {
143 m_diag_opts = std::make_unique<clang::DiagnosticOptions>();
144 m_os = std::make_unique<llvm::raw_string_ostream>(m_output);
146 std::make_unique<clang::TextDiagnosticPrinter>(*m_os, *m_diag_opts);
149void StoringDiagnosticConsumer::HandleDiagnostic(
150 clang::DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &info) {
151 if (HandleModuleRemark(info))
156 m_diag_printer->HandleDiagnostic(DiagLevel, info);
159 m_diagnostics.push_back(IDAndDiagnostic(DiagLevel, m_output));
162void StoringDiagnosticConsumer::ClearDiagnostics() { m_diagnostics.clear(); }
164void StoringDiagnosticConsumer::DumpDiagnostics(Stream &error_stream) {
165 for (IDAndDiagnostic &diag : m_diagnostics) {
166 switch (diag.first) {
171 case clang::DiagnosticsEngine::Level::Ignored:
177void StoringDiagnosticConsumer::BeginSourceFile(
178 const clang::LangOptions &LangOpts,
const clang::Preprocessor *PP) {
179 m_diag_printer->BeginSourceFile(LangOpts, PP);
182void StoringDiagnosticConsumer::EndSourceFile() {
183 m_current_progress_up =
nullptr;
184 m_diag_printer->EndSourceFile();
187bool StoringDiagnosticConsumer::HandleModuleRemark(
188 const clang::Diagnostic &info) {
189 Log *log =
GetLog(LLDBLog::Types | LLDBLog::Expressions);
190 switch (info.getID()) {
191 case clang::diag::remark_module_build: {
192 const auto &module_name = info.getArgStdStr(0);
193 SetCurrentModuleProgress(module_name);
194 m_module_build_stack.push_back(module_name);
196 const auto &module_path = info.getArgStdStr(1);
197 LLDB_LOG(log,
"Building Clang module {0} as {1}", module_name, module_path);
200 case clang::diag::remark_module_build_done: {
202 m_module_build_stack.pop_back();
203 if (m_module_build_stack.empty()) {
204 m_current_progress_up =
nullptr;
209 const auto &resumed_module_name = m_module_build_stack.back();
210 SetCurrentModuleProgress(resumed_module_name);
213 const auto &module_name = info.getArgStdStr(0);
214 LLDB_LOG(log,
"Finished building Clang module {0}", module_name);
217 case clang::diag::remark_module_import: {
218 const auto &module_name = info.getArgStdStr(0);
219 const auto &module_path = info.getArgStdStr(1);
220 LLDB_LOG(log,
"Importing Clang module {0} from {1}", module_name,
230void StoringDiagnosticConsumer::SetCurrentModuleProgress(
231 std::string module_name) {
232 if (!m_current_progress_up)
233 m_current_progress_up =
234 std::make_unique<Progress>(
"Building Clang modules");
236 m_current_progress_up->Increment(1, std::move(module_name));
244ClangModulesDeclVendorImpl::ClangModulesDeclVendorImpl(
245 std::unique_ptr<clang::DiagnosticOptions> diagnostic_options,
246 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
247 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
248 std::unique_ptr<clang::CompilerInstance> compiler_instance,
249 std::unique_ptr<clang::Parser> parser)
250 : m_diagnostic_options(std::move(diagnostic_options)),
251 m_diagnostics_engine(std::move(diagnostics_engine)),
252 m_compiler_invocation(std::move(compiler_invocation)),
253 m_compiler_instance(std::move(compiler_instance)),
254 m_parser(std::move(parser)) {
258 std::make_shared<TypeSystemClang>(
"ClangModulesDeclVendor ASTContext",
259 m_compiler_instance->getASTContext());
262void ClangModulesDeclVendorImpl::ReportModuleExportsHelper(
263 ExportedModuleSet &exports, clang::Module *module) {
269 llvm::SmallVector<clang::Module *, 2> sub_exports;
271 module->getExportedModules(sub_exports);
273 for (clang::Module *module : sub_exports)
274 ReportModuleExportsHelper(exports, module);
277void ClangModulesDeclVendorImpl::ReportModuleExports(
279 ExportedModuleSet exports_set;
281 ReportModuleExportsHelper(exports_set, module);
283 for (ModuleID module : exports_set)
284 exports.push_back(module);
288ClangModulesDeclVendorImpl::AddModule(
const SourceModule &module,
289 ModuleVector *exported_modules) {
292 if (m_compiler_instance->hadModuleLoaderFatalFailure())
293 return llvm::createStringError(
294 "couldn't load a module because the module loader is in a fatal state");
298 std::vector<ConstString> imported_module;
301 imported_module.push_back(path_component);
304 ImportedModuleMap::iterator mi = m_imported_modules.find(imported_module);
306 if (mi != m_imported_modules.end()) {
307 if (exported_modules)
308 ReportModuleExports(*exported_modules, mi->second);
309 return llvm::Error::success();
313 clang::HeaderSearch &HS =
314 m_compiler_instance->getPreprocessor().getHeaderSearchInfo();
322 bool is_system_module = (std::distance(path_begin, path_end) >=
323 std::distance(sysroot_begin, sysroot_end)) &&
324 std::equal(sysroot_begin, sysroot_end, path_begin);
326 if (!is_system_module) {
327 bool is_system =
true;
328 bool is_framework =
false;
329 auto dir = HS.getFileMgr().getOptionalDirectoryRef(
332 return llvm::createStringError(
333 "couldn't find module search path directory %s",
336 auto file = HS.lookupModuleMapFile(*dir, is_framework);
338 return llvm::createStringError(
"couldn't find modulemap file in %s",
341 if (HS.parseAndLoadModuleMapFile(*file, is_system,
343 return llvm::createStringError(
344 "failed to parse and load modulemap file in %s",
349 if (!HS.lookupModule(module.
path.front().GetStringRef()))
350 return llvm::createStringErrorV(
351 "header search couldn't locate module '{0}'", module.
path.front());
353 llvm::SmallVector<clang::IdentifierLoc, 4> clang_path;
356 clang::SourceManager &source_manager =
357 m_compiler_instance->getASTContext().getSourceManager();
360 clang_path.emplace_back(
361 source_manager.getLocForStartOfFile(source_manager.getMainFileID())
362 .getLocWithOffset(m_source_location_index++),
363 &m_compiler_instance->getASTContext().Idents.get(
368 StoringDiagnosticConsumer *diagnostic_consumer =
369 static_cast<StoringDiagnosticConsumer *
>(
370 m_compiler_instance->getDiagnostics().getClient());
372 diagnostic_consumer->ClearDiagnostics();
374 clang::Module *top_level_module = DoGetModule(clang_path.front(),
false);
376 if (!top_level_module) {
377 lldb_private::StreamString error_stream;
378 diagnostic_consumer->DumpDiagnostics(error_stream);
380 return llvm::createStringErrorV(
"couldn't load top-level module {0}:\n{1}",
381 module.
path.front().GetStringRef(),
385 clang::Module *submodule = top_level_module;
387 for (
auto &component : llvm::ArrayRef<ConstString>(module.
path).drop_front()) {
388 clang::Module *found = submodule->findSubmodule(component.GetStringRef());
390 lldb_private::StreamString error_stream;
391 diagnostic_consumer->DumpDiagnostics(error_stream);
393 return llvm::createStringErrorV(
394 "couldn't load submodule '{0}' of module '{1}':\n{2}",
395 component.GetStringRef(), submodule->getFullModuleName(),
405 m_compiler_instance->makeModuleVisible(
406 submodule, clang::Module::NameVisibilityKind::AllVisible,
409 clang::Module *requested_module = DoGetModule(clang_path,
true);
411 if (requested_module !=
nullptr) {
412 if (exported_modules)
413 ReportModuleExports(*exported_modules, requested_module);
415 m_imported_modules[imported_module] = requested_module;
419 return llvm::Error::success();
422 return llvm::createStringErrorV(
"unknown error while loading module {0}\n",
423 module.
path.front().GetStringRef());
445llvm::Error ClangModulesDeclVendorImpl::AddModulesForCompileUnit(
447 if (!LanguageSupportsClangModules(cu.
GetLanguage()))
448 return llvm::Error::success();
450 llvm::Error errors = llvm::Error::success();
453 if (
auto err = AddModule(imported_module, &exported_modules))
454 errors = llvm::joinErrors(std::move(errors), std::move(err));
462ClangModulesDeclVendorImpl::FindDecls(
ConstString name,
bool append,
463 uint32_t max_matches,
464 std::vector<CompilerDecl> &decls) {
471 clang::IdentifierInfo &ident =
472 m_compiler_instance->getASTContext().Idents.get(name.
GetStringRef());
474 clang::LookupResult lookup_result(
475 m_compiler_instance->getSema(), clang::DeclarationName(&ident),
476 clang::SourceLocation(), clang::Sema::LookupOrdinaryName);
478 m_compiler_instance->getSema().LookupName(
480 m_compiler_instance->getSema().getScopeForContext(
481 m_compiler_instance->getASTContext().getTranslationUnitDecl()));
483 uint32_t num_matches = 0;
485 for (clang::NamedDecl *named_decl : lookup_result) {
486 if (num_matches >= max_matches)
489 decls.push_back(m_ast_context->GetCompilerDecl(named_decl));
496void ClangModulesDeclVendorImpl::ForEachMacro(
498 std::function<
bool(llvm::StringRef, llvm::StringRef)> handler) {
502 typedef std::map<ModuleID, ssize_t> ModulePriorityMap;
503 ModulePriorityMap module_priorities;
505 ssize_t priority = 0;
507 for (ModuleID module : modules)
508 module_priorities[module] = priority++;
510 if (m_compiler_instance->getPreprocessor().getExternalSource()) {
511 m_compiler_instance->getPreprocessor()
513 ->ReadDefinedMacros();
516 for (clang::Preprocessor::macro_iterator
517 mi = m_compiler_instance->getPreprocessor().macro_begin(),
518 me = m_compiler_instance->getPreprocessor().macro_end();
520 const clang::IdentifierInfo *ii =
nullptr;
523 if (clang::IdentifierInfoLookup *lookup =
524 m_compiler_instance->getPreprocessor()
525 .getIdentifierTable()
526 .getExternalIdentifierLookup()) {
527 lookup->get(mi->first->getName());
533 ssize_t found_priority = -1;
534 clang::MacroInfo *macro_info =
nullptr;
536 for (clang::ModuleMacro *module_macro :
537 m_compiler_instance->getPreprocessor().getLeafModuleMacros(ii)) {
538 clang::Module *module = module_macro->getOwningModule();
541 ModulePriorityMap::iterator pi =
542 module_priorities.find(
reinterpret_cast<ModuleID
>(module));
544 if (pi != module_priorities.end() && pi->second > found_priority) {
545 macro_info = module_macro->getMacroInfo();
546 found_priority = pi->second;
550 clang::Module *top_level_module =
module->getTopLevelModule();
552 if (top_level_module != module) {
553 ModulePriorityMap::iterator pi = module_priorities.find(
554 reinterpret_cast<ModuleID
>(top_level_module));
556 if ((pi != module_priorities.end()) && pi->second > found_priority) {
557 macro_info = module_macro->getMacroInfo();
558 found_priority = pi->second;
564 std::string macro_expansion =
"#define ";
565 llvm::StringRef macro_identifier = mi->first->getName();
566 macro_expansion.append(macro_identifier.str());
569 if (macro_info->isFunctionLike()) {
570 macro_expansion.append(
"(");
572 bool first_arg =
true;
574 for (
auto pi = macro_info->param_begin(),
575 pe = macro_info->param_end();
578 macro_expansion.append(
", ");
582 macro_expansion.append((*pi)->getName().str());
585 if (macro_info->isC99Varargs()) {
587 macro_expansion.append(
"...");
589 macro_expansion.append(
", ...");
590 }
else if (macro_info->isGNUVarargs())
591 macro_expansion.append(
"...");
593 macro_expansion.append(
")");
596 macro_expansion.append(
" ");
598 bool first_token =
true;
600 for (clang::MacroInfo::const_tokens_iterator
601 ti = macro_info->tokens_begin(),
602 te = macro_info->tokens_end();
605 macro_expansion.append(
" ");
609 if (ti->isLiteral()) {
610 if (
const char *literal_data = ti->getLiteralData()) {
611 std::string token_str(literal_data, ti->getLength());
612 macro_expansion.append(token_str);
614 bool invalid =
false;
615 const char *literal_source =
616 m_compiler_instance->getSourceManager().getCharacterData(
617 ti->getLocation(), &invalid);
621 macro_expansion.append(
"<unknown literal value>");
623 macro_expansion.append(
624 std::string(literal_source, ti->getLength()));
627 }
else if (
const char *punctuator_spelling =
628 clang::tok::getPunctuatorSpelling(ti->getKind())) {
629 macro_expansion.append(punctuator_spelling);
630 }
else if (
const char *keyword_spelling =
631 clang::tok::getKeywordSpelling(ti->getKind())) {
632 macro_expansion.append(keyword_spelling);
634 switch (ti->getKind()) {
635 case clang::tok::TokenKind::identifier:
636 macro_expansion.append(ti->getIdentifierInfo()->getName().str());
638 case clang::tok::TokenKind::raw_identifier:
639 macro_expansion.append(ti->getRawIdentifier().str());
642 macro_expansion.append(ti->getName());
648 if (handler(macro_identifier, macro_expansion)) {
656clang::ModuleLoadResult
657ClangModulesDeclVendorImpl::DoGetModule(clang::ModuleIdPath path,
659 clang::Module::NameVisibilityKind visibility =
660 make_visible ? clang::Module::AllVisible : clang::Module::Hidden;
662 const bool is_inclusion_directive =
false;
664 return m_compiler_instance->loadModule(path.front().getLoc(), path,
665 visibility, is_inclusion_directive);
682 std::vector<std::string> compiler_invocation_arguments = {
685 "-fimplicit-module-maps",
691 "-fmodules-validate-system-headers",
692 "-Werror=non-modular-include-in-framework-module",
693 "-Xclang=-fincremental-extensions",
697 target.
GetPlatform()->AddClangModuleCompilationOptions(
698 &target, compiler_invocation_arguments);
705 llvm::SmallString<128> path;
707 props.GetClangModulesCachePath().GetPath(path);
708 std::string module_cache_argument(
"-fmodules-cache-path=");
709 module_cache_argument.append(std::string(path.str()));
710 compiler_invocation_arguments.push_back(module_cache_argument);
715 for (
size_t spi = 0, spe = module_search_paths.
GetSize(); spi < spe; ++spi) {
718 std::string search_path_argument =
"-I";
719 search_path_argument.append(search_path.
GetPath());
721 compiler_invocation_arguments.push_back(search_path_argument);
728 compiler_invocation_arguments.push_back(
"-resource-dir");
729 compiler_invocation_arguments.push_back(clang_resource_dir.
GetPath());
733 std::vector<const char *> compiler_invocation_argument_cstrs;
734 compiler_invocation_argument_cstrs.reserve(
735 compiler_invocation_arguments.size());
736 for (
const std::string &arg : compiler_invocation_arguments)
737 compiler_invocation_argument_cstrs.push_back(arg.c_str());
739 auto diag_options_up =
740 clang::CreateAndPopulateDiagOpts(compiler_invocation_argument_cstrs);
741 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine =
742 clang::CompilerInstance::createDiagnostics(
744 new StoringDiagnosticConsumer);
747 LLDB_LOG(log,
"ClangModulesDeclVendor's compiler flags {0:$[ ]}",
748 llvm::make_range(compiler_invocation_arguments.begin(),
749 compiler_invocation_arguments.end()));
751 clang::CreateInvocationOptions CIOpts;
752 CIOpts.Diags = diagnostics_engine;
753 std::shared_ptr<clang::CompilerInvocation> invocation =
754 clang::createInvocation(compiler_invocation_argument_cstrs,
760 std::unique_ptr<llvm::MemoryBuffer> source_buffer =
761 llvm::MemoryBuffer::getMemBuffer(
762 "extern int __lldb __attribute__((unavailable));",
766 source_buffer.release());
768 auto instance = std::make_unique<clang::CompilerInstance>(invocation);
772 instance->createFileManager();
773 instance->setDiagnostics(diagnostics_engine);
775 std::unique_ptr<clang::FrontendAction> action(
new clang::SyntaxOnlyAction);
777 instance->setTarget(clang::TargetInfo::CreateTargetInfo(
778 *diagnostics_engine, instance->getInvocation().getTargetOpts()));
780 if (!instance->hasTarget())
783 instance->getTarget().adjust(*diagnostics_engine, instance->getLangOpts(),
786 if (!action->BeginSourceFile(*instance,
787 instance->getFrontendOpts().Inputs[0]))
790 instance->createASTReader();
792 instance->createSema(action->getTranslationUnitKind(),
nullptr);
794 const bool skipFunctionBodies =
false;
795 std::unique_ptr<clang::Parser> parser(
new clang::Parser(
796 instance->getPreprocessor(), instance->getSema(), skipFunctionBodies));
798 instance->getPreprocessor().EnterMainSourceFile();
799 parser->Initialize();
801 clang::Parser::DeclGroupPtrTy parsed;
802 auto ImportState = clang::Sema::ModuleImportState::NotACXX20Module;
803 while (!parser->ParseTopLevelDecl(parsed, ImportState))
806 return new ClangModulesDeclVendorImpl(
807 std::move(diag_options_up), std::move(diagnostics_engine),
808 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".