Rewrote PrettyPrinter to output the document exactly as it was before

This means at the moment the PrettyPrinter isn't doing anything useful,
but the idea is that from here we can improve it to adapt the code to
a certain style.

Reviewed-by: Roberto Raggi
This commit is contained in:
Thorbjørn Lindeijer
2009-05-11 17:00:29 +02:00
parent ecc2b25ddc
commit 581dca0ad9
7 changed files with 310 additions and 427 deletions

View File

@@ -73,8 +73,9 @@ public:
void rewrite(const TranslationUnit *unit,
const QByteArray &contents,
QByteArray *out) const
QByteArray *out)
{
_source = contents;
const char *source = contents.constData();
unsigned previousTokenEndPosition = 0;
for (unsigned i = 0; i < unit->tokenCount(); ++i) {
@@ -104,6 +105,9 @@ public:
previousTokenEndPosition = tk.end();
}
}
protected:
QByteArray _source;
};
class SimpleRefactor: protected ASTVisitor, Rewrite {
@@ -192,12 +196,13 @@ protected:
return false;
}
virtual bool visit(CppCastExpressionAST *ast) {
virtual bool visit(CppCastExpressionAST *ast)
{
// Replace the C++ cast expression (e.g. static_cast<foo>(a)) with
// the one generated by the pretty printer.
std::ostringstream o;
PrettyPrinter pp(control(), o);
pp(ast);
pp(ast, _source);
remove(ast->firstToken(), ast->lastToken());
const std::string str = o.str();
insertTextBefore(ast->firstToken(), str.c_str());
@@ -267,7 +272,7 @@ int main(int argc, char *argv[])
printf("%s\n", out.constData());
} else if (test_pretty_printer) {
PrettyPrinter pp(&control, std::cout);
pp(unit.ast());
pp(unit.ast(), source);
}
return EXIT_SUCCESS;
}