Skip to content

Commit 7ba19f9

Browse files
authored
Incremental parsing issue fixes (#736)
Fix segmentation fault due to access of file ID on non-loaded file. Eliminated use of size() on ODB query result in SourceManager. Added isSingletonResult instead to dbutil.
1 parent 5f49828 commit 7ba19f9

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

parser/src/sourcemanager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <util/hash.h>
77
#include <util/logutil.h>
8+
#include <util/dbutil.h>
89

910
#include <parser/sourcemanager.h>
1011

@@ -238,9 +239,9 @@ void SourceManager::removeFile(const model::File& file_)
238239
_transaction([&]() {
239240
if(file_.content)
240241
{
241-
auto relFiles = _db->query<model::File>(
242+
odb::result<model::File> relFiles = _db->query<model::File>(
242243
odb::query<model::File>::content == file_.content.object_id());
243-
if (relFiles.size() == 1)
244+
if (util::isSingleResult(relFiles))
244245
{
245246
removeContent = true;
246247
_db->erase<model::FileContent>(file_.content.object_id());

plugins/cpp_metrics/model/include/model/cppastnodemetrics.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ struct CppRecordMetricsView
4545
double value;
4646
};
4747

48+
#pragma db view \
49+
object(CppAstNodeMetrics) \
50+
object(CppAstNode : CppAstNodeMetrics::astNodeId == CppAstNode::id) \
51+
object(File : CppAstNode::location.file)
52+
struct CppAstNodeMetricsFileView
53+
{
54+
#pragma db column(CppAstNode::id)
55+
CppAstNodeId astNodeId;
56+
57+
#pragma db column(File::id)
58+
FileId fileId;
59+
};
60+
4861
#pragma db view \
4962
object(CppAstNodeMetrics) \
5063
object(CppAstNode : CppAstNodeMetrics::astNodeId == CppAstNode::id) \

plugins/cpp_metrics/parser/src/cppmetricsparser.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ CppMetricsParser::CppMetricsParser(ParserContext& ctx_): AbstractParser(ctx_)
3939
_fileIdCache.insert(fm.file);
4040
}
4141

42-
for (const model::CppAstNodeMetrics& anm
43-
: _ctx.db->query<model::CppAstNodeMetrics>())
42+
for (const model::CppAstNodeMetricsFileView& anm
43+
: _ctx.db->query<model::CppAstNodeMetricsFileView>())
4444
{
45-
auto node = _ctx.db->query_one<model::CppAstNode>(
46-
odb::query<model::CppAstNode>::id == anm.astNodeId);
47-
_astNodeIdCache.insert({anm.astNodeId, node->location.file->id});
45+
_astNodeIdCache.emplace(anm.astNodeId, anm.fileId);
4846
}
4947
});
5048
}

util/include/util/dbutil.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,40 @@ inline std::string getDbDriver()
9797
#endif
9898
}
9999

100+
/// @brief Determines if the specified ODB query result only contains
101+
/// a single entity. That single entity is then stored in 'single_'.
102+
/// @tparam TEntity The type of entities in the query result.
103+
/// @param result_ The ODB query result in question.
104+
/// @param single_ The variable that receives the first entity (if any).
105+
/// @return Returns true if 'result_' only contained 'single_';
106+
/// otherwise false.
107+
template<typename TEntity>
108+
bool isSingleResult(odb::result<TEntity>& result_, TEntity& single_)
109+
{
110+
auto it_b = result_.begin();
111+
const auto it_e = result_.end();
112+
if (it_b != it_e)
113+
{
114+
single_ = *it_b;
115+
return ++it_b == it_e;
116+
}
117+
else return false;
118+
}
119+
120+
/// @brief Determines if the specified ODB query result only contains
121+
/// a single entity.
122+
/// @tparam TEntity The type of entities in the query result.
123+
/// @param result_ The ODB query result in question.
124+
/// @return Returns true if 'result_' only contained a single entity;
125+
/// otherwise false.
126+
template<typename TEntity>
127+
bool isSingleResult(odb::result<TEntity>& result_)
128+
{
129+
auto it_b = result_.begin();
130+
const auto it_e = result_.end();
131+
return (it_b != it_e) && (++it_b == it_e);
132+
}
133+
100134
} // util
101135
} // cc
102136

0 commit comments

Comments
 (0)