Clang import; Tweaks

This commit is contained in:
Daniel Marjamäki 2020-01-09 10:49:47 +01:00
parent 0becff9d7f
commit 3d2d4c5cbd
2 changed files with 25 additions and 8 deletions

View File

@ -49,6 +49,7 @@ static const std::string IntegerLiteral = "IntegerLiteral";
static const std::string MemberExpr = "MemberExpr";
static const std::string NamespaceDecl = "NamespaceDecl";
static const std::string NullStmt = "NullStmt";
static const std::string ParenExpr = "ParenExpr";
static const std::string ParmVarDecl = "ParmVarDecl";
static const std::string RecordDecl = "RecordDecl";
static const std::string ReturnStmt = "ReturnStmt";
@ -235,9 +236,11 @@ void clangastdump::AstNode::dumpAst(int num, int indent) const
for (auto tok: mExtTokens)
std::cout << " " << tok;
std::cout << std::endl;
for (int c = 0; c < children.size(); ++c)
for (int c = 0; c < children.size(); ++c) {
if (children[c])
children[c]->dumpAst(c, indent + 2);
}
}
void clangastdump::AstNode::setLocations(TokenList *tokenList, int file, int line, int col)
{
@ -360,6 +363,7 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
if (nodeType == CompoundStmt) {
for (AstNodePtr child: children) {
child->createTokens(tokenList);
if (!Token::Match(tokenList->back(), "[;{}]"))
child->addtoken(tokenList, ";");
}
return nullptr;
@ -389,11 +393,11 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
if (nodeType == ForStmt) {
Token *forToken = addtoken(tokenList, "for");
Token *par1 = addtoken(tokenList, "(");
Token *expr1 = children[0]->createTokens(tokenList);
Token *expr1 = children[0] ? children[0]->createTokens(tokenList) : nullptr;
Token *sep1 = addtoken(tokenList, ";");
Token *expr2 = children[2]->createTokens(tokenList);
Token *expr2 = children[2] ? children[2]->createTokens(tokenList) : nullptr;
Token *sep2 = addtoken(tokenList, ";");
Token *expr3 = children[3]->createTokens(tokenList);
Token *expr3 = children[3] ? children[3]->createTokens(tokenList) : nullptr;
Token *par2 = addtoken(tokenList, ")");
par1->link(par2);
par1->astOperand1(forToken);
@ -464,6 +468,13 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
dot->astOperand2(member);
return dot;
}
if (nodeType == ParenExpr) {
Token *par1 = addtoken(tokenList, "(");
Token *expr = children[0]->createTokens(tokenList);
Token *par2 = addtoken(tokenList, ")");
par1->link(par2);
return expr;
}
if (nodeType == RecordDecl) {
const Token *classDef = addtoken(tokenList, "struct");
const std::string &recordName = getSpelling();
@ -596,6 +607,10 @@ void clangastdump::AstNode::createTokensForCXXRecord(TokenList *tokenList)
else if (child->nodeType == FieldDecl)
children2.push_back(child);
}
if (children2.empty()) {
addtoken(tokenList, ";");
return;
}
Scope *scope = createScope(tokenList, Scope::ScopeType::eClass, children2);
scope->classDef = classToken;
scope->className = className;

View File

@ -242,8 +242,10 @@ const char * CppCheck::extraVersion()
unsigned int CppCheck::check(const std::string &path)
{
if (mSettings.clang) {
const std::string clang = Path::isCPP(path) ? "clang++" : "clang";
/* Experimental: import clang ast dump */
const std::string cmd1 = "clang -v -fsyntax-only " + path + " 2>&1";
const std::string cmd1 = clang + " -v -fsyntax-only " + path + " 2>&1";
const std::pair<bool, std::string> res1 = executeCommand(cmd1);
if (!res1.first) {
std::cerr << "Failed to execute '" + cmd1 + "'" << std::endl;
@ -262,7 +264,7 @@ unsigned int CppCheck::check(const std::string &path)
}
}
const std::string cmd = "clang -cc1 -ast-dump " + includes + path;
const std::string cmd = clang + " -cc1 -ast-dump " + includes + path;
std::pair<bool, std::string> res = executeCommand(cmd);
if (!res.first) {
std::cerr << "Failed to execute '" + cmd + "'" << std::endl;