Skip to content

Commit b34f0dc

Browse files
authored
Merge pull request #423 from zporky/fix_parser_segfault
Fix C++ parser segfaults when the parent node of a variable
2 parents 8df91f7 + bc80651 commit b34f0dc

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

plugins/cpp/parser/src/symbolhelper.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <model/fileloc.h>
88
#include <model/fileloc-odb.hxx>
9+
#include <util/logutil.h>
910

1011
#include "symbolhelper.h"
1112

@@ -75,9 +76,28 @@ std::string getMangledName(
7576

7677
if (const clang::DeclContext* dc = nd_->getParentFunctionOrMethod())
7778
{
78-
result += ':' + getMangledName(
79-
mangleContext_,
80-
llvm::dyn_cast<clang::FunctionDecl>(dc));
79+
// Fixes issue #415 of parser segfaults.
80+
// https://github.com/Ericsson/CodeCompass/issues/415
81+
//
82+
// There are contexts in which getParentFunctionOrMethod() is not
83+
// null, but the result is not FunctionDecl (such as CapturedDecl),
84+
// e.g. when the to-be-mangled node is inside an OMP block.
85+
// In that case, try to find some parent DeclContext we can
86+
// reasonably mangle.
87+
while (!llvm::isa<clang::FunctionDecl>(dc) &&
88+
!llvm::isa<clang::TranslationUnitDecl>(dc))
89+
{
90+
LOG(debug) << "Yet another getparentFunctionMethod() && "
91+
"! llvm::dyn_cast<clang::FunctionDecl>(dc) ";
92+
LOG(debug) << dc->getDeclKindName();
93+
94+
dc = dc->getLookupParent();
95+
}
96+
97+
if (const auto* dcfd = llvm::dyn_cast<clang::FunctionDecl>(dc))
98+
result += ':' + getMangledName(mangleContext_, dcfd);
99+
else if (llvm::isa<clang::TranslationUnitDecl>(dc))
100+
result += ':' + std::to_string(fileLoc_.file.object_id());
81101

82102
if (const clang::ParmVarDecl* pvd =
83103
llvm::dyn_cast<clang::ParmVarDecl>(nd_))

0 commit comments

Comments
 (0)