Clang import; CompoundAssignOperator

This commit is contained in:
Daniel Marjamäki 2020-01-12 18:41:46 +01:00
parent ae7ff940e6
commit 9f3df5d630
2 changed files with 34 additions and 6 deletions

View File

@ -33,6 +33,7 @@ static const std::string CallExpr = "CallExpr";
static const std::string CharacterLiteral = "CharacterLiteral"; static const std::string CharacterLiteral = "CharacterLiteral";
static const std::string ClassTemplateDecl = "ClassTemplateDecl"; static const std::string ClassTemplateDecl = "ClassTemplateDecl";
static const std::string ClassTemplateSpecializationDecl = "ClassTemplateSpecializationDecl"; static const std::string ClassTemplateSpecializationDecl = "ClassTemplateSpecializationDecl";
static const std::string CompoundAssignOperator = "CompoundAssignOperator";
static const std::string CompoundStmt = "CompoundStmt"; static const std::string CompoundStmt = "CompoundStmt";
static const std::string ContinueStmt = "ContinueStmt"; static const std::string ContinueStmt = "ContinueStmt";
static const std::string CStyleCastExpr = "CStyleCastExpr"; static const std::string CStyleCastExpr = "CStyleCastExpr";
@ -243,12 +244,22 @@ namespace clangimport {
std::string clangimport::AstNode::getSpelling() const std::string clangimport::AstNode::getSpelling() const
{ {
int retTypeIndex = mExtTokens.size() - 1; if (nodeType == CompoundAssignOperator) {
if (nodeType == FunctionDecl) { int typeIndex = 1;
while (mExtTokens[retTypeIndex][0] != '\'') while (typeIndex < mExtTokens.size() && mExtTokens[typeIndex][0] != '\'')
retTypeIndex--; typeIndex++;
int nameIndex = typeIndex + 1;
while (nameIndex < mExtTokens.size() && mExtTokens[nameIndex][0] != '\'')
nameIndex++;
return (nameIndex < mExtTokens.size()) ? unquote(mExtTokens[nameIndex]) : "";
} }
const std::string &str = mExtTokens[retTypeIndex - 1];
int typeIndex = mExtTokens.size() - 1;
if (nodeType == FunctionDecl) {
while (mExtTokens[typeIndex][0] != '\'')
typeIndex--;
}
const std::string &str = mExtTokens[typeIndex - 1];
if (str.compare(0,4,"col:") == 0) if (str.compare(0,4,"col:") == 0)
return ""; return "";
if (str.compare(0,8,"<invalid") == 0) if (str.compare(0,8,"<invalid") == 0)
@ -259,7 +270,6 @@ std::string clangimport::AstNode::getSpelling() const
std::string clangimport::AstNode::getType() const std::string clangimport::AstNode::getType() const
{ {
int typeIndex = 1; int typeIndex = 1;
typeIndex = 1;
while (typeIndex < mExtTokens.size() && mExtTokens[typeIndex][0] != '\'') while (typeIndex < mExtTokens.size() && mExtTokens[typeIndex][0] != '\'')
typeIndex++; typeIndex++;
if (typeIndex >= mExtTokens.size()) if (typeIndex >= mExtTokens.size())
@ -487,6 +497,14 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
createTokensForCXXRecord(tokenList); createTokensForCXXRecord(tokenList);
return nullptr; return nullptr;
} }
if (nodeType == CompoundAssignOperator) {
Token *lhs = children[0]->createTokens(tokenList);
Token *assign = addtoken(tokenList, getSpelling());
Token *rhs = children[1]->createTokens(tokenList);
assign->astOperand1(lhs);
assign->astOperand2(rhs);
return assign;
}
if (nodeType == CompoundStmt) { if (nodeType == CompoundStmt) {
for (AstNodePtr child: children) { for (AstNodePtr child: children) {
child->createTokens(tokenList); child->createTokens(tokenList);

View File

@ -34,6 +34,7 @@ private:
TEST_CASE(class1); TEST_CASE(class1);
TEST_CASE(classTemplateDecl1); TEST_CASE(classTemplateDecl1);
TEST_CASE(classTemplateDecl2); TEST_CASE(classTemplateDecl2);
TEST_CASE(compoundAssignOperator);
TEST_CASE(continueStmt); TEST_CASE(continueStmt);
TEST_CASE(cstyleCastExpr); TEST_CASE(cstyleCastExpr);
TEST_CASE(cxxBoolLiteralExpr); TEST_CASE(cxxBoolLiteralExpr);
@ -172,6 +173,15 @@ private:
ASSERT_EQUALS("class C { int foo ( ) { return 0 ; } }", parse(clang)); ASSERT_EQUALS("class C { int foo ( ) { return 0 ; } }", parse(clang));
} }
void compoundAssignOperator() {
const char clang[] = "`-FunctionDecl 0x3570690 <1.cpp:2:1, col:25> col:6 f 'void ()'\n"
" `-CompoundStmt 0x3570880 <col:10, col:25>\n"
" `-CompoundAssignOperator 0x3570848 <col:19, col:22> 'int' lvalue '+=' ComputeLHSTy='int' ComputeResultTy='int'\n"
" |-DeclRefExpr 0x3570800 <col:19> 'int' lvalue Var 0x3570788 'x' 'int'\n"
" `-IntegerLiteral 0x3570828 <col:22> 'int' 1";
ASSERT_EQUALS("void f ( ) { x += 1 ; }", parse(clang));
}
void continueStmt() { void continueStmt() {
const char clang[] = "`-FunctionDecl 0x2c31b18 <1.c:1:1, col:34> col:6 foo 'void ()'\n" const char clang[] = "`-FunctionDecl 0x2c31b18 <1.c:1:1, col:34> col:6 foo 'void ()'\n"
" `-CompoundStmt 0x2c31c40 <col:12, col:34>\n" " `-CompoundStmt 0x2c31c40 <col:12, col:34>\n"