diff --git a/src/MiniAstDumper.cpp b/src/MiniAstDumper.cpp index a62064e..0453870 100644 --- a/src/MiniAstDumper.cpp +++ b/src/MiniAstDumper.cpp @@ -1,154 +1,201 @@ /* 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 "clazy_stl.h" +#include "StringUtils.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()); - m_cborBuf = (uint8_t*)malloc(m_bufferSize); + m_cborBuf = reinterpret_cast(malloc(m_bufferSize)); const std::string currentCppFile = fileEntry->getName(); 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); } 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"; - + delete m_cborBuf; } 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; } - 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); - + dumpCXXRecordDecl(rec, &m_cborStuffArray); } 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 *) +bool MiniASTDumperConsumer::VisitStmt(Stmt *stmt) { + if (auto callExpr = dyn_cast(stmt)) { + dumpCallExpr(callExpr, &m_cborStuffArray); + } + return true; } void MiniASTDumperConsumer::HandleTranslationUnit(ASTContext &ctx) { TraverseDecl(ctx.getTranslationUnitDecl()); } +void MiniASTDumperConsumer::dumpCXXMethodDecl(CXXMethodDecl *method, CborEncoder *encoder) +{ + CborEncoder recordMap; + cbor_encoder_create_map(encoder, &recordMap, 5); + + //cborEncodeString(recordMap, "type"); + //cborEncodeInt(recordMap, method->getDeclKind()); + + cborEncodeString(recordMap, "name"); + cborEncodeString(recordMap, method->getQualifiedNameAsString().c_str()); + + cborEncodeString(recordMap, "id"); + cborEncodeInt(recordMap, int64_t(method)); + + //cborEncodeString(recordMap, "loc"); + //cborEncodeString(recordMap, clazy::getLocStart(method).printToString(m_ci.getSourceManager()).c_str()); + + cbor_encoder_close_container(encoder, &recordMap); +} + +void MiniASTDumperConsumer::dumpCXXRecordDecl(CXXRecordDecl *rec, CborEncoder *encoder) +{ + CborEncoder recordMap; + cbor_encoder_create_map(encoder, &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"); // TODO: replace with file id + cborEncodeString(recordMap, clazy::getLocStart(rec).printToString(m_ci.getSourceManager()).c_str()); + + cbor_encoder_close_container(&m_cborStuffArray, &recordMap); + + CborEncoder cborMethodList; + cbor_encoder_create_array(encoder, &cborMethodList, CborIndefiniteLength); + for (auto method : rec->methods()) { + dumpCXXMethodDecl(method, &cborMethodList); + } + cbor_encoder_close_container(encoder, &cborMethodList); + +} + +void MiniASTDumperConsumer::dumpCallExpr(CallExpr *callExpr, CborEncoder *encoder) +{ + if (!callExpr->getDirectCallee()) + return; + + CborEncoder callMap; + cbor_encoder_create_map(encoder, &callMap, 1); + + cborEncodeString(callMap, "calleeName"); // TODO: replace with ID + cborEncodeString(callMap, clazy::name(callExpr->getDirectCallee()).str().c_str()); + + cbor_encoder_close_container(encoder, &callMap); +} + 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 ad7ceda..73f6e0f 100644 --- a/src/MiniAstDumper.h +++ b/src/MiniAstDumper.h @@ -1,77 +1,80 @@ /* 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 dumpCXXMethodDecl(clang::CXXMethodDecl *, CborEncoder *encoder); + void dumpCXXRecordDecl(clang::CXXRecordDecl *, CborEncoder *encoder); + void dumpCallExpr(clang::CallExpr *, CborEncoder *encoder); void cborEncodeString(CborEncoder&, const char *); void cborEncodeInt(CborEncoder&, int64_t); uint8_t *m_cborBuf = nullptr; - int m_bufferSize = 1024 * 1024 * 20; // 20MB to start with + size_t m_bufferSize = 1024 * 1024 * 20; // 20MB to start with CborEncoder m_cborEncoder, m_cborRootMapEncoder, m_cborStuffArray; clang::CompilerInstance &m_ci; }; #endif