-
Notifications
You must be signed in to change notification settings - Fork 50
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
Касьянычев М.П. 381806-1 #50
base: master
Are you sure you want to change the base?
Касьянычев М.П. 381806-1 #50
Conversation
|
||
void run(const MatchFinder::MatchResult &Result) override { | ||
// Your code goes here | ||
|
||
const auto *Item = Result.Nodes.getNodeAs<CStyleCastExpr>("cast"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if Item
is a null pointer? How to protect the program from crashing in such cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Необходимо добавить проверку, что Item не пуст.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by emptiness?
CastCallBack(Rewriter& rewriter) { | ||
// Your code goes here | ||
}; | ||
CastCallBack(Rewriter& rewriter) : rewriter_(rewriter) { }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is ;
required here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нет, можно убрать.
Item->getSubExprAsWritten()->getBeginLoc()); | ||
|
||
StringRef DestTypeString = | ||
Lexer::getSourceText(CharSourceRange::getTokenRange(Item->getLParenLoc().getLocWithOffset(1), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please describe what the getSourceText()
function does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Формирует строку для источника в диапазоне CharSourceRange.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the range getTokenRange(res->getLParenLoc().getLocWithOffset(1), res->getRParenLoc().getLocWithOffset(-1))
Result.Context->getLangOpts()); | ||
|
||
std::string str = ("static_cast<" + DestTypeString + ">").str(); | ||
const Expr *SubExpr = Item->getSubExprAsWritten()->IgnoreImpCasts(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please describe the reason for using IgnoreImpCasts()
here. What would be the difference if we didn't use this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IgnoreImpCasts() предназначен для игнорирования всех неявных приведений типов. Но в нашей задаче таких приведений нет, следовательно, даже если не использовать этот метод, разницы не будет.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you try to print the AST of the test.cpp
example, you will see that you have several ImplicitCastExpr
nodes:
Depending on which approaches you use to get the subexpression node, you will get different result nodes for the test.cpp
example:
Code:
// getSubExpr() retrieves the first cast subexpression
llvm::outs() << "getSubExpr(): " << castNode->getSubExpr()->getStmtClassName();
// 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.
llvm::outs() << "getSubExprAsWritten(): " << castNode->getSubExprAsWritten()->getStmtClassName();
// getSubExpr() retrieves the first cast subexpression.
// ignoreImpCasts() skips past any implicit casts which might surround this expression until
// reaching a fixed point. It skips the following AST nodes: ImplicitCastExpr, FullExpr.
llvm::outs() << "getSubExpr()->ignoreImpCasts(): " << castNode->getSubExpr()->IgnoreImpCasts()->getStmtClassName();
Output:
getSubExpr(): ImplicitCastExpr
getSubExprAsWritten(): DeclRefExpr
getSubExpr()->ignoreImpCasts(): DeclRefExpr
No description provided.