Clang import; FunctionTemplateDecl

This commit is contained in:
Daniel Marjamäki 2020-01-08 20:47:52 +01:00
parent 27be51cb49
commit eb3270959e
2 changed files with 62 additions and 0 deletions

View File

@ -39,6 +39,7 @@ static const std::string DeclStmt = "DeclStmt";
static const std::string FieldDecl = "FieldDecl";
static const std::string ForStmt = "ForStmt";
static const std::string FunctionDecl = "FunctionDecl";
static const std::string FunctionTemplateDecl = "FunctionTemplateDecl";
static const std::string IfStmt = "IfStmt";
static const std::string ImplicitCastExpr = "ImplicitCastExpr";
static const std::string IntegerLiteral = "IntegerLiteral";
@ -49,6 +50,7 @@ static const std::string ParmVarDecl = "ParmVarDecl";
static const std::string RecordDecl = "RecordDecl";
static const std::string ReturnStmt = "ReturnStmt";
static const std::string StringLiteral = "StringLiteral";
static const std::string TemplateArgument = "TemplateArgument";
static const std::string TypedefDecl = "TypedefDecl";
static const std::string UnaryOperator = "UnaryOperator";
static const std::string VarDecl = "VarDecl";
@ -401,6 +403,17 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
createTokensFunctionDecl(tokenList);
return nullptr;
}
if (nodeType == FunctionTemplateDecl) {
bool first = true;
for (AstNodePtr child: children) {
if (child->nodeType == FunctionDecl) {
if (!first)
child->createTokens(tokenList);
first = false;
}
}
return nullptr;
}
if (nodeType == IfStmt) {
AstNodePtr cond = children[2];
AstNodePtr then = children[3];
@ -503,6 +516,19 @@ void clangastdump::AstNode::createTokensFunctionDecl(TokenList *tokenList)
const int retTypeIndex = nameIndex + 1;
addTypeTokens(tokenList, mExtTokens[retTypeIndex]);
Token *nameToken = addtoken(tokenList, mExtTokens[nameIndex]);
if (!children.empty() && children[0]->nodeType == TemplateArgument) {
std::string templateParameters;
for (AstNodePtr child: children) {
if (child->nodeType == TemplateArgument) {
if (templateParameters.empty())
templateParameters = "<";
else
templateParameters += ",";
templateParameters += unquote(child->mExtTokens.back());
}
}
nameToken->str(nameToken->str() + templateParameters + ">");
}
Scope *nestedIn = const_cast<Scope *>(nameToken->scope());
symbolDatabase->scopeList.push_back(Scope(nullptr, nullptr, nestedIn));
Scope &scope = symbolDatabase->scopeList.back();

View File

@ -37,6 +37,8 @@ private:
TEST_CASE(funcdecl2);
TEST_CASE(funcdecl3);
TEST_CASE(funcdecl4);
TEST_CASE(functionTemplateDecl1);
TEST_CASE(functionTemplateDecl2);
TEST_CASE(ifelse);
TEST_CASE(memberExpr);
TEST_CASE(namespaceDecl);
@ -153,6 +155,40 @@ private:
ASSERT_EQUALS("unsigned long fwrite ( const void * , unsigned long , unsigned long , FILE * ) ;", parse(clang));
}
void functionTemplateDecl1() {
const char clang[] = "`-FunctionTemplateDecl 0x3242860 <a.cpp:1:1, col:46> col:21 foo";
ASSERT_EQUALS("", parse(clang));
}
void functionTemplateDecl2() {
const char clang[] = "|-FunctionTemplateDecl 0x333a860 <a.cpp:1:1, col:46> col:21 foo\n"
"| |-TemplateTypeParmDecl 0x333a5f8 <col:10, col:16> col:16 referenced class depth 0 index 0 T\n"
"| |-FunctionDecl 0x333a7c0 <col:19, col:46> col:21 foo 'T (T)'\n"
"| | |-ParmVarDecl 0x333a6c0 <col:25, col:27> col:27 referenced t 'T'\n"
"| | `-CompoundStmt 0x333a980 <col:30, col:46>\n"
"| | `-ReturnStmt 0x333a968 <col:32, col:43>\n"
"| | `-BinaryOperator 0x333a940 <col:39, col:43> '<dependent type>' '+'\n"
"| | |-DeclRefExpr 0x333a8f8 <col:39> 'T' lvalue ParmVar 0x333a6c0 't' 'T'\n"
"| | `-IntegerLiteral 0x333a920 <col:43> 'int' 1\n"
"| `-FunctionDecl 0x333ae00 <col:19, col:46> col:21 used foo 'int (int)'\n"
"| |-TemplateArgument type 'int'\n"
"| |-ParmVarDecl 0x333ad00 <col:25, col:27> col:27 used t 'int':'int'\n"
"| `-CompoundStmt 0x333b0a8 <col:30, col:46>\n"
"| `-ReturnStmt 0x333b090 <col:32, col:43>\n"
"| `-BinaryOperator 0x333b068 <col:39, col:43> 'int' '+'\n"
"| |-ImplicitCastExpr 0x333b050 <col:39> 'int':'int' <LValueToRValue>\n"
"| | `-DeclRefExpr 0x333b028 <col:39> 'int':'int' lvalue ParmVar 0x333ad00 't' 'int':'int'\n"
"| `-IntegerLiteral 0x333a920 <col:43> 'int' 1\n"
"`-FunctionDecl 0x333a9f8 <line:2:1, col:22> col:1 invalid bar 'int ()'\n"
" `-CompoundStmt 0x333b010 <col:7, col:22>\n"
" `-CallExpr 0x333afe0 <col:9, col:19> 'int':'int'\n"
" |-ImplicitCastExpr 0x333afc8 <col:9, col:16> 'int (*)(int)' <FunctionToPointerDecay>\n"
" | `-DeclRefExpr 0x333af00 <col:9, col:16> 'int (int)' lvalue Function 0x333ae00 'foo' 'int (int)' (FunctionTemplate 0x333a860 'foo')\n"
" `-IntegerLiteral 0x333ab48 <col:18> 'int' 1";
ASSERT_EQUALS("int foo<int> ( int t@1 ) { return t@1 + 1 ; }\n"
"int bar ( ) { foo ( 1 ) ; }", parse(clang));
}
void ifelse() {
const char clang[] = "`-FunctionDecl 0x2637ba8 <1.c:1:1, line:4:1> line:1:5 foo 'int (int)'\n"
" |-ParmVarDecl 0x2637ae0 <col:9, col:13> col:13 used x 'int'\n"