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-1 #29

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

Conversation

Zayyya
Copy link

@Zayyya Zayyya commented May 26, 2022

No description provided.


_rewriter.ReplaceText(lParen, 1, "static_cast<");
_rewriter.ReplaceText(rParen, 1, ">(");
_rewriter.InsertTextAfterToken(castExpr->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 how to change the program code to get the following result:

int main() {
    float fooooo;
    int i = static_cast<int>(fooooo)      ;
    return 0;
}

for the origin test.cpp:

int main() {

    float fooooo;

    int i = (int)          fooooo      ;

    return 0;

}

Copy link
Author

Choose a reason for hiding this comment

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

Remove the opening bracket from _rewriter.ReplaceText(rParen, 1, ">(");
To add it, getSubExprAsWritten()->getBeginLoc() is usually used to get the initial position of the variable, then "(" is inserted at this token.

// Your code goes here
if (auto castExpr = Result.Nodes.getNodeAs<clang::CStyleCastExpr>("cast")) {
auto lParen = castExpr->getLParenLoc();
auto rParen = castExpr->getRParenLoc();
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 difference between the use of getRParenLoc() and getSubExprAsWritten()->getBeginLoc().

Copy link
Author

Choose a reason for hiding this comment

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

In this case:
getRParenLoc() gets the location of the closing brace before the variable.
getSubExprAsWritten()->getBeginLoc() gets the starting location of the variable, there may be spaces between the closing brace and the variable.

@ElizJogar
Copy link
Owner

  • How to prevent parentheses from being added if they are already present in the (int)(f) code?
  • How to prevent changing the C style cast code for a special case - when we want to suppress warnings about unused variables?

@Zayyya
Copy link
Author

Zayyya commented May 28, 2022

  1. You can check the variable for the presence of brackets around it, for this you can use the asi function, it is a template and checks the parameter for belonging to the passed template type.
    In my case, you can check like this:
if (!isa<ParenExpr>(castExpr->getSubExprAsWritten())) {
   // Main code goes here
}
  1. It is necessary to find out the type of operation required for the conversion, for this we use the getCastKind () function, calling it on castExpr. If the type is void, then you do not need to make changes to the source code.
if (castExpr->getCastKind() != CK_ToVoid) {
   // Main code goes here
}

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