Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-existent files resolving as working directory #594

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion parser/src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ void incrementalCleanup(cc::parser::ParserContext& ctx_)
cc::model::FilePtr delFile = ctx_.srcMgr.getFile(item.first);

// Delete File and FileContent (only when no other File references it)
ctx_.srcMgr.removeFile(*delFile);
if (delFile)
{
ctx_.srcMgr.removeFile(*delFile);
}
break;
}
case cc::parser::IncrementalStatus::ADDED:
Expand Down
3 changes: 3 additions & 0 deletions parser/src/sourcemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ model::FilePtr SourceManager::getCreateFileEntry(

model::FilePtr SourceManager::getFile(const std::string& path_)
{
if (path_.empty())
return nullptr;

//--- Create canonical form of the path ---//

boost::system::error_code ec;
Expand Down
27 changes: 16 additions & 11 deletions plugins/cpp/parser/src/clangastvisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1246,13 +1246,16 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
if (start_.isInvalid() || end_.isInvalid())
{
fileLoc.file = getFile(start_);
const std::string& type = fileLoc.file.load()->type;
if (type != model::File::DIRECTORY_TYPE && type != _cppSourceType)
if (fileLoc.file)
{
fileLoc.file->type = _cppSourceType;
_ctx.srcMgr.updateFile(*fileLoc.file);
const std::string& type = fileLoc.file.load()->type;
if (type != model::File::DIRECTORY_TYPE && type != _cppSourceType)
{
fileLoc.file->type = _cppSourceType;
_ctx.srcMgr.updateFile(*fileLoc.file);
}
}
return fileLoc;
return fileLoc;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary indentation.

}

clang::SourceLocation realStart = start_;
Expand All @@ -1268,14 +1271,16 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>

fileLoc.file = getFile(realStart);

const std::string& type = fileLoc.file.load()->type;
if (type != model::File::DIRECTORY_TYPE && type != _cppSourceType)
if (fileLoc.file)
{
fileLoc.file->type = _cppSourceType;
_ctx.srcMgr.updateFile(*fileLoc.file);
const std::string& type = fileLoc.file.load()->type;
if (type != model::File::DIRECTORY_TYPE && type != _cppSourceType)
{
fileLoc.file->type = _cppSourceType;
_ctx.srcMgr.updateFile(*fileLoc.file);
}
}

return fileLoc;
return fileLoc;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary indentation.

}

bool isFunctionPointer(const clang::ValueDecl* vd_) const
Expand Down
105 changes: 58 additions & 47 deletions plugins/cpp/parser/src/cppparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,23 +259,28 @@ void CppParser::addCompileCommand(
{
model::BuildSource buildSource;
buildSource.file = _ctx.srcMgr.getFile(srcTarget.first);
buildSource.file->parseStatus = error_
? model::File::PSPartiallyParsed
: model::File::PSFullyParsed;
_ctx.srcMgr.updateFile(*buildSource.file);
buildSource.action = buildAction_;
sources.push_back(std::move(buildSource));
if (buildSource.file)
{
buildSource.file->parseStatus = error_
? model::File::PSPartiallyParsed
: model::File::PSFullyParsed;
_ctx.srcMgr.updateFile(*buildSource.file);
buildSource.action = buildAction_;
sources.push_back(std::move(buildSource));
}

model::BuildTarget buildTarget;
buildTarget.file = _ctx.srcMgr.getFile(srcTarget.second);
buildTarget.action = buildAction_;
if (buildTarget.file->type != model::File::BINARY_TYPE)
if (buildTarget.file)
{
buildTarget.file->type = model::File::BINARY_TYPE;
_ctx.srcMgr.updateFile(*buildTarget.file);
buildTarget.action = buildAction_;
if (buildTarget.file->type != model::File::BINARY_TYPE)
{
buildTarget.file->type = model::File::BINARY_TYPE;
_ctx.srcMgr.updateFile(*buildTarget.file);
}
targets.push_back(std::move(buildTarget));
}

targets.push_back(std::move(buildTarget));
}

_ctx.srcMgr.persistFiles();
Expand Down Expand Up @@ -374,17 +379,20 @@ std::vector<std::vector<std::string>> CppParser::createCleanupOrder()
{
auto file = _ctx.srcMgr.getFile(item.first);

auto inclusions = _ctx.db->query<model::CppHeaderInclusion>(
odb::query<model::CppHeaderInclusion>::included == file->id);

for (const auto& inclusion : inclusions)
if (file)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the early return technique if the nesting is too deep.

{
bool inserted;
model::FilePtr includer = inclusion.includer.load();
boost::graph_traits<Graph>::edge_descriptor e;
boost::tie(e, inserted) = boost::add_edge(
fileNameToVertex.at(includer->path),
fileNameToVertex.at(file->path), g);
auto inclusions = _ctx.db->query<model::CppHeaderInclusion>(
odb::query<model::CppHeaderInclusion>::included == file->id);

for (const auto& inclusion : inclusions)
{
bool inserted;
model::FilePtr includer = inclusion.includer.load();
boost::graph_traits<Graph>::edge_descriptor e;
boost::tie(e, inserted) = boost::add_edge(
fileNameToVertex.at(includer->path),
fileNameToVertex.at(file->path), g);
}
}
}
});
Expand Down Expand Up @@ -622,38 +630,41 @@ bool CppParser::cleanupWorker(const std::string& path_)
// Fetch file from SourceManager by path
model::FilePtr delFile = _ctx.srcMgr.getFile(path_);

// Query CppAstNode
auto defCppAstNodes = _ctx.db->query<model::CppAstNode>(
odb::query<model::CppAstNode>::location.file == delFile->id);

for (const model::CppAstNode& astNode : defCppAstNodes)
if (delFile)
{
// Delete CppEntity
_ctx.db->erase_query<model::CppEntity>(odb::query<model::CppEntity>::astNodeId == astNode.id);
// Query CppAstNode
auto defCppAstNodes = _ctx.db->query<model::CppAstNode>(
odb::query<model::CppAstNode>::location.file == delFile->id);

if (astNode.astType == model::CppAstNode::AstType::Definition)
for (const model::CppAstNode& astNode : defCppAstNodes)
{
// Delete CppInheritance
_ctx.db->erase_query<model::CppInheritance>(
odb::query<model::CppInheritance>::derived == astNode.entityHash);

// Delete CppFriendship
_ctx.db->erase_query<model::CppFriendship>(
odb::query<model::CppFriendship>::target == astNode.entityHash);
// Delete CppEntity
_ctx.db->erase_query<model::CppEntity>(odb::query<model::CppEntity>::astNodeId == astNode.id);

if (astNode.astType == model::CppAstNode::AstType::Definition)
{
// Delete CppInheritance
_ctx.db->erase_query<model::CppInheritance>(
odb::query<model::CppInheritance>::derived == astNode.entityHash);

// Delete CppFriendship
_ctx.db->erase_query<model::CppFriendship>(
odb::query<model::CppFriendship>::target == astNode.entityHash);
}
}
}

// Delete BuildAction
auto delSources = _ctx.db->query<model::BuildSource>(
odb::query<model::BuildSource>::file == delFile->id);
for (const model::BuildSource& source : delSources)
{
_ctx.db->erase<model::BuildAction>(source.action->id);
}
// Delete BuildAction
auto delSources = _ctx.db->query<model::BuildSource>(
odb::query<model::BuildSource>::file == delFile->id);
for (const model::BuildSource& source : delSources)
{
_ctx.db->erase<model::BuildAction>(source.action->id);
}

// Delete CppEdge (connected to File)
_ctx.db->erase_query<model::CppEdge>(odb::query<model::CppEdge>::from == delFile->id);
// Delete CppEdge (connected to File)
_ctx.db->erase_query<model::CppEdge>(odb::query<model::CppEdge>::from == delFile->id);

}
break;
}

Expand Down
45 changes: 27 additions & 18 deletions plugins/cpp/parser/src/ppincludecallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,39 +77,48 @@ void PPIncludeCallback::InclusionDirective(

std::string includedPath = searchPath_.str() + '/' + fileName_.str();
model::FilePtr included = _ctx.srcMgr.getFile(includedPath);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding extra indentation everywhere, maybe it was simpler to bring the getFile() calls from line 79 and 94 to the beginning before line 76 and make an early return if either of them is null. It would indicate better that no action is needed in that case.

included->parseStatus = model::File::PSFullyParsed;
if (included->type != model::File::DIRECTORY_TYPE &&
included->type != _cppSourceType)
if (included)
{
included->type = _cppSourceType;
_ctx.srcMgr.updateFile(*included);
included->parseStatus = model::File::PSFullyParsed;
if (included->type != model::File::DIRECTORY_TYPE &&
included->type != _cppSourceType)
{
included->type = _cppSourceType;
_ctx.srcMgr.updateFile(*included);
}
}

//--- Includer file ---//

std::string includerPath = presLoc.getFilename();
model::FilePtr includer = _ctx.srcMgr.getFile(includerPath);
includer->parseStatus = model::File::PSFullyParsed;
if (includer->type != model::File::DIRECTORY_TYPE &&
includer->type != _cppSourceType)
if (includer)
{
includer->type = _cppSourceType;
_ctx.srcMgr.updateFile(*includer);
includer->parseStatus = model::File::PSFullyParsed;
if (includer->type != model::File::DIRECTORY_TYPE &&
includer->type != _cppSourceType)
{
includer->type = _cppSourceType;
_ctx.srcMgr.updateFile(*includer);
}
}

//--- CppAstNode ---//

model::CppAstNodePtr fileNode =
createFileAstNode(included, filenameRange_.getAsRange());
if (included && includer)
{
model::CppAstNodePtr fileNode =
createFileAstNode(included, filenameRange_.getAsRange());

if (_entityCache.insert(*fileNode))
_astNodes.push_back(fileNode);
if (_entityCache.insert(*fileNode))
_astNodes.push_back(fileNode);

model::CppHeaderInclusionPtr inclusion(new model::CppHeaderInclusion);
inclusion->includer = includer;
inclusion->included = included;
model::CppHeaderInclusionPtr inclusion(new model::CppHeaderInclusion);
inclusion->includer = includer;
inclusion->included = included;

_headerIncs.push_back(inclusion);
_headerIncs.push_back(inclusion);
}
}

} // parser
Expand Down
15 changes: 9 additions & 6 deletions plugins/cpp/parser/src/ppmacrocallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,17 @@ void PPMacroCallback::addFileLoc(
_fileLocUtil.setRange(start_, end_, fileLoc.range);
fileLoc.file = _ctx.srcMgr.getFile(_fileLocUtil.getFilePath(start_));

const std::string& type = fileLoc.file.load()->type;
if (type != model::File::DIRECTORY_TYPE && type != _cppSourceType)
if (fileLoc.file)
{
fileLoc.file->type = _cppSourceType;
_ctx.srcMgr.updateFile(*fileLoc.file);
}
const std::string& type = fileLoc.file.load()->type;
if (type != model::File::DIRECTORY_TYPE && type != _cppSourceType)
{
fileLoc.file->type = _cppSourceType;
_ctx.srcMgr.updateFile(*fileLoc.file);
}

astNode_->location = fileLoc;
astNode_->location = fileLoc;
}
}

bool PPMacroCallback::isBuiltInMacro(const clang::MacroInfo* mi_) const
Expand Down