-
Notifications
You must be signed in to change notification settings - Fork 49
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { }; | ||
|
||
void run(const MatchFinder::MatchResult &Result) override { | ||
// Your code goes here | ||
|
||
const auto *Item = Result.Nodes.getNodeAs<CStyleCastExpr>("cast"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Как вариант дописать такую проверку: |
||
SourceManager &SourceM = *Result.SourceManager; | ||
|
||
auto ReRange = CharSourceRange::getCharRange (Item->getLParenLoc(), | ||
Item->getSubExprAsWritten()->getBeginLoc()); | ||
|
||
StringRef DestTypeString = | ||
Lexer::getSourceText(CharSourceRange::getTokenRange(Item->getLParenLoc().getLocWithOffset(1), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please describe why you are using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getTokenRange() - возвращает диапазон токенов до начала последнего токена |
||
Item->getRParenLoc().getLocWithOffset(-1)), | ||
SourceM, | ||
Result.Context->getLangOpts()); | ||
|
||
std::string str = ("static_cast<" + DestTypeString + ">").str(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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()); | ||
} | ||
} |
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.
Is
;
required here?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.
Нет, можно обойтись и без ; потому что ; находится в конце блока.