|
| 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 |
0 commit comments