From 5640c86d21765efd41283d770d1ac0a1297a0c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bendeg=C3=BAz=20Fily=C3=B3?= Date: Fri, 30 Oct 2020 13:06:07 +0100 Subject: [PATCH 1/7] Ast visitor function for using declarations. --- plugins/cpp/parser/src/clangastvisitor.h | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/plugins/cpp/parser/src/clangastvisitor.h b/plugins/cpp/parser/src/clangastvisitor.h index 699d6334f..b5f83bae4 100644 --- a/plugins/cpp/parser/src/clangastvisitor.h +++ b/plugins/cpp/parser/src/clangastvisitor.h @@ -956,6 +956,33 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor return true; } + + + + bool VisitUsingDecl(clang::UsingDecl* ud_) + { + + //--- CppAstNode ---// + + model::CppAstNodePtr astNode = std::make_shared(); + + astNode->astValue = getSourceText( + _clangSrcMgr, + ud_->getBeginLoc(), + ud_->getLocation(), + true); + std::string usr = getUSR(ud_); + astNode->location = getFileLoc(ud_->getBeginLoc(), ud_->getEndLoc()); + astNode->entityHash = util::fnvHash(usr); + astNode->astType = model::CppAstNode::AstType::Definition; + astNode->id = model::createIdentifier(*astNode); + + if (insertToCache(ud_, astNode)) + _astNodes.push_back(astNode); + + return true; + } + bool VisitCXXConstructExpr(clang::CXXConstructExpr* ce_) { From 3c682a00baf37bfae35ca9a5abd285652cd59469 Mon Sep 17 00:00:00 2001 From: filbeofITK Date: Thu, 1 Jul 2021 12:20:57 +0200 Subject: [PATCH 2/7] Added the using directive definition visitor function, so it works with namespaces as well. --- plugins/cpp/parser/src/clangastvisitor.h | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/plugins/cpp/parser/src/clangastvisitor.h b/plugins/cpp/parser/src/clangastvisitor.h index b5f83bae4..fc4314822 100644 --- a/plugins/cpp/parser/src/clangastvisitor.h +++ b/plugins/cpp/parser/src/clangastvisitor.h @@ -983,6 +983,33 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor return true; } + + bool VisitUsingDirectiveDecl(clang::UsingDirectiveDecl* udd_) + { + //--- CppAstNode ---// + + model::CppAstNodePtr astNode = std::make_shared(); + + astNode->astValue = getSourceText( + _clangSrcMgr, + udd_->getBeginLoc(), + udd_->getLocation(), + true); + std::string usr = getUSR(udd_); + astNode->location = getFileLoc(udd_->getBeginLoc(), udd_->getEndLoc()); + astNode->entityHash = util::fnvHash(usr); + astNode->astType = model::CppAstNode::AstType::Definition; + astNode->id = model::createIdentifier(*astNode); + + if (insertToCache(udd_, astNode)) + _astNodes.push_back(astNode); + + return true; + + + } + + bool VisitCXXConstructExpr(clang::CXXConstructExpr* ce_) { From e3ecefa888f7126e9267180d592fc3fe554acc16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Cser=C3=A9p?= Date: Sat, 11 Feb 2023 00:42:10 +0100 Subject: [PATCH 3/7] Fix VisitUsingDirectiveDecl --- plugins/cpp/parser/src/clangastvisitor.h | 30 ++++++++++-------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/plugins/cpp/parser/src/clangastvisitor.h b/plugins/cpp/parser/src/clangastvisitor.h index fc4314822..306b9c360 100644 --- a/plugins/cpp/parser/src/clangastvisitor.h +++ b/plugins/cpp/parser/src/clangastvisitor.h @@ -957,24 +957,22 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor return true; } - - bool VisitUsingDecl(clang::UsingDecl* ud_) { - //--- CppAstNode ---// model::CppAstNodePtr astNode = std::make_shared(); - + astNode->astValue = getSourceText( _clangSrcMgr, ud_->getBeginLoc(), ud_->getLocation(), true); - std::string usr = getUSR(ud_); astNode->location = getFileLoc(ud_->getBeginLoc(), ud_->getEndLoc()); - astNode->entityHash = util::fnvHash(usr); - astNode->astType = model::CppAstNode::AstType::Definition; + astNode->entityHash = util::fnvHash(getUSR(ud_)); + astNode->symbolType = model::CppAstNode::SymbolType::Other; + astNode->astType = model::CppAstNode::AstType::Usage; + astNode->id = model::createIdentifier(*astNode); if (insertToCache(ud_, astNode)) @@ -983,34 +981,32 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor return true; } - bool VisitUsingDirectiveDecl(clang::UsingDirectiveDecl* udd_) { - //--- CppAstNode ---// + //--- CppAstNode ---// model::CppAstNodePtr astNode = std::make_shared(); - + + const clang::NamespaceDecl* nd = udd_->getNominatedNamespace(); + astNode->astValue = getSourceText( _clangSrcMgr, udd_->getBeginLoc(), udd_->getLocation(), true); - std::string usr = getUSR(udd_); astNode->location = getFileLoc(udd_->getBeginLoc(), udd_->getEndLoc()); - astNode->entityHash = util::fnvHash(usr); - astNode->astType = model::CppAstNode::AstType::Definition; + astNode->entityHash = util::fnvHash(getUSR(nd)); + astNode->symbolType = model::CppAstNode::SymbolType::Namespace; + astNode->astType = model::CppAstNode::AstType::Usage; + astNode->id = model::createIdentifier(*astNode); if (insertToCache(udd_, astNode)) _astNodes.push_back(astNode); return true; - - } - - bool VisitCXXConstructExpr(clang::CXXConstructExpr* ce_) { model::CppAstNodePtr astNode = std::make_shared(); From 06c31a30f88d9c7ae714bb7f56126c5a95e7abff Mon Sep 17 00:00:00 2001 From: Bela Schaum Date: Mon, 2 Oct 2023 21:29:35 +0200 Subject: [PATCH 4/7] AST visitor for using declarations. Multiple node --- plugins/cpp/parser/src/clangastvisitor.h | 35 +++++++++++++----------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/plugins/cpp/parser/src/clangastvisitor.h b/plugins/cpp/parser/src/clangastvisitor.h index 690dffd8e..2a90447e6 100644 --- a/plugins/cpp/parser/src/clangastvisitor.h +++ b/plugins/cpp/parser/src/clangastvisitor.h @@ -961,24 +961,27 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor bool VisitUsingDecl(clang::UsingDecl* ud_) { //--- CppAstNode ---// - - model::CppAstNodePtr astNode = std::make_shared(); - astNode->astValue = getSourceText( - _clangSrcMgr, - ud_->getBeginLoc(), - ud_->getLocation(), - true); - astNode->location = getFileLoc(ud_->getBeginLoc(), ud_->getEndLoc()); - astNode->entityHash = util::fnvHash(getUSR(ud_)); - astNode->symbolType = model::CppAstNode::SymbolType::Other; - astNode->astType = model::CppAstNode::AstType::Usage; + for (const clang::UsingShadowDecl* nd : ud_->shadows()) { + model::CppAstNodePtr astNode = std::make_shared(); + + astNode->astValue = getSourceText( + _clangSrcMgr, + ud_->getBeginLoc(), + ud_->getLocation(), + true); + astNode->location = getFileLoc(ud_->getBeginLoc(), ud_->getEndLoc()); + astNode->entityHash = util::fnvHash(getUSR(nd->getTargetDecl())); + + astNode->symbolType = model::CppAstNode::SymbolType::Other; + astNode->astType = model::CppAstNode::AstType::Usage; + + astNode->id = model::createIdentifier(*astNode); + + if (insertToCache(nd, astNode)) + _astNodes.push_back(astNode); + } - astNode->id = model::createIdentifier(*astNode); - - if (insertToCache(ud_, astNode)) - _astNodes.push_back(astNode); - return true; } From cd2df2604ccd4a9d882091c286881cf18456c12e Mon Sep 17 00:00:00 2001 From: Bela Schaum Date: Sat, 14 Oct 2023 09:28:00 +0200 Subject: [PATCH 5/7] Fix namespace test --- plugins/cpp/test/src/cppparsertest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/cpp/test/src/cppparsertest.cpp b/plugins/cpp/test/src/cppparsertest.cpp index 4358af277..2770b40d7 100644 --- a/plugins/cpp/test/src/cppparsertest.cpp +++ b/plugins/cpp/test/src/cppparsertest.cpp @@ -686,11 +686,11 @@ TEST_F(CppParserTest, Namespace) model::CppNamespace myNamespace1 = _db->query_value( QCppNamespace::name == "MyNamespace1"); model::CppAstNode astNode = _db->query_value( - QCppAstNode::entityHash == myNamespace1.entityHash); + QCppAstNode::entityHash == myNamespace1.entityHash && + QCppAstNode::astType == model::CppAstNode::AstType::Definition); EXPECT_EQ(astNode.symbolType, model::CppAstNode::SymbolType::Namespace); EXPECT_EQ(astNode.location.range.start.line, 1); - EXPECT_EQ(astNode.astType, model::CppAstNode::AstType::Definition); model::CppNamespace myNamespace2 = _db->query_value( QCppNamespace::name == "MyNamespace2"); From 2a7ddf397c722a8c9613c764b702890f00d866a0 Mon Sep 17 00:00:00 2001 From: Bela Schaum Date: Tue, 24 Oct 2023 22:08:47 +0200 Subject: [PATCH 6/7] Add tests + Add astType --- plugins/cpp/model/include/model/cppastnode.h | 2 + plugins/cpp/parser/src/clangastvisitor.h | 2 +- .../cpp/test/sources/parser/CMakeLists.txt | 1 + plugins/cpp/test/sources/parser/using.cpp | 33 +++++++++ plugins/cpp/test/src/cppparsertest.cpp | 69 ++++++++++++++++++- 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 plugins/cpp/test/sources/parser/using.cpp diff --git a/plugins/cpp/model/include/model/cppastnode.h b/plugins/cpp/model/include/model/cppastnode.h index ab6a8bc48..919c724ee 100644 --- a/plugins/cpp/model/include/model/cppastnode.h +++ b/plugins/cpp/model/include/model/cppastnode.h @@ -57,6 +57,7 @@ struct CppAstNode LocalTypeLoc, TypedefTypeLoc, InheritanceTypeLoc, + Resolution, Other = 1000 }; @@ -131,6 +132,7 @@ inline std::string astTypeToString(CppAstNode::AstType type_) case CppAstNode::AstType::LocalTypeLoc: return "LocalTypeLoc"; case CppAstNode::AstType::TypedefTypeLoc: return "TypedefTypeLoc"; case CppAstNode::AstType::InheritanceTypeLoc: return "InheritanceTypeLoc"; + case CppAstNode::AstType::Resolution: return "Resolution"; case CppAstNode::AstType::Other: return "Other"; } diff --git a/plugins/cpp/parser/src/clangastvisitor.h b/plugins/cpp/parser/src/clangastvisitor.h index 2a90447e6..2fa8f71a1 100644 --- a/plugins/cpp/parser/src/clangastvisitor.h +++ b/plugins/cpp/parser/src/clangastvisitor.h @@ -974,7 +974,7 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor astNode->entityHash = util::fnvHash(getUSR(nd->getTargetDecl())); astNode->symbolType = model::CppAstNode::SymbolType::Other; - astNode->astType = model::CppAstNode::AstType::Usage; + astNode->astType = model::CppAstNode::AstType::Resolution; astNode->id = model::createIdentifier(*astNode); diff --git a/plugins/cpp/test/sources/parser/CMakeLists.txt b/plugins/cpp/test/sources/parser/CMakeLists.txt index 9c76a5ae1..d1a7f125a 100644 --- a/plugins/cpp/test/sources/parser/CMakeLists.txt +++ b/plugins/cpp/test/sources/parser/CMakeLists.txt @@ -9,5 +9,6 @@ add_library(CppTestProject STATIC cxxrecord.cpp enum.cpp function.cpp + using.cpp variable.cpp namespace.cpp) diff --git a/plugins/cpp/test/sources/parser/using.cpp b/plugins/cpp/test/sources/parser/using.cpp new file mode 100644 index 000000000..435cf15a6 --- /dev/null +++ b/plugins/cpp/test/sources/parser/using.cpp @@ -0,0 +1,33 @@ +namespace Nested +{ +namespace MyNamespace +{ +class C +{ +}; + +void g(C) { } + +void g(double) { } + +constexpr C VAR1{}; + +template +constexpr T VAR2 = T{}; +} +} + +using namespace Nested; +void g() {} +using MyNamespace::g; + +void using_fun() +{ + using MyNamespace::C; + using MyNamespace::VAR1; + using MyNamespace::VAR2; + + g(); + g(VAR1); + g(VAR2); +} \ No newline at end of file diff --git a/plugins/cpp/test/src/cppparsertest.cpp b/plugins/cpp/test/src/cppparsertest.cpp index 2770b40d7..e09ea776e 100644 --- a/plugins/cpp/test/src/cppparsertest.cpp +++ b/plugins/cpp/test/src/cppparsertest.cpp @@ -83,6 +83,10 @@ TEST_F(CppParserTest, FilesAreInDatabase) file = _db->query_value(QFile::filename == "namespace.cpp"); EXPECT_EQ(file.type, "CPP"); EXPECT_EQ(file.parseStatus, model::File::PSFullyParsed); + + file = _db->query_value(QFile::filename == "using.cpp"); + EXPECT_EQ(file.type, "CPP"); + EXPECT_EQ(file.parseStatus, model::File::PSFullyParsed); }); } @@ -546,7 +550,7 @@ TEST_F(CppParserTest, Fields) { _transaction([&, this] { model::CppVariable fieldFunction = _db->query_value( - QCppFunction::name == "fieldFunction"); + QCppVariable::name == "fieldFunction"); RCppAstNode astNodes = _db->query( QCppAstNode::entityHash == fieldFunction.entityHash); @@ -702,3 +706,66 @@ TEST_F(CppParserTest, Namespace) EXPECT_EQ(astNode.astType, model::CppAstNode::AstType::Definition); }); } + +TEST_F(CppParserTest, Using) +{ + _transaction([&, this] { + model::CppNamespace nested = _db->query_value( + QCppNamespace::name == "Nested"); + + model::CppAstNode astNode = _db->query_value( + QCppAstNode::entityHash == nested.entityHash && + QCppAstNode::astType == model::CppAstNode::AstType::Usage); + + EXPECT_EQ(astNode.symbolType, model::CppAstNode::SymbolType::Namespace); + EXPECT_EQ(astNode.location.range.start.line, 20); + + + model::CppRecord cClass = _db->query_value( + QCppRecord::qualifiedName == "Nested::MyNamespace::C"); + + astNode = _db->query_value( + QCppAstNode::entityHash == cClass.entityHash && + QCppAstNode::astType == model::CppAstNode::AstType::Resolution); + + EXPECT_EQ(astNode.symbolType, model::CppAstNode::SymbolType::Other); + EXPECT_EQ(astNode.location.range.start.line, 26); + + + model::CppVariable var1 = _db->query_value( + QCppVariable::name == "VAR1"); + + astNode = _db->query_value( + QCppAstNode::entityHash == var1.entityHash && + QCppAstNode::astType == model::CppAstNode::AstType::Resolution); + + EXPECT_EQ(astNode.symbolType, model::CppAstNode::SymbolType::Other); + EXPECT_EQ(astNode.location.range.start.line, 27); + + astNode = _db->query_value( + QCppAstNode::entityHash == var1.entityHash && + QCppAstNode::astType == model::CppAstNode::AstType::Read); + + EXPECT_EQ(astNode.symbolType, model::CppAstNode::SymbolType::Variable); + EXPECT_EQ(astNode.location.range.start.line, 31); + + RCppFunction functions_with_g = _db->query( + QCppFunction::qualifiedName == "Nested::MyNamespace::g"); + + for (const model::CppFunction& func : functions_with_g) { + astNode = _db->query_value( + QCppAstNode::entityHash == func.entityHash && + QCppAstNode::astType == model::CppAstNode::AstType::Resolution); + + EXPECT_EQ(astNode.symbolType, model::CppAstNode::SymbolType::Other); + EXPECT_EQ(astNode.location.range.start.line, 22); + + + astNode = _db->query_value( + QCppAstNode::entityHash == func.entityHash && + QCppAstNode::astType == model::CppAstNode::AstType::Usage); + + EXPECT_EQ(astNode.symbolType, model::CppAstNode::SymbolType::Function); + } + }); +} \ No newline at end of file From b108fc2e5c851d12f52cab266765a0e17b477b46 Mon Sep 17 00:00:00 2001 From: Bela Schaum Date: Mon, 13 Nov 2023 09:42:22 +0100 Subject: [PATCH 7/7] Rename resolution to UsingLoc --- plugins/cpp/model/include/model/cppastnode.h | 4 ++-- plugins/cpp/parser/src/clangastvisitor.h | 2 +- plugins/cpp/test/src/cppparsertest.cpp | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/cpp/model/include/model/cppastnode.h b/plugins/cpp/model/include/model/cppastnode.h index b1d47fc41..67f93507b 100644 --- a/plugins/cpp/model/include/model/cppastnode.h +++ b/plugins/cpp/model/include/model/cppastnode.h @@ -58,7 +58,7 @@ struct CppAstNode LocalTypeLoc, TypedefTypeLoc, InheritanceTypeLoc, - Resolution, + UsingLoc, Other = 1000 }; @@ -134,7 +134,7 @@ inline std::string astTypeToString(CppAstNode::AstType type_) case CppAstNode::AstType::LocalTypeLoc: return "LocalTypeLoc"; case CppAstNode::AstType::TypedefTypeLoc: return "TypedefTypeLoc"; case CppAstNode::AstType::InheritanceTypeLoc: return "InheritanceTypeLoc"; - case CppAstNode::AstType::Resolution: return "Resolution"; + case CppAstNode::AstType::UsingLoc: return "UsingLoc"; case CppAstNode::AstType::Other: return "Other"; } diff --git a/plugins/cpp/parser/src/clangastvisitor.h b/plugins/cpp/parser/src/clangastvisitor.h index c84cf38dc..f50af517c 100644 --- a/plugins/cpp/parser/src/clangastvisitor.h +++ b/plugins/cpp/parser/src/clangastvisitor.h @@ -1015,7 +1015,7 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor astNode->entityHash = util::fnvHash(getUSR(nd->getTargetDecl())); astNode->symbolType = model::CppAstNode::SymbolType::Other; - astNode->astType = model::CppAstNode::AstType::Resolution; + astNode->astType = model::CppAstNode::AstType::UsingLoc; astNode->id = model::createIdentifier(*astNode); diff --git a/plugins/cpp/test/src/cppparsertest.cpp b/plugins/cpp/test/src/cppparsertest.cpp index e09ea776e..f90e21d2f 100644 --- a/plugins/cpp/test/src/cppparsertest.cpp +++ b/plugins/cpp/test/src/cppparsertest.cpp @@ -726,7 +726,7 @@ TEST_F(CppParserTest, Using) astNode = _db->query_value( QCppAstNode::entityHash == cClass.entityHash && - QCppAstNode::astType == model::CppAstNode::AstType::Resolution); + QCppAstNode::astType == model::CppAstNode::AstType::UsingLoc); EXPECT_EQ(astNode.symbolType, model::CppAstNode::SymbolType::Other); EXPECT_EQ(astNode.location.range.start.line, 26); @@ -737,7 +737,7 @@ TEST_F(CppParserTest, Using) astNode = _db->query_value( QCppAstNode::entityHash == var1.entityHash && - QCppAstNode::astType == model::CppAstNode::AstType::Resolution); + QCppAstNode::astType == model::CppAstNode::AstType::UsingLoc); EXPECT_EQ(astNode.symbolType, model::CppAstNode::SymbolType::Other); EXPECT_EQ(astNode.location.range.start.line, 27); @@ -755,7 +755,7 @@ TEST_F(CppParserTest, Using) for (const model::CppFunction& func : functions_with_g) { astNode = _db->query_value( QCppAstNode::entityHash == func.entityHash && - QCppAstNode::astType == model::CppAstNode::AstType::Resolution); + QCppAstNode::astType == model::CppAstNode::AstType::UsingLoc); EXPECT_EQ(astNode.symbolType, model::CppAstNode::SymbolType::Other); EXPECT_EQ(astNode.location.range.start.line, 22);