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-3 #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ using namespace clang::tooling;

class CastCallBack : public MatchFinder::MatchCallback {
public:
CastCallBack(Rewriter& rewriter) {
// Your code goes here
};
CastCallBack(Rewriter& rewriter) : rewriter(rewriter) {}

void run(const MatchFinder::MatchResult &Result) override {
// Your code goes here
auto cStyleCExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
if (cStyleCExpr) {
auto lPloc = cStyleCExpr->getLParenLoc();
auto rPloc = cStyleCExpr->getRParenLoc();
auto endLoc = cStyleCExpr->getEndLoc();

rewriter.ReplaceText(lPloc, 1, "static_cast<");
rewriter.ReplaceText(rPloc, 1, ">(");
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 how to change the program code to get the following result:

int main() {
    float fooooo;
    int i = static_cast<int>          (fooooo)      ;
    return 0;
}

for the origin test.cpp:

int main() {

    float fooooo;

    int i = (int)          fooooo      ;

    return 0;

}

Copy link
Author

@AddMe-pls AddMe-pls May 28, 2022

Choose a reason for hiding this comment

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

Вместо строки:

rewriter.ReplaceText(rPloc, 1, ">(");

сделать строку

rewriter.ReplaceText(rPloc, 1, ">");

далее найти subexpr, найти его начало (первый токен) и в полученное расположение вставить "(", то есть выполнить команду:

rewriter.InsertText(cStyleCExpr->getSubExprAsWritten()->getBeginLoc(), "(");

rewriter.InsertTextAfterToken(endLoc, ")");
}
}
private:
Rewriter& rewriter;
};

class MyASTConsumer : public ASTConsumer {
Expand Down