Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2011 Google Inc. All Rights Reserved. | |
| 2 // Author: ajwong@google.com (Albert Wong) | |
| 3 | |
| 4 #include <iostream> | |
| 5 #include <string> | |
| 6 | |
| 7 #include "BindMigrateConsumer.h" | |
| 8 | |
| 9 #include "clang/AST/AST.h" | |
| 10 #include "clang/AST/Decl.h" | |
| 11 #include "clang/AST/DeclGroup.h" | |
| 12 #include "clang/AST/DeclVisitor.h" | |
| 13 #include "clang/AST/DeclarationName.h" | |
| 14 #include "clang/AST/ExprCXX.h" | |
| 15 #include "clang/AST/ParentMap.h" | |
| 16 #include "clang/AST/StmtVisitor.h" | |
| 17 #include "clang/AST/RecursiveASTVisitor.h" | |
| 18 #include "clang/AST/TypeLocVisitor.h" | |
| 19 #include "clang/Basic/FileManager.h" | |
| 20 #include "clang/Basic/IdentifierTable.h" | |
| 21 #include "clang/Basic/SourceManager.h" | |
| 22 #include "clang/Index/DeclReferenceMap.h" | |
| 23 #include "clang/Lex/Lexer.h" | |
| 24 #include "clang/Rewrite/ASTConsumers.h" | |
| 25 #include "llvm/ADT/DenseSet.h" | |
| 26 #include "llvm/ADT/OwningPtr.h" | |
| 27 #include "llvm/ADT/SmallPtrSet.h" | |
| 28 #include "llvm/ADT/StringExtras.h" | |
| 29 #include "llvm/Support/MemoryBuffer.h" | |
| 30 #include "llvm/Support/raw_ostream.h" | |
| 31 #include "clang/Frontend/CompilerInstance.h" | |
| 32 | |
| 33 #include "FileRemapper.h" | |
| 34 #include "TransformPostTask.h" | |
| 35 | |
| 36 using namespace std; | |
| 37 | |
| 38 namespace clang { | |
| 39 | |
| 40 BindMigrateConsumer::BindMigrateConsumer(CompilerInstance* instance, | |
| 41 arcmt_hack::FileRemapper* remapper) | |
| 42 : instance_(instance), | |
| 43 error_emitter_(instance, "[bind-migrate] "), | |
| 44 remapper_(remapper) { | |
| 45 } | |
| 46 | |
| 47 BindMigrateConsumer::~BindMigrateConsumer() { | |
| 48 } | |
| 49 | |
| 50 void BindMigrateConsumer::Initialize(ASTContext &context) { | |
| 51 rewriter_.setSourceMgr(context.getSourceManager(), context.getLangOptions()); | |
| 52 } | |
| 53 | |
| 54 void BindMigrateConsumer::HandleTranslationUnit(ASTContext &context) { | |
| 55 TransformPostTask visitor(&context, &rewriter_, &error_emitter_); | |
| 56 | |
| 57 // modify calls | |
| 58 TranslationUnitDecl *translation_unit = context.getTranslationUnitDecl(); | |
| 59 for (DeclContext::decl_iterator it = translation_unit->decls_begin(), | |
| 60 end = translation_unit->decls_end(); it != end; ++it) { | |
| 61 // TODO(ajwong): Could test for | |
| 62 // | |
| 63 // context.getSourceManager()->isFromMainFile((*it)->getLocation()) | |
| 64 // | |
| 65 // here to skip everything but the main file. | |
| 66 visitor.TraverseDecl(*it); | |
| 67 } | |
| 68 | |
| 69 // Get the buffer corresponding to MainFileID. | |
| 70 for (Rewriter::buffer_iterator it = rewriter_.buffer_begin(); | |
| 71 it != rewriter_.buffer_end(); | |
| 72 ++it) { | |
| 73 const FileEntry *file = | |
| 74 context.getSourceManager().getFileEntryForID(it->first); | |
| 75 assert(file); | |
| 76 | |
| 77 std::string new_filename = file->getName(); | |
| 78 new_filename += "-trans"; | |
| 79 llvm::SmallString<512> new_text; | |
| 80 llvm::raw_svector_ostream vecOS(new_text); | |
| 81 it->second.write(vecOS); | |
| 82 vecOS.flush(); | |
| 83 llvm::MemoryBuffer *mem_buf = llvm::MemoryBuffer::getMemBufferCopy( | |
| 84 StringRef(new_text.data(), new_text.size()), new_filename); | |
| 85 llvm::SmallString<64> orig_filename(file->getName()); | |
| 86 instance_->getFileManager().FixupRelativePath(orig_filename); | |
| 87 remapper_->remap(orig_filename.str(), mem_buf); | |
| 88 } | |
| 89 | |
| 90 | |
| 91 // TODO(ajwong): We should just hook into BeginInvocation() like ARCMigrate | |
| 92 // does. As is, it is hard to flush at the end of the compilation sequence | |
| 93 // 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
| |
| 94 remapper_->writeParallelTree("/tmp/whatevs", instance_->getDiagnostics()); | |
| 95 } | |
| 96 | |
| 97 } // namespace clang | |
| OLD | NEW |