OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <iostream> |
| 6 #include <string> |
| 7 |
| 8 #include "BindMigrateConsumer.h" |
| 9 |
| 10 #include "clang/AST/AST.h" |
| 11 #include "clang/AST/Decl.h" |
| 12 #include "clang/AST/DeclGroup.h" |
| 13 #include "clang/AST/DeclVisitor.h" |
| 14 #include "clang/AST/DeclarationName.h" |
| 15 #include "clang/AST/ExprCXX.h" |
| 16 #include "clang/AST/ParentMap.h" |
| 17 #include "clang/AST/StmtVisitor.h" |
| 18 #include "clang/AST/RecursiveASTVisitor.h" |
| 19 #include "clang/AST/TypeLocVisitor.h" |
| 20 #include "clang/Basic/FileManager.h" |
| 21 #include "clang/Basic/IdentifierTable.h" |
| 22 #include "clang/Basic/SourceManager.h" |
| 23 #include "clang/Index/DeclReferenceMap.h" |
| 24 #include "clang/Lex/Lexer.h" |
| 25 #include "clang/Rewrite/ASTConsumers.h" |
| 26 #include "llvm/ADT/DenseSet.h" |
| 27 #include "llvm/ADT/OwningPtr.h" |
| 28 #include "llvm/ADT/SmallPtrSet.h" |
| 29 #include "llvm/ADT/StringExtras.h" |
| 30 #include "llvm/Support/MemoryBuffer.h" |
| 31 #include "llvm/Support/FileSystem.h" |
| 32 #include "llvm/Support/raw_ostream.h" |
| 33 #include "clang/Frontend/CompilerInstance.h" |
| 34 |
| 35 #include "BindMigrateOptions.h" |
| 36 #include "OldCallbackTransform.h" |
| 37 #include "PostTaskTransform.h" |
| 38 #include "Util.h" |
| 39 |
| 40 using namespace std; |
| 41 |
| 42 namespace clang { |
| 43 |
| 44 BindMigrateConsumer::BindMigrateConsumer(CompilerInstance* instance, |
| 45 const BindMigrateOptions& options) |
| 46 : instance_(instance), |
| 47 error_emitter_(instance, "[bind-migrate] "), |
| 48 options_(options) { |
| 49 } |
| 50 |
| 51 BindMigrateConsumer::~BindMigrateConsumer() { |
| 52 } |
| 53 |
| 54 void BindMigrateConsumer::Initialize(ASTContext &context) { |
| 55 remapper_.initFromDisk(options_.out_dir, |
| 56 instance_->getDiagnostics(), |
| 57 /*ignoreIfFilesChanges=*/true); |
| 58 |
| 59 rewriter_.setSourceMgr(context.getSourceManager(), context.getLangOptions()); |
| 60 } |
| 61 |
| 62 void BindMigrateConsumer::HandleTranslationUnit(ASTContext &context) { |
| 63 OldCallbackTransform old_callback_tf(&context, &rewriter_, &error_emitter_); |
| 64 PostTaskTransform post_task_tf(&context, &rewriter_, &error_emitter_); |
| 65 |
| 66 // Create actual TransformApplier with all the wanted transforms. |
| 67 std::vector<Transform*> transforms; |
| 68 transforms.push_back(&old_callback_tf); |
| 69 transforms.push_back(&post_task_tf); |
| 70 TransformApplier transform_applier(transforms); |
| 71 TransformCandidatePrinter transform_candidate_printer( |
| 72 transforms, &context.getSourceManager()); |
| 73 |
| 74 // modify calls |
| 75 TranslationUnitDecl *translation_unit = context.getTranslationUnitDecl(); |
| 76 for (DeclContext::decl_iterator it = translation_unit->decls_begin(), |
| 77 end = translation_unit->decls_end(); it != end; ++it) { |
| 78 |
| 79 SourceManager& source_manager = context.getSourceManager(); |
| 80 |
| 81 // Only consider our files. |
| 82 const FileEntry* file_entry = |
| 83 GetValidFile(source_manager, (*it)->getLocation()); |
| 84 if (!file_entry) { |
| 85 continue; |
| 86 } |
| 87 |
| 88 // Apply blacklist. |
| 89 string file_name = file_entry->getName(); |
| 90 bool in_blacklist = false; |
| 91 for (vector<string>::const_iterator path = |
| 92 options_.ignore_path_prefix.begin(); |
| 93 path != options_.ignore_path_prefix.end(); |
| 94 ++path) { |
| 95 llvm::SmallString<512> abs_path; |
| 96 llvm::Twine(*path).toVector(abs_path); |
| 97 llvm::sys::fs::make_absolute(abs_path); |
| 98 if (file_name.find(abs_path.c_str()) != string::npos) { |
| 99 in_blacklist = true; |
| 100 break; |
| 101 } |
| 102 } |
| 103 if (in_blacklist) continue; |
| 104 |
| 105 // Apply whitelist. |
| 106 bool in_whitelist = options_.only_path_prefix.empty(); |
| 107 for (vector<string>::const_iterator path = |
| 108 options_.only_path_prefix.begin(); |
| 109 path != options_.only_path_prefix.end(); |
| 110 ++path) { |
| 111 llvm::SmallString<512> abs_path; |
| 112 llvm::Twine(*path).toVector(abs_path); |
| 113 llvm::sys::fs::make_absolute(abs_path); |
| 114 if (file_name.find(abs_path.c_str()) != string::npos) { |
| 115 in_whitelist = true; |
| 116 break; |
| 117 } |
| 118 } |
| 119 if (!in_whitelist) continue; |
| 120 |
| 121 if (options_.mode == "print") { |
| 122 transform_candidate_printer.TraverseDecl(*it); |
| 123 } else if (options_.mode == "transform") { |
| 124 transform_applier.TraverseDecl(*it); |
| 125 } |
| 126 } |
| 127 |
| 128 // Get the buffer corresponding to MainFileID. |
| 129 for (Rewriter::buffer_iterator it = rewriter_.buffer_begin(); |
| 130 it != rewriter_.buffer_end(); |
| 131 ++it) { |
| 132 const FileEntry *file = |
| 133 context.getSourceManager().getFileEntryForID(it->first); |
| 134 assert(file); |
| 135 |
| 136 std::string new_filename = file->getName(); |
| 137 new_filename += "-trans"; |
| 138 llvm::SmallString<512> new_text; |
| 139 llvm::raw_svector_ostream vecOS(new_text); |
| 140 it->second.write(vecOS); |
| 141 vecOS.flush(); |
| 142 llvm::MemoryBuffer *mem_buf = llvm::MemoryBuffer::getMemBufferCopy( |
| 143 StringRef(new_text.data(), new_text.size()), new_filename); |
| 144 llvm::SmallString<64> orig_filename(file->getName()); |
| 145 instance_->getFileManager().FixupRelativePath(orig_filename); |
| 146 remapper_.remap(orig_filename.str(), mem_buf); |
| 147 } |
| 148 |
| 149 // TODO(ajwong): We should just hook into BeginInvocation() like ARCMigrate |
| 150 // does. As is, it is hard to flush at the end of the compilation sequence |
| 151 // because there's no such thing as EndInvocation(). |
| 152 remapper_.writeParallelTree(options_.out_dir, instance_->getDiagnostics()); |
| 153 } |
| 154 |
| 155 } // namespace clang |
OLD | NEW |