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

View File

@ -2926,20 +2926,24 @@ void SymbolDatabase::printVariable(const Variable *var, const char *indent) cons
std::cout << indent << "mTypeStartToken: " << tokenToString(var->typeStartToken(), mTokenizer) << std::endl;
std::cout << indent << "mTypeEndToken: " << tokenToString(var->typeEndToken(), mTokenizer) << std::endl;
const Token * autoTok = nullptr;
std::cout << indent << " ";
for (const Token * tok = var->typeStartToken(); tok != var->typeEndToken()->next(); tok = tok->next()) {
std::cout << " " << tokenType(tok);
if (tok->str() == "auto")
autoTok = tok;
}
std::cout << std::endl;
if (autoTok) {
const ValueType * valueType = autoTok->valueType();
std::cout << indent << " auto valueType: " << valueType << std::endl;
if (var->typeStartToken()->valueType()) {
std::cout << indent << " " << valueType->str() << std::endl;
if (var->typeStartToken()) {
const Token * autoTok = nullptr;
std::cout << indent << " ";
for (const Token * tok = var->typeStartToken(); tok != var->typeEndToken()->next(); tok = tok->next()) {
std::cout << " " << tokenType(tok);
if (tok->str() == "auto")
autoTok = tok;
}
std::cout << std::endl;
if (autoTok) {
const ValueType * valueType = autoTok->valueType();
std::cout << indent << " auto valueType: " << valueType << std::endl;
if (var->typeStartToken()->valueType()) {
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 << "mAccess: " << accessControlToString(var->accessControl()) << std::endl;