LLVM
JackyMa 3 years ago
parent 7f801de04b
commit e91e42364b

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

@ -26,13 +26,14 @@ void MySecondCheckCheck::registerMatchers(MatchFinder *Finder) {
void MySecondCheckCheck::check(const MatchFinder::MatchResult &Result) { void MySecondCheckCheck::check(const MatchFinder::MatchResult &Result) {
// FIXME: Add callback implementation. // 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 /// Func doesn't used in main file
if (!MatchedDecl->isUsed() && MatchedDecl->getName() != "main") 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(); << MatchedDecl->getName();
} }
} }
} // namespace gjb } // namespace gjb

Binary file not shown.
Loading…
Cancel
Save