diff --git a/src/MiniAstDumper.cpp b/src/MiniAstDumper.cpp index af5a903..a62064e 100644 --- a/src/MiniAstDumper.cpp +++ b/src/MiniAstDumper.cpp @@ -1,127 +1,154 @@ /* 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 "SourceCompatibilityHelpers.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 &ci, llvm::StringRef) { return std::unique_ptr(new MiniASTDumperConsumer(ci)); } 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"; + m_cborBuf = (uint8_t*)malloc(m_bufferSize); const std::string currentCppFile = fileEntry->getName(); - - cbor_encoder_init(&m_cborEncoder, m_cborBuf, sizeof(m_cborBuf), 0); + cbor_encoder_init(&m_cborEncoder, m_cborBuf, m_bufferSize, 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)); + + llvm::errs() << "Finished " << m_bufferSize << "\n"; + } bool MiniASTDumperConsumer::VisitDecl(Decl *decl) { 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"; + + CborEncoder recordMap; + cbor_encoder_create_map(&m_cborStuffArray, &recordMap, 4); + + cborEncodeString(recordMap, "type"); + cborEncodeInt(recordMap, rec->getDeclKind()); + + cborEncodeString(recordMap, "name"); + cborEncodeString(recordMap, rec->getQualifiedNameAsString().c_str()); + + cborEncodeString(recordMap, "id"); + cborEncodeInt(recordMap, int64_t(rec)); + + cborEncodeString(recordMap, "loc"); + cborEncodeString(recordMap, clazy::getLocStart(rec).printToString(m_ci.getSourceManager()).c_str()); + + cbor_encoder_close_container(&m_cborStuffArray, &recordMap); + } 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"; } } + } else if () { + } return true; } bool MiniASTDumperConsumer::VisitStmt(Stmt *) { return true; } void MiniASTDumperConsumer::HandleTranslationUnit(ASTContext &ctx) { TraverseDecl(ctx.getTranslationUnitDecl()); } +void MiniASTDumperConsumer::cborEncodeString(CborEncoder &enc, const char *str) +{ + cbor_encode_text_stringz(&enc, str); +} + +void MiniASTDumperConsumer::cborEncodeInt(CborEncoder &enc, int64_t v) +{ + cbor_encode_int(&enc, v); +} + static FrontendPluginRegistry::Add X2("clazyMiniAstDumper", "Clazy Mini AST Dumper plugin"); diff --git a/src/MiniAstDumper.h b/src/MiniAstDumper.h index 257db74..ad7ceda 100644 --- a/src/MiniAstDumper.h +++ b/src/MiniAstDumper.h @@ -1,74 +1,77 @@ /* 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(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; + void cborEncodeString(CborEncoder&, const char *); + void cborEncodeInt(CborEncoder&, int64_t); - uint8_t m_cborBuf[1600]; + uint8_t *m_cborBuf = nullptr; + int m_bufferSize = 1024 * 1024 * 20; // 20MB to start with CborEncoder m_cborEncoder, m_cborRootMapEncoder, m_cborStuffArray; clang::CompilerInstance &m_ci; }; #endif