@@ -135,105 +135,12 @@ int NinjaGenerator::gen_dynamic(std::string& comp_db_path, Scanner::Config& c)
135135DECL_STRONG_ID_INV (module_id_t , 0 );
136136
137137struct DepsCollector : public DepInfoObserver {
138- ScanItemSet& item_set;
139- module_id_t next_module_id = module_id_t { 1 }; // 0 is reserved for invalid
140- // todo: expose a lower level API from the scanner so we don't have to duplicate this stuff
141- vector_map<module_id_t , scan_item_idx_t > module_exported_by;
142- // todo: create a bidirectional hash-map that doesn't store the strings twice
143- vector_map<module_id_t , std::string> reverse_module_map;
144- std::unordered_map<std::string, module_id_t > module_map;
145- struct module_info {
146- module_id_t exports = {};
147- std::vector<module_id_t > imports;
148- };
149- vector_map<scan_item_idx_t , module_info> item_to_module_info;
150- scan_item_idx_t current_item_idx = {};
151- std::unordered_set<std::string> all_files;
152-
153- module_id_t register_module (DataBlockView name) {
154- if (auto pname = std::get_if<std::string_view>(&name); pname) {
155- auto str_name = (std::string) * pname;
156- auto [itr, inserted] = module_map.try_emplace (str_name, next_module_id);
157- if (inserted) {
158- reverse_module_map.emplace_back (std::move (str_name));
159- next_module_id++;
160- }
161- return itr->second ;
162- }
163- throw std::runtime_error (" failed to get module module name" );
164- }
165-
166- DepsCollector (ScanItemSet& item_set) : item_set(item_set) {
167- auto nr_items = item_set.items .size ();
168- // each item exports at most one module
169- // module id = 0 is reserved for the invalid state
170- module_exported_by.resize (id_cast<module_id_t >(nr_items + 1 ));
171- module_map.reserve ((std::size_t )nr_items + 1 );
172- reverse_module_map.reserve (id_cast<module_id_t >(nr_items + 1 ));
173- reverse_module_map.emplace_back ();
174- for (auto & idx : module_exported_by)
175- idx.invalidate ();
176- item_to_module_info.resize (nr_items);
177- }
178-
179- module_info& get_module_info () {
180- return item_to_module_info[current_item_idx];
181- }
182-
183- void results_for_item (scan_item_idx_t item_idx, bool out_of_date) override {
184- current_item_idx = item_idx;
185- }
186- void export_module (DataBlockView name) override {
187- auto id = register_module (name);
188- get_module_info ().exports = id;
189- module_exported_by[id] = current_item_idx;
190- }
191- void import_module (DataBlockView name) override {
192- get_module_info ().imports .push_back (register_module (name));
193- }
138+ std::unordered_set<std::string> all_file_deps;
194139 void include_header (DataBlockView path) override {
195- all_files .insert ((std::string)std::get<std::string_view>(path));
140+ all_file_deps .insert ((std::string)std::get<std::string_view>(path));
196141 }
197- void import_header (DataBlockView path) override {}
198142 void other_file_dep (DataBlockView path) override {
199- all_files.insert ((std::string)std::get<std::string_view>(path));
200- }
201- void item_finished () override {}
202-
203- std::vector<scan_item_idx_t > queue;
204- std::vector<bool > is_in_queue;
205-
206- template <typename F>
207- bool visit_transitive_imports (scan_item_idx_t root_idx, F&& visitor_func) {
208- queue.resize ((std::size_t )item_to_module_info.size ());
209- if (queue.empty ())
210- return true ;
211- is_in_queue.resize ((std::size_t )item_to_module_info.size ());
212- queue[0 ] = root_idx;
213- is_in_queue[(std::size_t )root_idx] = true ;
214- std::size_t s = 0 , e = 0 ;
215- while (s <= e) {
216- auto idx = queue[s++];
217- auto & info = item_to_module_info[idx];
218- if (idx != root_idx)
219- visitor_func (idx);
220- for (module_id_t id : info.imports ) {
221- auto exp_idx = module_exported_by[id];
222- if (!exp_idx.is_valid ()) {
223- if (reverse_module_map[id] == " std.core" )
224- continue ;
225- fmt::print (stderr, " '{}' imports '{}' which is not exported by any TU\n " ,
226- item_set.items [exp_idx].path , reverse_module_map[id]);
227- return false ;
228- }
229- if (!is_in_queue[(std::size_t )exp_idx]) {
230- is_in_queue[(std::size_t )exp_idx] = true ;
231- queue[++e] = exp_idx;
232- }
233- }
234- }
235- is_in_queue.clear ();
236- return true ;
143+ all_file_deps.insert ((std::string)std::get<std::string_view>(path));
237144 }
238145};
239146
@@ -278,9 +185,11 @@ int NinjaGenerator::scan(std::string& comp_db_path, Scanner::Config& c)
278185 c.item_set = scan_item_set_from_comp_db (
279186 NinjaGenerator::comp_db_to_read (comp_db_path, c)
280187 );
281- DepsCollector collector (c. item_set ) ;
188+ DepsCollector collector;
282189 c.observer = &collector;
190+ ModuleVisitor module_visitor;
283191 c.submit_previous_results = true ;
192+ c.module_visitor = &module_visitor;
284193
285194 auto config_owned_view = Scanner::ConfigOwnedView::from (c);
286195 auto config_view = Scanner::ConfigView::from (config_owned_view);
@@ -293,7 +202,10 @@ int NinjaGenerator::scan(std::string& comp_db_path, Scanner::Config& c)
293202 return 1 ;
294203 }
295204
296- for (auto file : collector.all_files ) {
205+ if (module_visitor.missing_imports )
206+ return 1 ;
207+
208+ for (auto file : collector.all_file_deps ) {
297209 auto deps_prefix = " Note: including file:" ; // = "-";
298210 fmt::print (" {} {}\n " , deps_prefix, file); // ninja looks for the prefix
299211 }
@@ -308,8 +220,7 @@ int NinjaGenerator::scan(std::string& comp_db_path, Scanner::Config& c)
308220 for (auto i : c.item_set .items .indices ()) {
309221 auto & item = c.item_set .items [i];
310222 output_files[i] = get_output_file (item, c);
311- auto & info = collector.item_to_module_info [i];
312- if (info.exports .is_valid ()) {
223+ if (module_visitor.has_export [i]) {
313224 bmi_files[i] = get_bmi_file (output_files[i]);
314225 ninja_bmi_files[i] = ninja_escape (bmi_files[i]);
315226 }
@@ -325,29 +236,29 @@ int NinjaGenerator::scan(std::string& comp_db_path, Scanner::Config& c)
325236 rsp_mem.clear ();
326237 write_if_changed_guard rsp_guard (rsp_mem, response_file);
327238
328- auto & info = collector.item_to_module_info [i];
329- if (info.exports .is_valid () || !info.imports .empty ()) {
239+ bool has_export = (module_visitor.has_export [i]);
240+ bool has_import = (!module_visitor.imports_item [i].empty ());
241+
242+ if (has_export || has_import) {
330243 std::string ifcdir = get_ifc_path (c.item_set .items [i], c);
331244 fmt::format_to (rsp_mem, " /experimental:module /module:stdIfcDir \" {}\" " , ifcdir);
332245 }
333246
334- if (info. exports . is_valid () ) {
247+ if (has_export ) {
335248 fmt::print (dd_fout, " | {}" , ninja_bmi_files[i]);
336249 fmt::format_to (rsp_mem, " /module:interface /module:output \" {}\" " , bmi_files[i]);
337250 }
338251
339252 fmt::print (dd_fout, " : dyndep | {}" , ninja_escape (response_file));
340- if (info. imports . empty () ) {
253+ if (!has_import ) {
341254 fmt::print (dd_fout, " \n " );
342255 continue ;
343256 }
344257
345- bool ok = collector .visit_transitive_imports (i, [&](scan_item_idx_t exported_by_item_idx) {
258+ module_visitor .visit_transitive_imports (i, [&](scan_item_idx_t exported_by_item_idx) {
346259 fmt::print (dd_fout, " {}" , ninja_bmi_files[exported_by_item_idx]);
347260 fmt::format_to (rsp_mem, " /module:reference \" {}\" " , bmi_files[exported_by_item_idx]);
348261 });
349- if (!ok)
350- return 1 ;
351262 fmt::print (dd_fout, " \n " );
352263 }
353264 return 0 ;
0 commit comments