-
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-3 #47
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 |
---|---|---|
|
@@ -18,13 +18,26 @@ using namespace clang::ast_matchers; | |
using namespace clang::tooling; | ||
|
||
class CastCallBack : public MatchFinder::MatchCallback { | ||
Rewriter& rwr; | ||
public: | ||
CastCallBack(Rewriter& rewriter) { | ||
// Your code goes here | ||
}; | ||
CastCallBack(Rewriter& rewriter) : rwr(rewriter) {}; | ||
|
||
void run(const MatchFinder::MatchResult &Result) override { | ||
// Your code goes here | ||
const auto *expr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast"); | ||
if (expr != nullptr) { | ||
auto loc = CharSourceRange::getCharRange(expr->getLParenLoc(), | ||
expr->getSubExprAsWritten()->getBeginLoc()); | ||
auto tSource = Lexer::getSourceText(CharSourceRange::getTokenRange( | ||
expr->getLParenLoc().getLocWithOffset(1), | ||
expr->getRParenLoc().getLocWithOffset(-1)), *Result.SourceManager, | ||
Result.Context->getLangOpts()); | ||
auto res = ("static_cast<" + tSource + ">(").str(); | ||
rwr.ReplaceText(loc, res); | ||
auto closePL = Lexer::getLocForEndOfToken( | ||
expr->getSubExprAsWritten()->IgnoreImpCasts()->getEndLoc(), 0, | ||
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 what 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. Получает подвыражение приведения как написано в исходном коде, просматривая неявные приведения или другие промежуточные узлы, которые были добавлены семантическим анализом. 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. If you try to print the AST of the Depending on which approaches you use to get the subexpression node, you will get different result nodes for the Code: // getSubExpr() retrieves the first cast subexpression
llvm::outs() << "getSubExpr(): " << castNode->getSubExpr()->getStmtClassName();
// getSubExprAsWritten() retrieves the cast subexpression as it was written in the source
// code, looking through any implicit casts or other intermediate nodes
// introduced by semantic analysis.
llvm::outs() << "getSubExprAsWritten(): " << castNode->getSubExprAsWritten()->getStmtClassName();
// getSubExpr() retrieves the first cast subexpression.
// ignoreImpCasts() skips past any implicit casts which might surround this expression until
// reaching a fixed point. It skips the following AST nodes: ImplicitCastExpr, FullExpr.
llvm::outs() << "getSubExpr()->ignoreImpCasts(): " << castNode->getSubExpr()->IgnoreImpCasts()->getStmtClassName(); Output: getSubExpr(): ImplicitCastExpr
getSubExprAsWritten(): DeclRefExpr
getSubExpr()->ignoreImpCasts(): DeclRefExpr |
||
*Result.SourceManager, Result.Context->getLangOpts()); | ||
rwr.InsertText(closePL, ")"); | ||
} | ||
} | ||
}; | ||
|
||
|
@@ -47,7 +60,7 @@ class MyASTConsumer : public ASTConsumer { | |
class CStyleCheckerFrontendAction : public ASTFrontendAction { | ||
public: | ||
CStyleCheckerFrontendAction() = default; | ||
|
||
void EndSourceFileAction() override { | ||
rewriter_.getEditBuffer(rewriter_.getSourceMgr().getMainFileID()) | ||
.write(llvm::outs()); | ||
|
@@ -65,7 +78,7 @@ 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.
Please describe why you are using
Lexer::getLocForEndOfToken(expr->getSubExprAsWritten()->IgnoreImpCasts()->getEndLoc(),...)
here.What would be the difference if
expr->getSubExprAsWritten()->IgnoreImpCasts()->getEndLoc().getLocOffset(1)
was used asclosePL
?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.
Lexer::getLocForEndOfToken определяет локацию за последним элементом токена
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.
What is the difference in the use with
expr->getSubExprAsWritten()->IgnoreImpCasts()->getEndLoc().getLocOffset(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.
Ну эта функция возвращает изначальное местоположение, сразу за концом токена, на который ссылается переданное местоположение expr-> getSubExprAsWritten () -> IgnoreImpCasts () -> getEndLoc ()
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.
What happens if we use
expr->getSubExprAsWritten()->IgnoreImpCasts()->getEndLoc().getLocOffset(1)
instead ofLexer::getLocForEndOfToken()
?When does the approach with
expr->getSubExprAsWritten()->IgnoreImpCasts()->getEndLoc().getLocOffset(1)
not work?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.
SubExpr->getEndLoc().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.
@eshulankina