Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 57 additions & 17 deletions plugins/cpp/parser/src/clangastvisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>

model::CppAstNodePtr baseNode = std::make_shared<model::CppAstNode>();

baseNode->astValue = baseDecl->getNameAsString();
baseNode->astValue = getSourceLine(it->getLocStart());
baseNode->location = getFileLoc(it->getLocStart(), it->getLocEnd());
baseNode->mangledName
= getMangledName(_mngCtx, baseDecl, baseNode->location);
Expand Down Expand Up @@ -553,7 +553,9 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>

model::CppAstNodePtr astNode = std::make_shared<model::CppAstNode>();

astNode->astValue = rd->getNameAsString();
astNode->astValue = getSourceLine(loc.getLocStart());
if (astNode->astValue.empty())
astNode->astValue = rd->getNameAsString();
astNode->location = getFileLoc(loc.getLocStart(), loc.getLocEnd());
astNode->mangledName = getMangledName(_mngCtx, rd);
astNode->mangledNameHash = util::fnvHash(astNode->mangledName);
Expand Down Expand Up @@ -927,7 +929,7 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>

const clang::CXXConstructorDecl* ctor = ce_->getConstructor();

astNode->astValue = getSignature(ctor);
astNode->astValue = getSourceLine(ce_->getLocStart());
astNode->location = getFileLoc(ce_->getLocStart(), ce_->getLocEnd());
astNode->mangledName = getMangledName(_mngCtx, ctor);
astNode->mangledNameHash = util::fnvHash(astNode->mangledName);
Expand Down Expand Up @@ -956,7 +958,7 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>

model::CppAstNodePtr astNode = std::make_shared<model::CppAstNode>();

astNode->astValue = getSignature(functionDecl);
astNode->astValue = getSourceLine(ne_->getLocStart());
astNode->location = getFileLoc(ne_->getLocStart(), ne_->getLocEnd());
astNode->mangledName = getMangledName(_mngCtx, functionDecl);
astNode->mangledNameHash = util::fnvHash(astNode->mangledName);
Expand Down Expand Up @@ -984,7 +986,7 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>

model::CppAstNodePtr astNode = std::make_shared<model::CppAstNode>();

astNode->astValue = getSignature(functionDecl);
astNode->astValue = getSourceLine(de_->getLocStart());
astNode->location = getFileLoc(de_->getLocStart(), de_->getLocEnd());
astNode->mangledName = getMangledName(_mngCtx, functionDecl);
astNode->mangledNameHash = util::fnvHash(astNode->mangledName);
Expand Down Expand Up @@ -1014,21 +1016,26 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>

model::CppAstNodePtr astNode = std::make_shared<model::CppAstNode>();

astNode->astValue
= funcCallee
? getSignature(funcCallee)
: namedCallee->getNameAsString();
if (isVirtualCall(ce_))
{
astNode->astValue = funcCallee
? getSignature(funcCallee)
: namedCallee->getNameAsString();
astNode->astType = model::CppAstNode::AstType::VirtualCall;
}
else
{
astNode->astValue = getSourceLine(ce_->getLocStart());
astNode->astType = model::CppAstNode::AstType::Usage;
}

astNode->location = getFileLoc(ce_->getLocStart(), ce_->getLocEnd());
astNode->mangledName = getMangledName(
_mngCtx,
namedCallee,
getFileLoc(namedCallee->getLocStart(), namedCallee->getLocEnd()));
astNode->mangledNameHash = util::fnvHash(astNode->mangledName);
astNode->symbolType = model::CppAstNode::SymbolType::Function;
astNode->astType
= isVirtualCall(ce_)
? model::CppAstNode::AstType::VirtualCall
: model::CppAstNode::AstType::Usage;

astNode->id = model::createIdentifier(*astNode);

Expand All @@ -1043,13 +1050,12 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
const clang::ValueDecl* decl = dr_->getDecl();

model::CppAstNodePtr astNode = std::make_shared<model::CppAstNode>();

astNode->astValue = getSourceLine(dr_->getLocStart());
if (const clang::VarDecl* vd = llvm::dyn_cast<clang::VarDecl>(decl))
{
model::FileLoc location =
getFileLoc(vd->getLocation(), vd->getLocation());

astNode->astValue = vd->getNameAsString();
astNode->location = getFileLoc(dr_->getLocStart(), dr_->getLocEnd());
astNode->mangledName = getMangledName(_mngCtx, vd, location);
astNode->mangledNameHash = util::fnvHash(astNode->mangledName);
Expand All @@ -1067,7 +1073,6 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
else if (const clang::EnumConstantDecl* ec
= llvm::dyn_cast<clang::EnumConstantDecl>(decl))
{
astNode->astValue = ec->getNameAsString();
astNode->location = getFileLoc(ec->getLocStart(), ec->getLocEnd());
astNode->mangledName = getMangledName(_mngCtx, ec);
astNode->mangledNameHash = util::fnvHash(astNode->mangledName);
Expand All @@ -1079,7 +1084,6 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
else if (const clang::FunctionDecl* fd
= llvm::dyn_cast<clang::FunctionDecl>(decl))
{
astNode->astValue = getSignature(fd);
astNode->location = getFileLoc(fd->getLocStart(), fd->getLocEnd());
astNode->mangledName = getMangledName(_mngCtx, fd);
astNode->mangledNameHash = util::fnvHash(astNode->mangledName);
Expand Down Expand Up @@ -1408,6 +1412,42 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
return false;
}

std::string getSourceLine(const clang::SourceLocation& begin_)
{
if (begin_.isInvalid())
return std::string();

clang::SourceLocation begin = _clangSrcMgr.translateLineCol(
_clangSrcMgr.getFileID(begin_),
_clangSrcMgr.getSpellingLineNumber(begin_), 1);

clang::SourceLocation end = _clangSrcMgr.translateLineCol(
_clangSrcMgr.getFileID(begin_),
_clangSrcMgr.getSpellingLineNumber(begin_) + 1, 1);

return getSourceText(begin, end);
}

std::string getSourceText(
const clang::SourceLocation& begin_,
const clang::SourceLocation& end_)
{
clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(
_clangSrcMgr.getSpellingLoc(begin_), _clangSrcMgr.getSpellingLoc(end_));

if (range.isInvalid())
return std::string();

clang::LangOptions langOpts;
clang::StringRef src =
clang::Lexer::getSourceText(range, _clangSrcMgr, langOpts);

// Some reason `src` can contain null terminated string character(\0)
// on which pgsql will throw an error: invalid byte sequence for
// encoding "SQL_ASCII": 0x00. For this reason we call c_str on the string.
return src.str().c_str();
}

// TODO: This should be in the model.
template <typename Cont>
void persistAll(Cont& cont_)
Expand Down