|
| 1 | +#include <cppmetricsparser/cppmetricsparser.h> |
| 2 | + |
| 3 | +#include <model/cppastnodemetrics.h> |
| 4 | +#include <model/cppastnodemetrics-odb.hxx> |
| 5 | +#include <model/cppfilemetrics.h> |
| 6 | +#include <model/cppfilemetrics-odb.hxx> |
| 7 | + |
| 8 | +#include <model/cppastnode.h> |
| 9 | +#include <model/cppastnode-odb.hxx> |
| 10 | + |
| 11 | +#include <boost/filesystem.hpp> |
| 12 | + |
| 13 | +#include <util/logutil.h> |
| 14 | +#include <util/odbtransaction.h> |
| 15 | + |
| 16 | +#include <memory> |
| 17 | + |
| 18 | +namespace cc |
| 19 | +{ |
| 20 | +namespace parser |
| 21 | +{ |
| 22 | + |
| 23 | +CppMetricsParser::CppMetricsParser(ParserContext& ctx_): AbstractParser(ctx_) |
| 24 | +{ |
| 25 | + util::OdbTransaction {_ctx.db} ([&, this] { |
| 26 | + for (const model::CppFileMetrics& fm |
| 27 | + : _ctx.db->query<model::CppFileMetrics>()) |
| 28 | + { |
| 29 | + _fileIdCache.insert(fm.file); |
| 30 | + } |
| 31 | + |
| 32 | + for (const model::CppAstNodeMetrics& anm |
| 33 | + : _ctx.db->query<model::CppAstNodeMetrics>()) |
| 34 | + { |
| 35 | + auto node = _ctx.db->query_one<model::CppAstNode>( |
| 36 | + odb::query<model::CppAstNode>::id == anm.astNodeId); |
| 37 | + _astNodeIdCache.insert({anm.astNodeId, node->location.file->id}); |
| 38 | + } |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +bool CppMetricsParser::cleanupDatabase() |
| 43 | +{ |
| 44 | + if (!_fileIdCache.empty()) |
| 45 | + { |
| 46 | + try |
| 47 | + { |
| 48 | + util::OdbTransaction {_ctx.db} ([this] { |
| 49 | + for (const model::File& file |
| 50 | + : _ctx.db->query<model::File>( |
| 51 | + odb::query<model::File>::id.in_range(_fileIdCache.begin(), _fileIdCache.end()))) |
| 52 | + { |
| 53 | + auto it = _ctx.fileStatus.find(file.path); |
| 54 | + if (it != _ctx.fileStatus.end() && |
| 55 | + (it->second == cc::parser::IncrementalStatus::DELETED || |
| 56 | + it->second == cc::parser::IncrementalStatus::MODIFIED || |
| 57 | + it->second == cc::parser::IncrementalStatus::ACTION_CHANGED)) |
| 58 | + { |
| 59 | + LOG(info) << "[cxxmetricsparser] Database cleanup: " << file.path; |
| 60 | + |
| 61 | + _ctx.db->erase_query<model::CppFileMetrics>(odb::query<model::CppFileMetrics>::file == file.id); |
| 62 | + _fileIdCache.erase(file.id); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + for (const auto& pair : _astNodeIdCache) |
| 67 | + { |
| 68 | + auto file = _ctx.db->query_one<model::File>( |
| 69 | + odb::query<model::File>::id == pair.second); |
| 70 | + |
| 71 | + auto it = _ctx.fileStatus.find(file->path); |
| 72 | + if (it != _ctx.fileStatus.end() && |
| 73 | + (it->second == cc::parser::IncrementalStatus::DELETED || |
| 74 | + it->second == cc::parser::IncrementalStatus::MODIFIED || |
| 75 | + it->second == cc::parser::IncrementalStatus::ACTION_CHANGED)) |
| 76 | + { |
| 77 | + LOG(info) << "[cxxmetricsparser] Database cleanup: " << file->path; |
| 78 | + |
| 79 | + _ctx.db->erase_query<model::CppAstNodeMetrics>(odb::query<model::CppAstNodeMetrics>::astNodeId == pair.first); |
| 80 | + _astNodeIdCache.erase(pair.first); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + catch (odb::database_exception&) |
| 86 | + { |
| 87 | + LOG(fatal) << "Transaction failed in cxxmetrics parser!"; |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +void CppMetricsParser::functionParameters() |
| 95 | +{ |
| 96 | + util::OdbTransaction {_ctx.db} ([&, this] |
| 97 | + { |
| 98 | + for (const model::CppFunctionParamCountWithId& paramCount |
| 99 | + : _ctx.db->query<model::CppFunctionParamCountWithId>()) |
| 100 | + { |
| 101 | + model::CppAstNodeMetrics funcParams; |
| 102 | + funcParams.astNodeId = paramCount.id; |
| 103 | + funcParams.type = model::CppAstNodeMetrics::Type::PARAMETER_COUNT; |
| 104 | + funcParams.value = paramCount.count; |
| 105 | + _ctx.db->persist(funcParams); |
| 106 | + } |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +bool CppMetricsParser::parse() |
| 111 | +{ |
| 112 | + // Function parameter number metric. |
| 113 | + functionParameters(); |
| 114 | + |
| 115 | + return true; |
| 116 | +} |
| 117 | + |
| 118 | +CppMetricsParser::~CppMetricsParser() |
| 119 | +{ |
| 120 | +} |
| 121 | + |
| 122 | +/* These two methods are used by the plugin manager to allow dynamic loading |
| 123 | + of CodeCompass Parser plugins. Clang (>= version 6.0) gives a warning that |
| 124 | + these C-linkage specified methods return types that are not proper from a |
| 125 | + C code. |
| 126 | +
|
| 127 | + These codes are NOT to be called from any C code. The C linkage is used to |
| 128 | + turn off the name mangling so that the dynamic loader can easily find the |
| 129 | + symbol table needed to set the plugin up. |
| 130 | +*/ |
| 131 | +// When writing a plugin, please do NOT copy this notice to your code. |
| 132 | +#pragma clang diagnostic push |
| 133 | +#pragma clang diagnostic ignored "-Wreturn-type-c-linkage" |
| 134 | +extern "C" |
| 135 | +{ |
| 136 | + boost::program_options::options_description getOptions() |
| 137 | + { |
| 138 | + boost::program_options::options_description description("C++ Metrics Plugin"); |
| 139 | + |
| 140 | + return description; |
| 141 | + } |
| 142 | + |
| 143 | + std::shared_ptr<CppMetricsParser> make(ParserContext& ctx_) |
| 144 | + { |
| 145 | + return std::make_shared<CppMetricsParser>(ctx_); |
| 146 | + } |
| 147 | +} |
| 148 | +#pragma clang diagnostic pop |
| 149 | + |
| 150 | +} // parser |
| 151 | +} // cc |
0 commit comments