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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Neopoz9yy
Copy link

No description provided.

if (MatchedCast) {
rewriter->ReplaceText(
CharSourceRange::getCharRange(MatchedCast->getLParenLoc(),
MatchedCast->getSubExprAsWritten()->getBeginLoc()),
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 difference between getSubExprAsWritten()->getBeginLoc() and getRParenLoc().getLocWithOffset(1).

Copy link
Author

@Neopoz9yy Neopoz9yy May 28, 2022

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(
Copy link
Owner

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.

Copy link
Author

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()), то результатом будет
lPeMRM1vkqnd5o9kVUE9A5pIvPS1Kt78TBEZG9LQh0k_BhqKtC1NEFLQP9oEl43JFJ730V4zvGW6ci4o6hSNnjws
где правой границей является начало переменной

Copy link
Owner

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.

@ElizJogar
Copy link
Owner

  • How to prevent changing the C style cast code in macros?
  • 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 parentheses from being added if they are already present in the (int)(f) code?

@Neopoz9yy
Copy link
Author

Neopoz9yy commented May 28, 2022

  • How to prevent changing the C style cast code in macros?
if (MatchedCast->getExprLoc().isMacroID())
    return;
  • How to prevent changing the C style cast code for a special case - when we want to suppress warnings about unused variables?
if (MatchedCast->getCastKind() == CK_ToVoid)
    return;
  • How to prevent parentheses from being added if they are already present in the (int)(f) code?
/ ... /

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;
}

Результат:

изображение

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