#Copyright (c) 2025 Huawei Technologies Co., Ltd.
#This program is free software, you can redistribute it and/or modify it under the terms and conditions of
#CANN Open Software License Agreement Version 2.0 (the "License").
#Please refer to the License for details. You may not use this file except in compliance with the License.
#THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
#INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
#See LICENSE in the root of the software repository for the full text of the License.
import sys
sys.stdout.reconfigure(encoding='utf-8')
p = r'D:\AI\Vector_error_testing_angent\skills\ascendc-fuzz-cpp-gen\scripts\gen_error_cpp.py'
with open(p, 'rb') as f:
data = f.read()
# Problem: emit_raw lines contain Python source like:
# emit_raw('LOG_PRINT("...\\n");')
# where \\n in Python source = 2 actual chars: backslash + n
# which becomes C++ output: LOG_PRINT("...\n") <- CORRECT!
#
# BUT some lines have FOUR chars: backslash backslash n
# emit_raw('LOG_PRINT("...\\\\n");')
# which becomes C++ output: LOG_PRINT("...\\n") <- WRONG, literal backslash-n
#
# We need to find and fix lines where emit_raw has too many backslashes before n
lines = data.split(b'\r\n')
fixed = 0
for i, line in enumerate(lines):
if b'emit_raw' not in line:
continue
# Count consecutive backslashes before 'n' in string arguments
# Replace occurrences of \\n (two backslashes + n) with single backslash + n
# but ONLY when it's an even number > 1
# Simple approach: replace all \\\\n with \\n in emit_raw lines
old = line
# Replace 4-backslash+latin-n with 2-backslash+latin-n
# In raw bytes: find the 2-byte sequence 0x5C 0x5C 0x6E and replace with 0x5C 0x6E
line = line.replace(b'\\forward slash', b'xxx') # test first
# OK let me just do it character by character
lines = data.split(b'\r\n')
new_lines = []
fixed = 0
for line in lines:
if b'emit_raw' in line or (b'emit(' in line and b'n' in line):
# Process: find all backslash sequences before 'n' and reduce by half
result = bytearray()
i = 0
while i < len(line):
if line[i:i+1] == b'\\' and i + 1 < len(line) and line[i+1:i+2] == b'\\' and i + 2 < len(line) and line[i+2:i+3] == b'n':
# Found \\\\n -> reduce to \\n
result.extend(b'\\n')
i += 3
fixed += 1
else:
result.append(line[i])
i += 1
new_lines.append(bytes(result))
else:
new_lines.append(line)
new_data = b'\r\n'.join(new_lines)
print(f'Fixed {fixed} occurrences')
# Verify
count = new_data.count(b'\\forward')
if count > 0:
print('ERROR: test replace still present')
sys.exit(1)
# Check remaining \\n patterns
# We want: single \\n (1 backslash + n) in C++ output lines
# NOT: double \\n (2 backslashes + n)
remaining = 0
for line in new_data.split(b'\r\n'):
if b'emit_raw' in line:
# After fix, should have single \x5c\x6e not double \x5c\x5c\x6e
idx = 0
while True:
idx = line.find(b'\x5c\x6e', idx) # backslash-n
if idx < 0:
break
# Check if preceded by another backslash
if idx > 0 and line[idx-1:idx] == b'\x5c':
remaining += 1
idx += 2
print(f'Remaining double-backslash+n in emit_raw: {remaining}')
with open(p, 'wb') as f:
f.write(new_data)
print('Written')