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 <memory> |
| 6 #include <string> |
| 7 #include <sstream> |
| 8 |
| 9 #include "clang/Basic/Diagnostic.h" |
| 10 #include "clang/Frontend/CompilerInstance.h" |
| 11 #include "clang/Frontend/FrontendAction.h" |
| 12 #include "clang/Frontend/FrontendPluginRegistry.h" |
| 13 #include "llvm/Support/raw_ostream.h" |
| 14 |
| 15 #include "BindMigrateConsumer.h" |
| 16 #include "BindMigrateOptions.h" |
| 17 |
| 18 using namespace std; |
| 19 |
| 20 namespace clang { |
| 21 |
| 22 void SplitString(const string &input, char delim, vector<string>* output) { |
| 23 stringstream stream(input); |
| 24 string arg; |
| 25 while(getline(stream, arg, delim)) { |
| 26 output->push_back(arg); |
| 27 } |
| 28 } |
| 29 |
| 30 class BindMigrateAction : public PluginASTAction { |
| 31 public: |
| 32 BindMigrateAction() { |
| 33 } |
| 34 |
| 35 virtual ~BindMigrateAction() { |
| 36 } |
| 37 |
| 38 protected: |
| 39 ASTConsumer *CreateASTConsumer(CompilerInstance &instance, llvm::StringRef) { |
| 40 return new BindMigrateConsumer(&instance, options_); |
| 41 } |
| 42 |
| 43 virtual bool BeginInvocation(CompilerInstance &instance) { |
| 44 return true; |
| 45 } |
| 46 |
| 47 virtual bool ParseArgs(const CompilerInstance &instance, |
| 48 const std::vector<std::string>& args) { |
| 49 if (!instance.getLangOpts().CPlusPlus) { |
| 50 return false; |
| 51 } |
| 52 for (unsigned i = 0, e = args.size(); i != e; ++i) { |
| 53 size_t equal_pos = args[i].find("="); |
| 54 if (equal_pos == string::npos) { |
| 55 Diagnostic &diagnostics = instance.getDiagnostics(); |
| 56 unsigned daig_id = diagnostics.getCustomDiagID( |
| 57 Diagnostic::Error, "invalid argument '" + args[i] + "'"); |
| 58 diagnostics.Report(daig_id); |
| 59 continue; |
| 60 } |
| 61 |
| 62 string optname = args[i].substr(0, equal_pos); |
| 63 string optval = args[i].substr(equal_pos + 1); |
| 64 |
| 65 if (optname == "out-dir") { |
| 66 if (optval.empty()) { |
| 67 Diagnostic &diagnostics = instance.getDiagnostics(); |
| 68 unsigned daig_id = diagnostics.getCustomDiagID( |
| 69 Diagnostic::Error, "out-dir cannot be empty '" + args[i] + "'"); |
| 70 diagnostics.Report(daig_id); |
| 71 continue; |
| 72 } |
| 73 options_.out_dir = optval; |
| 74 } else if (optname == "only-path-prefix") { |
| 75 if (optval.empty()) { |
| 76 Diagnostic &diagnostics = instance.getDiagnostics(); |
| 77 unsigned daig_id = diagnostics.getCustomDiagID( |
| 78 Diagnostic::Error, |
| 79 "only-path-prefix should be comma separate list: '" + |
| 80 args[i] + "'"); |
| 81 diagnostics.Report(daig_id); |
| 82 continue; |
| 83 } |
| 84 SplitString(optval, ',', &options_.only_path_prefix); |
| 85 } else if (optname == "only_types") { |
| 86 if (optval.empty()) { |
| 87 Diagnostic &diagnostics = instance.getDiagnostics(); |
| 88 unsigned daig_id = diagnostics.getCustomDiagID( |
| 89 Diagnostic::Error, |
| 90 "only-types should be comma separate list: '" + args[i] + "'"); |
| 91 diagnostics.Report(daig_id); |
| 92 continue; |
| 93 } |
| 94 SplitString(optval, ',', &options_.only_types); |
| 95 } else if (optname == "ignore-path-prefix") { |
| 96 if (optval.empty()) { |
| 97 Diagnostic &diagnostics = instance.getDiagnostics(); |
| 98 unsigned daig_id = diagnostics.getCustomDiagID( |
| 99 Diagnostic::Error, |
| 100 "ignore-path-prefix should be comma separate list: '" + |
| 101 args[i] + "'"); |
| 102 diagnostics.Report(daig_id); |
| 103 continue; |
| 104 } |
| 105 SplitString(optval, ',', &options_.ignore_path_prefix); |
| 106 } else if (optname == "mode") { |
| 107 options_.mode = optval; |
| 108 } |
| 109 } |
| 110 |
| 111 return true; |
| 112 } |
| 113 |
| 114 private: |
| 115 BindMigrateOptions options_; |
| 116 }; |
| 117 |
| 118 } // namespace clang |
| 119 |
| 120 static clang::FrontendPluginRegistry::Add<clang::BindMigrateAction> |
| 121 X("bind-migrate", "Migrate old callback constructs to base::Bind."); |
OLD | NEW |