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

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

Merged
merged 6 commits into from
Jan 13, 2024

Conversation

firewave
Copy link
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
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 marked this pull request as ready for review January 5, 2023 12:50
@danmar
Copy link
Owner

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
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
lib/path.h Outdated Show resolved Hide resolved
lib/path.h Show resolved Hide resolved
lib/tokenize.cpp Outdated
@@ -4053,7 +4053,7 @@ void Tokenizer::setVarIdPass1()
}

try { /* Ticket #8151 */
decl = setVarIdParseDeclaration(&tok2, variableMap, scopeStack.top().isExecutable, isCPP(), isC());
decl = setVarIdParseDeclaration(&tok2, variableMap, scopeStack.top().isExecutable, isCPP(), isC(), isHeader());
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 have a good feeling for this isHeader(). you are not supposed to check a header directly:

cppcheck file.h

Copy link
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.

lib/tokenize.cpp Outdated
@@ -3563,7 +3563,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const VariableMap& varia
tok2 = tok2->linkAt(1)->next();
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
Owner

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
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
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
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
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
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.

Copy link
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
Owner

@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 danmar:main Jan 13, 2024
64 checks passed
@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