9#include "clang/Basic/Diagnostic.h"
10#include "clang/Basic/DiagnosticFrontend.h"
11#include "clang/Basic/TargetInfo.h"
12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Frontend/FrontendActions.h"
14#include "clang/Frontend/TextDiagnosticPrinter.h"
15#include "clang/Lex/Preprocessor.h"
16#include "clang/Lex/PreprocessorOptions.h"
17#include "clang/Parse/Parser.h"
18#include "clang/Sema/Lookup.h"
19#include "clang/Serialization/ASTReader.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/Path.h"
22#include "llvm/Support/Threading.h"
46class StoringDiagnosticConsumer :
public clang::DiagnosticConsumer {
48 StoringDiagnosticConsumer();
50 void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
51 const clang::Diagnostic &info)
override;
53 void ClearDiagnostics();
57 void BeginSourceFile(
const clang::LangOptions &LangOpts,
58 const clang::Preprocessor *PP =
nullptr)
override;
59 void EndSourceFile()
override;
62 bool HandleModuleRemark(
const clang::Diagnostic &info);
63 void SetCurrentModuleProgress(std::string module_name);
65 typedef std::pair<clang::DiagnosticsEngine::Level, std::string>
67 std::vector<IDAndDiagnostic> m_diagnostics;
70 std::unique_ptr<clang::TextDiagnosticPrinter> m_diag_printer;
72 std::unique_ptr<llvm::raw_string_ostream> m_os;
76 std::unique_ptr<Progress> m_current_progress_up;
77 std::vector<std::string> m_module_build_stack;
84 ClangModulesDeclVendorImpl(
85 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
86 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
87 std::unique_ptr<clang::CompilerInstance> compiler_instance,
88 std::unique_ptr<clang::Parser> parser);
90 ~ClangModulesDeclVendorImpl()
override =
default;
93 Stream &error_stream)
override;
96 Stream &error_stream)
override;
99 std::vector<CompilerDecl> &decls)
override;
102 const ModuleVector &modules,
103 std::function<
bool(llvm::StringRef, llvm::StringRef)> handler)
override;
106 typedef llvm::DenseSet<ModuleID> ExportedModuleSet;
107 void ReportModuleExportsHelper(ExportedModuleSet &exports,
108 clang::Module *module);
110 void ReportModuleExports(ModuleVector &exports, clang::Module *module);
112 clang::ModuleLoadResult DoGetModule(clang::ModuleIdPath path,
115 bool m_enabled =
false;
117 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> m_diagnostics_engine;
118 std::shared_ptr<clang::CompilerInvocation> m_compiler_invocation;
119 std::unique_ptr<clang::CompilerInstance> m_compiler_instance;
120 std::unique_ptr<clang::Parser> m_parser;
121 size_t m_source_location_index =
124 typedef std::vector<ConstString> ImportedModule;
125 typedef std::map<ImportedModule, clang::Module *> ImportedModuleMap;
126 typedef llvm::DenseSet<ModuleID> ImportedModuleSet;
127 ImportedModuleMap m_imported_modules;
128 ImportedModuleSet m_user_imported_modules;
131 std::shared_ptr<TypeSystemClang> m_ast_context;
135StoringDiagnosticConsumer::StoringDiagnosticConsumer() {
136 auto *options =
new clang::DiagnosticOptions();
137 m_os = std::make_unique<llvm::raw_string_ostream>(m_output);
139 std::make_unique<clang::TextDiagnosticPrinter>(*m_os, options);
142void StoringDiagnosticConsumer::HandleDiagnostic(
143 clang::DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &info) {
144 if (HandleModuleRemark(info))
149 m_diag_printer->HandleDiagnostic(DiagLevel, info);
153 m_diagnostics.push_back(IDAndDiagnostic(DiagLevel, m_output));
156void StoringDiagnosticConsumer::ClearDiagnostics() { m_diagnostics.clear(); }
158void StoringDiagnosticConsumer::DumpDiagnostics(
Stream &error_stream) {
159 for (IDAndDiagnostic &diag : m_diagnostics) {
160 switch (diag.first) {
165 case clang::DiagnosticsEngine::Level::Ignored:
171void StoringDiagnosticConsumer::BeginSourceFile(
172 const clang::LangOptions &LangOpts,
const clang::Preprocessor *PP) {
173 m_diag_printer->BeginSourceFile(LangOpts, PP);
176void StoringDiagnosticConsumer::EndSourceFile() {
177 m_current_progress_up =
nullptr;
178 m_diag_printer->EndSourceFile();
181bool StoringDiagnosticConsumer::HandleModuleRemark(
182 const clang::Diagnostic &info) {
183 Log *log =
GetLog(LLDBLog::Types | LLDBLog::Expressions);
184 switch (info.getID()) {
185 case clang::diag::remark_module_build: {
186 const auto &module_name = info.getArgStdStr(0);
187 SetCurrentModuleProgress(module_name);
188 m_module_build_stack.push_back(module_name);
190 const auto &module_path = info.getArgStdStr(1);
191 LLDB_LOG(log,
"Building Clang module {0} as {1}", module_name, module_path);
194 case clang::diag::remark_module_build_done: {
196 m_module_build_stack.pop_back();
197 if (m_module_build_stack.empty()) {
198 m_current_progress_up =
nullptr;
203 const auto &resumed_module_name = m_module_build_stack.back();
204 SetCurrentModuleProgress(resumed_module_name);
207 const auto &module_name = info.getArgStdStr(0);
208 LLDB_LOG(log,
"Finished building Clang module {0}", module_name);
216void StoringDiagnosticConsumer::SetCurrentModuleProgress(
217 std::string module_name) {
218 if (!m_current_progress_up)
219 m_current_progress_up =
220 std::make_unique<Progress>(
"Building Clang modules");
222 m_current_progress_up->Increment(1, std::move(module_name));
230ClangModulesDeclVendorImpl::ClangModulesDeclVendorImpl(
231 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine,
232 std::shared_ptr<clang::CompilerInvocation> compiler_invocation,
233 std::unique_ptr<clang::CompilerInstance> compiler_instance,
234 std::unique_ptr<clang::Parser> parser)
235 : m_diagnostics_engine(std::move(diagnostics_engine)),
236 m_compiler_invocation(std::move(compiler_invocation)),
237 m_compiler_instance(std::move(compiler_instance)),
238 m_parser(std::move(parser)) {
242 std::make_shared<TypeSystemClang>(
"ClangModulesDeclVendor ASTContext",
243 m_compiler_instance->getASTContext());
246void ClangModulesDeclVendorImpl::ReportModuleExportsHelper(
247 ExportedModuleSet &exports, clang::Module *module) {
253 llvm::SmallVector<clang::Module *, 2> sub_exports;
255 module->getExportedModules(sub_exports);
257 for (clang::Module *module : sub_exports)
258 ReportModuleExportsHelper(exports, module);
261void ClangModulesDeclVendorImpl::ReportModuleExports(
263 ExportedModuleSet exports_set;
265 ReportModuleExportsHelper(exports_set, module);
267 for (ModuleID module : exports_set)
268 exports.push_back(module);
271bool ClangModulesDeclVendorImpl::AddModule(
const SourceModule &module,
272 ModuleVector *exported_modules,
276 if (m_compiler_instance->hadModuleLoaderFatalFailure()) {
277 error_stream.
PutCString(
"error: Couldn't load a module because the module "
278 "loader is in a fatal state.\n");
284 std::vector<ConstString> imported_module;
287 imported_module.push_back(path_component);
290 ImportedModuleMap::iterator mi = m_imported_modules.find(imported_module);
292 if (mi != m_imported_modules.end()) {
293 if (exported_modules)
294 ReportModuleExports(*exported_modules, mi->second);
299 clang::HeaderSearch &HS =
300 m_compiler_instance->getPreprocessor().getHeaderSearchInfo();
308 bool is_system_module = (std::distance(path_begin, path_end) >=
309 std::distance(sysroot_begin, sysroot_end)) &&
310 std::equal(sysroot_begin, sysroot_end, path_begin);
312 if (!is_system_module) {
314 error_stream.
Printf(
"error: No module map file in %s\n",
319 bool is_system =
true;
320 bool is_framework =
false;
321 auto dir = HS.getFileMgr().getOptionalDirectoryRef(
325 auto file = HS.lookupModuleMapFile(*dir, is_framework);
328 if (!HS.loadModuleMapFile(*file, is_system))
332 if (!HS.lookupModule(module.
path.front().GetStringRef())) {
333 error_stream.
Printf(
"error: Header search couldn't locate module %s\n",
334 module.
path.front().AsCString());
338 llvm::SmallVector<std::pair<clang::IdentifierInfo *, clang::SourceLocation>,
343 clang::SourceManager &source_manager =
344 m_compiler_instance->getASTContext().getSourceManager();
347 clang_path.push_back(std::make_pair(
348 &m_compiler_instance->getASTContext().Idents.get(
350 source_manager.getLocForStartOfFile(source_manager.getMainFileID())
351 .getLocWithOffset(m_source_location_index++)));
355 StoringDiagnosticConsumer *diagnostic_consumer =
356 static_cast<StoringDiagnosticConsumer *
>(
357 m_compiler_instance->getDiagnostics().getClient());
359 diagnostic_consumer->ClearDiagnostics();
361 clang::Module *top_level_module = DoGetModule(clang_path.front(),
false);
363 if (!top_level_module) {
364 diagnostic_consumer->DumpDiagnostics(error_stream);
365 error_stream.
Printf(
"error: Couldn't load top-level module %s\n",
366 module.
path.front().AsCString());
370 clang::Module *submodule = top_level_module;
372 for (
auto &component : llvm::ArrayRef<ConstString>(module.
path).drop_front()) {
373 submodule = submodule->findSubmodule(component.GetStringRef());
375 diagnostic_consumer->DumpDiagnostics(error_stream);
376 error_stream.
Printf(
"error: Couldn't load submodule %s\n",
377 component.GetCString());
382 clang::Module *requested_module = DoGetModule(clang_path,
true);
384 if (requested_module !=
nullptr) {
385 if (exported_modules)
386 ReportModuleExports(*exported_modules, requested_module);
388 m_imported_modules[imported_module] = requested_module;
417bool ClangModulesDeclVendorImpl::AddModulesForCompileUnit(
420 if (LanguageSupportsClangModules(cu.
GetLanguage())) {
422 if (!AddModule(imported_module, &exported_modules, error_stream))
431ClangModulesDeclVendorImpl::FindDecls(
ConstString name,
bool append,
432 uint32_t max_matches,
433 std::vector<CompilerDecl> &decls) {
440 clang::IdentifierInfo &ident =
441 m_compiler_instance->getASTContext().Idents.get(name.
GetStringRef());
443 clang::LookupResult lookup_result(
444 m_compiler_instance->getSema(), clang::DeclarationName(&ident),
445 clang::SourceLocation(), clang::Sema::LookupOrdinaryName);
447 m_compiler_instance->getSema().LookupName(
449 m_compiler_instance->getSema().getScopeForContext(
450 m_compiler_instance->getASTContext().getTranslationUnitDecl()));
452 uint32_t num_matches = 0;
454 for (clang::NamedDecl *named_decl : lookup_result) {
455 if (num_matches >= max_matches)
458 decls.push_back(m_ast_context->GetCompilerDecl(named_decl));
465void ClangModulesDeclVendorImpl::ForEachMacro(
467 std::function<
bool(llvm::StringRef, llvm::StringRef)> handler) {
471 typedef std::map<ModuleID, ssize_t> ModulePriorityMap;
472 ModulePriorityMap module_priorities;
474 ssize_t priority = 0;
476 for (ModuleID module : modules)
477 module_priorities[module] = priority++;
479 if (m_compiler_instance->getPreprocessor().getExternalSource()) {
480 m_compiler_instance->getPreprocessor()
482 ->ReadDefinedMacros();
485 for (clang::Preprocessor::macro_iterator
486 mi = m_compiler_instance->getPreprocessor().macro_begin(),
487 me = m_compiler_instance->getPreprocessor().macro_end();
489 const clang::IdentifierInfo *ii =
nullptr;
492 if (clang::IdentifierInfoLookup *lookup =
493 m_compiler_instance->getPreprocessor()
494 .getIdentifierTable()
495 .getExternalIdentifierLookup()) {
496 lookup->get(mi->first->getName());
502 ssize_t found_priority = -1;
503 clang::MacroInfo *macro_info =
nullptr;
505 for (clang::ModuleMacro *module_macro :
506 m_compiler_instance->getPreprocessor().getLeafModuleMacros(ii)) {
507 clang::Module *module = module_macro->getOwningModule();
510 ModulePriorityMap::iterator pi =
511 module_priorities.find(
reinterpret_cast<ModuleID
>(module));
513 if (pi != module_priorities.end() && pi->second > found_priority) {
514 macro_info = module_macro->getMacroInfo();
515 found_priority = pi->second;
519 clang::Module *top_level_module = module->getTopLevelModule();
521 if (top_level_module != module) {
522 ModulePriorityMap::iterator pi = module_priorities.find(
523 reinterpret_cast<ModuleID
>(top_level_module));
525 if ((pi != module_priorities.end()) && pi->second > found_priority) {
526 macro_info = module_macro->getMacroInfo();
527 found_priority = pi->second;
533 std::string macro_expansion =
"#define ";
534 llvm::StringRef macro_identifier = mi->first->getName();
535 macro_expansion.append(macro_identifier.str());
538 if (macro_info->isFunctionLike()) {
539 macro_expansion.append(
"(");
541 bool first_arg =
true;
543 for (
auto pi = macro_info->param_begin(),
544 pe = macro_info->param_end();
547 macro_expansion.append(
", ");
551 macro_expansion.append((*pi)->getName().str());
554 if (macro_info->isC99Varargs()) {
556 macro_expansion.append(
"...");
558 macro_expansion.append(
", ...");
559 }
else if (macro_info->isGNUVarargs())
560 macro_expansion.append(
"...");
562 macro_expansion.append(
")");
565 macro_expansion.append(
" ");
567 bool first_token =
true;
569 for (clang::MacroInfo::const_tokens_iterator
570 ti = macro_info->tokens_begin(),
571 te = macro_info->tokens_end();
574 macro_expansion.append(
" ");
578 if (ti->isLiteral()) {
579 if (
const char *literal_data = ti->getLiteralData()) {
580 std::string token_str(literal_data, ti->getLength());
581 macro_expansion.append(token_str);
583 bool invalid =
false;
584 const char *literal_source =
585 m_compiler_instance->getSourceManager().getCharacterData(
586 ti->getLocation(), &invalid);
590 macro_expansion.append(
"<unknown literal value>");
592 macro_expansion.append(
593 std::string(literal_source, ti->getLength()));
596 }
else if (
const char *punctuator_spelling =
597 clang::tok::getPunctuatorSpelling(ti->getKind())) {
598 macro_expansion.append(punctuator_spelling);
599 }
else if (
const char *keyword_spelling =
600 clang::tok::getKeywordSpelling(ti->getKind())) {
601 macro_expansion.append(keyword_spelling);
603 switch (ti->getKind()) {
604 case clang::tok::TokenKind::identifier:
605 macro_expansion.append(ti->getIdentifierInfo()->getName().str());
607 case clang::tok::TokenKind::raw_identifier:
608 macro_expansion.append(ti->getRawIdentifier().str());
611 macro_expansion.append(ti->getName());
617 if (handler(macro_identifier, macro_expansion)) {
625clang::ModuleLoadResult
626ClangModulesDeclVendorImpl::DoGetModule(clang::ModuleIdPath path,
628 clang::Module::NameVisibilityKind visibility =
629 make_visible ? clang::Module::AllVisible : clang::Module::Hidden;
631 const bool is_inclusion_directive =
false;
633 return m_compiler_instance->loadModule(path.front().second, path, visibility,
634 is_inclusion_directive);
651 std::vector<std::string> compiler_invocation_arguments = {
654 "-fimplicit-module-maps",
660 "-fmodules-validate-system-headers",
661 "-Werror=non-modular-include-in-framework-module",
662 "-Xclang=-fincremental-extensions",
665 target.
GetPlatform()->AddClangModuleCompilationOptions(
666 &target, compiler_invocation_arguments);
673 llvm::SmallString<128> path;
675 props.GetClangModulesCachePath().GetPath(path);
676 std::string module_cache_argument(
"-fmodules-cache-path=");
677 module_cache_argument.append(std::string(path.str()));
678 compiler_invocation_arguments.push_back(module_cache_argument);
683 for (
size_t spi = 0, spe = module_search_paths.
GetSize(); spi < spe; ++spi) {
686 std::string search_path_argument =
"-I";
687 search_path_argument.append(search_path.
GetPath());
689 compiler_invocation_arguments.push_back(search_path_argument);
696 compiler_invocation_arguments.push_back(
"-resource-dir");
697 compiler_invocation_arguments.push_back(clang_resource_dir.
GetPath());
701 std::vector<const char *> compiler_invocation_argument_cstrs;
702 compiler_invocation_argument_cstrs.reserve(
703 compiler_invocation_arguments.size());
704 for (
const std::string &arg : compiler_invocation_arguments)
705 compiler_invocation_argument_cstrs.push_back(arg.c_str());
707 auto diag_options_up =
708 clang::CreateAndPopulateDiagOpts(compiler_invocation_argument_cstrs);
709 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics_engine =
710 clang::CompilerInstance::createDiagnostics(diag_options_up.release(),
711 new StoringDiagnosticConsumer);
714 LLDB_LOG(log,
"ClangModulesDeclVendor's compiler flags {0:$[ ]}",
715 llvm::make_range(compiler_invocation_arguments.begin(),
716 compiler_invocation_arguments.end()));
718 clang::CreateInvocationOptions CIOpts;
719 CIOpts.Diags = diagnostics_engine;
720 std::shared_ptr<clang::CompilerInvocation> invocation =
721 clang::createInvocation(compiler_invocation_argument_cstrs,
727 std::unique_ptr<llvm::MemoryBuffer> source_buffer =
728 llvm::MemoryBuffer::getMemBuffer(
729 "extern int __lldb __attribute__((unavailable));",
733 source_buffer.release());
735 std::unique_ptr<clang::CompilerInstance> instance(
736 new clang::CompilerInstance);
740 instance->setDiagnostics(diagnostics_engine.get());
741 instance->setInvocation(invocation);
743 std::unique_ptr<clang::FrontendAction> action(
new clang::SyntaxOnlyAction);
745 instance->setTarget(clang::TargetInfo::CreateTargetInfo(
746 *diagnostics_engine, instance->getInvocation().TargetOpts));
748 if (!instance->hasTarget())
751 instance->getTarget().adjust(*diagnostics_engine, instance->getLangOpts());
753 if (!action->BeginSourceFile(*instance,
754 instance->getFrontendOpts().Inputs[0]))
757 instance->createASTReader();
759 instance->createSema(action->getTranslationUnitKind(),
nullptr);
761 const bool skipFunctionBodies =
false;
762 std::unique_ptr<clang::Parser> parser(
new clang::Parser(
763 instance->getPreprocessor(), instance->getSema(), skipFunctionBodies));
765 instance->getPreprocessor().EnterMainSourceFile();
766 parser->Initialize();
768 clang::Parser::DeclGroupPtrTy parsed;
769 auto ImportState = clang::Sema::ModuleImportState::NotACXX20Module;
770 while (!parser->ParseTopLevelDecl(parsed, ImportState))
773 return new ClangModulesDeclVendorImpl(std::move(diagnostics_engine),
774 std::move(invocation),
775 std::move(instance), std::move(parser));
static const char * ModuleImportBufferName
static llvm::raw_ostream & error(Stream &strm)
#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.
uint32_t FindDecls(ConstString name, bool append, uint32_t max_matches, std::vector< clang::NamedDecl * > &decls)
static ClangModulesDeclVendor * Create(Target &target)
virtual bool AddModulesForCompileUnit(CompileUnit &cu, ModuleVector &exported_modules, Stream &error_stream)=0
Add all modules referred to in a given compilation unit to the list of modules to search.
virtual void ForEachMacro(const ModuleVector &modules, std::function< bool(llvm::StringRef, llvm::StringRef)> handler)=0
Enumerate all the macros that are defined by a given set of modules that are already imported.
std::vector< ModuleID > ModuleVector
static bool LanguageSupportsClangModules(lldb::LanguageType language)
Query whether Clang supports modules for a particular language.
virtual bool AddModule(const SourceModule &module, ModuleVector *exported_modules, Stream &error_stream)=0
Add a module to the list of modules to search.
~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.
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
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()
A stream class that can stream formatted output to a file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
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".