Cache the result of errs() and implement formatted logging.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@109740 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
John McCall
2010-07-29 08:14:41 +00:00
parent 3dd706b528
commit 62dc1f3d82
2 changed files with 58 additions and 37 deletions

View File

@@ -162,8 +162,8 @@ class FunctionDifferenceEngine {
if (Ref) { if (Ref) {
if (Ref == R) return false; if (Ref == R) return false;
Engine.logf("successor %s cannot be equivalent to %s; " Engine.logf("successor %l cannot be equivalent to %r; "
"it's already equivalent to %s") "it's already equivalent to %r")
<< L << R << Ref; << L << R << Ref;
return true; return true;
} }
@@ -220,7 +220,7 @@ class FunctionDifferenceEngine {
for (unsigned I = 0, E = L.arg_size(); I != E; ++I) for (unsigned I = 0, E = L.arg_size(); I != E; ++I)
if (!equivalentAsOperands(L.getArgument(I), R.getArgument(I))) { if (!equivalentAsOperands(L.getArgument(I), R.getArgument(I))) {
if (Complain) if (Complain)
Engine.logf("arguments %s and %s differ") Engine.logf("arguments %l and %r differ")
<< L.getArgument(I) << R.getArgument(I); << L.getArgument(I) << R.getArgument(I);
return true; return true;
} }
@@ -309,7 +309,7 @@ class FunctionDifferenceEngine {
LCases.erase(CaseValue); LCases.erase(CaseValue);
} else if (!Difference) { } else if (!Difference) {
if (Complain) if (Complain)
Engine.logf("right switch has extra case %s") << CaseValue; Engine.logf("right switch has extra case %r") << CaseValue;
Difference = true; Difference = true;
} }
} }
@@ -317,7 +317,7 @@ class FunctionDifferenceEngine {
for (DenseMap<ConstantInt*,BasicBlock*>::iterator for (DenseMap<ConstantInt*,BasicBlock*>::iterator
I = LCases.begin(), E = LCases.end(); I != E; ++I) { I = LCases.begin(), E = LCases.end(); I != E; ++I) {
if (Complain) if (Complain)
Engine.logf("left switch has extra case %s") << I->first; Engine.logf("left switch has extra case %l") << I->first;
Difference = true; Difference = true;
} }
return Difference; return Difference;
@@ -333,7 +333,7 @@ class FunctionDifferenceEngine {
for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) { for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) {
Value *LO = L->getOperand(I), *RO = R->getOperand(I); Value *LO = L->getOperand(I), *RO = R->getOperand(I);
if (!equivalentAsOperands(LO, RO)) { if (!equivalentAsOperands(LO, RO)) {
if (Complain) Engine.logf("operands %s and %s differ") << LO << RO; if (Complain) Engine.logf("operands %l and %r differ") << LO << RO;
return true; return true;
} }
} }
@@ -580,13 +580,13 @@ void DifferenceEngine::diff(Module *L, Module *R) {
if (Function *RFn = R->getFunction(LFn->getName())) if (Function *RFn = R->getFunction(LFn->getName()))
Queue.push_back(std::make_pair(LFn, RFn)); Queue.push_back(std::make_pair(LFn, RFn));
else else
logf("function %s exists only in left module") << LFn; logf("function %l exists only in left module") << LFn;
} }
for (Module::iterator I = R->begin(), E = R->end(); I != E; ++I) { for (Module::iterator I = R->begin(), E = R->end(); I != E; ++I) {
Function *RFn = &*I; Function *RFn = &*I;
if (!LNames.count(RFn->getName())) if (!LNames.count(RFn->getName()))
logf("function %s exists only in right module") << RFn; logf("function %r exists only in right module") << RFn;
} }
for (SmallVectorImpl<std::pair<Function*,Function*> >::iterator for (SmallVectorImpl<std::pair<Function*,Function*> >::iterator

View File

@@ -14,6 +14,7 @@
#include <llvm/Bitcode/ReaderWriter.h> #include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/Support/raw_ostream.h> #include <llvm/Support/raw_ostream.h>
#include <llvm/Support/ErrorHandling.h>
#include <llvm/LLVMContext.h> #include <llvm/LLVMContext.h>
#include <llvm/Module.h> #include <llvm/Module.h>
#include <llvm/Type.h> #include <llvm/Type.h>
@@ -99,6 +100,7 @@ void ComputeNumbering(Function *F, DenseMap<Value*,unsigned> &Numbering) {
class DiffConsumer : public DifferenceEngine::Consumer { class DiffConsumer : public DifferenceEngine::Consumer {
private: private:
raw_ostream &out;
Module *LModule; Module *LModule;
Module *RModule; Module *RModule;
SmallVector<DiffContext, 5> contexts; SmallVector<DiffContext, 5> contexts;
@@ -107,21 +109,21 @@ private:
void printValue(Value *V, bool isL) { void printValue(Value *V, bool isL) {
if (V->hasName()) { if (V->hasName()) {
errs() << (isa<GlobalValue>(V) ? '@' : '%') << V->getName(); out << (isa<GlobalValue>(V) ? '@' : '%') << V->getName();
return; return;
} }
if (V->getType()->isVoidTy()) { if (V->getType()->isVoidTy()) {
if (isa<StoreInst>(V)) { if (isa<StoreInst>(V)) {
errs() << "store to "; out << "store to ";
printValue(cast<StoreInst>(V)->getPointerOperand(), isL); printValue(cast<StoreInst>(V)->getPointerOperand(), isL);
} else if (isa<CallInst>(V)) { } else if (isa<CallInst>(V)) {
errs() << "call to "; out << "call to ";
printValue(cast<CallInst>(V)->getCalledValue(), isL); printValue(cast<CallInst>(V)->getCalledValue(), isL);
} else if (isa<InvokeInst>(V)) { } else if (isa<InvokeInst>(V)) {
errs() << "invoke to "; out << "invoke to ";
printValue(cast<InvokeInst>(V)->getCalledValue(), isL); printValue(cast<InvokeInst>(V)->getCalledValue(), isL);
} else { } else {
errs() << *V; out << *V;
} }
return; return;
} }
@@ -134,17 +136,17 @@ private:
if (isL) { if (isL) {
if (ctxt.LNumbering.empty()) if (ctxt.LNumbering.empty())
ComputeNumbering(cast<Function>(ctxt.L), ctxt.LNumbering); ComputeNumbering(cast<Function>(ctxt.L), ctxt.LNumbering);
errs() << '%' << ctxt.LNumbering[V]; out << '%' << ctxt.LNumbering[V];
return; return;
} else { } else {
if (ctxt.RNumbering.empty()) if (ctxt.RNumbering.empty())
ComputeNumbering(cast<Function>(ctxt.R), ctxt.RNumbering); ComputeNumbering(cast<Function>(ctxt.R), ctxt.RNumbering);
errs() << '%' << ctxt.RNumbering[V]; out << '%' << ctxt.RNumbering[V];
return; return;
} }
} }
errs() << "<anonymous>"; out << "<anonymous>";
} }
void header() { void header() {
@@ -154,28 +156,28 @@ private:
if (I->Differences) continue; if (I->Differences) continue;
if (isa<Function>(I->L)) { if (isa<Function>(I->L)) {
// Extra newline between functions. // Extra newline between functions.
if (Differences) errs() << "\n"; if (Differences) out << "\n";
Function *L = cast<Function>(I->L); Function *L = cast<Function>(I->L);
Function *R = cast<Function>(I->R); Function *R = cast<Function>(I->R);
if (L->getName() != R->getName()) if (L->getName() != R->getName())
errs() << "in function " << L->getName() << " / " << R->getName() << ":\n"; out << "in function " << L->getName() << " / " << R->getName() << ":\n";
else else
errs() << "in function " << L->getName() << ":\n"; out << "in function " << L->getName() << ":\n";
} else if (isa<BasicBlock>(I->L)) { } else if (isa<BasicBlock>(I->L)) {
BasicBlock *L = cast<BasicBlock>(I->L); BasicBlock *L = cast<BasicBlock>(I->L);
BasicBlock *R = cast<BasicBlock>(I->R); BasicBlock *R = cast<BasicBlock>(I->R);
errs() << " in block "; out << " in block ";
printValue(L, true); printValue(L, true);
errs() << " / "; out << " / ";
printValue(R, false); printValue(R, false);
errs() << ":\n"; out << ":\n";
} else if (isa<Instruction>(I->L)) { } else if (isa<Instruction>(I->L)) {
errs() << " in instruction "; out << " in instruction ";
printValue(I->L, true); printValue(I->L, true);
errs() << " / "; out << " / ";
printValue(I->R, false); printValue(I->R, false);
errs() << ":\n"; out << ":\n";
} }
I->Differences = true; I->Differences = true;
@@ -184,12 +186,12 @@ private:
void indent() { void indent() {
unsigned N = Indent; unsigned N = Indent;
while (N--) errs() << ' '; while (N--) out << ' ';
} }
public: public:
DiffConsumer(Module *L, Module *R) DiffConsumer(Module *L, Module *R)
: LModule(L), RModule(R), Differences(false), Indent(0) {} : out(errs()), LModule(L), RModule(R), Differences(false), Indent(0) {}
bool hadDifferences() const { return Differences; } bool hadDifferences() const { return Differences; }
@@ -206,18 +208,37 @@ public:
void log(StringRef text) { void log(StringRef text) {
header(); header();
indent(); indent();
errs() << text << "\n"; out << text << '\n';
} }
void logf(const DifferenceEngine::LogBuilder &Log) { void logf(const DifferenceEngine::LogBuilder &Log) {
header(); header();
indent(); indent();
// FIXME: we don't know whether these are l-values or r-values (ha!) unsigned arg = 0;
// Print them in some saner way!
errs() << Log.getFormat() << "\n"; StringRef format = Log.getFormat();
for (unsigned I = 0, E = Log.getNumArguments(); I != E; ++I) while (true) {
Log.getArgument(I)->dump(); size_t percent = format.find('%');
if (percent == StringRef::npos) {
out << format;
break;
}
assert(format[percent] == '%');
if (percent > 0) out << format.substr(0, percent);
switch (format[percent+1]) {
case '%': out << '%'; break;
case 'l': printValue(Log.getArgument(arg++), true); break;
case 'r': printValue(Log.getArgument(arg++), false); break;
default: llvm_unreachable("unknown format character");
}
format = format.substr(percent+2);
}
out << '\n';
} }
void logd(const DifferenceEngine::DiffLogBuilder &Log) { void logd(const DifferenceEngine::DiffLogBuilder &Log) {
@@ -227,22 +248,22 @@ public:
indent(); indent();
switch (Log.getLineKind(I)) { switch (Log.getLineKind(I)) {
case DifferenceEngine::DC_match: case DifferenceEngine::DC_match:
errs() << " "; out << " ";
Log.getLeft(I)->dump(); Log.getLeft(I)->dump();
//printValue(Log.getLeft(I), true); //printValue(Log.getLeft(I), true);
break; break;
case DifferenceEngine::DC_left: case DifferenceEngine::DC_left:
errs() << "< "; out << "< ";
Log.getLeft(I)->dump(); Log.getLeft(I)->dump();
//printValue(Log.getLeft(I), true); //printValue(Log.getLeft(I), true);
break; break;
case DifferenceEngine::DC_right: case DifferenceEngine::DC_right:
errs() << "> "; out << "> ";
Log.getRight(I)->dump(); Log.getRight(I)->dump();
//printValue(Log.getRight(I), false); //printValue(Log.getRight(I), false);
break; break;
} }
//errs() << "\n"; //out << "\n";
} }
} }