!18567 Fix a bug of replace linefeed if \r\n

Merge pull request !18567 from huanghui/fix-replace-linefeed-charactor
This commit is contained in:
i-robot 2021-06-21 19:48:23 +08:00 committed by Gitee
commit 9b4e654df0
1 changed files with 50 additions and 29 deletions

View File

@ -24,8 +24,8 @@
#include "ir/graph_utils.h"
namespace mindspore {
// namespace to support debug trace information
namespace trace {
namespace {
std::vector<DebugInfoPtr> GetSourceCodeDebugInfoVec(DebugInfoPtr debug_info, bool is_debug = false) {
std::vector<DebugInfoPtr> debug_with_loc_vec;
while (debug_info != nullptr) {
@ -41,9 +41,35 @@ std::vector<DebugInfoPtr> GetSourceCodeDebugInfoVec(DebugInfoPtr debug_info, boo
return debug_with_loc_vec;
}
void ReplaceLinefeed(std::string *txt) {
MS_EXCEPTION_IF_NULL(txt);
std::vector<int> erases;
constexpr char cr = '\r';
constexpr char lf = '\n';
constexpr char slash = '/';
for (size_t i = 0; i < txt->length(); i++) {
if ((*txt)[i] == lf) {
(*txt)[i] = slash;
}
if ((*txt)[i] == cr) {
(*txt)[i] = slash;
if (i + 1 < txt->length() && (*txt)[i + 1] == lf) {
erases.emplace_back(i + 1);
i++;
}
}
}
constexpr auto n = 1;
std::reverse(erases.begin(), erases.end());
for (const auto &index : erases) {
txt->erase(index, n);
}
}
} // namespace
DebugInfoPtr GetSourceCodeDebugInfo(const DebugInfoPtr &info) {
auto debug_with_loc_vec = GetSourceCodeDebugInfoVec(info);
if (debug_with_loc_vec.size() > 0) {
if (!debug_with_loc_vec.empty()) {
return debug_with_loc_vec[0];
} else {
return info;
@ -61,10 +87,10 @@ std::string GetDebugInfo(const DebugInfoPtr &info, SourceLineTip tip) {
return "";
}
// a trace info identifies a node transform, so we can trace the node transform through
// a link of trace info and debug info
// A trace info identifies a node transform, so we can trace the node transform through
// A link of trace info and debug info
std::string GetInfoWithAction(const std::vector<DebugInfoPtr> &info_vec, SourceLineTip tip) {
if (info_vec.size() < 1) {
if (info_vec.empty()) {
return "";
}
if (info_vec.size() == 1) {
@ -73,10 +99,10 @@ std::string GetInfoWithAction(const std::vector<DebugInfoPtr> &info_vec, SourceL
std::string traced_info = info_vec[0]->location()->ToString(tip);
for (size_t i = 1; i < info_vec.size(); i++) {
auto action_name = info_vec[i - 1]->trace_info()->GetActionBetweenNode(info_vec[i]);
if (action_name == "") {
if (action_name.empty()) {
break;
}
traced_info = traced_info + action_name + info_vec[i]->location()->ToString(tip);
traced_info += action_name + info_vec[i]->location()->ToString(tip);
}
return traced_info;
}
@ -86,7 +112,7 @@ std::string GetTracedDebugInfo(const DebugInfoPtr &info, SourceLineTip tip) {
return "";
}
auto info_vec = GetSourceCodeDebugInfoVec(info);
if (info_vec.size() == 0) {
if (info_vec.empty()) {
return "";
} else if (info_vec.size() == 1) {
return info_vec[0]->location()->ToString(tip);
@ -123,15 +149,14 @@ std::string DumpSourceLines(AnfNode *node) {
if (!info_vec.empty()) {
oss << "\n";
}
for (auto info : info_vec) {
for (const auto &info : info_vec) {
MS_EXCEPTION_IF_NULL(info);
auto loc = info->location();
if (loc == nullptr) {
continue;
}
auto loc_str = loc->ToString(kSourceLineTipDiscard);
std::replace(loc_str.begin(), loc_str.end(), '\r', '/');
std::replace(loc_str.begin(), loc_str.end(), '\n', '/');
ReplaceLinefeed(&loc_str);
oss << loc_str << "\n";
}
@ -144,17 +169,16 @@ std::string DumpSourceLines(AnfNode *node) {
return oss.str();
}
oss << "Corresponding forward node candidate:\n";
for (auto iter = primal_debug_infos.begin(); iter != primal_debug_infos.end(); iter++) {
info_vec = GetSourceCodeDebugInfoVec(*iter);
for (auto info : info_vec) {
for (auto &primal_debug_info : primal_debug_infos) {
info_vec = GetSourceCodeDebugInfoVec(primal_debug_info);
for (const auto &info : info_vec) {
MS_EXCEPTION_IF_NULL(info);
auto loc = info->location();
if (loc == nullptr) {
continue;
}
auto loc_str = loc->ToString(kSourceLineTipDiscard);
std::replace(loc_str.begin(), loc_str.end(), '\r', '/');
std::replace(loc_str.begin(), loc_str.end(), '\n', '/');
ReplaceLinefeed(&loc_str);
oss << loc_str << "\n";
}
}
@ -168,15 +192,14 @@ std::vector<std::string> GetSourceLineList(const AnfNodePtr &node) {
return result;
}
auto info_vec = GetSourceCodeDebugInfoVec(node->debug_info());
for (auto info : info_vec) {
for (const auto &info : info_vec) {
MS_EXCEPTION_IF_NULL(info);
auto loc = info->location();
if (loc == nullptr) {
continue;
}
auto loc_str = loc->ToString(kSourceLineTipDiscard);
std::replace(loc_str.begin(), loc_str.end(), '\r', '/');
std::replace(loc_str.begin(), loc_str.end(), '\n', '/');
ReplaceLinefeed(&loc_str);
result.push_back(loc_str + "\n");
}
if (!node->isa<CNode>()) {
@ -187,18 +210,17 @@ std::vector<std::string> GetSourceLineList(const AnfNodePtr &node) {
if (primal_debug_infos.empty()) {
return result;
}
result.push_back("Corresponding forward node candidate:\n");
for (auto iter = primal_debug_infos.begin(); iter != primal_debug_infos.end(); iter++) {
info_vec = GetSourceCodeDebugInfoVec(*iter);
for (auto info : info_vec) {
result.emplace_back("Corresponding forward node candidate:\n");
for (auto &primal_debug_info : primal_debug_infos) {
info_vec = GetSourceCodeDebugInfoVec(primal_debug_info);
for (const auto &info : info_vec) {
MS_EXCEPTION_IF_NULL(info);
auto loc = info->location();
if (loc == nullptr) {
continue;
}
auto loc_str = loc->ToString(kSourceLineTipDiscard);
std::replace(loc_str.begin(), loc_str.end(), '\r', '/');
std::replace(loc_str.begin(), loc_str.end(), '\n', '/');
ReplaceLinefeed(&loc_str);
result.push_back(loc_str + "\n");
}
}
@ -212,7 +234,7 @@ std::vector<LocationPtr> GetSourceLocationList(const AnfNodePtr &node) {
return result;
}
auto info_vec = GetSourceCodeDebugInfoVec(node->debug_info());
for (auto info : info_vec) {
for (const auto &info : info_vec) {
MS_EXCEPTION_IF_NULL(info);
if (info->location() != nullptr) {
result.emplace_back(info->location());
@ -228,7 +250,7 @@ std::string GetDebugTraceInfo(const AnfNodePtr &node, bool is_debug) {
}
auto info_vec = GetSourceCodeDebugInfoVec(node->debug_info(), is_debug);
std::ostringstream oss;
for (auto info : info_vec) {
for (const auto &info : info_vec) {
MS_EXCEPTION_IF_NULL(info);
auto trace_info = info->trace_info();
if (trace_info != nullptr) {
@ -240,8 +262,7 @@ std::string GetDebugTraceInfo(const AnfNodePtr &node, bool is_debug) {
continue;
}
auto loc_str = loc->ToString(kSourceLineTipDiscard);
std::replace(loc_str.begin(), loc_str.end(), '\r', '/');
std::replace(loc_str.begin(), loc_str.end(), '\n', '/');
ReplaceLinefeed(&loc_str);
oss << loc_str << "\n";
}
return oss.str();