diff --git a/JackyMa/FuncReturnCheck/head.h b/JackyMa/FuncReturnCheck/head.h new file mode 100644 index 0000000..ec15269 --- /dev/null +++ b/JackyMa/FuncReturnCheck/head.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 diff --git a/JackyMa/FuncReturnCheck/test.cpp b/JackyMa/FuncReturnCheck/test.cpp new file mode 100644 index 0000000..49c2c7e --- /dev/null +++ b/JackyMa/FuncReturnCheck/test.cpp @@ -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; +} diff --git a/JackyMa/FuncUsedCheck/head.h b/JackyMa/FuncUsedCheck/head.h new file mode 100644 index 0000000..2165dfc --- /dev/null +++ b/JackyMa/FuncUsedCheck/head.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 diff --git a/JackyMa/FuncUsedCheck/test.cpp b/JackyMa/FuncUsedCheck/test.cpp new file mode 100644 index 0000000..7ed124f --- /dev/null +++ b/JackyMa/FuncUsedCheck/test.cpp @@ -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; +} diff --git a/JackyMa/FuncUsedCheck/test1.cpp b/JackyMa/FuncUsedCheck/test1.cpp new file mode 100644 index 0000000..0ca53b4 --- /dev/null +++ b/JackyMa/FuncUsedCheck/test1.cpp @@ -0,0 +1,5 @@ +#include "head.h" + +void test(){ + foo(1); +} diff --git a/JackyMa/MyFirstCheckCheck.cpp b/JackyMa/MyFirstCheckCheck.cpp new file mode 100644 index 0000000..fcfbc77 --- /dev/null +++ b/JackyMa/MyFirstCheckCheck.cpp @@ -0,0 +1,60 @@ +//===--- 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 diff --git a/JackyMa/MyFirstCheckCheck.h b/JackyMa/MyFirstCheckCheck.h new file mode 100644 index 0000000..28d3556 --- /dev/null +++ b/JackyMa/MyFirstCheckCheck.h @@ -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 diff --git a/JackyMa/MySecondCheckCheck.cpp b/JackyMa/MySecondCheckCheck.cpp new file mode 100644 index 0000000..d587df2 --- /dev/null +++ b/JackyMa/MySecondCheckCheck.cpp @@ -0,0 +1,40 @@ +//===--- 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 diff --git a/JackyMa/MySecondCheckCheck.h b/JackyMa/MySecondCheckCheck.h new file mode 100644 index 0000000..189d840 --- /dev/null +++ b/JackyMa/MySecondCheckCheck.h @@ -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 diff --git a/JackyMa/clang-tidy安装编译及开发指南.pdf b/JackyMa/clang-tidy安装编译及开发指南.pdf new file mode 100644 index 0000000..dace7f6 Binary files /dev/null and b/JackyMa/clang-tidy安装编译及开发指南.pdf differ diff --git a/JackyMa/实验报告.docx b/JackyMa/实验报告.docx new file mode 100644 index 0000000..ddd330c Binary files /dev/null and b/JackyMa/实验报告.docx differ