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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ using namespace clang::ast_matchers;
using namespace clang::tooling;

class CastCallBack : public MatchFinder::MatchCallback {
private: Rewriter &rewriter;
public:
CastCallBack(Rewriter& rewriter) {
CastCallBack(Rewriter& rewriter_):rewriter(rewriter_) {
// 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; }

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.

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.

rewriter.InsertText(leftParenLoc,"static_cast<");
rewriter.InsertText(rigthParenLoc,">(");
rewriter.InsertText(endLine,")");
}
};

Expand Down