-
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 #53
base: master
Are you sure you want to change the base?
Conversation
auto ReplaceRange = CharSourceRange::getCharRange( | ||
CastExpr->getLParenLoc(), CastExpr->getSubExprAsWritten()->getBeginLoc()); | ||
auto castIgnore = CastExpr->getSubExprAsWritten()->IgnoreImpCasts(); | ||
if (!isa<ParenExpr>(castIgnore)) { |
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 does this check mean? Please describe examples of when this check returns true
and when this check returns false
.
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.
Данная проверка нужна в тех случаях, когда переменная после static_cast стоит не в скобках.
Если убрать проверку получается: int i = static_castf
А нужно: int i = static_cast(f)
Если проверка возвращает истину int i = (int)f, то добавляется открывающаяся и закрывающаяся скобки до и после переменной.
Если проверка возвращает ложь int i = (int)(f), в этом случае уже есть скобки и ничего добавлять не надо.
std::string replaceText = ("static_cast<" + DestTypeString + ">").str(); | ||
auto ReplaceRange = CharSourceRange::getCharRange( | ||
CastExpr->getLParenLoc(), CastExpr->getSubExprAsWritten()->getBeginLoc()); | ||
auto castIgnore = CastExpr->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 why you are using IgnoreImpCasts()
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.
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
auto castIgnore = CastExpr->getSubExprAsWritten()->IgnoreImpCasts(); | ||
if (!isa<ParenExpr>(castIgnore)) { | ||
replaceText.push_back('('); | ||
_rewriter.InsertText(Lexer::getLocForEndOfToken(castIgnore->getEndLoc(), |
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 of using the Lexer::getLocForEndOfToken()
function here.
What would be the difference if SubExpr->getEndLoc().getLocOffset(1)
was used 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.
С помощью Lexer::getLocForEndOfToken мы можем произвести захват всего токена,
а subExpr->getEndLoc().getLocWithOffset(1) позволяет захватить переменные состоящие из 1 символа.
SourceManager &SM = *Result.SourceManager; | ||
if (CastExpr->getExprLoc().isMacroID()) | ||
return; | ||
if (CastExpr->getCastKind() == CK_ToVoid) |
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 this check does and why it is there.
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.
Здесь используется isMacroID(), то есть проверка приведение в макросе, если проверка сработает, то дальнейшие действия выполняться не будут.
CK_ToVoid - если проверка выполнится, то приведение к войду будет проигнорировано
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.
Describing the reason for having these checks here would be a plus.
No description provided.