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-2 #52

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

Conversation

mark0wka
Copy link

No description provided.

public:
virtual void run(const MatchFinder::MatchResult &Result) {
if (const auto *CastT = Result.Nodes.getNodeAs<CStyleCastExpr>("cast")) {
if (CastT->getExprLoc().isMacroID())
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.

Проверяем является ли выражение макросом. Если не является, то приведение типов в нем игнорируется

if (CastT->getExprLoc().isMacroID())
return;

if (CastT->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.

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.

Проверка на наличие типа void перед переменной. Такая ((void)<var_name>) конструкция используется, чтобы указать неиспользуемые переменные, что предотвратит появление предупреждений от компилятора. Возвращает true, если выражение имеет вид (void)<var_name>, false, если в скобках указан другой тип


const auto* CastIgn = CastT->getSubExprAsWritten()->IgnoreImpCasts();

if (!isa<ParenExpr>(CastIgn)) {
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.

Проверка на наличие скобок у выражения. Если их нет, то они добавляются, если есть, то ничего не происходит


std::string TextT(("static_cast<" + StringT + ">").str());

const auto* CastIgn = CastT->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 the reason 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

@eshulankina eshulankina May 30, 2021

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

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