Clang import; IfStmt

This commit is contained in:
Daniel Marjamäki 2020-01-05 19:18:32 +01:00
parent b621c35539
commit fa727185e5
2 changed files with 82 additions and 26 deletions

View File

@ -29,6 +29,7 @@ static const std::string CallExpr = "CallExpr";
static const std::string CompoundStmt = "CompoundStmt"; static const std::string CompoundStmt = "CompoundStmt";
static const std::string DeclRefExpr = "DeclRefExpr"; static const std::string DeclRefExpr = "DeclRefExpr";
static const std::string FunctionDecl = "FunctionDecl"; static const std::string FunctionDecl = "FunctionDecl";
static const std::string IfStmt = "IfStmt";
static const std::string ImplicitCastExpr = "ImplicitCastExpr"; static const std::string ImplicitCastExpr = "ImplicitCastExpr";
static const std::string IntegerLiteral = "IntegerLiteral"; static const std::string IntegerLiteral = "IntegerLiteral";
static const std::string ParmVarDecl = "ParmVarDecl"; static const std::string ParmVarDecl = "ParmVarDecl";
@ -81,6 +82,7 @@ namespace clangastdump {
private: private:
Token *addtoken(TokenList *tokenList, const std::string &str); Token *addtoken(TokenList *tokenList, const std::string &str);
Token *addTypeTokens(TokenList *tokenList, const std::string &str); Token *addTypeTokens(TokenList *tokenList, const std::string &str);
Scope *createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNode *astNode);
std::string getSpelling() const; std::string getSpelling() const;
std::string getType() const; std::string getType() const;
@ -132,19 +134,27 @@ void clangastdump::AstNode::setLocations(TokenList *tokenList, int file, int lin
col = std::atoi(ext.substr(5).c_str()); col = std::atoi(ext.substr(5).c_str());
else if (ext.compare(0,6,"<line:") == 0) else if (ext.compare(0,6,"<line:") == 0)
line = std::atoi(ext.substr(6).c_str()); line = std::atoi(ext.substr(6).c_str());
else if (ext[0] == '<') else if (ext[0] == '<' && ext.find(":") != std::string::npos)
file = tokenList->appendFileIfNew(ext.substr(1,ext.find(":") - 1)); file = tokenList->appendFileIfNew(ext.substr(1,ext.find(":") - 1));
} }
mFile = file; mFile = file;
mLine = line; mLine = line;
mCol = col; mCol = col;
for (auto child: children) for (auto child: children) {
if (child)
child->setLocations(tokenList, file, line, col); child->setLocations(tokenList, file, line, col);
}
} }
Token *clangastdump::AstNode::addtoken(TokenList *tokenList, const std::string &str) Token *clangastdump::AstNode::addtoken(TokenList *tokenList, const std::string &str)
{ {
const Scope *scope;
if (!tokenList->back())
scope = &mSymbolDatabase->scopeList.front();
else
scope = tokenList->back()->scope();
tokenList->addtoken(str, mLine, mFile); tokenList->addtoken(str, mLine, mFile);
tokenList->back()->scope(scope);
if (getType() == "int") if (getType() == "int")
tokenList->back()->setValueType(new ValueType(ValueType::Sign::SIGNED, ValueType::Type::INT, 0)); tokenList->back()->setValueType(new ValueType(ValueType::Sign::SIGNED, ValueType::Type::INT, 0));
return tokenList->back(); return tokenList->back();
@ -157,6 +167,29 @@ Token *clangastdump::AstNode::addTypeTokens(TokenList *tokenList, const std::str
return addtoken(tokenList, str.substr(1,str.find(" (")-1)); return addtoken(tokenList, str.substr(1,str.find(" (")-1));
} }
Scope *clangastdump::AstNode::createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNode *astNode)
{
const Scope *nestedIn;
if (!tokenList->back())
nestedIn = &mSymbolDatabase->scopeList.front();
else if (tokenList->back()->str() == "}")
nestedIn = tokenList->back()->link()->previous()->scope();
else
nestedIn = tokenList->back()->scope();
mSymbolDatabase->scopeList.push_back(Scope(nullptr, nullptr, nestedIn));
Scope *scope = &mSymbolDatabase->scopeList.back();
scope->type = scopeType;
Token *bodyStart = addtoken(tokenList, "{");
tokenList->back()->scope(scope);
astNode->createTokens(tokenList);
addtoken(tokenList, ";");
Token *bodyEnd = addtoken(tokenList, "}");
bodyStart->link(bodyEnd);
scope->bodyStart = bodyStart;
scope->bodyEnd = bodyEnd;
return scope;
}
Token *clangastdump::AstNode::createTokens(TokenList *tokenList) Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
{ {
if (nodeType == BinaryOperator) { if (nodeType == BinaryOperator) {
@ -177,14 +210,14 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
return call; return call;
} }
if (nodeType == CompoundStmt) { if (nodeType == CompoundStmt) {
Token *start = addtoken(tokenList, "{"); bool first = true;
for (AstNodePtr child: children) { for (AstNodePtr child: children) {
child->createTokens(tokenList); if (!first)
child->addtoken(tokenList, ";"); child->addtoken(tokenList, ";");
first = false;
child->createTokens(tokenList);
} }
Token *end = addtoken(tokenList, "}"); return nullptr;
start->link(end);
return start;
} }
if (nodeType == DeclRefExpr) { if (nodeType == DeclRefExpr) {
Token *vartok = addtoken(tokenList, unquote(mExtTokens[mExtTokens.size() - 2])); Token *vartok = addtoken(tokenList, unquote(mExtTokens[mExtTokens.size() - 2]));
@ -202,6 +235,7 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
mSymbolDatabase->functionScopes.push_back(&scope); mSymbolDatabase->functionScopes.push_back(&scope);
globalScope.functionList.push_back(Function(nameToken)); globalScope.functionList.push_back(Function(nameToken));
scope.function = &globalScope.functionList.back(); scope.function = &globalScope.functionList.back();
scope.type = Scope::ScopeType::eFunction;
Token *par1 = addtoken(tokenList, "("); Token *par1 = addtoken(tokenList, "(");
for (AstNodePtr child: children) { for (AstNodePtr child: children) {
if (child->nodeType != ParmVarDecl) if (child->nodeType != ParmVarDecl)
@ -225,12 +259,25 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
} }
Token *par2 = addtoken(tokenList, ")"); Token *par2 = addtoken(tokenList, ")");
par1->link(par2); par1->link(par2);
Token *bodyStart = addtoken(tokenList, "{");
bodyStart->scope(&scope);
children.back()->createTokens(tokenList); children.back()->createTokens(tokenList);
if (Token::simpleMatch(par2, ") {")) { Token *bodyEnd = addtoken(tokenList, "}");
Token *bodyStart = par2->next();
scope.bodyStart = bodyStart; scope.bodyStart = bodyStart;
scope.bodyEnd = bodyStart->link(); scope.bodyEnd = bodyEnd;
bodyStart->link(bodyEnd);
return nullptr;
} }
if (nodeType == IfStmt) {
AstNode *cond = children[2].get();
AstNode *then = children[3].get();
Token *iftok = addtoken(tokenList, "if");
Token *par1 = addtoken(tokenList, "(");
par1->astOperand1(iftok);
par1->astOperand2(cond->createTokens(tokenList));
Token *par2 = addtoken(tokenList, ")");
par1->link(par2);
createScope(tokenList, Scope::ScopeType::eIf, then);
return nullptr; return nullptr;
} }
if (nodeType == ImplicitCastExpr) if (nodeType == ImplicitCastExpr)
@ -267,6 +314,11 @@ void clangastdump::parseClangAstDump(Tokenizer *tokenizer, std::istream &f)
const std::string::size_type pos1 = line.find("-"); const std::string::size_type pos1 = line.find("-");
if (pos1 == std::string::npos) if (pos1 == std::string::npos)
continue; continue;
if (!tree.empty() && line.substr(pos1) == "-<<<NULL>>>") {
const int level = (pos1 - 1) / 2;
tree[level - 1]->children.push_back(nullptr);
continue;
}
const std::string::size_type pos2 = line.find(" ", pos1); const std::string::size_type pos2 = line.find(" ", pos1);
if (pos2 < pos1 + 4 || pos2 == std::string::npos) if (pos2 < pos1 + 4 || pos2 == std::string::npos)
continue; continue;

View File

@ -2926,6 +2926,7 @@ void SymbolDatabase::printVariable(const Variable *var, const char *indent) cons
std::cout << indent << "mTypeStartToken: " << tokenToString(var->typeStartToken(), mTokenizer) << std::endl; std::cout << indent << "mTypeStartToken: " << tokenToString(var->typeStartToken(), mTokenizer) << std::endl;
std::cout << indent << "mTypeEndToken: " << tokenToString(var->typeEndToken(), mTokenizer) << std::endl; std::cout << indent << "mTypeEndToken: " << tokenToString(var->typeEndToken(), mTokenizer) << std::endl;
if (var->typeStartToken()) {
const Token * autoTok = nullptr; const Token * autoTok = nullptr;
std::cout << indent << " "; std::cout << indent << " ";
for (const Token * tok = var->typeStartToken(); tok != var->typeEndToken()->next(); tok = tok->next()) { for (const Token * tok = var->typeStartToken(); tok != var->typeEndToken()->next(); tok = tok->next()) {
@ -2941,6 +2942,9 @@ void SymbolDatabase::printVariable(const Variable *var, const char *indent) cons
std::cout << indent << " " << valueType->str() << std::endl; std::cout << indent << " " << valueType->str() << std::endl;
} }
} }
} else if (var->valueType()) {
std::cout << indent << " " << var->valueType()->str() << std::endl;
}
std::cout << indent << "mIndex: " << var->index() << std::endl; std::cout << indent << "mIndex: " << var->index() << std::endl;
std::cout << indent << "mAccess: " << accessControlToString(var->accessControl()) << std::endl; std::cout << indent << "mAccess: " << accessControlToString(var->accessControl()) << std::endl;
std::cout << indent << "mFlags: " << std::endl; std::cout << indent << "mFlags: " << std::endl;