Skip to content

Commit 17e1a10

Browse files
authored
Merge pull request #566 from andocz/fix-gettags-segfault
Improve error handling of CppServiceHandler
2 parents e565660 + b317b94 commit 17e1a10

File tree

2 files changed

+87
-32
lines changed

2 files changed

+87
-32
lines changed

plugins/cpp/service/include/service/cppservice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ class CppServiceHandler : virtual public LanguageServiceIf
383383

384384
std::shared_ptr<std::string> _datadir;
385385
const cc::webserver::ServerContext& _context;
386+
387+
std::string toShortDiagnosticString(const model::CppAstNode& node) const;
386388
};
387389

388390
}

plugins/cpp/service/src/cppservice.cpp

Lines changed: 85 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,10 @@ void CppServiceHandler::getProperties(
289289
return_["Type"] = variable.qualifiedType;
290290
}
291291
else
292-
LOG(warning) << "Database query result was not expected to be empty. "
293-
<< __FILE__ << ", line #" << __LINE__;
292+
LOG(warning)
293+
<< "Unexpected empty result when querying properties "
294+
"of C++ variable: "
295+
<< toShortDiagnosticString(node);
294296

295297
break;
296298
}
@@ -310,8 +312,10 @@ void CppServiceHandler::getProperties(
310312
return_["Signature"] = function.name;
311313
}
312314
else
313-
LOG(warning) << "Database query result was not expected to be empty. "
314-
<< __FILE__ << ", line #" << __LINE__;
315+
LOG(warning)
316+
<< "Unexpected empty result when querying properties "
317+
"of C++ function: "
318+
<< toShortDiagnosticString(node);
315319

316320
break;
317321
}
@@ -334,8 +338,10 @@ void CppServiceHandler::getProperties(
334338
return_["Qualified name"] = type.qualifiedName;
335339
}
336340
else
337-
LOG(warning) << "Database query result was not expected to be empty. "
338-
<< __FILE__ << ", line #" << __LINE__;
341+
LOG(warning)
342+
<< "Unexpected empty result when querying properties "
343+
"of C++ type: "
344+
<< toShortDiagnosticString(node);
339345

340346
break;
341347
}
@@ -353,8 +359,10 @@ void CppServiceHandler::getProperties(
353359
return_["Qualified name"] = type.qualifiedName;
354360
}
355361
else
356-
LOG(warning) << "Database query result was not expected to be empty. "
357-
<< __FILE__ << ", line #" << __LINE__;
362+
LOG(warning)
363+
<< "Unexpected empty result when querying properties "
364+
"of C++ typedef: "
365+
<< toShortDiagnosticString(node);
358366

359367
break;
360368
}
@@ -373,8 +381,10 @@ void CppServiceHandler::getProperties(
373381
return_["Value"] = std::to_string(enumConst.value);
374382
}
375383
else
376-
LOG(warning) << "Database query result was not expected to be empty. "
377-
<< __FILE__ << ", line #" << __LINE__;
384+
LOG(warning)
385+
<< "Unexpected empty result when querying properties "
386+
"of C++ enum constant: "
387+
<< toShortDiagnosticString(node);
378388
}
379389
}
380390
});
@@ -492,8 +502,10 @@ std::int32_t CppServiceHandler::getReferenceCount(
492502
TypeQuery::entityHash == function.typeHash).count;
493503
}
494504
else
495-
LOG(warning) << "Database query result was not expected to be empty. "
496-
<< __FILE__ << ", line #" << __LINE__;
505+
LOG(warning)
506+
<< "Unexpected empty result when counting return types "
507+
"of C++ function: "
508+
<< toShortDiagnosticString(node);
497509

498510
return 0;
499511
}
@@ -526,8 +538,10 @@ std::int32_t CppServiceHandler::getReferenceCount(
526538
TypeQuery::entityHash == variable.typeHash).count;
527539
}
528540
else
529-
LOG(warning) << "Database query result was not expected to be empty. "
530-
<< __FILE__ << ", line #" << __LINE__;
541+
LOG(warning)
542+
<< "Unexpected empty result when counting types "
543+
"of C++ variable: "
544+
<< toShortDiagnosticString(node);
531545

532546
return 0;
533547
}
@@ -774,8 +788,10 @@ void CppServiceHandler::getReferences(
774788
std::to_string(var.load()->astNodeId)));
775789
}
776790
else
777-
LOG(warning) << "Database query result was not expected to be empty. "
778-
<< __FILE__ << ", line #" << __LINE__;
791+
LOG(warning)
792+
<< "Unexpected empty result when querying parameters "
793+
"of C++ function: "
794+
<< toShortDiagnosticString(node);
779795

780796
break;
781797
}
@@ -795,8 +811,10 @@ void CppServiceHandler::getReferences(
795811
std::to_string(var.load()->astNodeId)));
796812
}
797813
else
798-
LOG(warning) << "Database query result was not expected to be empty. "
799-
<< __FILE__ << ", line #" << __LINE__;
814+
LOG(warning)
815+
<< "Unexpected empty result when querying local variables "
816+
"of C++ function: "
817+
<< toShortDiagnosticString(node);
800818

801819
break;
802820
}
@@ -822,8 +840,10 @@ void CppServiceHandler::getReferences(
822840
}
823841
}
824842
else
825-
LOG(warning) << "Database query result was not expected to be empty. "
826-
<< __FILE__ << ", line #" << __LINE__;
843+
LOG(warning)
844+
<< "Unexpected empty result when querying return type "
845+
"of C++ function: "
846+
<< toShortDiagnosticString(node);
827847

828848
break;
829849
}
@@ -884,8 +904,10 @@ void CppServiceHandler::getReferences(
884904
}
885905
}
886906
else
887-
LOG(warning) << "Database query result was not expected to be empty. "
888-
<< __FILE__ << ", line #" << __LINE__;
907+
LOG(warning)
908+
<< "Unexpected empty result when querying type "
909+
"of C++ variable: "
910+
<< toShortDiagnosticString(node);
889911

890912
break;
891913
}
@@ -983,8 +1005,10 @@ void CppServiceHandler::getReferences(
9831005
nodes = std::vector<model::CppAstNode>(result.begin(), result.end());
9841006
}
9851007
else
986-
LOG(warning) << "Database query result was not expected to be empty. "
987-
<< __FILE__ << ", line #" << __LINE__;
1008+
LOG(warning)
1009+
<< "Unexpected empty result when querying underlying type "
1010+
"of C++ typedef: "
1011+
<< toShortDiagnosticString(node);
9881012

9891013
break;
9901014
}
@@ -1009,8 +1033,10 @@ void CppServiceHandler::getReferences(
10091033
});
10101034
}
10111035
else
1012-
LOG(warning) << "Database query result was not expected to be empty. "
1013-
<< __FILE__ << ", line #" << __LINE__;
1036+
LOG(warning)
1037+
<< "Unexpected empty result when querying enum constants "
1038+
"of C++ enum: "
1039+
<< toShortDiagnosticString(node);
10141040

10151041
break;
10161042
}
@@ -1611,14 +1637,22 @@ CppServiceHandler::getTags(const std::vector<model::CppAstNode>& nodes_)
16111637
tags[node.id].push_back(visibility);
16121638
}
16131639

1614-
//--- Virtual Tag ---//
1640+
//--- Other Tags ---//
16151641

16161642
FuncResult funcNodes = _db->query<cc::model::CppFunction>(
16171643
FuncQuery::entityHash == defNode.entityHash);
1618-
const model::CppFunction& funcNode = *funcNodes.begin();
1644+
if (!funcNodes.empty())
1645+
{
1646+
const model::CppFunction& funcNode = *funcNodes.begin();
16191647

1620-
for (const model::Tag& tag : funcNode.tags)
1621-
tags[node.id].push_back(model::tagToString(tag));
1648+
for (const model::Tag& tag : funcNode.tags)
1649+
tags[node.id].push_back(model::tagToString(tag));
1650+
}
1651+
else
1652+
LOG(warning)
1653+
<< "Unexpected empty result when querying tags "
1654+
"of C++ function: "
1655+
<< toShortDiagnosticString(node);
16221656

16231657
break;
16241658
}
@@ -1650,8 +1684,10 @@ CppServiceHandler::getTags(const std::vector<model::CppAstNode>& nodes_)
16501684
tags[node.id].push_back(model::tagToString(tag));
16511685
}
16521686
else
1653-
LOG(warning) << "Database query result was not expected to be empty. "
1654-
<< __FILE__ << ", line #" << __LINE__;
1687+
LOG(warning)
1688+
<< "Unexpected empty result when querying tags "
1689+
"of C++ variable: "
1690+
<< toShortDiagnosticString(node);
16551691

16561692
break;
16571693
}
@@ -1698,6 +1734,23 @@ std::size_t CppServiceHandler::queryCallsCount(
16981734
return _db->query_value<model::CppAstCount>(astCallsQuery(node)).count;
16991735
}
17001736

1737+
inline std::string
1738+
CppServiceHandler::toShortDiagnosticString(const model::CppAstNode& node) const
1739+
{
1740+
return std::string(node.astValue)
1741+
.append("\n AST node #").append(std::to_string(node.id))
1742+
.append("\n in file ").append(node.location.file.load()->path)
1743+
.append(" (")
1744+
.append(std::to_string(
1745+
static_cast<signed>(node.location.range.start.line))).append(":")
1746+
.append(std::to_string(
1747+
static_cast<signed>(node.location.range.start.column))).append(" - ")
1748+
.append(std::to_string(
1749+
static_cast<signed>(node.location.range.end.line))).append(":")
1750+
.append(std::to_string(
1751+
static_cast<signed>(node.location.range.end.column))).append(")");
1752+
}
1753+
17011754
} // language
17021755
} // service
17031756
} // cc

0 commit comments

Comments
 (0)