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

Вагина В.Д. 381806-1 #53

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

Conversation

valeria-vag
Copy link

No description provided.

eshulankina
eshulankina previously approved these changes May 28, 2021
auto ReplaceRange = CharSourceRange::getCharRange(
CastExpr->getLParenLoc(), CastExpr->getSubExprAsWritten()->getBeginLoc());
auto castIgnore = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
if (!isa<ParenExpr>(castIgnore)) {
Copy link
Owner

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.

Copy link
Author

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();
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 why you are using IgnoreImpCasts() here.

Copy link
Author

Choose a reason for hiding this comment

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

IgnoreImpCasts() пропускает неявные приведения типов. В этом примере можно не использовать.

Copy link
Owner

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:
image

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(),
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 the Lexer::getLocForEndOfToken() function here.
What would be the difference if SubExpr->getEndLoc().getLocOffset(1) 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.

С помощью Lexer::getLocForEndOfToken мы можем произвести захват всего токена,
а subExpr->getEndLoc().getLocWithOffset(1) позволяет захватить переменные состоящие из 1 символа.

SourceManager &SM = *Result.SourceManager;
if (CastExpr->getExprLoc().isMacroID())
return;
if (CastExpr->getCastKind() == CK_ToVoid)
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 what this check does and why it is there.

Copy link
Author

Choose a reason for hiding this comment

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

Здесь используется isMacroID(), то есть проверка приведение в макросе, если проверка сработает, то дальнейшие действия выполняться не будут.
CK_ToVoid - если проверка выполнится, то приведение к войду будет проигнорировано

Copy link
Owner

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.

@eshulankina eshulankina dismissed their stale review May 28, 2021 01:49

Approved by accident. Sorry about that!

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