log: Use `dict` instead of `std::vector<std::pair>` for `log_expect_{error, warning...
authorAlberto Gonzalez <boqwxp@airmail.cc>
Thu, 14 May 2020 22:52:07 +0000 (22:52 +0000)
committerAlberto Gonzalez <boqwxp@airmail.cc>
Fri, 15 May 2020 00:55:32 +0000 (00:55 +0000)
kernel/log.cc
kernel/log.h
passes/cmds/logger.cc

index a21ba480a6d6313fe6c0d1c743aba664d9d09770..104bee07883f0e512f4abbcaf03d6c01f75ba818 100644 (file)
@@ -42,7 +42,7 @@ std::vector<FILE*> log_files;
 std::vector<std::ostream*> log_streams;
 std::map<std::string, std::set<std::string>> log_hdump;
 std::vector<YS_REGEX_TYPE> log_warn_regexes, log_nowarn_regexes, log_werror_regexes;
-std::vector<std::pair<YS_REGEX_TYPE,LogExpectedItem>> log_expect_log, log_expect_warning, log_expect_error;
+dict<std::string, LogExpectedItem> log_expect_log, log_expect_warning, log_expect_error;
 std::set<std::string> log_warnings, log_experimentals, log_experimentals_ignored;
 int log_warnings_count = 0;
 int log_warnings_count_noexpect = 0;
@@ -181,7 +181,7 @@ void logv(const char *format, va_list ap)
                                                log_warning("Found log message matching -W regex:\n%s", str.c_str());
 
                                for (auto &item : log_expect_log)
-                                       if (YS_REGEX_NS::regex_search(linebuffer, item.first))
+                                       if (YS_REGEX_NS::regex_search(linebuffer, item.second.pattern))
                                                item.second.current_count++;
 
                                linebuffer.clear();
@@ -256,7 +256,7 @@ static void logv_warning_with_prefix(const char *prefix,
 
                bool warning_match = false;
                for (auto &item : log_expect_warning)
-                       if (YS_REGEX_NS::regex_search(message, item.first)) {
+                       if (YS_REGEX_NS::regex_search(message, item.second.pattern)) {
                                item.second.current_count++;
                                warning_match = true;
                        }
@@ -349,7 +349,7 @@ static void logv_error_with_prefix(const char *prefix,
                log_error_atexit();
 
        for (auto &item : log_expect_error)
-               if (YS_REGEX_NS::regex_search(log_last_error, item.first))
+               if (YS_REGEX_NS::regex_search(log_last_error, item.second.pattern))
                        item.second.current_count++;
 
        if (check_expected_logs)
@@ -672,31 +672,31 @@ void log_check_expected()
        for (auto &item : log_expect_warning) {
                if (item.second.current_count == 0) {
                        log_warn_regexes.clear();
-                       log_error("Expected warning pattern '%s' not found !\n", item.second.pattern.c_str());
+                       log_error("Expected warning pattern '%s' not found !\n", item.first.c_str());
                }
                if (item.second.current_count != item.second.expected_count) {
                        log_warn_regexes.clear();
                        log_error("Expected warning pattern '%s' found %d time(s), instead of %d time(s) !\n",
-                               item.second.pattern.c_str(), item.second.current_count, item.second.expected_count);
+                               item.first.c_str(), item.second.current_count, item.second.expected_count);
                }
        }
 
        for (auto &item : log_expect_log) {
                if (item.second.current_count == 0) {
                        log_warn_regexes.clear();
-                       log_error("Expected log pattern '%s' not found !\n", item.second.pattern.c_str());
+                       log_error("Expected log pattern '%s' not found !\n", item.first.c_str());
                }
                if (item.second.current_count != item.second.expected_count) {
                        log_warn_regexes.clear();
                        log_error("Expected log pattern '%s' found %d time(s), instead of %d time(s) !\n",
-                               item.second.pattern.c_str(), item.second.current_count, item.second.expected_count);
+                               item.first.c_str(), item.second.current_count, item.second.expected_count);
                }
        }
 
        for (auto &item : log_expect_error)
                if (item.second.current_count == item.second.expected_count) {
                        log_warn_regexes.clear();
-                       log("Expected error pattern '%s' found !!!\n", item.second.pattern.c_str());
+                       log("Expected error pattern '%s' found !!!\n", item.first.c_str());
                        #ifdef EMSCRIPTEN
                                throw 0;
                        #elif defined(_MSC_VER)
@@ -707,7 +707,7 @@ void log_check_expected()
                } else {
                        display_error_log_msg = false;
                        log_warn_regexes.clear();
-                       log_error("Expected error pattern '%s' not found !\n", item.second.pattern.c_str());
+                       log_error("Expected error pattern '%s' not found !\n", item.first.c_str());
                }
 }
 
index dee5d44d78969ce2545b22a3310229b0467c4b65..54bfcb911d91fcc9aaa15f0009d8357f33cb9e50 100644 (file)
@@ -202,19 +202,16 @@ void log_flush();
 
 struct LogExpectedItem
 {
-       LogExpectedItem(std::string pattern, int expected) :
-               expected_count(expected),
-               current_count(0),
-               pattern(pattern)
-       {
-       }
+       LogExpectedItem(const YS_REGEX_TYPE &pat, int expected) :
+                       pattern(pat), expected_count(expected), current_count(0) {}
+       LogExpectedItem() : expected_count(0), current_count(0) {}
 
+       YS_REGEX_TYPE pattern;
        int expected_count;
        int current_count;
-       std::string pattern;
 };
 
-extern std::vector<std::pair<YS_REGEX_TYPE,LogExpectedItem>> log_expect_log, log_expect_warning, log_expect_error;
+extern dict<std::string, LogExpectedItem> log_expect_log, log_expect_warning, log_expect_error;
 void log_check_expected();
 
 const char *log_signal(const RTLIL::SigSpec &sig, bool autoint = true);
index 50b89d92fb065ca70a09eebb3eb8971f7f23385e..c9532eced8499de88d727e597030119fe855f0fb 100644 (file)
@@ -159,39 +159,12 @@ struct LoggerPass : public Pass {
                                        log_cmd_error("Expected error message occurrences must be 1 !\n");
                                log("Added regex '%s' for warnings to expected %s list.\n", pattern.c_str(), type.c_str());
                                try {
-                                       if (type=="error") {
-                                               auto it = log_expect_error.begin();
-                                               auto ie = log_expect_error.end();
-                                               for (; it != ie; it++)
-                                                       if (it->second.pattern == pattern) {
-                                                               it->second.expected_count = count;
-                                                               break;
-                                                       }
-                                               if (it == ie)
-                                                       log_expect_error.emplace_back(YS_REGEX_COMPILE(pattern), LogExpectedItem(pattern, count));
-                                       }
-                                       else if (type=="warning") {
-                                               auto it = log_expect_warning.begin();
-                                               auto ie = log_expect_warning.end();
-                                               for (; it != ie; it++)
-                                                       if (it->second.pattern == pattern) {
-                                                               it->second.expected_count = count;
-                                                               break;
-                                                       }
-                                               if (it == ie)
-                                                       log_expect_warning.emplace_back(YS_REGEX_COMPILE(pattern), LogExpectedItem(pattern, count));
-                                       }
-                                       else if (type=="log") {
-                                               auto it = log_expect_log.begin();
-                                               auto ie = log_expect_log.end();
-                                               for (; it != ie; it++)
-                                                       if (it->second.pattern == pattern) {
-                                                               it->second.expected_count = count;
-                                                               break;
-                                                       }
-                                               if (it == ie)
-                                                       log_expect_log.emplace_back(YS_REGEX_COMPILE(pattern), LogExpectedItem(pattern, count));
-                                       }
+                                       if (type == "error")
+                                               log_expect_error[pattern] = LogExpectedItem(YS_REGEX_COMPILE(pattern), count);
+                                       else if (type == "warning")
+                                               log_expect_warning[pattern] = LogExpectedItem(YS_REGEX_COMPILE(pattern), count);
+                                       else if (type == "log")
+                                               log_expect_log[pattern] = LogExpectedItem(YS_REGEX_COMPILE(pattern), count);
                                        else log_abort();
                                }
                                catch (const YS_REGEX_NS::regex_error& e) {