diff --git a/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h b/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h index 5abe1af83..ddc99aa07 100644 --- a/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h +++ b/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h @@ -41,6 +41,8 @@ class CppMetricsParser : public AbstractParser // and member functions for every type. void lackOfCohesion(); + bool extract(); + std::vector _inputPaths; std::unordered_set _fileIdCache; std::unordered_map _astNodeIdCache; diff --git a/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp b/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp index 300a3819f..49698406b 100644 --- a/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp +++ b/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp @@ -314,63 +314,64 @@ class AstNodeLoc } }; -bool CppMetricsParser::parse() +class MetricsExtractor { - functionParameters(); - functionMcCabe(); - functionBumpyRoad(); - lackOfCohesion(); +private: + cc::parser::ParserContext& _ctx; + fs::path _root; + fs::path _code; + std::unordered_map _cache; - std::string sName; - std::cout << "\nExtraction name: "; - std::getline(std::cin, sName); - boost::trim(sName); - if (!sName.empty()) +public: + const fs::path& RootDir() const { return _root; } + const fs::path& CodeDir() const { return _code; } + +public: + MetricsExtractor(cc::parser::ParserContext& ctx_, const char* name_) : + _ctx(ctx_), + _root("/home/dani/Artifacts/Extract/"), + _code(), + _cache() { - boost::filesystem::path pRootDir = "/home/dani/Artifacts/Extract/"; - pRootDir /= sName; - boost::filesystem::path pCodeDir = pRootDir / "Code/"; - boost::filesystem::create_directories(pCodeDir); - std::cout << "Extracting to: " << pRootDir.c_str() << std::endl; + _root /= name_; + _code = _root / "Code/"; + fs::create_directories(_code); + } + void Extract(model::CppAstNodeMetrics::Type type_, const char* name_) + { util::OdbTransaction {_ctx.db} ([&, this] { - typedef model::CppAstNodeMetrics::Type Type; - std::unordered_map> umReg; - umReg.emplace(Type::PARAMETER_COUNT, - std::make_unique((pRootDir / "ParamCount.ccr").c_str())); - umReg.emplace(Type::MCCABE, - std::make_unique((pRootDir / "McCabe.ccr").c_str())); - umReg.emplace(Type::BUMPY_ROAD, - std::make_unique((pRootDir / "BumpyRoad.ccr").c_str())); - umReg.emplace(Type::LACK_OF_COHESION, - std::make_unique((pRootDir / "LackOfCoh.ccr").c_str())); - umReg.emplace(Type::LACK_OF_COHESION_HS, - std::make_unique((pRootDir / "LackOfCohHS.ccr").c_str())); - - std::unordered_map umCode; + std::ostringstream ossName; + ossName << name_ << ".ccr"; + std::ofstream ofsReg((_root / ossName.str()).c_str()); constexpr auto npos = model::Position::npos; + + typedef odb::query::query_columns QView; + const auto& QViewType = QView::CppAstNodeMetrics::type; + for (const model::CppMetricsLocationView& view - : _ctx.db->query()) + : _ctx.db->query(QViewType == type_)) { - if ((view.startLine != npos && view.startColumn != npos && view.endLine != npos && view.endColumn != npos) - && (view.startLine < view.endLine || (view.startLine == view.endLine && view.startColumn < view.endColumn))) + if ((view.startLine != npos && view.startColumn != npos + && view.endLine != npos && view.endColumn != npos) + && (view.startLine < view.endLine + || (view.startLine == view.endLine && view.startColumn < view.endColumn))) { AstNodeLoc loc(view.filePath, view.startLine, view.startColumn, view.endLine, view.endColumn); - auto resCode = umCode.insert(std::make_pair(loc, umCode.size())); + auto resCode = _cache.insert(std::make_pair(loc, _cache.size())); if (resCode.second) { - std::ostringstream sDstPath; - sDstPath << pCodeDir.c_str() << resCode.first->second << ".ccx"; - + ossName.str(""); + ossName << resCode.first->second << ".ccx"; std::ifstream isSrc(loc.file()); - std::ofstream osDst(sDstPath.str()); + std::ofstream osDst((_code / ossName.str()).c_str()); - AstNodeLoc::Pos ln = 1; + AstNodeLoc::Pos ln = 1; AstNodeLoc::Pos col = 1; bool extract = false; int ch; @@ -391,25 +392,64 @@ bool CppMetricsParser::parse() if (extract) { - std::cerr << loc.file() + std::cerr << "Error while extracting:\n\t" + << loc.file() << '\t' << loc.range().start.line << ':' << loc.range().start.column << '-' << loc.range().end.line << ':' << loc.range().end.column + << "\n\tln = " << ln << ", col = " << col << std::endl; - std::cerr << "ln = " << ln << ", col = " << col << std::endl; assert(false); } } - std::ostream& osReg = *umReg[view.type]; - osReg << view.qualifiedName << '\t' << view.value << '\t' << resCode.first->second << '\n'; + ofsReg << view.qualifiedName << '\t' + << view.value << '\t' + << resCode.first->second << '\n'; } } }); } - else std::cout << "No extraction took place." << std::endl; + +}; + +bool CppMetricsParser::parse() +{ + functionParameters(); + functionMcCabe(); + functionBumpyRoad(); + lackOfCohesion(); + + extract(); return true; } +bool CppMetricsParser::extract() +{ + std::string sName; + std::cout << "\nExtraction name: "; + std::getline(std::cin, sName); + boost::trim(sName); + if (!sName.empty()) + { + MetricsExtractor me(_ctx, sName.c_str()); + std::cout << "Extracting to: " << me.RootDir().c_str() << std::endl; + + typedef model::CppAstNodeMetrics::Type Type; + me.Extract(Type::PARAMETER_COUNT, "ParamCount"); + me.Extract(Type::MCCABE, "McCabe"); + me.Extract(Type::BUMPY_ROAD, "BumpyRoad"); + me.Extract(Type::LACK_OF_COHESION, "LackOfCoh"); + me.Extract(Type::LACK_OF_COHESION_HS, "LackOfCohHS"); + + return true; + } + else + { + std::cout << "No extraction took place." << std::endl; + return false; + } +} + CppMetricsParser::~CppMetricsParser() { }