diff --git a/src/MiniAstDumper.cpp b/src/MiniAstDumper.cpp index 4766174..af5a903 100644 --- a/src/MiniAstDumper.cpp +++ b/src/MiniAstDumper.cpp @@ -1,76 +1,127 @@ /* 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 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 #include +#include + using namespace clang; using namespace std; MiniAstDumperASTAction::MiniAstDumperASTAction() { } bool MiniAstDumperASTAction::ParseArgs(const CompilerInstance &, const std::vector &) { return true; } -std::unique_ptr MiniAstDumperASTAction::CreateASTConsumer(CompilerInstance &, llvm::StringRef) +std::unique_ptr MiniAstDumperASTAction::CreateASTConsumer(CompilerInstance &ci, llvm::StringRef) { - return std::unique_ptr(new MiniASTDumperConsumer()); + return std::unique_ptr(new MiniASTDumperConsumer(ci)); } -MiniASTDumperConsumer::MiniASTDumperConsumer() +MiniASTDumperConsumer::MiniASTDumperConsumer(CompilerInstance &ci) + : m_ci(ci) { + auto &sm = m_ci.getASTContext().getSourceManager(); + const FileEntry *fileEntry = sm.getFileEntryForID(sm.getMainFileID()); + + llvm::errs() << "Found TU: " << fileEntry->getName() << "\n"; + + const std::string currentCppFile = fileEntry->getName(); + + + cbor_encoder_init(&m_cborEncoder, m_cborBuf, sizeof(m_cborBuf), 0); + cbor_encoder_create_map(&m_cborEncoder, &m_cborRootMapEncoder, 2); + cbor_encode_text_stringz(&m_cborRootMapEncoder, "tu"); + cbor_encode_text_stringz(&m_cborRootMapEncoder, currentCppFile.c_str()); + cbor_encode_text_stringz(&m_cborRootMapEncoder, "stuff"); + cbor_encoder_create_array(&m_cborRootMapEncoder, &m_cborStuffArray, CborIndefiniteLength); + + + //cbor_encode_boolean(&m_cborRootMapEncoder, some_value); + // } MiniASTDumperConsumer::~MiniASTDumperConsumer() { + cbor_encoder_close_container(&m_cborRootMapEncoder, &m_cborRootMapEncoder); + cbor_encoder_close_container(&m_cborEncoder, &m_cborStuffArray); + + size_t size = cbor_encoder_get_buffer_size(&m_cborEncoder, m_cborBuf); + + std::ofstream myFile ("data.bin", std::ios::out | ios::binary); + myFile.write(reinterpret_cast(m_cborBuf), long(size)); } bool MiniASTDumperConsumer::VisitDecl(Decl *decl) { - if (auto rec = dyn_cast(decl)) { - llvm::errs() << "Found record: " << rec->getQualifiedNameAsString() << "\n"; + if (auto tsd = dyn_cast(decl)) { + llvm::errs() << "ClassTemplateSpecializationDecl: " + tsd->getQualifiedNameAsString() + "\n"; + } else if (auto rec = dyn_cast(decl)) { + if (!rec->isThisDeclarationADefinition()) { + // No forward-declarations + return true; + } + + if (rec->getDescribedClassTemplate()) { + // This is a template. We'll rather print it's specializations when catching ClassTemplateDecl + return true; + } + llvm::errs() << "Found record: " << rec->getQualifiedNameAsString() + << "; this=" << rec + << "\n"; + } else if (auto ctd = dyn_cast(decl)) { + llvm::errs() << "Found template: " << ctd->getNameAsString() + << "; this=" << ctd + << "\n"; + for (auto s : ctd->specializations()) { + llvm::errs() << "Found specialization: " << s->getQualifiedNameAsString() << "\n"; + auto &args = s->getTemplateArgs(); + const unsigned int count = args.size(); + for (unsigned int i = 0; i < count; ++i) { + args.get(i).print(PrintingPolicy({}), llvm::errs()); + llvm::errs() << "\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 X2("clazyMiniAstDumper", "Clazy Mini AST Dumper plugin"); diff --git a/src/MiniAstDumper.h b/src/MiniAstDumper.h index 0f46706..257db74 100644 --- a/src/MiniAstDumper.h +++ b/src/MiniAstDumper.h @@ -1,68 +1,74 @@ /* 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 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 "cbor.h" + #include #include #include #include #include #include #include #include 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 &args_) override; std::unique_ptr CreateASTConsumer(clang::CompilerInstance &ci, llvm::StringRef) override; }; class MiniASTDumperConsumer : public clang::ASTConsumer , public clang::RecursiveASTVisitor { public: - explicit MiniASTDumperConsumer(); + explicit MiniASTDumperConsumer(clang::CompilerInstance &ci); ~MiniASTDumperConsumer() override; bool VisitDecl(clang::Decl *decl); bool VisitStmt(clang::Stmt *stm); void HandleTranslationUnit(clang::ASTContext &ctx) override; private: MiniASTDumperConsumer(const MiniASTDumperConsumer &) = delete; + + uint8_t m_cborBuf[1600]; + CborEncoder m_cborEncoder, m_cborRootMapEncoder, m_cborStuffArray; + clang::CompilerInstance &m_ci; }; #endif