Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/clangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,11 @@ namespace clangimport {

std::string clangimport::AstNode::getSpelling() const
{
if (mJsonObject.count("name") == 0)
return {};
return mJsonObject.at("name").get<std::string>();
if (mJsonObject.count("opcode") > 0)
return mJsonObject.at("opcode").get<std::string>();
if (mJsonObject.count("name") > 0)
return mJsonObject.at("name").get<std::string>();
return {};
}

std::string clangimport::AstNode::getQualType() const
Expand Down Expand Up @@ -751,7 +753,7 @@ Token *clangimport::AstNode::createTokens(TokenList &tokenList)
}
if (mKind == BinaryOperator) {
Token *tok1 = getChild(0)->createTokens(tokenList);
Token *binop = addtoken(tokenList, mJsonObject.at("opcode").get<std::string>());
Token *binop = addtoken(tokenList, getSpelling());
Token *tok2 = children[1]->createTokens(tokenList);
binop->astOperand1(tok1);
binop->astOperand2(tok2);
Expand Down Expand Up @@ -1249,16 +1251,16 @@ Token *clangimport::AstNode::createTokens(TokenList &tokenList)
return addtoken(tokenList, getSpelling());
}
if (mKind == UnaryOperator) {
const std::string& opcode = mJsonObject.at("opcode").get<std::string>();
const std::string& spelling = getSpelling();
const bool postfix = mJsonObject.count("isPostfix") > 0 && mJsonObject.at("isPostfix").get<bool>();
if (postfix) {
Token* tok = getChild(0)->createTokens(tokenList);
Token *unaryOp = addtoken(tokenList, opcode);
Token *unaryOp = addtoken(tokenList, spelling);
setValueType(unaryOp);
unaryOp->astOperand1(tok);
return unaryOp;
} else {
Token *unaryOp = addtoken(tokenList, opcode);
Token *unaryOp = addtoken(tokenList, spelling);
setValueType(unaryOp);
unaryOp->astOperand1(getChild(0)->createTokens(tokenList));
return unaryOp;
Expand Down
4 changes: 4 additions & 0 deletions test/cli/clang-import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ def test_tokens_function2(tmp_path):
assert '3:\n' in stdout
assert '4: return 12 ;' in stdout

def test_tokens_function3(tmp_path):
test_file = tmp_path / 'test.cpp'
stdout = _run_cppcheck_debug(test_file, '\nint x; void foo(){ x |= 1 << 30; }')
assert '2: int x@var1 ; void foo ( ) { x@var1 |=@exprUNIQUE 1 <<@exprUNIQUE 30 ; }\n' in stdout

def test_tokens_for(tmp_path):
test_file = tmp_path / 'test.cpp'
Expand Down
Loading