Skip to content

fix #337 - Multiline comment not handled properly #431

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

clock999
Copy link

@clock999 clock999 commented May 23, 2025

To fix the issue:
#337

@clock999
Copy link
Author

clock999 commented May 23, 2025

Here are the corresponding links about this issue:

https://sourceforge.net/p/cppcheck/discussion/general/thread/37c6eba151/
https://github.com/danmar/simplecpp/issues/337

@danmar
Copy link
Owner

danmar commented Jun 22, 2025

The PR title is misleading. The fix is to handle line splicing differently. simplecpp doesn't contain any uninitvar checking and there is no uninitialized variable usage I hope.

#337 is much more proper.

@clock999 clock999 changed the title fix #12379: FN: uninitvar due to line splicing fix #337 - Multiline comment not handled properly Jun 22, 2025
@@ -788,6 +788,35 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
while (stream.good() && ch != '\r' && ch != '\n') {
Copy link
Owner

Choose a reason for hiding this comment

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

I am thinking if something like this could work:

    char lastChar = ' ';
    while (stream.good() && (lastChar == '\\' || (ch != '\r' && ch != '\n')) {
        currentToken += ch;
        lastChar = ch;
        ch = stream.readChar();
    }

@@ -434,7 +434,7 @@ static void comment_multiline()
const char code[] = "#define ABC {// \\\n"
"}\n"
"void f() ABC\n";
ASSERT_EQUALS("\n\nvoid f ( ) { }", preprocess(code));
ASSERT_EQUALS("\n\nvoid f ( ) {", preprocess(code));
Copy link
Owner

Choose a reason for hiding this comment

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

is this the only line splicing test we have?

I think we should test what happens when there are multiple newlines after the :

//  x \\\n
\n
\n
...

and an explicit test for \r\n:

//  x \\\r\n
...

and I wonder what the behavior (according to standard / according to gcc+msvc+etc) is if there is space after the backslash:

// x \\ \n
...

Copy link
Owner

Choose a reason for hiding this comment

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

I don't remember exactly how the location is adjusted right now. Will the locations below the line splicing comment be correct? i.e.:

// hello \
world
x=1;

will x=1; line number be 3?

tmp_ch = stream.readChar();
}
if(!stream.good()) {
if(!tmp.empty()) {
Copy link
Owner

Choose a reason for hiding this comment

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

this if (!tmp.empty()) is redundant.

}

ch = tmp_ch;
continue;
Copy link
Owner

Choose a reason for hiding this comment

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

this continue is not need right?

tmp_ch = stream.readChar();
}
if(!stream.good()) {
break;
Copy link
Owner

Choose a reason for hiding this comment

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

this break can be removed right since the outer loop will break if stream is not good.

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.

2 participants