File indexing completed on 2024-04-28 16:57:51

0001 /*
0002     This file is part of the clazy static checker.
0003 
0004     Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
0005     Author: SĂ©rgio Martins <sergio.martins@kdab.com>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "MiniAstDumper.h"
0024 
0025 #include <clang/Frontend/CompilerInstance.h>
0026 #include <clang/Frontend/FrontendPluginRegistry.h>
0027 #include <clang/Basic/FileManager.h>
0028 
0029 using namespace clang;
0030 using namespace std;
0031 
0032 MiniAstDumperASTAction::MiniAstDumperASTAction()
0033 {
0034 }
0035 
0036 bool MiniAstDumperASTAction::ParseArgs(const CompilerInstance &, const std::vector<string> &)
0037 {
0038     return true;
0039 }
0040 
0041 std::unique_ptr<ASTConsumer> MiniAstDumperASTAction::CreateASTConsumer(CompilerInstance &, llvm::StringRef)
0042 {
0043     return std::unique_ptr<MiniASTDumperConsumer>(new MiniASTDumperConsumer());
0044 }
0045 
0046 MiniASTDumperConsumer::MiniASTDumperConsumer()
0047 {
0048 }
0049 
0050 MiniASTDumperConsumer::~MiniASTDumperConsumer()
0051 {
0052 }
0053 
0054 bool MiniASTDumperConsumer::VisitDecl(Decl *decl)
0055 {
0056     if (auto rec = dyn_cast<CXXRecordDecl>(decl)) {
0057         llvm::errs() << "Found record: " << rec->getQualifiedNameAsString() << "\n";
0058     }
0059 
0060     return true;
0061 }
0062 
0063 bool MiniASTDumperConsumer::VisitStmt(Stmt *)
0064 {
0065     return true;
0066 }
0067 
0068 void MiniASTDumperConsumer::HandleTranslationUnit(ASTContext &ctx)
0069 {
0070     auto &sm = ctx.getSourceManager();
0071     const FileEntry *fileEntry = sm.getFileEntryForID(sm.getMainFileID());
0072     llvm::errs() << "Found TU: " << fileEntry->getName() << "\n";
0073     TraverseDecl(ctx.getTranslationUnitDecl());
0074 }
0075 
0076 static FrontendPluginRegistry::Add<MiniAstDumperASTAction>
0077 X2("clazyMiniAstDumper", "Clazy Mini AST Dumper plugin");