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

Пронин И.П. 381806-1 #36

Open
wants to merge 1 commit into
base: master
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
39 changes: 33 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,40 @@ using namespace clang::tooling;

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

Choose a reason for hiding this comment

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

Is ; required here?

Copy link
Author

Choose a reason for hiding this comment

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

Нет, можно обойтись и без ; потому что ; находится в конце блока.


void run(const MatchFinder::MatchResult &Result) override {
// Your code goes here

const auto *Item = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
Copy link
Owner

Choose a reason for hiding this comment

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

What if Item is a null pointer? How to protect the program from crashing in such cases?

Copy link
Author

Choose a reason for hiding this comment

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

Как вариант дописать такую проверку:
if (Item == nullptr)
return;

SourceManager &SourceM = *Result.SourceManager;

auto ReRange = CharSourceRange::getCharRange (Item->getLParenLoc(),
Item->getSubExprAsWritten()->getBeginLoc());

StringRef DestTypeString =
Lexer::getSourceText(CharSourceRange::getTokenRange(Item->getLParenLoc().getLocWithOffset(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 why you are using getTokenRange() here.
What is the difference between the getTokenRange() and the getCharRange() method?

Copy link
Author

Choose a reason for hiding this comment

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

getTokenRange() - возвращает диапазон токенов до начала последнего токена
getCharRange() - возвращает диапазон символов до последнего символа включительно
Допустимо использование обоих вариантов, просто аргументы функций будут различны.

Item->getRParenLoc().getLocWithOffset(-1)),
SourceM,
Result.Context->getLangOpts());

std::string str = ("static_cast<" + DestTypeString + ">").str();
Copy link
Owner

Choose a reason for hiding this comment

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

Why is .str() used here? What happens if we get rid of .str() call?

Copy link
Author

Choose a reason for hiding this comment

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

Эта функция нужна чтобы вписать type в строку static_cast.

const Expr *SubExpr = Item->getSubExprAsWritten()->IgnoreImpCasts();

if(!isa<ParenExpr>(SubExpr)) {
Copy link
Owner

@eshulankina eshulankina May 26, 2021

Choose a reason for hiding this comment

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

What does this check mean? Please describe examples of when this check returns true and when this check returns false.

Copy link
Author

Choose a reason for hiding this comment

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

Эта проверка возвращает true или false в зависимости от того, заключено ли текущее выражение в скобки. Пример: int a = (int)f - true; int a = (int)(f) - false.

str.push_back('(');
rewriter_.InsertText(Lexer::getLocForEndOfToken(SubExpr->getEndLoc(),
0,
SourceM,
Result.Context->getLangOpts()),
")");
}

rewriter_.ReplaceText(ReRange, str);

}

private:
Rewriter& rewriter_;
};

class MyASTConsumer : public ASTConsumer {
Expand Down Expand Up @@ -65,8 +92,8 @@ class CStyleCheckerFrontendAction : public ASTFrontendAction {
static llvm::cl::OptionCategory CastMatcherCategory("cast-matcher options");

int main(int argc, const char **argv) {
auto Parser = llvm::ExitOnError()(CommonOptionsParser::create(argc, argv, CastMatcherCategory));
CommonOptionsParser Parser(argc, argv, CastMatcherCategory);

ClangTool Tool(Parser.getCompilations(), Parser.getSourcePathList());
return Tool.run(newFrontendActionFactory<CStyleCheckerFrontendAction>().get());
}
}