Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Unified Diff: tools/clang/BindMigrate/BindMigrateConsumer.cpp

Issue 7886056: Clang plugin that rewrites PostTask(_, NewRunnableMethod(...)) to PostTask(_, base::Bind(...)); (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: break into files and make saner Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/clang/BindMigrate/BindMigrateConsumer.cpp
diff --git a/tools/clang/BindMigrate/BindMigrateConsumer.cpp b/tools/clang/BindMigrate/BindMigrateConsumer.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..4b8bae39f6a0bf2411623f79a55158c0b104db09
--- /dev/null
+++ b/tools/clang/BindMigrate/BindMigrateConsumer.cpp
@@ -0,0 +1,97 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+// Author: ajwong@google.com (Albert Wong)
+
+#include <iostream>
+#include <string>
+
+#include "BindMigrateConsumer.h"
+
+#include "clang/AST/AST.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/DeclGroup.h"
+#include "clang/AST/DeclVisitor.h"
+#include "clang/AST/DeclarationName.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/AST/ParentMap.h"
+#include "clang/AST/StmtVisitor.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/AST/TypeLocVisitor.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/IdentifierTable.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Index/DeclReferenceMap.h"
+#include "clang/Lex/Lexer.h"
+#include "clang/Rewrite/ASTConsumers.h"
+#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
+#include "clang/Frontend/CompilerInstance.h"
+
+#include "FileRemapper.h"
+#include "TransformPostTask.h"
+
+using namespace std;
+
+namespace clang {
+
+BindMigrateConsumer::BindMigrateConsumer(CompilerInstance* instance,
+ arcmt_hack::FileRemapper* remapper)
+ : instance_(instance),
+ error_emitter_(instance, "[bind-migrate] "),
+ remapper_(remapper) {
+}
+
+BindMigrateConsumer::~BindMigrateConsumer() {
+}
+
+void BindMigrateConsumer::Initialize(ASTContext &context) {
+ rewriter_.setSourceMgr(context.getSourceManager(), context.getLangOptions());
+}
+
+void BindMigrateConsumer::HandleTranslationUnit(ASTContext &context) {
+ TransformPostTask visitor(&context, &rewriter_, &error_emitter_);
+
+ // modify calls
+ TranslationUnitDecl *translation_unit = context.getTranslationUnitDecl();
+ for (DeclContext::decl_iterator it = translation_unit->decls_begin(),
+ end = translation_unit->decls_end(); it != end; ++it) {
+ // TODO(ajwong): Could test for
+ //
+ // context.getSourceManager()->isFromMainFile((*it)->getLocation())
+ //
+ // here to skip everything but the main file.
+ visitor.TraverseDecl(*it);
+ }
+
+ // Get the buffer corresponding to MainFileID.
+ for (Rewriter::buffer_iterator it = rewriter_.buffer_begin();
+ it != rewriter_.buffer_end();
+ ++it) {
+ const FileEntry *file =
+ context.getSourceManager().getFileEntryForID(it->first);
+ assert(file);
+
+ std::string new_filename = file->getName();
+ new_filename += "-trans";
+ llvm::SmallString<512> new_text;
+ llvm::raw_svector_ostream vecOS(new_text);
+ it->second.write(vecOS);
+ vecOS.flush();
+ llvm::MemoryBuffer *mem_buf = llvm::MemoryBuffer::getMemBufferCopy(
+ StringRef(new_text.data(), new_text.size()), new_filename);
+ llvm::SmallString<64> orig_filename(file->getName());
+ instance_->getFileManager().FixupRelativePath(orig_filename);
+ remapper_->remap(orig_filename.str(), mem_buf);
+ }
+
+
+ // TODO(ajwong): We should just hook into BeginInvocation() like ARCMigrate
+ // does. As is, it is hard to flush at the end of the compilation sequence
+ // because there's no such thing as EndInvocation().
Nico 2011/09/16 03:00:54 HandleTranslationUnit() is called only after every
awong 2011/09/16 10:24:32 I misunderstood BeginInvocation. What I was tryin
+ remapper_->writeParallelTree("/tmp/whatevs", instance_->getDiagnostics());
+}
+
+} // namespace clang

Powered by Google App Engine
This is Rietveld 408576698