diff --git a/dev-scripts/miniAstDumper.py b/dev-scripts/miniAstDumper.py new file mode 100755 index 0000000..8f6652a --- /dev/null +++ b/dev-scripts/miniAstDumper.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python + +import cbor, sys, time + + +class FunctionCall: + def __init__(self): + self.callee_name = "" + +class CXXMethod: + def __init__(self): + self.id = 0 + self.qualified_name = "" + self.method_flags = 0 + +class CXXClass: + def __init__(self): + self.id = 0 + self.qualified_name = "" + self.methods = [] + self.class_flags = 0 + +class GlobalAST: + def __init__(self): + self.cxx_classes = [] + self.function_calls = [] + + +_globalAST = GlobalAST() + +def read_file(filename): + f = open(filename, 'rb') + contents = f.read() + f.close() + return contents + +def read_cbor(filename): + contents = read_file(filename); + return cbor.loads(contents) + +def load_cbor(filename, globalAST): + cborData = read_cbor(filename) + + if 'stuff' in cborData: + for stuff in cborData['stuff']: + if 'type' in stuff: + if stuff['type'] == 31: # CXXRecordDecl + cxxclass = CXXClass() + cxxclass.id = stuff['id'] + cxxclass.qualified_name = stuff['name'] + + if 'methods' in stuff: + for m in stuff['methods']: + method = CXXMethod() + methods.id = m['id'] + methods.qualified_name = m['name'] + cxxclass.methods.append(method) + + + + globalAST.cxx_classes.append(cxxclass) + elif stuff['type'] == 48: # CallExpr + funccall = FunctionCall() + funccall.callee_name = stuff['calleeName'] + globalAST.function_calls.append(funccall) + + + + +load_cbor(sys.argv[1], _globalAST) + + +for c in _globalAST.cxx_classes: + print(c.qualified_name) + +#print(cborData) diff --git a/src/MiniAstDumper.cpp b/src/MiniAstDumper.cpp index 1dbd1b4..d1a4477 100644 --- a/src/MiniAstDumper.cpp +++ b/src/MiniAstDumper.cpp @@ -1,245 +1,273 @@ /* 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 "QtUtils.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_currentCppFile = fileEntry->getName(); m_cborBuf = reinterpret_cast(malloc(m_bufferSize)); cbor_encoder_init(&m_cborEncoder, m_cborBuf, m_bufferSize, 0); cbor_encoder_create_map(&m_cborEncoder, &m_cborRootMapEncoder, 3); cbor_encode_text_stringz(&m_cborRootMapEncoder, "tu"); cbor_encode_text_stringz(&m_cborRootMapEncoder, m_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_cborStuffArray); cbor_encode_text_stringz(&m_cborRootMapEncoder, "files"); dumpFileMap(&m_cborRootMapEncoder); cbor_encoder_close_container(&m_cborEncoder, &m_cborRootMapEncoder); size_t size = cbor_encoder_get_buffer_size(&m_cborEncoder, m_cborBuf); const std::string cborFileName = m_currentCppFile + ".cbor"; std::ofstream myFile(cborFileName, std::ios::out | ios::binary); myFile.write(reinterpret_cast(m_cborBuf), long(size)); 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; } 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"; }*/ } } return true; } 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, 2); 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); + cbor_encoder_create_map(encoder, &recordMap, CborIndefiniteLength); 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()); + cborEncodeString(recordMap, "loc"); + dumpLocation(clazy::getLocStart(rec), &recordMap); - cbor_encoder_close_container(&m_cborStuffArray, &recordMap); + if (clazy::isQObject(rec)) { // TODO: Use flags + cborEncodeString(recordMap, "isQObject"); + cbor_encode_boolean(&recordMap, true); + } + cborEncodeString(recordMap, "methods"); CborEncoder cborMethodList; - cbor_encoder_create_array(encoder, &cborMethodList, CborIndefiniteLength); + cbor_encoder_create_array(&recordMap, &cborMethodList, CborIndefiniteLength); for (auto method : rec->methods()) { dumpCXXMethodDecl(method, &cborMethodList); } - cbor_encoder_close_container(encoder, &cborMethodList); + cbor_encoder_close_container(encoder, &cborMethodList);*/ + cbor_encoder_close_container(&m_cborStuffArray, &recordMap); } void MiniASTDumperConsumer::dumpCallExpr(CallExpr *callExpr, CborEncoder *encoder) { if (!callExpr->getDirectCallee()) return; CborEncoder callMap; cbor_encoder_create_map(encoder, &callMap, 3); cborEncodeString(callMap, "type"); // TODO: replace with ID cborEncodeInt(callMap, callExpr->getStmtClass()); cborEncodeString(callMap, "calleeName"); // TODO: replace with ID cborEncodeString(callMap, clazy::name(callExpr->getDirectCallee()).str().c_str()); cborEncodeString(callMap, "loc"); dumpLocation(clazy::getLocStart(callExpr), &callMap); cbor_encoder_close_container(encoder, &callMap); } void MiniASTDumperConsumer::dumpLocation(SourceLocation loc, CborEncoder *encoder) { CborEncoder locMap; cbor_encoder_create_map(encoder, &locMap, 3); auto &sm = m_ci.getSourceManager(); const FileID fileId = sm.getFileID(loc); m_fileIds[fileId.getHashValue()] = sm.getFilename(loc).str(); cborEncodeString(locMap, "fileId"); cborEncodeInt(locMap, fileId.getHashValue()); auto ploc = sm.getPresumedLoc(loc); cborEncodeString(locMap, "line"); cborEncodeInt(locMap, ploc.getLine()); cborEncodeString(locMap, "column"); cborEncodeInt(locMap, ploc.getColumn()); - cbor_encoder_close_container(encoder, &locMap); } void MiniASTDumperConsumer::dumpFileMap(CborEncoder *encoder) { CborEncoder fileMap; cbor_encoder_create_map(encoder, &fileMap, m_fileIds.size()); for (auto it : m_fileIds) { cborEncodeString(fileMap, std::to_string(it.first).c_str()); cborEncodeString(fileMap, it.second.c_str()); } cbor_encoder_close_container(encoder, &fileMap); } void MiniASTDumperConsumer::cborEncodeString(CborEncoder &enc, const char *str) { - cbor_encode_text_stringz(&enc, str); + if (cbor_encode_text_stringz(&enc, str) != CborNoError) + llvm::errs() << "cborEncodeString error\n"; } void MiniASTDumperConsumer::cborEncodeInt(CborEncoder &enc, int64_t v) { - cbor_encode_int(&enc, v); + if (cbor_encode_int(&enc, v) != CborNoError) + llvm::errs() << "cborEncodeInt error\n"; +} + +void MiniASTDumperConsumer::cborEncodeBool(CborEncoder &enc, bool b) +{ + if (cbor_encode_boolean(&enc, b) != CborNoError) + llvm::errs() << "cborEncodeBool error\n"; +} + +void MiniASTDumperConsumer::cborCreateMap(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length) +{ + if (cbor_encoder_create_map(encoder, mapEncoder, length) != CborNoError) + llvm::errs() << "cborCreateMap error\n"; +} + +void MiniASTDumperConsumer::cborCreateArray(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length) +{ + if (cbor_encoder_create_array(encoder, arrayEncoder, length) != CborNoError) + llvm::errs() << "cborCreateMap error\n"; +} + +void MiniASTDumperConsumer::cborCloseContainer(CborEncoder *encoder, const CborEncoder *containerEncoder) +{ + if (cbor_encoder_close_container(encoder, containerEncoder) != CborNoError) + llvm::errs() << "cborCloseContainer error\n"; } static FrontendPluginRegistry::Add X2("clazyMiniAstDumper", "Clazy Mini AST Dumper plugin"); diff --git a/src/MiniAstDumper.h b/src/MiniAstDumper.h index eb4823b..53ac3f3 100644 --- a/src/MiniAstDumper.h +++ b/src/MiniAstDumper.h @@ -1,86 +1,91 @@ /* 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 dumpLocation(clang::SourceLocation, CborEncoder *encoder); void dumpFileMap(CborEncoder *encoder); void cborEncodeString(CborEncoder&, const char *); void cborEncodeInt(CborEncoder&, int64_t); + void cborEncodeBool(CborEncoder &enc, bool); + void cborCreateMap(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length); + void cborCreateArray(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length); + void cborCloseContainer(CborEncoder *encoder, const CborEncoder *containerEncoder); + uint8_t *m_cborBuf = nullptr; size_t m_bufferSize = 1024 * 1024 * 20; // 20MB to start with CborEncoder m_cborEncoder, m_cborRootMapEncoder, m_cborStuffArray; clang::CompilerInstance &m_ci; std::unordered_map m_fileIds; std::string m_currentCppFile; }; #endif