Skip to content

added Path::identify()/Path::isHeader2() and deprecated flawed Path::isCPP(), Path::isC() and Path::isHeader()#4681

Merged
firewave merged 6 commits intocppcheck-opensource:mainfrom
firewave:ext
Jan 13, 2024
Merged

added Path::identify()/Path::isHeader2() and deprecated flawed Path::isCPP(), Path::isC() and Path::isHeader()#4681
firewave merged 6 commits intocppcheck-opensource:mainfrom
firewave:ext

Conversation

@firewave
Copy link
Copy Markdown
Collaborator

@firewave firewave commented Jan 3, 2023

None of the Path::is*() function is working as expected or consistent (detailed via additional unit tests and deprecation comments).
I tried to fix them but that would have made made cases with headers quite awkward to implement so I opted to generate a new function. This also optimized Path::acceptFile() a file.

I am not 100% happy with including settings.h but I didn't see a place to move this to. If #3774 gets merged we might be able to move the enum into keywords.h (that could also be renamed).

I kept the old function in case somebody else is using these (they are in the API after all) and will remove them after the next release.

@firewave
Copy link
Copy Markdown
Collaborator Author

firewave commented Jan 4, 2023

An issue with the existing function is that isC() == false implies isHeader() == true where as isCPP() == true is the other way around and implies isHeader() == true (and even missing an extension).

This was relied on in tokenize.cpp by setVarIdParseDeclaration().

It is quite possible there's a lot of other code which needs to be adjusted but so far the tests did not expose anything else.

@firewave firewave force-pushed the ext branch 2 times, most recently from 92b61c6 to 16ac8c1 Compare January 4, 2023 12:17
@firewave firewave marked this pull request as ready for review January 5, 2023 12:50
@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Jan 7, 2023

An issue with the existing function is that isC() == false implies isHeader() == true where as isCPP() == true is the other way around and implies isHeader() == true (and even missing an extension).

what? if the extension is ".cpp" then both isC() and isHeader() returns false right? and isCPP() returns true.

@firewave
Copy link
Copy Markdown
Collaborator Author

firewave commented Jan 7, 2023

what? if the extension is ".cpp" then both isC() and isHeader() returns false right? and isCPP() returns true.

Yes.

I was referring to the code I adjusted in setVarIdParseDeclaration(). That has handling for headers by checking for isC() == false which meant that everything but a C source file is matched - see 88990ba. Sorry for not linking that before.

@firewave firewave added the merge-after-next-release Wait with merging this PR until after the next Release label Jan 18, 2023
Comment thread lib/path.h Outdated
Comment thread lib/path.h
Comment thread lib/tokenize.cpp Outdated

try { /* Ticket #8151 */
decl = setVarIdParseDeclaration(&tok2, variableMap, scopeStack.top().isExecutable, isCPP(), isC());
decl = setVarIdParseDeclaration(&tok2, variableMap, scopeStack.top().isExecutable, isCPP(), isC(), isHeader());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't have a good feeling for this isHeader(). you are not supposed to check a header directly:

cppcheck file.h

Copy link
Copy Markdown
Collaborator Author

@firewave firewave Jan 30, 2023

Choose a reason for hiding this comment

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

People do that and it will happen with IDE integrations. There's also a push to improve handling of static analysis of headers within clang-tidy and CMake.

Comment thread lib/tokenize.cpp Outdated
continue;
}
if (Token::Match(tok2, "struct|union|enum") || (!c && Token::Match(tok2, "class|typename"))) {
if (Token::Match(tok2, "struct|union|enum") || ((!c || header) && Token::Match(tok2, "class|typename"))) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the || header is unfortunate. the question is what language it is.. and header has no information about it.

With this code a header is always assumed to contain C++ code. So if a header with C code contains some int class; variable declaration then after your changes that class variable will be treated as a C++ keyword.

If the user uses --language=c then this || header code will "override" that.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That whole logic is unfortunate but this keeps the intended behavior intact - I think. At least the tests pass. I will re-visit this when my head is clearer.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The logic is really strange. Out of the box we treat header files as C and bail out but this code wants to treat those as C++ by default. I think we should get rid of that inconsistency. I will try to perform and write a few more tests for this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The changed behavior appears to be consistent and should be applied IMO. I came across this again in #5853 which also exposes the flawed logic.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The new test results are not yet final. Will take another look later today.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Okay - I cleaned up that change a lot and updated the test results.

That looks a bit saner now overall but I am a bit baffled why we are able to parse the C++ code in C code. It should bail out and tell you that it is invalid C code.

@firewave firewave marked this pull request as draft January 30, 2023 15:52
@firewave firewave force-pushed the ext branch 2 times, most recently from c4f5d78 to 8ee166e Compare August 14, 2023 11:00
@firewave firewave removed the merge-after-next-release Wait with merging this PR until after the next Release label Jan 6, 2024
@firewave firewave force-pushed the ext branch 3 times, most recently from 5e7e5d2 to 44b7e3c Compare January 8, 2024 08:49
@firewave firewave marked this pull request as ready for review January 8, 2024 08:49
Copy link
Copy Markdown
Collaborator

@chrchr-github chrchr-github left a comment

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Collaborator

@danmar danmar left a comment

Choose a reason for hiding this comment

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

lgtm

@firewave firewave merged commit d5a054c into cppcheck-opensource:main Jan 13, 2024
@firewave firewave deleted the ext branch January 13, 2024 15:20
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