-
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-2 #37
base: main
Are you sure you want to change the base?
Conversation
if (MatchedCast) { | ||
rewriter->ReplaceText( | ||
CharSourceRange::getCharRange(MatchedCast->getLParenLoc(), | ||
MatchedCast->getSubExprAsWritten()->getBeginLoc()), |
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 difference between getSubExprAsWritten()->getBeginLoc()
and getRParenLoc().getLocWithOffset(1)
.
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()->getBeginLoc()
вернет местоположение начала переменной, для которой вызывается преобразование
getRParenLoc().getLocWithOffset(1)
вернет местоположение закрывающей круглой скобки со смещением вправо на 1
Для кода :
int main() {
float f;
int i = (int) f;
return 0;
}
Cиспользованием getSubExprAsWritten()->getBeginLoc()
результатом будет :
int main() {
float f;
int i = static_cast<int>(f);
return 0;
}
А для getRParenLoc().getLocWithOffset(1)
:
int main() {
float f;
int i = static_cast<int>( f);
return 0;
}
main.cpp
Outdated
CharSourceRange::getCharRange(MatchedCast->getLParenLoc(), | ||
MatchedCast->getSubExprAsWritten()->getBeginLoc()), | ||
("static_cast<" + | ||
Lexer::getSourceText(CharSourceRange::getTokenRange( |
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 descrbe the difference between the use of getCharRange()
and getTokenRange()
with code 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.
getCharRange()
вернет строку в заданном диапазоне
getTokenRange()
вернет строку с последним токеном
Если в данном коде использовать ReplaceText()
и в первом аргументе getCharRange()
заменить на getTokenRange()
с диапазоном (getLParenLoc(), getBeginLoc())
, то результатом будет
где правой границей является начало переменной
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.
• getCharRange()
returns range specifying the starting/ending character by the specified locations.
• getTokenRange()
returns range specifying the start of the range and the start of the last token of the range.
|
if (MatchedCast->getExprLoc().isMacroID())
return;
if (MatchedCast->getCastKind() == CK_ToVoid)
return;
/ ... /
std::string CastText = ("static_cast<" +
Lexer::getSourceText(CharSourceRange::getTokenRange(
MatchedCast->getLParenLoc().getLocWithOffset(1),
MatchedCast->getRParenLoc().getLocWithOffset(-1)),
*Result.SourceManager, LangOptions()) +
">").str();
if (!isa<ParenExpr>(MatchedCast->getSubExprAsWritten())) {
CastText.push_back('(');
rewriter->InsertText(Lexer::getLocForEndOfToken(
MatchedCast->getEndLoc(), 0,
*Result.SourceManager, LangOptions()), ")");
}
rewriter->ReplaceText(
CharSourceRange::getCharRange(MatchedCast->getLParenLoc(),
MatchedCast->getSubExprAsWritten()->getBeginLoc()),
CastText);
/ ... / Для исходного кода : int main() {
float f;
int i = (int) f;
double l = (double)(f);
return 0;
} Результат: |
No description provided.