Clang import; fix wrong 'sizeof' import

This commit is contained in:
Daniel Marjamäki 2020-01-17 03:14:52 +01:00
parent 53623ddf6f
commit bc39cd73f0
2 changed files with 25 additions and 3 deletions

View File

@ -252,12 +252,21 @@ std::string clangimport::AstNode::getSpelling() const
int typeIndex = 1;
while (typeIndex < mExtTokens.size() && mExtTokens[typeIndex][0] != '\'')
typeIndex++;
// name is next quoted token
int nameIndex = typeIndex + 1;
while (nameIndex < mExtTokens.size() && mExtTokens[nameIndex][0] != '\'')
nameIndex++;
return (nameIndex < mExtTokens.size()) ? unquote(mExtTokens[nameIndex]) : "";
}
if (nodeType == UnaryExprOrTypeTraitExpr) {
int typeIndex = 1;
while (typeIndex < mExtTokens.size() && mExtTokens[typeIndex][0] != '\'')
typeIndex++;
int nameIndex = typeIndex + 1;
return (nameIndex < mExtTokens.size()) ? unquote(mExtTokens[nameIndex]) : "";
}
int typeIndex = mExtTokens.size() - 1;
if (nodeType == FunctionDecl) {
while (mExtTokens[typeIndex][0] != '\'')
@ -759,7 +768,10 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
if (nodeType == UnaryExprOrTypeTraitExpr) {
Token *tok1 = addtoken(tokenList, getSpelling());
Token *par1 = addtoken(tokenList, "(");
addTypeTokens(tokenList, mExtTokens.back());
if (children.empty())
addTypeTokens(tokenList, mExtTokens.back());
else
addTypeTokens(tokenList, children[0]->getType());
Token *par2 = addtoken(tokenList, ")");
par1->link(par2);
par2->link(par1);

View File

@ -65,7 +65,8 @@ private:
TEST_CASE(typedefDecl1);
TEST_CASE(typedefDecl2);
TEST_CASE(typedefDecl3);
TEST_CASE(unaryExprOrTypeTraitExpr);
TEST_CASE(unaryExprOrTypeTraitExpr1);
TEST_CASE(unaryExprOrTypeTraitExpr2);
TEST_CASE(unaryOperator);
TEST_CASE(vardecl1);
TEST_CASE(vardecl2);
@ -495,13 +496,22 @@ private:
ASSERT_EQUALS("typedef char * __builtin_ms_va_list ;", parse(clang));
}
void unaryExprOrTypeTraitExpr() {
void unaryExprOrTypeTraitExpr1() {
const char clang[] = "`-VarDecl 0x24cc610 <a.cpp:1:1, col:19> col:5 x 'int' cinit\n"
" `-ImplicitCastExpr 0x24cc6e8 <col:9, col:19> 'int' <IntegralCast>\n"
" `-UnaryExprOrTypeTraitExpr 0x24cc6c8 <col:9, col:19> 'unsigned long' sizeof 'int'\n";
ASSERT_EQUALS("int x@1 = sizeof ( int ) ;", parse(clang));
}
void unaryExprOrTypeTraitExpr2() {
const char clang[] = "`-VarDecl 0x27c6c00 <line:3:5, col:23> col:9 x 'int' cinit\n"
" `-ImplicitCastExpr 0x27c6cc8 <col:13, col:23> 'int' <IntegralCast>\n"
" `-UnaryExprOrTypeTraitExpr 0x27c6ca8 <col:13, col:23> 'unsigned long' sizeof\n"
" `-ParenExpr 0x27c6c88 <col:19, col:23> 'char [10]' lvalue\n"
" `-DeclRefExpr 0x27c6c60 <col:20> 'char [10]' lvalue Var 0x27c6b48 'buf' 'char [10]'";
ASSERT_EQUALS("int x@1 = sizeof ( char [10] ) ;", parse(clang));
}
void unaryOperator() {
const char clang[] = "`-FunctionDecl 0x2dd9748 <1.cpp:2:1, col:44> col:5 foo 'int (int *)'\n"
" |-ParmVarDecl 0x2dd9680 <col:14, col:19> col:19 used p 'int *'\n"