diff --git a/dev-scripts/miniAstDumper.py b/dev-scripts/miniAstDumper.py index 8f6652a..e16acd4 100755 --- a/dev-scripts/miniAstDumper.py +++ b/dev-scripts/miniAstDumper.py @@ -1,76 +1,135 @@ #!/usr/bin/env python -import cbor, sys, time +import cbor, sys, time, os + +class SourceLocation: + def __init__(self): + self.filename = "" + self.lineNumber = -1 + self.columnNumber = -1 + + def asString(self): + return self.filename + ":" + str(self.lineNumber) + ":" + str(self.columnNumber) + + def dump(self): + print(self.asString()) class FunctionCall: def __init__(self): self.callee_name = "" + self.loc_start = SourceLocation() 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 parse_loc(cborLoc, file_map, tu_cwd): + loc = SourceLocation() + loc.filename = file_map[str(cborLoc['fileId'])] + + # Make absolute + if not loc.filename.startswith('/'): # TODO windows + loc.filename = tu_cwd + '/' + loc.filename + + # Normalize + loc.filename = os.path.normpath(loc.filename) + + loc.lineNumber = cborLoc['line'] + loc.columnNumber = cborLoc['column'] + return loc + 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) + file_map = {} + + current_tu_cwd = cborData['cwd'] + + # populate the file map + if 'files' in cborData: + for fileId in cborData['files'].keys(): + file_map[fileId] = cborData['files'][fileId] + + 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.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'] + method.id = m['id'] + method.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'] + funccall.loc_start = parse_loc(stuff['loc'], file_map, current_tu_cwd) globalAST.function_calls.append(funccall) +def get_class_by_name(qualified_name): + result = [] + for c in _globalAST.cxx_classes: + if c.qualified_name == qualified_name: + result.append(c) + return result +def get_calls_by_name(callee_name): + result = [] + for f in _globalAST.function_calls: + if f.callee_name == callee_name: + result.append(f) + return result load_cbor(sys.argv[1], _globalAST) +#string_class = get_class_by_name("QString")[0] + +for f in get_calls_by_name("QObject::connect"): + print(f.loc_start.dump()) + +#for f in _globalAST.function_calls: + # print(f.callee_name) + +#for m in string_class.methods: + # print(m.qualified_name) -for c in _globalAST.cxx_classes: - print(c.qualified_name) +#for c in _globalAST.cxx_classes: + # print(c.qualified_name) #print(cborData) diff --git a/src/MiniAstDumper.cpp b/src/MiniAstDumper.cpp index d1a4477..a53b5fb 100644 --- a/src/MiniAstDumper.cpp +++ b/src/MiniAstDumper.cpp @@ -1,273 +1,278 @@ /* 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 +#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); + cborCreateMap(&m_cborEncoder, &m_cborRootMapEncoder, 4); + cborEncodeString(m_cborRootMapEncoder, "tu"); + cborEncodeString(m_cborRootMapEncoder, m_currentCppFile.c_str()); + + char cwd[4096]; // TODO: Just use std::filesystem::current_path + cborEncodeString(m_cborRootMapEncoder, "cwd"); + cborEncodeString(m_cborRootMapEncoder, std::string(getcwd(cwd, sizeof(cwd))).c_str()); + + cborEncodeString(m_cborRootMapEncoder, "stuff"); + cborCreateArray(&m_cborRootMapEncoder, &m_cborStuffArray, CborIndefiniteLength); } MiniASTDumperConsumer::~MiniASTDumperConsumer() { - cbor_encoder_close_container(&m_cborRootMapEncoder, &m_cborStuffArray); + cborCloseContainer(&m_cborRootMapEncoder, &m_cborStuffArray); - cbor_encode_text_stringz(&m_cborRootMapEncoder, "files"); + cborEncodeString(m_cborRootMapEncoder, "files"); dumpFileMap(&m_cborRootMapEncoder); - cbor_encoder_close_container(&m_cborEncoder, &m_cborRootMapEncoder); + cborCloseContainer(&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); + cborCreateMap(encoder, &recordMap, 2); cborEncodeString(recordMap, "name"); cborEncodeString(recordMap, method->getQualifiedNameAsString().c_str()); cborEncodeString(recordMap, "id"); cborEncodeInt(recordMap, int64_t(method)); - cbor_encoder_close_container(encoder, &recordMap); + cborCloseContainer(encoder, &recordMap); } void MiniASTDumperConsumer::dumpCXXRecordDecl(CXXRecordDecl *rec, CborEncoder *encoder) { CborEncoder recordMap; - cbor_encoder_create_map(encoder, &recordMap, CborIndefiniteLength); + cborCreateMap(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"); dumpLocation(clazy::getLocStart(rec), &recordMap); if (clazy::isQObject(rec)) { // TODO: Use flags cborEncodeString(recordMap, "isQObject"); - cbor_encode_boolean(&recordMap, true); + cborEncodeBool(recordMap, true); } cborEncodeString(recordMap, "methods"); CborEncoder cborMethodList; - cbor_encoder_create_array(&recordMap, &cborMethodList, CborIndefiniteLength); + cborCreateArray(&recordMap, &cborMethodList, CborIndefiniteLength); for (auto method : rec->methods()) { dumpCXXMethodDecl(method, &cborMethodList); } - cbor_encoder_close_container(encoder, &cborMethodList);*/ - cbor_encoder_close_container(&m_cborStuffArray, &recordMap); + cborCloseContainer(&recordMap, &cborMethodList); + cborCloseContainer(&m_cborStuffArray, &recordMap); } void MiniASTDumperConsumer::dumpCallExpr(CallExpr *callExpr, CborEncoder *encoder) { - if (!callExpr->getDirectCallee()) + FunctionDecl *func = callExpr->getDirectCallee(); + if (!func || !func->getDeclName().isIdentifier()) return; CborEncoder callMap; - cbor_encoder_create_map(encoder, &callMap, 3); + cborCreateMap(encoder, &callMap, 3); - cborEncodeString(callMap, "type"); // TODO: replace with ID + cborEncodeString(callMap, "type"); cborEncodeInt(callMap, callExpr->getStmtClass()); cborEncodeString(callMap, "calleeName"); // TODO: replace with ID - cborEncodeString(callMap, clazy::name(callExpr->getDirectCallee()).str().c_str()); + cborEncodeString(callMap, func->getQualifiedNameAsString().c_str()); cborEncodeString(callMap, "loc"); dumpLocation(clazy::getLocStart(callExpr), &callMap); - cbor_encoder_close_container(encoder, &callMap); + cborCloseContainer(encoder, &callMap); } void MiniASTDumperConsumer::dumpLocation(SourceLocation loc, CborEncoder *encoder) { CborEncoder locMap; - cbor_encoder_create_map(encoder, &locMap, 3); + cborCreateMap(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); + cborCloseContainer(encoder, &locMap); } void MiniASTDumperConsumer::dumpFileMap(CborEncoder *encoder) { CborEncoder fileMap; - cbor_encoder_create_map(encoder, &fileMap, m_fileIds.size()); + cborCreateMap(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); + cborCloseContainer(encoder, &fileMap); } void MiniASTDumperConsumer::cborEncodeString(CborEncoder &enc, const char *str) { if (cbor_encode_text_stringz(&enc, str) != CborNoError) llvm::errs() << "cborEncodeString error\n"; } void MiniASTDumperConsumer::cborEncodeInt(CborEncoder &enc, int64_t 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");