From 203f59b4eae8e8e63f90206949e7bc8554d8f99e Mon Sep 17 00:00:00 2001 From: ervin7mk Date: Mon, 16 Jan 2023 08:57:14 +0100 Subject: [PATCH 1/2] Fix non-existent files resolving as working directory This commit should fix non-existent files and directories being shown in the infotree --- parser/src/sourcemanager.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/parser/src/sourcemanager.cpp b/parser/src/sourcemanager.cpp index 114db9661..9ec4a0783 100644 --- a/parser/src/sourcemanager.cpp +++ b/parser/src/sourcemanager.cpp @@ -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; From 9e4f9b97d2a9ec1468cd67fbee0597ed5b194194 Mon Sep 17 00:00:00 2001 From: ervin7mk Date: Mon, 16 Jan 2023 08:57:14 +0100 Subject: [PATCH 2/2] Fix non-existent files resolving as working directory This commit should fix non-existent files and directories being shown in the infotree --- parser/src/parser.cpp | 5 +- plugins/cpp/parser/src/clangastvisitor.h | 27 +++-- plugins/cpp/parser/src/cppparser.cpp | 105 ++++++++++--------- plugins/cpp/parser/src/ppincludecallback.cpp | 45 ++++---- plugins/cpp/parser/src/ppmacrocallback.cpp | 15 +-- 5 files changed, 114 insertions(+), 83 deletions(-) diff --git a/parser/src/parser.cpp b/parser/src/parser.cpp index 4c263d15b..857ebb139 100644 --- a/parser/src/parser.cpp +++ b/parser/src/parser.cpp @@ -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: diff --git a/plugins/cpp/parser/src/clangastvisitor.h b/plugins/cpp/parser/src/clangastvisitor.h index 699d6334f..6972078c8 100644 --- a/plugins/cpp/parser/src/clangastvisitor.h +++ b/plugins/cpp/parser/src/clangastvisitor.h @@ -1246,13 +1246,16 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor 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; } clang::SourceLocation realStart = start_; @@ -1268,14 +1271,16 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor 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; } bool isFunctionPointer(const clang::ValueDecl* vd_) const diff --git a/plugins/cpp/parser/src/cppparser.cpp b/plugins/cpp/parser/src/cppparser.cpp index 16ae1d6c0..187b66818 100644 --- a/plugins/cpp/parser/src/cppparser.cpp +++ b/plugins/cpp/parser/src/cppparser.cpp @@ -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(); @@ -374,17 +379,20 @@ std::vector> CppParser::createCleanupOrder() { auto file = _ctx.srcMgr.getFile(item.first); - auto inclusions = _ctx.db->query( - odb::query::included == file->id); - - for (const auto& inclusion : inclusions) + if (file) { - bool inserted; - model::FilePtr includer = inclusion.includer.load(); - boost::graph_traits::edge_descriptor e; - boost::tie(e, inserted) = boost::add_edge( - fileNameToVertex.at(includer->path), - fileNameToVertex.at(file->path), g); + auto inclusions = _ctx.db->query( + odb::query::included == file->id); + + for (const auto& inclusion : inclusions) + { + bool inserted; + model::FilePtr includer = inclusion.includer.load(); + boost::graph_traits::edge_descriptor e; + boost::tie(e, inserted) = boost::add_edge( + fileNameToVertex.at(includer->path), + fileNameToVertex.at(file->path), g); + } } } }); @@ -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( - odb::query::location.file == delFile->id); - - for (const model::CppAstNode& astNode : defCppAstNodes) + if (delFile) { - // Delete CppEntity - _ctx.db->erase_query(odb::query::astNodeId == astNode.id); + // Query CppAstNode + auto defCppAstNodes = _ctx.db->query( + odb::query::location.file == delFile->id); - if (astNode.astType == model::CppAstNode::AstType::Definition) + for (const model::CppAstNode& astNode : defCppAstNodes) { - // Delete CppInheritance - _ctx.db->erase_query( - odb::query::derived == astNode.entityHash); - - // Delete CppFriendship - _ctx.db->erase_query( - odb::query::target == astNode.entityHash); + // Delete CppEntity + _ctx.db->erase_query(odb::query::astNodeId == astNode.id); + + if (astNode.astType == model::CppAstNode::AstType::Definition) + { + // Delete CppInheritance + _ctx.db->erase_query( + odb::query::derived == astNode.entityHash); + + // Delete CppFriendship + _ctx.db->erase_query( + odb::query::target == astNode.entityHash); + } } - } - // Delete BuildAction - auto delSources = _ctx.db->query( - odb::query::file == delFile->id); - for (const model::BuildSource& source : delSources) - { - _ctx.db->erase(source.action->id); - } + // Delete BuildAction + auto delSources = _ctx.db->query( + odb::query::file == delFile->id); + for (const model::BuildSource& source : delSources) + { + _ctx.db->erase(source.action->id); + } - // Delete CppEdge (connected to File) - _ctx.db->erase_query(odb::query::from == delFile->id); + // Delete CppEdge (connected to File) + _ctx.db->erase_query(odb::query::from == delFile->id); + } break; } diff --git a/plugins/cpp/parser/src/ppincludecallback.cpp b/plugins/cpp/parser/src/ppincludecallback.cpp index b163732fe..13341e205 100644 --- a/plugins/cpp/parser/src/ppincludecallback.cpp +++ b/plugins/cpp/parser/src/ppincludecallback.cpp @@ -77,39 +77,48 @@ void PPIncludeCallback::InclusionDirective( std::string includedPath = searchPath_.str() + '/' + fileName_.str(); model::FilePtr included = _ctx.srcMgr.getFile(includedPath); - 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 diff --git a/plugins/cpp/parser/src/ppmacrocallback.cpp b/plugins/cpp/parser/src/ppmacrocallback.cpp index d86532e32..7b9d48fa1 100644 --- a/plugins/cpp/parser/src/ppmacrocallback.cpp +++ b/plugins/cpp/parser/src/ppmacrocallback.cpp @@ -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