15
15
#include " strong_id.h"
16
16
#include " multi_buffer.h"
17
17
#include " trace.h"
18
+ #include " cmd_line_utils.h"
18
19
19
20
namespace cppm {
20
21
@@ -577,36 +578,28 @@ struct ScannerImpl {
577
578
if (ood_items.empty ())
578
579
return data;
579
580
580
- // code page 65001 is UTF-8, use that for the paths - todo: test this
581
- std::string cmd = fmt::format (" chcp 65001 & \" {}\" --compilation-database=\" {}\" " , tool_path, comp_db_path);
582
- FILE* cmd_out = _popen (cmd.c_str (), " r" );
583
- if (!cmd_out)
584
- throw std::runtime_error (" failed to execute scanner tool" );
585
-
586
- // todo: ideally we should send the up-to-date item information to the observer here, after scanning started
587
-
588
- #if 0
589
- auto json_depformat = nlohmann::json::parse(cmd_out);
590
- fclose(cmd_out);
591
- #endif
592
-
593
581
// std::vector<file_table_idx_t> file_table_indexed_lookup;
594
582
std::unordered_map<std::string, file_table_idx_t > file_table_lookup;
595
583
std::unordered_map<std::string, module_table_idx_t > module_table_lookup;
596
584
597
- auto get_file_table_idx = [&](const std::string& str) {
585
+ auto get_file_table_idx = [&](std::string_view file) {
586
+ // todo: use heterogeneous lookup when available
587
+ auto str = (std::string)file;
598
588
auto [itr, inserted] = file_table_lookup.try_emplace (str, file_table_idx_t {});
599
589
if (inserted)
600
590
itr->second = data.file_table_buf .add (str);
601
591
return itr->second ;
602
592
};
603
593
604
- auto get_module_table_idx = [&](const std::string& str) {
594
+ auto get_module_table_idx = [&](std::string_view name) {
595
+ // todo: use heterogeneous lookup when available
596
+ auto str = (std::string)name;
605
597
auto [itr, inserted] = module_table_lookup.try_emplace (str, module_table_idx_t {});
606
598
if (inserted)
607
599
itr->second = data.module_table_buf .add (str);
608
600
return itr->second ;
609
601
};
602
+
610
603
#if 0
611
604
auto get_file_table_idx_2 = [&](std::size_t idx, const std::string& str) {
612
605
auto ft_idx = get_file_table_idx(str);
@@ -621,8 +614,22 @@ struct ScannerImpl {
621
614
};
622
615
#endif
623
616
617
+ std::unordered_map<std::string, scan_item_idx_t > item_lookup;
618
+ for (auto i : items.indices ()) {
619
+ // note: this doesn't work if there are multiple items with the same file path
620
+ item_lookup[get_rooted_path (item_root_path, items[i].path ).string ()] = i;
621
+ }
622
+ auto starts_with = [](std::string_view a, std::string_view b) {
623
+ return (a.substr (0 , b.size ()) == b);
624
+ };
625
+ int nr_lines = 0 ;
626
+ std::string current_file;
627
+ scan_item_idx_t current_item_idx = {};
628
+
629
+ // code page 65001 is UTF-8, use that for the paths - todo: test this
630
+ CmdArgs cmd { " chcp 65001 & \" {}\" --compilation-database=\" {}\" " , tool_path, comp_db_path };
631
+ auto ret = run_cmd_read_lines (cmd, [&](std::string_view line) {
624
632
#if 0
625
- for (auto& json_depinfo : json_depformat["sources"]) {
626
633
auto input_file = json_depinfo["input"];
627
634
auto item_idx = scan_item_idx_t { json_depinfo["_id"].get<std::size_t>() }; // note: otherwise we need to look at the outputs
628
635
@@ -636,34 +643,10 @@ struct ScannerImpl {
636
643
auto src_path = json_module["source_path"].get<std::string>();
637
644
item_deps_buf.add(get_file_table_idx(src_path));
638
645
}
639
- }
640
- #else
641
- std::unordered_map<std::string, scan_item_idx_t > item_lookup;
642
- for (auto i : items.indices ()) {
643
- // note: this doesn't work if there are multiple items with the same file path
644
- item_lookup[get_rooted_path (item_root_path, items[i].path ).string ()] = i;
645
- }
646
- constexpr int buf_size = 2 * 1024 * 1024 ;
647
- std::string line;
648
- line.resize (buf_size);
649
- auto starts_with = [](std::string_view a, std::string_view b) {
650
- return (a.substr (0 , b.size ()) == b);
651
- };
652
- auto subview = [](const std::string& str, std::size_t ofs) {
653
- std::string_view ret { str };
654
- ret.remove_prefix (ofs);
655
- return ret;
656
- };
657
- int nr_lines = 0 ;
658
- std::string current_file;
659
- scan_item_idx_t current_item_idx = {};
660
- while (fgets (&line[0 ], buf_size, cmd_out) != NULL ) {
646
+ #endif
647
+
661
648
// skip the first line: active code page ..
662
- if (nr_lines++ == 0 ) continue ;
663
- auto sz = line.find_first_of (" \n\r " , 0 , 3 ); // include null terminator in the search
664
- if (sz == std::string::npos)
665
- break ;
666
- line.resize (sz);
649
+ if (nr_lines++ == 0 ) return true ;
667
650
// fmt::print("{}\n", line);
668
651
if (starts_with (line, " :::: " )) {
669
652
if (current_file != " " ) observer->item_finished ();
@@ -678,12 +661,12 @@ struct ScannerImpl {
678
661
data.got_result [current_item_idx] = true ;
679
662
observer->results_for_item (current_item_idx, /* out_of_date=*/ true );
680
663
} else if (starts_with (line, " :exp " )) {
681
- auto name = subview ( line, 5 );
682
- data.exports [current_item_idx] = get_module_table_idx ((std::string) name);
664
+ auto name = line. substr ( 5 );
665
+ data.exports [current_item_idx] = get_module_table_idx (name);
683
666
observer->export_module (name);
684
667
} else if (starts_with (line, " :imp " )) {
685
- auto name = subview ( line, 5 );
686
- data.imports_buf .add (get_module_table_idx ((std::string) name));
668
+ auto name = line. substr ( 5 );
669
+ data.imports_buf .add (get_module_table_idx (name));
687
670
observer->import_module (name);
688
671
// todo: import header unit ?
689
672
} else {
@@ -694,11 +677,16 @@ struct ScannerImpl {
694
677
// observer->other_file_dep();
695
678
}
696
679
}
697
- line.resize (buf_size);
698
- }
680
+ return true ;
681
+ }, [](std::string_view err_line) {
682
+ fmt::print (" ERR: {}\n " , err_line);
683
+ return true ;
684
+ });
685
+
686
+ if (ret != 0 )
687
+ throw std::runtime_error (" failed to execute scanner tool" );
688
+
699
689
if (current_file != " " ) observer->item_finished ();
700
- fclose (cmd_out);
701
- #endif
702
690
703
691
return data;
704
692
}
0 commit comments