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 #53

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

Conversation

111nat
Copy link

@111nat 111nat commented May 28, 2022

No description provided.

// Your code goes here
};

CastCallBack(Rewriter& rewriter): _rewriter(rewriter) {};
void run(const MatchFinder::MatchResult &Result) override {
Copy link
Owner

Choose a reason for hiding this comment

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

  • How to prevent parentheses from being added if they are already present in the (int)(f) code?
  • How to prevent changing the C style cast code for a special case - when we want to suppress warnings about unused variables?
  • How to prevent changing the C style cast code in macros?

Copy link
Author

@111nat 111nat May 29, 2022

Choose a reason for hiding this comment

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

How to prevent parentheses from being added if they are already present in the (int)(f) code?
Для предотвращения возникновения нежеланных скобок можно использовать в нашем исходном выражении isa для проверки того, заключено ли оно в скобки:

if (!isa<ParenExpr>(c_e->getSubExprAsWritten()->IgnoreImpCasts())) 
    newText.append("(");
auto n_e = Lexer::getLocForEndOfToken(expr->getEndLoc(), 0, s_m, LangOptions());
if (!isa<ParenExpr>(c_e->getSubExprAsWritten()->IgnoreImpCasts())) 
    _rewriter.InsertText(endLoc, ")");

How to prevent changing the C style cast code for a special case - when we want to suppress warnings about unused variables?
Для этого нужно поместить переменную в выражение (void), чтобы компилятор понимал, что она используется.
Например, можно написать macro
#define UNUSED(x) (void)(x)
Вот такой пример использования

void f(int x) {
    UNUSED(x);
    ...
}

How to prevent changing the C style cast code in macros?

Для предотвращения изменения кода в макросах нужно добавить проверку:

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

Copy link
Owner

Choose a reason for hiding this comment

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

As for the question:
"How to prevent changing the C style cast code for a special case - when we want to suppress warnings about unused variables?"
The question is how to update your solution to not change the C style cast to the C++ style cast for cases like this: (void)variable;?

c_e->getLParenLoc().getLocWithOffset(1),
c_e->getRParenLoc().getLocWithOffset(-1)),
s_m, LangOptions());
const auto *expr = c_e->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 here getSubExprAsWritten() and IgnoreImpCasts(). What would be the difference with the use of getSubExprAsWritten()->IgnoreImpCasts(), getSubExpr()->IgnoreImpCasts() and getSubExprAsWritten()? Provide examples.

Copy link
Author

Choose a reason for hiding this comment

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

getSubExprAsWritten() извлекает подвыражение приведения, как оно было написано в исходном коде, просматривая любые неявные приведения или другие промежуточные узлы, введенные семантическим анализом
IgnoreImpCasts() позволяет пропускать любые неявные приведения, такие как ImplicitCastExpr, что может окружить это выражение до тех пор, пока не достигнет фиксированной точки.
getSubExpr()->IgnoreImpCasts() и getSubExprAsWritten() дадут одинаковый результат
Вместо getSubExprAsWritten() и IgnoreImpCasts() мы можем просто использовать getSubExprAsWritten()

s_m, LangOptions());
const auto *expr = c_e->getSubExprAsWritten()->IgnoreImpCasts();
auto n_t_b = ("static_cast<" + type_name + ">(").str();
auto n_e = Lexer::getLocForEndOfToken(expr->getEndLoc(), 0,
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 for using the Lexer::getLocForEndOfToken() function here. What alternative ways to achieve the same result do you know?

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() позволяет определить позицию в конце токена. Нам нужно поставит закрывающею скобку перед ";"
rewriter.InsertTextAfterToken(styleCastExpr->getEndLoc(),...) можно вставить данный вариант, он даст тот же результат.

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