Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon 6th Fundable Projects 2 No.29】 Fix modernize-concat-nested-namespaces-part-21 #64781

Merged
merged 1 commit into from
Jun 4, 2024

Conversation

walkalone20
Copy link
Contributor

PR Category

Others

PR Types

Bug fixes

Description

修复了一部分嵌套命名空间。由于是自动化修改,因而不必逐条检验。下面为自动修复代码:

import logging
import re
import copy
import os


def parse_clang_tidy_log(file_path, error_string):
    pattern = r"^(.*?):(\d+):(\d+): error: .*?\[" + re.escape(error_string) + r",-warnings-as-errors\]"
    regex = re.compile(pattern)
    result = {}

    with open(file_path, 'r') as file:
        lines = file.readlines()
    for line in lines:
        match = regex.match(line)
        if match:
            file_path, line_number, char_number = match.groups()
            file_path = file_path.replace('/paddle', 'E:/Paddle', 1)
            if file_path[0] != 'E':
                continue
            if file_path not in result:
                result[file_path] = []
            result[file_path].append((int(line_number), int(char_number)))
    return result

def get_namespace(line):
    match = re.match(r"namespace ([\w:]+) \{", line)
    if match:
        return match.group(1)[0:]
    # 匹配形如"}  // namespace *"的字符串,可以有任意数量的空格
    match = re.match(r"\}  // *namespace *([\w:]+)", line)
    if match:
        return match.group(1)[0:]
    
def create_begin_namespace(seqs : list):
    if len(seqs) == 0:
        return ""
    namespace = ""
    for seq in seqs:
        if namespace == "":
            namespace += seq
        else:
            namespace += f"::{seq}"
    return f"namespace {namespace} {{"

def create_end_namespace(seqs : list):
    if len(seqs) == 0:
        return ""
    namespace = ""
    for seq in seqs:
        if namespace == "":
            namespace += seq
        else:
            namespace += f"::{seq}"
    return f"}}  // namespace {namespace} "

def process_file(file_path):
    begin = r"namespace ([\w:]+) \{"
    end = r"\}  // *namespace *([\w:]+)"
    begin_regex = re.compile(begin)
    end_regex = re.compile(end)
    with open(file_path, 'r', encoding="utf-8") as file:
        lines = file.readlines()
    change = True
    for line in lines:
        if line == "namespace {":
            change = False
    if change == False:
        return
    
    stack = []
    layer = []
    line_no = []
    layer.append([])
    line_no.append(-1)
    ind = 0
    
    for line in lines:
        if begin_regex.match(line):
            namespace = get_namespace(line)
            if '::' in namespace:
                change = False
                continue
            # print("begin a namespace: ", namespace)
            stack.append(namespace)
        elif end_regex.match(line):
            namespace = get_namespace(line)
            # print("end a namespace: ", namespace)
            if '::' in namespace:
                change = False
                continue
            if len(stack) == 0 or stack[len(stack) - 1] != namespace:
                ind += 1
                continue
            stack.pop()
        else:
            # print("normal line: ", line)
            line_no.append(ind)
            layer.append(copy.deepcopy(stack))
        ind += 1
        
    if change == False:
        return
    
    newlines = []
    for i in range(1, len(line_no)):
        if layer[i] != layer[i - 1]:
            first_end = create_end_namespace(layer[i - 1])
            if first_end != "":
                newlines.append(first_end + "\n")
            second_begin = create_begin_namespace(layer[i])
            if second_begin != "":
                newlines.append(second_begin + "\n")
        newlines.append(lines[line_no[i]])
    
    if len(layer) > 0 and layer[len(layer) - 1] != []:
        newlines.append(create_end_namespace(layer[len(layer) - 1]) + "\n")
    with open(file_path, 'w', encoding="utf-8") as file:
        file.writelines(newlines)

logging.basicConfig(filename='error.log', level=logging.ERROR)

def find_cc_files(directory):
    for path in directory:
        if not os.path.exists(path):
            logging.error(f"Directory does not exist: {path}")
            continue
        process_file(path)

err = parse_clang_tidy_log(r"F:\Paddle\1.log", "modernize-concat-nested-namespaces")
find_cc_files(err)

相关链接:
#64128

Copy link

paddle-bot bot commented May 30, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label May 30, 2024
Copy link
Contributor

@zhiqiu zhiqiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@luotao1 luotao1 merged commit 0f7ed5e into PaddlePaddle:develop Jun 4, 2024
32 checks passed
@walkalone20 walkalone20 deleted the nested-namespace-part-21 branch June 4, 2024 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants