Resolve macro expansions properly

This commit is contained in:
Erki 2021-02-24 23:05:38 +02:00
parent 2df47d4bc5
commit 6f3dce5972

View File

@ -31,19 +31,32 @@ StatementMatcher UnaryArithmeticMatcher =
StatementMatcher CompoundMatcher = StatementMatcher CompoundMatcher =
compoundStmt().bind("compound_stmt"); compoundStmt().bind("compound_stmt");
//StatementMatcher ForStatementMatcher =
// forStmt(hasInitStatement(expr().bind("init"))).bind("for_stmt");
template<typename TOp> template<typename TOp>
StringRef getOpcode(const TOp *op) StringRef getOpcode(const TOp *op)
{ {
return op->getOpcodeStr(op->getOpcode()); return op->getOpcodeStr(op->getOpcode());
} }
SourceLocation ResolveOperationSourceLocation(const SourceManager& source_manager, const SourceLocation& original)
{
if (source_manager.isMacroBodyExpansion(original))
{
return source_manager.getExpansionLoc(original);
}
return original;
}
} }
void OperationFinder::addMatcher(MatchFinder &finder) void OperationFinder::addMatcher(MatchFinder &finder)
{ {
finder.addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, ArithmeticMatcher), this); finder.addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, ArithmeticMatcher), this);
finder.addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, AssignmentMatcher), this); finder.addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, AssignmentMatcher), this);
finder.addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, CompoundMatcher), this); //finder.addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, CompoundMatcher), this);
finder.addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, UnaryArithmeticMatcher), this); finder.addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, UnaryArithmeticMatcher), this);
} }
@ -73,9 +86,8 @@ void OperationFinder::_processAssignment(const clang::ast_matchers::MatchFinder:
assert(op); assert(op);
const auto& loc = op->getBeginLoc();
const auto& source_manager = *result.SourceManager; const auto& source_manager = *result.SourceManager;
const auto& loc = ResolveOperationSourceLocation(source_manager, op->getBeginLoc());
llvm::outs() << source_manager.getFilename(loc) << ":" llvm::outs() << source_manager.getFilename(loc) << ":"
<< source_manager.getSpellingLineNumber(loc) << ":" << source_manager.getSpellingLineNumber(loc) << ":"
@ -93,8 +105,8 @@ void OperationFinder::_processArithmetic(const MatchFinder::MatchResult& result)
assert(op); assert(op);
const auto& loc = op->getBeginLoc();
const auto& source_manager = *result.SourceManager; const auto& source_manager = *result.SourceManager;
const auto& loc = ResolveOperationSourceLocation(source_manager, op->getBeginLoc());
llvm::outs() << source_manager.getFilename(loc) << ":" llvm::outs() << source_manager.getFilename(loc) << ":"
<< source_manager.getSpellingLineNumber(loc) << ":" << source_manager.getSpellingLineNumber(loc) << ":"
@ -117,8 +129,8 @@ void OperationFinder::_processCompoundStmt(const clang::ast_matchers::MatchFinde
if (!op || !op->isCompoundAssignmentOp()) if (!op || !op->isCompoundAssignmentOp())
return; return;
const auto& loc = op->getBeginLoc();
const auto& source_manager = *result.SourceManager; const auto& source_manager = *result.SourceManager;
const auto& loc = ResolveOperationSourceLocation(source_manager, op->getBeginLoc());
llvm::outs() << source_manager.getFilename(loc) << ":" llvm::outs() << source_manager.getFilename(loc) << ":"
<< source_manager.getSpellingLineNumber(loc) << ":" << source_manager.getSpellingLineNumber(loc) << ":"
@ -138,8 +150,8 @@ void OperationFinder::_processUnaryArithmetic(const MatchFinder::MatchResult &re
assert(op); assert(op);
assert(lhs); assert(lhs);
const auto& loc = lhs->getBeginLoc();
const auto& source_manager = *result.SourceManager; const auto& source_manager = *result.SourceManager;
const auto& loc = ResolveOperationSourceLocation(source_manager, op->getBeginLoc());
llvm::outs() << source_manager.getFilename(loc) << ":" llvm::outs() << source_manager.getFilename(loc) << ":"
<< source_manager.getSpellingLineNumber(loc) << ":" << source_manager.getSpellingLineNumber(loc) << ":"