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 #30

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

Conversation

R1der-on-The-Storm
Copy link

No description provided.

@R1der-on-The-Storm
Copy link
Author

изображение

@R1der-on-The-Storm R1der-on-The-Storm changed the title Complete c-style-cast-checker task Лабораторная работа №3. Зайцев Александр 381906-1 May 26, 2022
rewriter.RemoveText(leftParenLoc,1);
rewriter.RemoveText(rigthParenLoc,1);
auto &sourceManager=*Result.SourceManager;
auto endLine=Lexer::getLocForEndOfToken(StyleCastExpr->getEndLoc(),0,sourceManager,LangOptions());
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.

  1. get::LocForEndOfToken() . Computes the source location just past the end of the token at this source location.
  2. rewriter.InsertText(styleCastExpr->getEndLoc().getLocOffset(1),")"); give us position right after (cast) and getEndLoc().getLocOffset(1) shifts this position by one to the right. If the variable declared in main has a name of two or more symbol and use rewriter.InsertText(styleCastExpr->getEndLoc().getLocOffset(1),")"); . like main we will get m)ain .
    3)rewriter.InsertTextAfterToken(styleCastExpr->getEndLoc(),")"); . If use this we will get the same position and output but different way.

// Your code goes here
};

void run(const MatchFinder::MatchResult &Result) override {
// Your code goes here
const auto *StyleCastExpr=Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
auto leftParenLoc=StyleCastExpr->getLParenLoc();
auto rigthParenLoc=StyleCastExpr->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.

getRParenLos() a special method of the CStyleCastExprclass, this method returns the position where the closing right bracket is located. An object of the CStyleCastExpr class is an indication of a node in the AST tree.
If call the getSubExprAsWritten() method, it will return the desired tree node, ignoring the auxiliary ImplicitCastExpr nodes.
getBeginLoc() is used here to get the start position of this variable. But need to separately call the method of removing the closing right bracket rewriter_.Remove Text(styleCastExpr->getRParenLoc(), 1); .
The main difference is that when using getRParenLoc(), the search for the desired position is carried out using a closing parenthesis, and when using getSubExprAsWritten(), it is carried out using a variable for which the CStyleCast transformation was called.

@ElizJogar
Copy link
Owner

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

// Your code goes here
};

void run(const MatchFinder::MatchResult &Result) override {
// Your code goes here
const auto *StyleCastExpr=Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
Copy link
Owner

Choose a reason for hiding this comment

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

What if StyleCastExpr is a null pointer? How to protect the program from crashing in such cases?

Copy link
Author

Choose a reason for hiding this comment

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

add this if(StyleCastExpr==nullptr) { return; }

@R1der-on-The-Storm
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?
Need to check if(styleCastExpr->getCastKind() == CK_To Void) and if it is successful, do not cast.

@R1der-on-The-Storm
Copy link
Author

How to prevent parentheses from being added if they are already present in the (int)(f) code?
like that
auto rangeVariable = Lexer::getSourceText(CharSourceRange::getTokenRange(
StyleCastExpr->getRParenLoc().getLocWithOffset(1),
StyleCastExpr->getEndLoc()),
*Result.SourceManager,
LangOptions()).str();
auto originalSize = rangeVariable.size();
rangeVariable.erase(
remove_if(
rangeVariable.begin(),
rangeVariable.end(),
[](char symbol) { return symbol == ' '; }),
rangeVariable.end());

    if (rangeVariable.front() != '(' && rangeVariable.back() != ')') {
    	rewriter.ReplaceText(rParenLoc.getLocWithOffset(1), originalSize, "(" + rangeVariable + ")");
    } else {
    	rewriter.ReplaceText(rParenLoc.getLocWithOffset(1), originalSize, rangeVariable);
    }

@R1der-on-The-Storm
Copy link
Author

How to prevent changing the C style cast code in macros?
like that
if(CExpression->getExprLoc().isMacroID()) { return;}

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