Some update.

This commit is contained in:
ykiko
2024-11-09 13:53:53 +08:00
parent 2f27dc2692
commit 511af1bd28
19 changed files with 144 additions and 54 deletions

2
.vscode/launch.json vendored
View File

@@ -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}"
},

View File

@@ -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"

2
deps/llvm vendored

Submodule deps/llvm updated: 68cd2926c6...cf991be623

View File

@@ -1,7 +1,6 @@
#include <Compiler/Compiler.h>
#include <clang/Lex/PreprocessorOptions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <Compiler/CodeComplete.h>
namespace clice {

View File

@@ -1,4 +1,4 @@
#include <AST/Resolver.h>
#include <Compiler/Resolver.h>
#include <clang/Sema/Template.h>
#include <clang/Sema/TreeTransform.h>
#include <clang/Sema/TemplateDeduction.h>

View File

@@ -2,7 +2,7 @@
#include <stack>
#include <AST/Selection.h>
#include <Compiler/Selection.h>
namespace clice {

View File

@@ -1,4 +1,4 @@
#include <AST/Utility.h>
#include <Compiler/Utility.h>
namespace clice {
@@ -75,7 +75,7 @@ const clang::NamedDecl* instantiatedFrom(const clang::NamedDecl* decl) {
return VD->getTemplateInstantiationPattern();
}
if(auto CRD = llvm::dyn_cast<clang::CXXRecordDecl>(decl)) {
if(auto CTD = CRD->getDescribedClassTemplate()) {
return CTD;

View File

@@ -3,7 +3,7 @@
#include <clang/AST/DeclCXX.h>
#include <clang/Index/USRGeneration.h>
#include <AST/Utility.h>
#include <Compiler/Utility.h>
#include <Compiler/Compiler.h>
#include <Index/Indexer.h>
#include <Support/FileSystem.h>

View File

@@ -232,7 +232,7 @@ public:
VISIT_DECL(FunctionTemplateDecl) {
/// `template <typename T> 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 <typename T> 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<int> 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);
}

View File

@@ -1,17 +1,7 @@
template <typename T, typename U>
struct foo {};
template <typename T>
struct foo<T, T> {};
template <>
struct foo<int, int> {};
template struct foo<char, int>;
template struct foo<char, char>;
foo<int, int> a;
foo<int, char> b;
foo<char, int> c;
foo<char, char> d;
void foo() {
typename T::type::type::type x;
T::x = 1;
T::x.y = 2;
typename T::type::type::name y;
}

View File

@@ -2,8 +2,9 @@
#include <Support/JSON.h>
#include <Compiler/Compiler.h>
#include <Support/FileSystem.h>
#include <AST/Utility.h>
#include <Compiler//Utility.h>
#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<ASTVisitor>;
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();
}
});
}

View File

@@ -1,6 +1,6 @@
#include "../Test.h"
#include <AST/Resolver.h>
#include <Compiler/Compiler.h>
#include <Compiler/Resolver.h>
namespace {

View File

@@ -1,6 +1,6 @@
#include "../Test.h"
#include <AST/Selection.h>
#include <Compiler/Compiler.h>
#include <Compiler//Selection.h>
namespace {

View File

@@ -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;