Skip to content

Commit c61cfd8

Browse files
committed
Adding reporting code spitting out virtual functions that override something
1 parent ecc00e8 commit c61cfd8

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

reporting/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support)
33
add_clang_library(clangTidyReportingModule
44
ReportingTidyModule.cpp
55
InterfaceLister.cpp
6+
VirtFuncLister.cpp
67

78
LINK_LIBS
89
clangAST

reporting/InterfaceLister.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ void InterfaceLister::check(const MatchFinder::MatchResult &Result) {
4141
}
4242
}
4343

44-
} // namespace aliceO2
44+
} // namespace reporting
4545
} // namespace tidy
4646
} // namespace clang

reporting/ReportingTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../ClangTidyModule.h"
1212
#include "../ClangTidyModuleRegistry.h"
1313
#include "InterfaceLister.h"
14+
#include "VirtFuncLister.h"
1415

1516
namespace clang {
1617
namespace tidy {
@@ -20,6 +21,8 @@ class ReportingModule : public ClangTidyModule {
2021
public:
2122
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
2223
CheckFactories.registerCheck<InterfaceLister>("Reporting-interfaces-used");
24+
25+
CheckFactories.registerCheck<VirtFuncLister>("Reporting-unusedvirtfunc");
2326
}
2427
};
2528

reporting/VirtFuncLister.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===--- VirtFuncLister.cpp - clang-tidy---------------------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "VirtFuncLister.h"
11+
#include "clang/AST/ASTContext.h"
12+
#include "clang/ASTMatchers/ASTMatchFinder.h"
13+
#include <iostream>
14+
15+
using namespace clang::ast_matchers;
16+
17+
namespace clang {
18+
namespace tidy {
19+
namespace reporting {
20+
21+
void VirtFuncLister::registerMatchers(MatchFinder *Finder) {
22+
Finder->addMatcher(cxxMethodDecl().bind("method"), this);
23+
}
24+
25+
void VirtFuncLister::check(const MatchFinder::MatchResult &Result) {
26+
const auto *MatchedDecl =
27+
Result.Nodes.getNodeAs<CXXMethodDecl>("method");
28+
if (MatchedDecl) {
29+
DeclarationNameInfo name_info = MatchedDecl->getNameInfo();
30+
std::string func_name = name_info.getAsString();
31+
32+
bool isvirtual = MatchedDecl->isVirtual();
33+
34+
llvm::errs() << "MEMBER FUNCTION DECL " << func_name << "("<<MatchedDecl<<")" <<
35+
" marked virtual "
36+
<< MatchedDecl->isVirtualAsWritten() << " is virtual "
37+
<< isvirtual << "\n";
38+
39+
// get class declaration of this member function
40+
// auto record = MatchedDecl->getCanonicalDecl()->getParent();
41+
42+
// we need to get source location the base node
43+
// as well as overriding node
44+
if (isvirtual) {
45+
auto iter = MatchedDecl->begin_overridden_methods();
46+
auto enditer = MatchedDecl->end_overridden_methods();
47+
48+
if (iter == enditer) {
49+
SourceLocation loc = MatchedDecl->getLocStart();//getLocation();
50+
loc.dump(MyContext->getSourceManager());
51+
llvm::errs() << "VIRTUAL-START-DECLARATION \n";
52+
} else {
53+
// otherwise find the base this is referring to
54+
decltype(iter) lastiter = iter;
55+
while (iter != enditer) {
56+
lastiter = iter;
57+
// counter number of paths up; if there is more than one
58+
// we have to give up for the moment
59+
int counter = 0;
60+
decltype(iter) countiter = iter;
61+
for (; countiter != enditer; ++countiter) {
62+
counter++;
63+
}
64+
if (counter > 1) {
65+
llvm::errs() << " OVERRIDING MULTIPLE FUNCTIONS NOT TREATED YET\n";
66+
return;
67+
}
68+
enditer = (*iter)->end_overridden_methods();
69+
iter = (*iter)->begin_overridden_methods();
70+
}
71+
SourceLocation loc = (*lastiter)->getLocStart();//getLocation();
72+
loc.dump(MyContext->getSourceManager());
73+
llvm::errs() << " OVERRIDEN-AT ";
74+
loc = MatchedDecl->getLocStart();//getLocation();
75+
loc.dump(MyContext->getSourceManager());
76+
llvm::errs() << "\n";
77+
}
78+
}
79+
}
80+
}
81+
82+
} // namespace reporting
83+
} // namespace tidy
84+
} // namespace clang

reporting/VirtFuncLister.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===--- MemberNamesCheck.h - clang-tidy-------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_VIRTFUNCLISTER_NAMES_H
11+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_VIRTFUNCLISTER_NAMES_H
12+
13+
#include "../ClangTidy.h"
14+
15+
namespace clang {
16+
namespace tidy {
17+
namespace reporting {
18+
19+
/// A simple tool/check that given a ClassName reports the interfaces
20+
/// used in the code base
21+
///
22+
class VirtFuncLister : public ClangTidyCheck {
23+
private:
24+
ClangTidyContext * MyContext;
25+
public:
26+
VirtFuncLister(StringRef Name, ClangTidyContext *Context)
27+
: ClangTidyCheck(Name, Context), MyContext(Context) {}
28+
29+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
30+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31+
};
32+
33+
} // namespace reporting
34+
} // namespace tidy
35+
} // namespace clang
36+
37+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_VIRTFUNCLISTER_NAMES_H

0 commit comments

Comments
 (0)