Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
|
e91e42364b | 3 years ago |
|
7f801de04b | 3 years ago |
|
078201c4e5 | 3 years ago |
|
fa7d46141b | 3 years ago |
|
6dbf02b5ba | 3 years ago |
@ -0,0 +1,61 @@
|
||||
//===--- MyFirstCheckCheck.cpp - clang-tidy -------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "MyFirstCheckCheck.h"
|
||||
#include "clang/AST/ASTContext.h"
|
||||
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
||||
|
||||
using namespace clang::ast_matchers;
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
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);
|
||||
}
|
||||
|
||||
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")){
|
||||
/// 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")){
|
||||
/// Func has ReturnStmt
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace gjb
|
||||
} // namespace tidy
|
||||
} // namespace clang
|
@ -0,0 +1,34 @@
|
||||
//===--- MyFirstCheckCheck.h - clang-tidy -----------------------*- C++ -*-===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GJB_MYFIRSTCHECKCHECK_H
|
||||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GJB_MYFIRSTCHECKCHECK_H
|
||||
|
||||
#include "../ClangTidyCheck.h"
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
namespace gjb {
|
||||
|
||||
/// FIXME: Write a short description.
|
||||
///
|
||||
/// For the user-facing documentation see:
|
||||
/// http://clang.llvm.org/extra/clang-tidy/checks/gjb-my-first-check.html
|
||||
class MyFirstCheckCheck : public ClangTidyCheck {
|
||||
public:
|
||||
MyFirstCheckCheck(StringRef Name, ClangTidyContext *Context)
|
||||
: ClangTidyCheck(Name, Context) {}
|
||||
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
|
||||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
|
||||
};
|
||||
|
||||
} // namespace gjb
|
||||
} // namespace tidy
|
||||
} // namespace clang
|
||||
|
||||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GJB_MYFIRSTCHECKCHECK_H
|
@ -0,0 +1,10 @@
|
||||
#ifndef HEAD_H
|
||||
#define HEAD_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int foo(int x);
|
||||
int foo1(int x);
|
||||
int foo2(int x);
|
||||
|
||||
#endif
|
@ -0,0 +1,20 @@
|
||||
#include "head.h"
|
||||
|
||||
int foo(int x){
|
||||
//return 2*x;
|
||||
}
|
||||
|
||||
int foo1(int x){
|
||||
return x;
|
||||
}
|
||||
|
||||
int foo2(int x){
|
||||
return x*x;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int x = foo(1);
|
||||
int y = foo1(1);
|
||||
int z = foo2(1);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
//===--- MySecondCheckCheck.cpp - clang-tidy ------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "MySecondCheckCheck.h"
|
||||
#include "clang/AST/ASTContext.h"
|
||||
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
||||
|
||||
using namespace clang::ast_matchers;
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
namespace gjb {
|
||||
|
||||
void MySecondCheckCheck::registerMatchers(MatchFinder *Finder) {
|
||||
// FIXME: Add matchers.
|
||||
Finder->addMatcher(functionDecl(isExpansionInMainFile())
|
||||
.bind("func"),
|
||||
this);
|
||||
}
|
||||
|
||||
void MySecondCheckCheck::check(const MatchFinder::MatchResult &Result) {
|
||||
// FIXME: Add callback implementation.
|
||||
|
||||
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")
|
||||
<< MatchedDecl->getName();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace gjb
|
||||
} // namespace tidy
|
||||
} // namespace clang
|
@ -0,0 +1,34 @@
|
||||
//===--- MySecondCheckCheck.h - clang-tidy ----------------------*- C++ -*-===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GJB_MYSECONDCHECKCHECK_H
|
||||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GJB_MYSECONDCHECKCHECK_H
|
||||
|
||||
#include "../ClangTidyCheck.h"
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
namespace gjb {
|
||||
|
||||
/// FIXME: Write a short description.
|
||||
///
|
||||
/// For the user-facing documentation see:
|
||||
/// http://clang.llvm.org/extra/clang-tidy/checks/gjb-my-second-check.html
|
||||
class MySecondCheckCheck : public ClangTidyCheck {
|
||||
public:
|
||||
MySecondCheckCheck(StringRef Name, ClangTidyContext *Context)
|
||||
: ClangTidyCheck(Name, Context) {}
|
||||
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
|
||||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
|
||||
};
|
||||
|
||||
} // namespace gjb
|
||||
} // namespace tidy
|
||||
} // namespace clang
|
||||
|
||||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GJB_MYSECONDCHECKCHECK_H
|
@ -0,0 +1,11 @@
|
||||
#ifndef HEAD_H
|
||||
#define HEAD_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int foo(int x);
|
||||
int foo1(int x);
|
||||
int foo2(int x);
|
||||
void test();
|
||||
|
||||
#endif
|
@ -0,0 +1,21 @@
|
||||
#include "head.h"
|
||||
|
||||
int foo(int x){
|
||||
return 2*x;
|
||||
}
|
||||
|
||||
int foo1(int x){
|
||||
return x;
|
||||
}
|
||||
|
||||
int foo2(int x){
|
||||
return x*x;
|
||||
}
|
||||
|
||||
int main(){
|
||||
test();
|
||||
//int x = foo(1);
|
||||
int y = foo1(1);
|
||||
int z = foo2(1);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
#include "head.h"
|
||||
|
||||
void test(){
|
||||
foo(1);
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue