diff options
author | Sérgio Martins <sergio.martins@kdab.com> | 2019-06-04 21:59:42 +0100 |
---|---|---|
committer | Sergio Martins <sergio.martins@kdab.com> | 2019-06-04 22:00:32 +0100 |
commit | bd40d70a1855df9c7de54a94c2f78b13c411113c (patch) | |
tree | 0db2e50f4da986e1e627021739a9c3d14de6f9a9 | |
parent | a8172f1df34f845a00173523ce70f795d6ef716f (diff) |
Add a mini AST dumper
Just to dump records and translation units-for now.
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/MiniAstDumper.cpp | 76 | ||||
-rw-r--r-- | src/MiniAstDumper.h | 68 |
3 files changed, 146 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 91217b5a..90700ed7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,7 +140,8 @@ endmacro() set(SYMBOL_FILE Lazy.exports) if (NOT CLAZY_BUILD_WITH_CLANG) - add_clang_plugin(ClazyPlugin ${CLAZY_PLUGIN_SRCS}) + set(CLAZY_MINI_AST_DUMPER_SRCS src/MiniAstDumper.cpp) + add_clang_plugin(ClazyPlugin ${CLAZY_PLUGIN_SRCS} ${CLAZY_MINI_AST_DUMPER_SRCS}) set_target_properties(ClazyPlugin PROPERTIES LINKER_LANGUAGE CXX PREFIX "" diff --git a/src/MiniAstDumper.cpp b/src/MiniAstDumper.cpp new file mode 100644 index 00000000..47661749 --- /dev/null +++ b/src/MiniAstDumper.cpp @@ -0,0 +1,76 @@ +/* + This file is part of the clazy static checker. + + Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Sérgio Martins <sergio.martins@kdab.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "MiniAstDumper.h" + +#include <clang/Frontend/CompilerInstance.h> +#include <clang/Frontend/FrontendPluginRegistry.h> + +using namespace clang; +using namespace std; + +MiniAstDumperASTAction::MiniAstDumperASTAction() +{ +} + +bool MiniAstDumperASTAction::ParseArgs(const CompilerInstance &, const std::vector<string> &) +{ + return true; +} + +std::unique_ptr<ASTConsumer> MiniAstDumperASTAction::CreateASTConsumer(CompilerInstance &, llvm::StringRef) +{ + return std::unique_ptr<MiniASTDumperConsumer>(new MiniASTDumperConsumer()); +} + +MiniASTDumperConsumer::MiniASTDumperConsumer() +{ +} + +MiniASTDumperConsumer::~MiniASTDumperConsumer() +{ +} + +bool MiniASTDumperConsumer::VisitDecl(Decl *decl) +{ + if (auto rec = dyn_cast<CXXRecordDecl>(decl)) { + llvm::errs() << "Found record: " << rec->getQualifiedNameAsString() << "\n"; + } + + return true; +} + +bool MiniASTDumperConsumer::VisitStmt(Stmt *) +{ + return true; +} + +void MiniASTDumperConsumer::HandleTranslationUnit(ASTContext &ctx) +{ + auto &sm = ctx.getSourceManager(); + const FileEntry *fileEntry = sm.getFileEntryForID(sm.getMainFileID()); + llvm::errs() << "Found TU: " << fileEntry->getName() << "\n"; + TraverseDecl(ctx.getTranslationUnitDecl()); +} + +static FrontendPluginRegistry::Add<MiniAstDumperASTAction> +X2("clazyMiniAstDumper", "Clazy Mini AST Dumper plugin"); diff --git a/src/MiniAstDumper.h b/src/MiniAstDumper.h new file mode 100644 index 00000000..0f467065 --- /dev/null +++ b/src/MiniAstDumper.h @@ -0,0 +1,68 @@ +/* + This file is part of the clazy static checker. + + Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Sérgio Martins <sergio.martins@kdab.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef CLAZY_MINI_AST_DUMPER +#define CLAZY_MINI_AST_DUMPER + +#include <clang/AST/ASTConsumer.h> +#include <clang/Frontend/FrontendAction.h> +#include <clang/AST/RecursiveASTVisitor.h> +#include <llvm/ADT/StringRef.h> + +#include <memory> +#include <vector> +#include <string> +#include <utility> + +namespace clang { +class CompilerInstance; +class ASTContext; +class Decl; +class Stmt; +} + +class MiniAstDumperASTAction : public clang::PluginASTAction +{ +public: + MiniAstDumperASTAction(); +protected: + bool ParseArgs(const clang::CompilerInstance &ci, const std::vector<std::string> &args_) override; + std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &ci, llvm::StringRef) override; +}; + +class MiniASTDumperConsumer + : public clang::ASTConsumer + , public clang::RecursiveASTVisitor<MiniASTDumperConsumer> +{ +public: + explicit MiniASTDumperConsumer(); + ~MiniASTDumperConsumer() override; + + bool VisitDecl(clang::Decl *decl); + bool VisitStmt(clang::Stmt *stm); + void HandleTranslationUnit(clang::ASTContext &ctx) override; + +private: + MiniASTDumperConsumer(const MiniASTDumperConsumer &) = delete; +}; + +#endif |