LLVM
JackyMa 3 years ago
parent 7f801de04b
commit e91e42364b

@ -18,41 +18,42 @@ namespace gjb {
void MyFirstCheckCheck::registerMatchers(MatchFinder *Finder) {
// FIXME: Add matchers.
Finder->addMatcher(functionDecl(isExpansionInMainFile(),
anyOf(hasDescendant(compoundStmt().bind("cpd")),
unless(hasDescendant(compoundStmt().bind("cpd")))
),
anyOf(hasDescendant(returnStmt().bind("ret")),
unless(hasDescendant(returnStmt().bind("ret")))
))
.bind("func"),
this);
Finder->addMatcher(
functionDecl(isExpansionInMainFile(),
anyOf(hasDescendant(compoundStmt().bind("cpd")),
unless(hasDescendant(compoundStmt().bind("cpd")))),
anyOf(hasDescendant(returnStmt().bind("ret")),
unless(hasDescendant(returnStmt().bind("ret")))))
.bind("func"),
this);
}
void MyFirstCheckCheck::check(const MatchFinder::MatchResult &Result) {
// FIXME: Add callback implementation.
/// get Node which we want to get
if(const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("func")){
if(const auto *MatchedDecl =
Result.Nodes.getNodeAs<FunctionDecl>("func")){
/// ReturnType is not 'void' or 'void *'
if(MatchedDecl->getReturnType().getAsString() != "void"
&& MatchedDecl->getReturnType().getAsString() != "void *"
/// Func Name is not "main"
&& MatchedDecl->getName() != "main"){
/// Func has definition
if(const auto *MatchedCpd = Result.Nodes.getNodeAs<CompoundStmt>("cpd")){
if(const auto *MatchedCpd =
Result.Nodes.getNodeAs<CompoundStmt>("cpd")){
/// Func has ReturnStmt
if(const auto *MatchedStmt = Result.Nodes.getNodeAs<ReturnStmt>("ret")){
if(const auto *MatchedStmt =
Result.Nodes.getNodeAs<ReturnStmt>("ret")){
}
/// Func does not have ReturnStmt
else{
diag(MatchedDecl->getLocation(), "Function '%0' has no return")
<< MatchedDecl->getName();
diag(MatchedDecl->getLocation(),
"Function '%0' has no return") << MatchedDecl->getName();
}
}
}
}
}
} // namespace gjb

@ -26,13 +26,14 @@ void MySecondCheckCheck::registerMatchers(MatchFinder *Finder) {
void MySecondCheckCheck::check(const MatchFinder::MatchResult &Result) {
// FIXME: Add callback implementation.
if (const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("func")){
if (const auto *MatchedDecl =
Result.Nodes.getNodeAs<FunctionDecl>("func")){
/// Func doesn't used in main file
if (!MatchedDecl->isUsed() && MatchedDecl->getName() != "main")
diag(MatchedDecl->getLocation(), "Function '%0' has never be used in main file")
diag(MatchedDecl->getLocation(),
"Function '%0' has never be used in main file")
<< MatchedDecl->getName();
}
}
} // namespace gjb

Binary file not shown.
Loading…
Cancel
Save