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

Лабораторная работа №3. Феоктистов Андрей. 381906-3 #36

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

feoandrew
Copy link

No description provided.

@feoandrew feoandrew changed the title Complete c-style-cast-checker task Лабораторная работа №3. Феоктистов Андрей. 381906-3 May 26, 2022
main.cpp Outdated
cast_str=("static_cast<"+type+">(").str(); // add left bracket

myrewriter.InsertText(
Lexer::getLocForEndOfToken(
Copy link
Owner

Choose a reason for hiding this comment

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

Please describe the reason of using theLexer::getLocForEndOfToken() function here.
What would be the difference if rewriter.InsertText(styleCastExpr->getEndLoc().getLocOffset(1), ")"); was used here?
What would be the difference if rewriter.InsertTextAfterToken(styleCastExpr->getEndLoc(), ")"); was used here?

Copy link
Author

Choose a reason for hiding this comment

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

If we use rewriter.InsertText(styleCastExpr->getEndLoc().getLocOffset(1), ")"); then ")" will be inserted immediately after expr + 1(offset) if the variable name is longer than one character we will get wrong result.

i used Lexer::getLocForEndOfToken() to get the position after last token in the line;

Now i see that i could use rewriter.InsertTextAfterToken(expr->getEndLoc(), ")"); instead cause it gives the same result.

main.cpp Outdated

myrewriter.InsertText(
Lexer::getLocForEndOfToken(
expr->getSubExprAsWritten()->IgnoreImpCasts()->getEndLoc(),
Copy link
Owner

Choose a reason for hiding this comment

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

Please describe why you are using here getSubExprAsWritten() and IgnoreImpCasts().
What would be the difference with the use of getSubExprAsWritten()->IgnoreImpCasts(), getSubExpr()->IgnoreImpCasts() and getSubExprAsWritten()?

Copy link
Author

Choose a reason for hiding this comment

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

getSubExprAsWritten() retrieves the cast subexpression as it was written in the source code, looking through any implicit casts or other intermediate nodes introduced by semantic analysis.
getSubExpr() retrieves the first cast subexpression and IgnoreImpCasts() skips past any implicit casts which might surround this expression until reaching a fixed point.

So it was no sense to use getSubExprAsWritten() and IgnoreImpCasts(), we can use just getSubExprAsWritten().

But we must use IgnoreImpCasts() with getSubExpr() cause if we will write only getSubExpr() we can get unexpected result.

getSubExpr()->IgnoreImpCasts() and getSubExprAsWritten() will give the same result.

@ElizJogar
Copy link
Owner

How to prevent changing the C style cast code for a special case - when we want to suppress warnings about unused variables?

@feoandrew
Copy link
Author

How to prevent changing the C style cast code for a special case - when we want to suppress warnings about unused variables?

-> To prevent changing the C style cast in this case we can use this check if (expr->getCastKind() == CK_ToVoid) and do nothing if it's returns true.

@feoandrew feoandrew requested a review from ElizJogar May 28, 2022 18:30
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