xaizek / unused-funcs (License: GPLv2+) (since 2018-12-07)
Clang-based standalone tool that detects unused external functions in a set of source files.
Commit 5115d2e5efb682f6a7471ff0c0d11ab11c296e79

clang-format code
Author: Raphael Knaus
Author date (UTC): 2018-01-15 22:04
Committer name: Raphael Knaus
Committer date (UTC): 2018-01-15 22:04
Parent(s): c1219b8781cf59e78bd5f0222e3d24001095913e
Signing key:
Tree: 77c924500cbd6dea6b17f975d92aaf77d29c485d
File Lines added Lines deleted
src/Finder.cpp 4 6
src/Finder.hpp 2 4
src/FuncInfo.cpp 1 1
src/FuncInfo.hpp 3 4
src/RefInfo.cpp 5 9
src/RefInfo.hpp 0 2
src/unused-funcs.cpp 5 5
File src/Finder.cpp changed (mode: 100644) (index ebec09e..f8f7ebb)
... ... public:
47 47 private: private:
48 48 Funcs::iterator registerFunc(const FunctionDecl &func, Funcs::iterator registerFunc(const FunctionDecl &func,
49 49 const SourceManager &sm) const; const SourceManager &sm) const;
50 void registerRef(const DeclRefExpr &ref,
51 const SourceManager &sm) const;
50 void registerRef(const DeclRefExpr &ref, const SourceManager &sm) const;
52 51
53 52 Funcs &funcs; Funcs &funcs;
54 53 }; };
55 54
56 MatchHelper::MatchHelper(Funcs &funcs)
57 : funcs(funcs) {}
55 MatchHelper::MatchHelper(Funcs &funcs) : funcs(funcs) {}
58 56
59 57 void MatchHelper::run(const Result &result) { void MatchHelper::run(const Result &result) {
60 58 using Func = FunctionDecl; using Func = FunctionDecl;
 
... ... void MatchHelper::registerRef(const DeclRefExpr &ref,
93 91 } }
94 92 } }
95 93
96 } // namespace
94 } // namespace
97 95
98 96 class Finder::Impl { class Finder::Impl {
99 97 public: public:
 
... ... Finder::Impl::Impl() : helper(funcs) {
116 114 } }
117 115
118 116 Finder::Impl::~Impl() { Finder::Impl::~Impl() {
119 for (auto & func : funcs) {
117 for (auto &func : funcs) {
120 118 const auto &funcInfo = func.second; const auto &funcInfo = func.second;
121 119
122 120 if (funcInfo.isFullyDeclared()) { if (funcInfo.isFullyDeclared()) {
File src/Finder.hpp changed (mode: 100644) (index f11568e..e520541)
23 23
24 24 #include <memory> #include <memory>
25 25
26
27 26 namespace clang { namespace clang {
28 27 namespace ast_matchers { namespace ast_matchers {
29 28 class MatchFinder; class MatchFinder;
30 } // namespace ast_matchers
31 } // namespace clang
32
29 } // namespace ast_matchers
30 } // namespace clang
33 31
34 32 class Finder { class Finder {
35 33 public: public:
File src/FuncInfo.cpp changed (mode: 100644) (index 52c6596..e5a9a53)
... ... void FuncInfo::registerRef(const clang::DeclRefExpr &ref,
52 52 bool FuncInfo::isUnused() const { return calls.empty(); } bool FuncInfo::isUnused() const { return calls.empty(); }
53 53
54 54 bool FuncInfo::canBeMadeStatic() const { bool FuncInfo::canBeMadeStatic() const {
55 for (const auto & call : calls) {
55 for (const auto &call : calls) {
56 56 if (!call.isInThisUnit(fileName)) { if (!call.isInThisUnit(fileName)) {
57 57 return false; return false;
58 58 } }
File src/FuncInfo.hpp changed (mode: 100644) (index 22e1c3c..ada220e)
28 28
29 29 #include "RefInfo.hpp" #include "RefInfo.hpp"
30 30
31
32 31 namespace clang { namespace clang {
33 32 class FunctionDecl; class FunctionDecl;
34 33 class DeclRefExpr; class DeclRefExpr;
35 34 class SourceManager; class SourceManager;
36 35 } // namespace clang } // namespace clang
37 36
38
39 37 class FuncInfo { class FuncInfo {
40 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const FuncInfo &fi);
38 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
39 const FuncInfo &fi);
41 40
42 41 public: public:
43 42 FuncInfo(const clang::FunctionDecl &func, const clang::SourceManager &sm); FuncInfo(const clang::FunctionDecl &func, const clang::SourceManager &sm);
 
... ... public:
53 52 private: private:
54 53 const std::string name; const std::string name;
55 54 std::string fileName; std::string fileName;
56 unsigned int lineNum {0};
55 unsigned int lineNum{0};
57 56 typedef std::vector<RefInfo> Refs; typedef std::vector<RefInfo> Refs;
58 57 Refs calls; Refs calls;
59 58 }; };
File src/RefInfo.cpp changed (mode: 100644) (index 1d1e2e2..985c3db)
24 24 #include <clang/Basic/SourceLocation.h> #include <clang/Basic/SourceLocation.h>
25 25 #include <clang/Basic/SourceManager.h> #include <clang/Basic/SourceManager.h>
26 26
27
28 27 namespace { namespace {
29 28 std::string getFilename(const clang::DeclRefExpr &ref, std::string getFilename(const clang::DeclRefExpr &ref,
30 29 const clang::SourceManager &sm) { const clang::SourceManager &sm) {
31 clang::FullSourceLoc fullLoc(ref.getExprLoc(), sm);
32 return sm.getFilename(fullLoc);
30 clang::FullSourceLoc fullLoc(ref.getExprLoc(), sm);
31 return sm.getFilename(fullLoc);
33 32 } }
34 } // namespace
35
33 } // namespace
36 34
37 RefInfo::RefInfo(const clang::DeclRefExpr &ref,
38 const clang::SourceManager &sm)
39 : fileName(getFilename(ref, sm)) {
40 }
35 RefInfo::RefInfo(const clang::DeclRefExpr &ref, const clang::SourceManager &sm)
36 : fileName(getFilename(ref, sm)) {}
41 37
42 38 bool RefInfo::isInThisUnit(const std::string &other) const { bool RefInfo::isInThisUnit(const std::string &other) const {
43 39 return other == fileName; return other == fileName;
File src/RefInfo.hpp changed (mode: 100644) (index c7058d0..47657ad)
23 23
24 24 #include <string> #include <string>
25 25
26
27 26 namespace clang { namespace clang {
28 27 class DeclRefExpr; class DeclRefExpr;
29 28 class SourceManager; class SourceManager;
30 29 } // namespace clang } // namespace clang
31 30
32
33 31 class RefInfo { class RefInfo {
34 32 public: public:
35 33 RefInfo(const clang::DeclRefExpr &ref, const clang::SourceManager &sm); RefInfo(const clang::DeclRefExpr &ref, const clang::SourceManager &sm);
File src/unused-funcs.cpp changed (mode: 100644) (index 8fe71b0..03d9228)
30 30 namespace ct = clang::tooling; namespace ct = clang::tooling;
31 31
32 32 namespace { namespace {
33 class CustomDiagnosticConsumer: public clang::DiagnosticConsumer {
34 public:
35 bool IncludeInDiagnosticCounts() const override { return false; }
36 };
37 } // namespace
33 class CustomDiagnosticConsumer : public clang::DiagnosticConsumer {
34 public:
35 bool IncludeInDiagnosticCounts() const override { return false; }
36 };
37 } // namespace
38 38
39 39 static llvm::cl::extrahelp commonHelp(ct::CommonOptionsParser::HelpMessage); static llvm::cl::extrahelp commonHelp(ct::CommonOptionsParser::HelpMessage);
40 40
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/unused-funcs

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/unused-funcs

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master