Skip to content

Commit f986b37

Browse files
committed
Clang import: Improved function pointer
1 parent 277da76 commit f986b37

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

lib/clangimport.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
#include "tokenize.h"
2323
#include "utils.h"
2424

25+
#include <cstring>
2526
#include <algorithm>
27+
#include <iostream>
2628
#include <memory>
29+
#include <stack>
2730
#include <vector>
28-
#include <iostream>
2931

3032
static const std::string AccessSpecDecl = "AccessSpecDecl";
3133
static const std::string ArraySubscriptExpr = "ArraySubscriptExpr";
@@ -110,8 +112,8 @@ static std::vector<std::string> splitString(const std::string &line)
110112
std::string::size_type pos1 = line.find_first_not_of(" ");
111113
while (pos1 < line.size()) {
112114
std::string::size_type pos2;
113-
if (line[pos1] == '*') {
114-
ret.push_back("*");
115+
if (std::strchr("*()", line[pos1])) {
116+
ret.push_back(line.substr(pos1,1));
115117
pos1 = line.find_first_not_of(" ", pos1 + 1);
116118
continue;
117119
}
@@ -509,8 +511,21 @@ const ::Type * clangimport::AstNode::addTypeTokens(TokenList *tokenList, const s
509511
} else
510512
type = unquote(str);
511513

512-
for (const std::string &s: splitString(type))
513-
addtoken(tokenList, s, false);
514+
if (type.find("(*)(") != std::string::npos) {
515+
type.erase(type.find("(*)("));
516+
type += "*";
517+
}
518+
519+
std::stack<Token *> lpar;
520+
for (const std::string &s: splitString(type)) {
521+
Token *tok = addtoken(tokenList, s, false);
522+
if (tok->str() == "(")
523+
lpar.push(tok);
524+
else if (tok->str() == ")") {
525+
Token::createMutualLinks(tok, lpar.top());
526+
lpar.pop();
527+
}
528+
}
514529

515530
// Set Type
516531
if (!scope) {
@@ -1379,20 +1394,19 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList)
13791394
Token * clangimport::AstNode::createTokensVarDecl(TokenList *tokenList)
13801395
{
13811396
const std::string addr = mExtTokens.front();
1382-
const Token *startToken = nullptr;
13831397
if (contains(mExtTokens, "static"))
1384-
startToken = addtoken(tokenList, "static");
1398+
addtoken(tokenList, "static");
13851399
int typeIndex = mExtTokens.size() - 1;
13861400
while (typeIndex > 1 && std::isalpha(mExtTokens[typeIndex][0]))
13871401
typeIndex--;
13881402
const std::string type = mExtTokens[typeIndex];
13891403
const std::string name = mExtTokens[typeIndex - 1];
1404+
const Token *startToken = tokenList->back();
13901405
const ::Type *recordType = addTypeTokens(tokenList, type);
1391-
if (!startToken && tokenList->back()) {
1392-
startToken = tokenList->back();
1393-
while (Token::Match(startToken->previous(), "%type%|*|&|&&"))
1394-
startToken = startToken->previous();
1395-
}
1406+
if (!startToken)
1407+
startToken = tokenList->front();
1408+
else if (startToken->str() != "static")
1409+
startToken = startToken->next();
13961410
Token *vartok1 = addtoken(tokenList, name);
13971411
Scope *scope = const_cast<Scope *>(tokenList->back()->scope());
13981412
scope->varlist.push_back(Variable(vartok1, unquote(type), startToken, vartok1->previous(), 0, scope->defaultAccess(), recordType, scope));

test/testclangimport.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class TestClangImport: public TestFixture {
104104
TEST_CASE(vardecl4);
105105
TEST_CASE(vardecl5);
106106
TEST_CASE(vardecl6);
107+
TEST_CASE(vardecl7);
107108
TEST_CASE(whileStmt1);
108109
TEST_CASE(whileStmt2);
109110

@@ -992,6 +993,11 @@ class TestClangImport: public TestFixture {
992993
ASSERT_EQUALS("static int x@1 = 3 ;", parse(clang));
993994
}
994995

996+
void vardecl7() {
997+
const char clang[] = "`-VarDecl 0x2071f20 <1.cpp:2:1, col:23> col:9 start 'void *(*)(void *)'";
998+
ASSERT_EQUALS("void * * start@1 ;", parse(clang));
999+
}
1000+
9951001
void whileStmt1() {
9961002
const char clang[] = "`-FunctionDecl 0x3d45b18 <1.c:1:1, line:3:1> line:1:6 foo 'void ()'\n"
9971003
" `-CompoundStmt 0x3d45c48 <col:12, line:3:1>\n"

0 commit comments

Comments
 (0)