Clang import; NamespaceDecl, varDecl4, varDecl5

This commit is contained in:
Daniel Marjamäki 2020-01-08 18:23:40 +01:00
parent b8ae957365
commit 54a9b61329
3 changed files with 48 additions and 6 deletions

View File

@ -41,6 +41,7 @@ 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 MemberExpr = "MemberExpr"; static const std::string MemberExpr = "MemberExpr";
static const std::string NamespaceDecl = "NamespaceDecl";
static const std::string NullStmt = "NullStmt"; static const std::string NullStmt = "NullStmt";
static const std::string ParmVarDecl = "ParmVarDecl"; static const std::string ParmVarDecl = "ParmVarDecl";
static const std::string RecordDecl = "RecordDecl"; static const std::string RecordDecl = "RecordDecl";
@ -145,6 +146,7 @@ namespace clangastdump {
void dumpAst(int num = 0, int indent = 0) const; void dumpAst(int num = 0, int indent = 0) const;
void createTokens1(TokenList *tokenList) { void createTokens1(TokenList *tokenList) {
//dumpAst();
setLocations(tokenList, 0, 1, 1); setLocations(tokenList, 0, 1, 1);
createTokens(tokenList); createTokens(tokenList);
if (nodeType == VarDecl || nodeType == RecordDecl || nodeType == TypedefDecl) if (nodeType == VarDecl || nodeType == RecordDecl || nodeType == TypedefDecl)
@ -444,6 +446,18 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
return addtoken(tokenList, mExtTokens.back()); return addtoken(tokenList, mExtTokens.back());
if (nodeType == NullStmt) if (nodeType == NullStmt)
return addtoken(tokenList, ";"); return addtoken(tokenList, ";");
if (nodeType == NamespaceDecl) {
if (children.empty())
return nullptr;
Token *defToken = addtoken(tokenList, "namespace");
Token *nameToken = (mExtTokens[mExtTokens.size() - 2].compare(0,4,"col:") == 0) ?
addtoken(tokenList, mExtTokens.back()) : nullptr;
Scope *scope = createScope(tokenList, Scope::ScopeType::eNamespace, children);
scope->classDef = defToken;
if (nameToken)
scope->className = nameToken->str();
return nullptr;
}
if (nodeType == MemberExpr) { if (nodeType == MemberExpr) {
Token *s = children[0]->createTokens(tokenList); Token *s = children[0]->createTokens(tokenList);
Token *dot = addtoken(tokenList, "."); Token *dot = addtoken(tokenList, ".");
@ -504,17 +518,19 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
Token * clangastdump::AstNode::createTokensVarDecl(TokenList *tokenList) Token * clangastdump::AstNode::createTokensVarDecl(TokenList *tokenList)
{ {
bool isInit = mExtTokens.back() == "cinit";
const std::string addr = mExtTokens.front(); const std::string addr = mExtTokens.front();
const std::string type = isInit ? mExtTokens[mExtTokens.size() - 2] : mExtTokens.back(); int typeIndex = mExtTokens.size() - 1;
const std::string name = isInit ? mExtTokens[mExtTokens.size() - 3] : mExtTokens[mExtTokens.size() - 2]; while (typeIndex > 1 && std::isalpha(mExtTokens[typeIndex][0]))
typeIndex--;
const std::string type = mExtTokens[typeIndex];
const std::string name = mExtTokens[typeIndex - 1];
addTypeTokens(tokenList, type); addTypeTokens(tokenList, type);
Token *vartok1 = addtoken(tokenList, name); Token *vartok1 = addtoken(tokenList, name);
Scope *scope = const_cast<Scope *>(tokenList->back()->scope()); Scope *scope = const_cast<Scope *>(tokenList->back()->scope());
const AccessControl accessControl = (scope->type == Scope::ScopeType::eGlobal) ? (AccessControl::Global) : (AccessControl::Local); const AccessControl accessControl = (scope->type == Scope::ScopeType::eGlobal) ? (AccessControl::Global) : (AccessControl::Local);
scope->varlist.push_back(Variable(vartok1, type, 0, accessControl, nullptr, scope)); scope->varlist.push_back(Variable(vartok1, type, 0, accessControl, nullptr, scope));
mData->varDecl(addr, vartok1, &scope->varlist.back()); mData->varDecl(addr, vartok1, &scope->varlist.back());
if (isInit) { if (mExtTokens.back() == "cinit") {
Token *eq = addtoken(tokenList, "="); Token *eq = addtoken(tokenList, "=");
eq->astOperand1(vartok1); eq->astOperand1(vartok1);
eq->astOperand2(children.back()->createTokens(tokenList)); eq->astOperand2(children.back()->createTokens(tokenList));

View File

@ -1745,8 +1745,11 @@ Variable::Variable(const Token *name_, const std::string &clangType,
pos = clangType.find("]", pos1); pos = clangType.find("]", pos1);
Dimension dim; Dimension dim;
dim.tok = nullptr; dim.tok = nullptr;
dim.known = true; dim.known = pos > pos1;
dim.num = MathLib::toLongNumber(clangType.substr(pos1, pos-pos1)); if (pos > pos1)
dim.num = MathLib::toLongNumber(clangType.substr(pos1, pos-pos1));
else
dim.num = 0;
mDimensions.push_back(dim); mDimensions.push_back(dim);
++pos; ++pos;
} while (pos < clangType.size() && clangType[pos] == '['); } while (pos < clangType.size() && clangType[pos] == '[');

View File

@ -38,6 +38,7 @@ private:
TEST_CASE(funcdecl4); TEST_CASE(funcdecl4);
TEST_CASE(ifelse); TEST_CASE(ifelse);
TEST_CASE(memberExpr); TEST_CASE(memberExpr);
TEST_CASE(namespaceDecl);
TEST_CASE(recordDecl); TEST_CASE(recordDecl);
TEST_CASE(typedefDecl1); TEST_CASE(typedefDecl1);
TEST_CASE(typedefDecl2); TEST_CASE(typedefDecl2);
@ -45,7 +46,12 @@ private:
TEST_CASE(vardecl1); TEST_CASE(vardecl1);
TEST_CASE(vardecl2); TEST_CASE(vardecl2);
TEST_CASE(vardecl3); TEST_CASE(vardecl3);
TEST_CASE(vardecl4);
TEST_CASE(vardecl5);
TEST_CASE(whileStmt); TEST_CASE(whileStmt);
// C++..
TEST_CASE(namespaceDecl);
} }
std::string parse(const char clang[]) { std::string parse(const char clang[]) {
@ -170,6 +176,13 @@ private:
parse(clang)); parse(clang));
} }
void namespaceDecl() {
const char clang[] = "`-NamespaceDecl 0x2e5f658 <hello.cpp:1:1, col:24> col:11 x\n"
" `-VarDecl 0x2e5f6d8 <col:15, col:19> col:19 var 'int'";
ASSERT_EQUALS("namespace x { int var@1 ; }",
parse(clang));
}
void recordDecl() { void recordDecl() {
const char clang[] = "`-RecordDecl 0x354eac8 <1.c:1:1, line:4:1> line:1:8 struct S definition\n" const char clang[] = "`-RecordDecl 0x354eac8 <1.c:1:1, line:4:1> line:1:8 struct S definition\n"
" |-FieldDecl 0x354eb88 <line:2:3, col:7> col:7 x 'int'\n" " |-FieldDecl 0x354eb88 <line:2:3, col:7> col:7 x 'int'\n"
@ -236,6 +249,16 @@ private:
ASSERT_EQUALS("const int * p@1 ;", parse(clang)); ASSERT_EQUALS("const int * p@1 ;", parse(clang));
} }
void vardecl4() {
const char clang[] = "|-VarDecl 0x23d6c78 <line:137:1, col:14> col:14 stdin 'FILE *' extern";
ASSERT_EQUALS("FILE * stdin@1 ;", parse(clang));
}
void vardecl5() {
const char clang[] = "|-VarDecl 0x2e31fc0 <line:27:1, col:38> col:26 sys_errlist 'const char *const []' extern";
ASSERT_EQUALS("const char *const [] sys_errlist@1 ;", parse(clang));
}
void whileStmt() { void whileStmt() {
const char clang[] = "`-FunctionDecl 0x3d45b18 <1.c:1:1, line:3:1> line:1:6 foo 'void ()'\n" const char clang[] = "`-FunctionDecl 0x3d45b18 <1.c:1:1, line:3:1> line:1:6 foo 'void ()'\n"
" `-CompoundStmt 0x3d45c48 <col:12, line:3:1>\n" " `-CompoundStmt 0x3d45c48 <col:12, line:3:1>\n"