Clang import: Improved function pointer

This commit is contained in:
Daniel Marjamäki 2020-12-30 16:23:54 +01:00
parent 277da763aa
commit f986b37a50
2 changed files with 33 additions and 13 deletions

View File

@ -22,10 +22,12 @@
#include "tokenize.h" #include "tokenize.h"
#include "utils.h" #include "utils.h"
#include <cstring>
#include <algorithm> #include <algorithm>
#include <memory>
#include <vector>
#include <iostream> #include <iostream>
#include <memory>
#include <stack>
#include <vector>
static const std::string AccessSpecDecl = "AccessSpecDecl"; static const std::string AccessSpecDecl = "AccessSpecDecl";
static const std::string ArraySubscriptExpr = "ArraySubscriptExpr"; static const std::string ArraySubscriptExpr = "ArraySubscriptExpr";
@ -110,8 +112,8 @@ static std::vector<std::string> splitString(const std::string &line)
std::string::size_type pos1 = line.find_first_not_of(" "); std::string::size_type pos1 = line.find_first_not_of(" ");
while (pos1 < line.size()) { while (pos1 < line.size()) {
std::string::size_type pos2; std::string::size_type pos2;
if (line[pos1] == '*') { if (std::strchr("*()", line[pos1])) {
ret.push_back("*"); ret.push_back(line.substr(pos1,1));
pos1 = line.find_first_not_of(" ", pos1 + 1); pos1 = line.find_first_not_of(" ", pos1 + 1);
continue; continue;
} }
@ -509,8 +511,21 @@ const ::Type * clangimport::AstNode::addTypeTokens(TokenList *tokenList, const s
} else } else
type = unquote(str); type = unquote(str);
for (const std::string &s: splitString(type)) if (type.find("(*)(") != std::string::npos) {
addtoken(tokenList, s, false); type.erase(type.find("(*)("));
type += "*";
}
std::stack<Token *> lpar;
for (const std::string &s: splitString(type)) {
Token *tok = addtoken(tokenList, s, false);
if (tok->str() == "(")
lpar.push(tok);
else if (tok->str() == ")") {
Token::createMutualLinks(tok, lpar.top());
lpar.pop();
}
}
// Set Type // Set Type
if (!scope) { if (!scope) {
@ -1379,20 +1394,19 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList)
Token * clangimport::AstNode::createTokensVarDecl(TokenList *tokenList) Token * clangimport::AstNode::createTokensVarDecl(TokenList *tokenList)
{ {
const std::string addr = mExtTokens.front(); const std::string addr = mExtTokens.front();
const Token *startToken = nullptr;
if (contains(mExtTokens, "static")) if (contains(mExtTokens, "static"))
startToken = addtoken(tokenList, "static"); addtoken(tokenList, "static");
int typeIndex = mExtTokens.size() - 1; int typeIndex = mExtTokens.size() - 1;
while (typeIndex > 1 && std::isalpha(mExtTokens[typeIndex][0])) while (typeIndex > 1 && std::isalpha(mExtTokens[typeIndex][0]))
typeIndex--; typeIndex--;
const std::string type = mExtTokens[typeIndex]; const std::string type = mExtTokens[typeIndex];
const std::string name = mExtTokens[typeIndex - 1]; const std::string name = mExtTokens[typeIndex - 1];
const Token *startToken = tokenList->back();
const ::Type *recordType = addTypeTokens(tokenList, type); const ::Type *recordType = addTypeTokens(tokenList, type);
if (!startToken && tokenList->back()) { if (!startToken)
startToken = tokenList->back(); startToken = tokenList->front();
while (Token::Match(startToken->previous(), "%type%|*|&|&&")) else if (startToken->str() != "static")
startToken = startToken->previous(); startToken = startToken->next();
}
Token *vartok1 = addtoken(tokenList, name); Token *vartok1 = addtoken(tokenList, name);
Scope *scope = const_cast<Scope *>(tokenList->back()->scope()); Scope *scope = const_cast<Scope *>(tokenList->back()->scope());
scope->varlist.push_back(Variable(vartok1, unquote(type), startToken, vartok1->previous(), 0, scope->defaultAccess(), recordType, scope)); scope->varlist.push_back(Variable(vartok1, unquote(type), startToken, vartok1->previous(), 0, scope->defaultAccess(), recordType, scope));

View File

@ -104,6 +104,7 @@ private:
TEST_CASE(vardecl4); TEST_CASE(vardecl4);
TEST_CASE(vardecl5); TEST_CASE(vardecl5);
TEST_CASE(vardecl6); TEST_CASE(vardecl6);
TEST_CASE(vardecl7);
TEST_CASE(whileStmt1); TEST_CASE(whileStmt1);
TEST_CASE(whileStmt2); TEST_CASE(whileStmt2);
@ -992,6 +993,11 @@ private:
ASSERT_EQUALS("static int x@1 = 3 ;", parse(clang)); ASSERT_EQUALS("static int x@1 = 3 ;", parse(clang));
} }
void vardecl7() {
const char clang[] = "`-VarDecl 0x2071f20 <1.cpp:2:1, col:23> col:9 start 'void *(*)(void *)'";
ASSERT_EQUALS("void * * start@1 ;", parse(clang));
}
void whileStmt1() { void whileStmt1() {
const char clang[] = "`-FunctionDecl 0x3d45b18 <1.c:1:1, line:3:1> line:1:6 foo 'void ()'\n" const char clang[] = "`-FunctionDecl 0x3d45b18 <1.c:1:1, line:3:1> line:1:6 foo 'void ()'\n"
" `-CompoundStmt 0x3d45c48 <col:12, line:3:1>\n" " `-CompoundStmt 0x3d45c48 <col:12, line:3:1>\n"