-
Notifications
You must be signed in to change notification settings - Fork 53
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
base: main
Are you sure you want to change the base?
Conversation
// Your code goes here | ||
}; | ||
|
||
CastCallBack(Rewriter& rewriter): _rewriter(rewriter) {}; | ||
void run(const MatchFinder::MatchResult &Result) override { |
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.
- 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?
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.
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;
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.
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(); |
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 here getSubExprAsWritten()
and IgnoreImpCasts()
. What would be the difference with the use of getSubExprAsWritten()->IgnoreImpCasts()
, getSubExpr()->IgnoreImpCasts()
and getSubExprAsWritten()
? Provide examples.
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.
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, |
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 the Lexer::getLocForEndOfToken()
function here. What alternative ways to achieve the same result do you know?
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()
позволяет определить позицию в конце токена. Нам нужно поставит закрывающею скобку перед ";"
rewriter.InsertTextAfterToken(styleCastExpr->getEndLoc(),...) можно вставить данный вариант, он даст тот же результат.
No description provided.