From 511af1bd2885afb5e2d5a5adaa9ee8ec9b05946b Mon Sep 17 00:00:00 2001 From: ykiko Date: Sat, 9 Nov 2024 13:53:53 +0800 Subject: [PATCH] Some update. --- .vscode/launch.json | 2 +- CMakeLists.txt | 1 - deps/llvm | 2 +- include/Compiler/CodeComplete.h | 0 include/{AST => Compiler}/Resolver.h | 0 include/{AST => Compiler}/Selection.h | 0 include/{AST => Compiler}/TypePrinter.h | 0 include/{AST => Compiler}/Utility.h | 0 src/Compiler/Compiler.cpp | 1 - src/{AST => Compiler}/Resolver.cpp | 2 +- src/{AST => Compiler}/Selection.cpp | 2 +- src/{AST => Compiler}/Utility.cpp | 4 +- src/Index/SymbolBuilder.h | 2 +- src/Index/SymbolCollector.cpp | 89 +++++++++++++++++++------ tests/ASTVisitor/test.cpp | 22 ++---- unittests/AST/ASTVisitor.cpp | 65 ++++++++++++++++-- unittests/AST/Resolver.cpp | 2 +- unittests/AST/Selection.cpp | 2 +- unittests/Index/Tester.h | 2 - 19 files changed, 144 insertions(+), 54 deletions(-) delete mode 100644 include/Compiler/CodeComplete.h rename include/{AST => Compiler}/Resolver.h (100%) rename include/{AST => Compiler}/Selection.h (100%) rename include/{AST => Compiler}/TypePrinter.h (100%) rename include/{AST => Compiler}/Utility.h (100%) rename src/{AST => Compiler}/Resolver.cpp (99%) rename src/{AST => Compiler}/Selection.cpp (99%) rename src/{AST => Compiler}/Utility.cpp (99%) diff --git a/.vscode/launch.json b/.vscode/launch.json index a74f32b5..b57adec8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -45,7 +45,7 @@ "program": "${workspaceFolder}/build/bin/clice-tests", "args": [ "--test-dir=/home/ykiko/C++/clice2/tests", - "--gtest_filter=Index.ClassTemplate*" + "--gtest_filter=Index.VarTemplate*" ], "cwd": "${workspaceFolder}" }, diff --git a/CMakeLists.txt b/CMakeLists.txt index dc45ae8c..3e4050c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,6 @@ endif() # build clice core part as library file(GLOB_RECURSE CLICE_CORE_SOURCES - "${CMAKE_SOURCE_DIR}/src/AST/*.cpp" "${CMAKE_SOURCE_DIR}/src/Compiler/*.cpp" "${CMAKE_SOURCE_DIR}/src/Index/*.cpp" "${CMAKE_SOURCE_DIR}/src/Feature/*.cpp" diff --git a/deps/llvm b/deps/llvm index 68cd2926..cf991be6 160000 --- a/deps/llvm +++ b/deps/llvm @@ -1 +1 @@ -Subproject commit 68cd2926c6c0333dd8ee1fc9ac35ab492eedd59c +Subproject commit cf991be623559ae58f6d1740cda67095cdeb75ba diff --git a/include/Compiler/CodeComplete.h b/include/Compiler/CodeComplete.h deleted file mode 100644 index e69de29b..00000000 diff --git a/include/AST/Resolver.h b/include/Compiler/Resolver.h similarity index 100% rename from include/AST/Resolver.h rename to include/Compiler/Resolver.h diff --git a/include/AST/Selection.h b/include/Compiler/Selection.h similarity index 100% rename from include/AST/Selection.h rename to include/Compiler/Selection.h diff --git a/include/AST/TypePrinter.h b/include/Compiler/TypePrinter.h similarity index 100% rename from include/AST/TypePrinter.h rename to include/Compiler/TypePrinter.h diff --git a/include/AST/Utility.h b/include/Compiler/Utility.h similarity index 100% rename from include/AST/Utility.h rename to include/Compiler/Utility.h diff --git a/src/Compiler/Compiler.cpp b/src/Compiler/Compiler.cpp index 97f152ac..2803e5cd 100644 --- a/src/Compiler/Compiler.cpp +++ b/src/Compiler/Compiler.cpp @@ -1,7 +1,6 @@ #include #include #include -#include namespace clice { diff --git a/src/AST/Resolver.cpp b/src/Compiler/Resolver.cpp similarity index 99% rename from src/AST/Resolver.cpp rename to src/Compiler/Resolver.cpp index 2c0209c9..e4bfb666 100644 --- a/src/AST/Resolver.cpp +++ b/src/Compiler/Resolver.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/src/AST/Selection.cpp b/src/Compiler/Selection.cpp similarity index 99% rename from src/AST/Selection.cpp rename to src/Compiler/Selection.cpp index 00477d9d..93baf213 100644 --- a/src/AST/Selection.cpp +++ b/src/Compiler/Selection.cpp @@ -2,7 +2,7 @@ #include -#include +#include namespace clice { diff --git a/src/AST/Utility.cpp b/src/Compiler/Utility.cpp similarity index 99% rename from src/AST/Utility.cpp rename to src/Compiler/Utility.cpp index 652157b4..313c6fff 100644 --- a/src/AST/Utility.cpp +++ b/src/Compiler/Utility.cpp @@ -1,4 +1,4 @@ -#include +#include namespace clice { @@ -75,7 +75,7 @@ const clang::NamedDecl* instantiatedFrom(const clang::NamedDecl* decl) { return VD->getTemplateInstantiationPattern(); } - + if(auto CRD = llvm::dyn_cast(decl)) { if(auto CTD = CRD->getDescribedClassTemplate()) { return CTD; diff --git a/src/Index/SymbolBuilder.h b/src/Index/SymbolBuilder.h index 87b5fae4..107e0dfd 100644 --- a/src/Index/SymbolBuilder.h +++ b/src/Index/SymbolBuilder.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/Index/SymbolCollector.cpp b/src/Index/SymbolCollector.cpp index c6e8a58b..21b8a74a 100644 --- a/src/Index/SymbolCollector.cpp +++ b/src/Index/SymbolCollector.cpp @@ -232,7 +232,7 @@ public: VISIT_DECL(FunctionTemplateDecl) { /// `template void foo();` - /// ^~~~ definition + /// ^~~~ declaration/definition if(auto location = builder.addLocation(decl->getLocation())) { auto symbol = builder.addSymbol(decl); symbol.addOccurrence(location); @@ -419,6 +419,17 @@ public: return true; } + VISIT_TYPELOC(TemplateTypeParmTypeLoc) { + /// `template void foo(T t)` + /// ^~~~ reference + if(auto location = builder.addLocation(loc.getNameLoc())) { + auto symbol = builder.addSymbol(loc.getDecl()); + symbol.addOccurrence(location); + symbol.addReference(location); + } + return true; + } + VISIT_TYPELOC(TemplateSpecializationTypeLoc) { /// `std::vector foo` /// ^~~~ reference @@ -430,6 +441,61 @@ public: return true; } + VISIT_TYPELOC(DependentNameTypeLoc) { + loc.getNameLoc().dump(srcMgr); + return true; + } + + VISIT_TYPELOC(DependentTemplateSpecializationTypeLoc) { + return true; + } + + /// ============================================================================ + /// Specifier + /// ============================================================================ + + bool TraverseCXXBaseSpecifier(const clang::CXXBaseSpecifier& base) { + return Base::TraverseCXXBaseSpecifier(base); + } + + /// We don't care about name specifier without location information. + constexpr bool TraverseNestedNameSpecifier [[gnu::const]] (clang::NestedNameSpecifier*) { + return true; + } + + bool TraverseNestedNameSpecifierLoc(clang::NestedNameSpecifierLoc loc) { + if(auto location = builder.addLocation(loc.getLocalSourceRange())) { + auto NNS = loc.getNestedNameSpecifier(); + switch(NNS->getKind()) { + case clang::NestedNameSpecifier::Namespace: { + auto symbol = builder.addSymbol(NNS->getAsNamespace()); + symbol.addOccurrence(location); + symbol.addReference(location); + break; + } + case clang::NestedNameSpecifier::NamespaceAlias: { + auto symbol = builder.addSymbol(NNS->getAsNamespaceAlias()); + symbol.addOccurrence(location); + symbol.addReference(location); + break; + } + case clang::NestedNameSpecifier::Identifier: { + assert(NNS->isDependent() && "Identifier NNS should be dependent"); + // FIXME: use TemplateResolver here. + break; + } + case clang::NestedNameSpecifier::TypeSpec: + case clang::NestedNameSpecifier::TypeSpecWithTemplate: + case clang::NestedNameSpecifier::Global: + case clang::NestedNameSpecifier::Super: { + break; + }; + } + } + + return Base::TraverseNestedNameSpecifierLoc(loc); + } + /// ============================================================================ /// Statement /// ============================================================================ @@ -466,29 +532,10 @@ public: } VISIT_EXPR(CallExpr) { + // TODO: consider lambda expression. return true; } - /// FIXME: - constexpr bool TraverseNestedNameSpecifier [[gnu::const]] (clang::NestedNameSpecifier*) { - return true; - } - - bool TraverseNestedNameSpecifierLoc(clang::NestedNameSpecifierLoc NNS) { - // FIXME: use TemplateResolver here. - auto range = NNS.getSourceRange(); - auto range2 = NNS.getLocalSourceRange(); - return Base::TraverseNestedNameSpecifierLoc(NNS); - } - - bool TraverseTemplateArgumentLoc(const clang::TemplateArgumentLoc& argument) { - return Base::TraverseTemplateArgumentLoc(argument); - } - - bool TraverseCXXBaseSpecifier(const clang::CXXBaseSpecifier& base) { - return Base::TraverseCXXBaseSpecifier(base); - } - bool TraverseConstructorInitializer(clang::CXXCtorInitializer* init) { return Base::TraverseConstructorInitializer(init); } diff --git a/tests/ASTVisitor/test.cpp b/tests/ASTVisitor/test.cpp index 07b0965b..654a287a 100644 --- a/tests/ASTVisitor/test.cpp +++ b/tests/ASTVisitor/test.cpp @@ -1,17 +1,7 @@ -template -struct foo {}; - template -struct foo {}; - -template <> -struct foo {}; - -template struct foo; - -template struct foo; - -foo a; -foo b; -foo c; -foo d; +void foo() { + typename T::type::type::type x; + T::x = 1; + T::x.y = 2; + typename T::type::type::name y; +} diff --git a/unittests/AST/ASTVisitor.cpp b/unittests/AST/ASTVisitor.cpp index 15793624..422f4a70 100644 --- a/unittests/AST/ASTVisitor.cpp +++ b/unittests/AST/ASTVisitor.cpp @@ -2,8 +2,9 @@ #include #include #include -#include +#include #include "../Test.h" +#include "clang/AST/DeclTemplate.h" namespace { @@ -15,7 +16,62 @@ public: ASTVisitor(clang::SourceManager& srcMgr) : srcMgr(srcMgr) {} - bool VisitNamedDecl$(clang::NamedDecl* decl) { + using Base = clang::RecursiveASTVisitor; + + bool VisitDependentNameTypeLoc(clang::DependentNameTypeLoc loc) { + loc.getNameLoc().dump(srcMgr); + /// loc.dump(); + return true; + } + + bool VisitDependentScopeDeclRefExpr(clang::DependentScopeDeclRefExpr* expr) { + expr->dump(); + + return true; + } + + bool VisitCXXDependentScopeMemberExpr(clang::CXXDependentScopeMemberExpr* expr) { + expr->dump(); + return true; + } + + bool TraverseNestedNameSpecifierLoc(clang::NestedNameSpecifierLoc loc) { + if(!loc) { + return true; + } + + auto NNS = loc.getNestedNameSpecifier(); + switch(NNS->getKind()) { + case clang::NestedNameSpecifier::Identifier: { + loc.getLocalSourceRange().dump(srcMgr); + llvm::outs() << NNS << "\n"; + break; + } + } + return Base::TraverseNestedNameSpecifierLoc(loc); + } + + bool $VisitFunctionTemplateDecl(clang::FunctionTemplateDecl* decl) { + for(auto spec: decl->specializations()) { + spec->getLocation().dump(srcMgr); + spec->getFunctionTypeLoc().getReturnLoc().getLocalSourceRange().dump(srcMgr); + spec->getQualifierLoc().getSourceRange().dump(srcMgr); + + auto info = spec->getTemplateSpecializationInfo(); + info->PointOfInstantiation.dump(srcMgr); + llvm::outs() << info->TemplateArguments << "\n"; + llvm::outs() << info->TemplateArgumentsAsWritten << "\n"; + } + return true; + } + + bool VisitVarTemplateSpecializationDecl(clang::VarTemplateSpecializationDecl* decl) { + decl->dump(); + decl->getTypeSourceInfo()->getTypeLoc().getSourceRange().dump(srcMgr); + return true; + } + + bool $VisitNamedDecl(clang::NamedDecl* decl) { auto loc = decl->getLocation(); /// is the token generated from a macro argument? llvm::outs() << "is in macro arg: " << srcMgr.isMacroArgExpansion(loc) << "\n"; @@ -26,7 +82,7 @@ public: return true; }; - bool VisitTemplateSpecializationTypeLoc(clang::TemplateSpecializationTypeLoc loc) { + bool $VisitTemplateSpecializationTypeLoc(clang::TemplateSpecializationTypeLoc loc) { auto RD = loc.getType()->getAsCXXRecordDecl(); llvm::outs() << loc.getTypePtr()->getTemplateName().getAsTemplateDecl()->getCanonicalDecl() << "\n"; @@ -36,7 +92,7 @@ public: return true; } - bool VisitMemberExpr(clang::MemberExpr* expr) { + bool $VisitMemberExpr(clang::MemberExpr* expr) { expr->getMemberDecl()->dump(); // llvm::outs() << "is in macro arg: " << srcMgr.isMacroArgExpansion(loc) << "\n"; // llvm::outs() << "is in macro body: " << srcMgr.isMacroBodyExpansion(loc) << "\n"; @@ -61,6 +117,7 @@ TEST(clice, ASTVisitor) { compiler.buildAST(); ASTVisitor visitor(compiler.srcMgr()); visitor.TraverseAST(compiler.context()); + // compiler.tu()->dump(); } }); } diff --git a/unittests/AST/Resolver.cpp b/unittests/AST/Resolver.cpp index e0eda687..0f85a833 100644 --- a/unittests/AST/Resolver.cpp +++ b/unittests/AST/Resolver.cpp @@ -1,6 +1,6 @@ #include "../Test.h" -#include #include +#include namespace { diff --git a/unittests/AST/Selection.cpp b/unittests/AST/Selection.cpp index 0f46ea87..81b96721 100644 --- a/unittests/AST/Selection.cpp +++ b/unittests/AST/Selection.cpp @@ -1,6 +1,6 @@ #include "../Test.h" -#include #include +#include namespace { diff --git a/unittests/Index/Tester.h b/unittests/Index/Tester.h index a0659f77..98802468 100644 --- a/unittests/Index/Tester.h +++ b/unittests/Index/Tester.h @@ -90,8 +90,6 @@ struct IndexerTester { RelationKind::Definition, [&](const FullLocation& location) { EXPECT_EQ(location.begin, annotation.position(target)); - /// llvm::outs() << info.line() << ":" << info.column() << - /// "\n"; }); EXPECT_TRUE(success); return *this;