Skip to content

Conversation

@lygstate
Copy link

@lygstate lygstate commented Aug 26, 2025

The script for comment convert is

import sys
import re

# Example Python script to convert single-line comments to block comments
def convert_slashes_to_block(code_str: bytes) -> bytes:
    """
    Converts consecutive C-style single-line '//' comments into a single
    multi-line '/* ... */' block comment.

    Args:
        code_str: A string containing C-style code.

    Returns:
        The modified string with block comments.
    """
    lines = code_str.split(b'\n')
    lines_with_double_slash = []
    slash_pos = -1
    lines_final = []
    for line in lines:
        need_append = False
        if line.strip().startswith(b'//') and not line.strip().startswith(b'///'):
            slash_pos_new = line.find(b'//')
            if (slash_pos == -1):
                slash_pos = slash_pos_new
                need_append = True
            elif slash_pos_new == slash_pos:
                need_append = True
        if need_append:
            lines_with_double_slash.append(line)
        else:
            if (len(lines_with_double_slash) > 0):
                if (len(lines_with_double_slash) == 1):
                    first_line = lines_with_double_slash[0]
                    comment_line = first_line[:slash_pos] + b'/* ' + first_line[slash_pos + 2:].strip() + b' */'
                    lines_final.append(comment_line)
                else:
                    first_line = lines_with_double_slash[0]
                    spaces = first_line[:slash_pos]
                    comment_line = spaces + b'/*'
                    lines_final.append(comment_line)
                    for slash_line in lines_with_double_slash:
                        comment_line = spaces + b' *' + slash_line[slash_pos + 2:]
                        lines_final.append(comment_line)
                    lines_final.append(spaces + b' */')
                lines_with_double_slash = []
                slash_pos = -1
            current_slash_pos = line.rfind(b' // ')
            if current_slash_pos != -1 and line[current_slash_pos:].find(b'"') == -1:
                lines_final.append(line[:current_slash_pos] + b' /* ' + line[current_slash_pos + 4:] + b' */')
            else:
                lines_final.append(line)
    return b'\n'.join(lines_final)

def convert_comments(filename):
    with open(filename, 'rb') as f:
        content = convert_slashes_to_block(f.read())
    with open(filename, 'wb') as f:
        f.write(content)
if len(sys.argv) > 1:
    print("First argument:", sys.argv[1])
    convert_comments(sys.argv[1])

@lygstate lygstate force-pushed the C90_support branch 4 times, most recently from f9edff8 to 5286002 Compare August 26, 2025 03:41
@eyalroz
Copy link
Owner

eyalroz commented Aug 26, 2025

I will not have time to properly look at this before mid-September.

But - I'll already say that replacing #if conditions with #ifdef's is unacceptable.

@lygstate lygstate force-pushed the C90_support branch 2 times, most recently from 1027bcb to b4d4292 Compare August 26, 2025 07:17
…an C++/C99-style single-line comments (`//`) by script

Rest will be convert manually

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
…an C++/C99-style single-line comments (`//`) manually

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
@lygstate
Copy link
Author

@eyalroz ping for take a look

Copy link
Owner

@eyalroz eyalroz left a comment

Choose a reason for hiding this comment

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

Mosty ok, but please note my requested changes and questions.

@lygstate lygstate force-pushed the C90_support branch 4 times, most recently from c1e6b43 to 28f37bd Compare September 27, 2025 16:38
Fixes ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
Fixes inline for C90
Enable Werror=declaration-after-statement for not breakage C90 in future

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
@lygstate
Copy link
Author

@eyalroz done, ping for review

@eyalroz eyalroz merged commit 88ef0ab into eyalroz:develop Oct 11, 2025
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants