|
| 1 | +//============================================================================== |
| 2 | +// FILE: |
| 3 | +// clangpass_tool.cpp |
| 4 | +// |
| 5 | +// DESCRIPTION: |
| 6 | +// Replaces all ::std:: names usages to custom runtime friendly |
| 7 | +// implementations |
| 8 | +// |
| 9 | +// USAGE: |
| 10 | +// * ./build/bin/clangpass_tool --replace-names=::std::atomic,::std::mutex |
| 11 | +// --insert-names=LTestAtomic,ltest::mutex |
| 12 | +// ./verifying/targets/nonlinear_queue.cpp |
| 13 | +// |
| 14 | +// License: The Unlicense |
| 15 | +//============================================================================== |
| 16 | +#include <clang/Basic/Diagnostic.h> |
| 17 | +#include <clang/Rewrite/Core/Rewriter.h> |
| 18 | +#include <clang/Tooling/Refactoring/Rename/RenamingAction.h> |
| 19 | +#include <clang/Tooling/Refactoring/Rename/USRFindingAction.h> |
| 20 | + |
| 21 | +#include "clang/Frontend/CompilerInstance.h" |
| 22 | +#include "clang/Tooling/CommonOptionsParser.h" |
| 23 | +#include "clang/Tooling/Refactoring.h" |
| 24 | +#include "include/clangpass.h" |
| 25 | +#include "llvm/Support/CommandLine.h" |
| 26 | + |
| 27 | +using namespace clang; |
| 28 | +using namespace replace_pass; |
| 29 | + |
| 30 | +//===----------------------------------------------------------------------===// |
| 31 | +// Command line options |
| 32 | +//===----------------------------------------------------------------------===// |
| 33 | +static cl::OptionCategory CodeRefactorCategory("atomics-replacer options"); |
| 34 | + |
| 35 | +static cl::opt<std::string> TemporaryPrefix{ |
| 36 | + "temp-prefix", cl::desc("Prefix for temporary files"), cl::init("__tmp_"), |
| 37 | + cl::cat(CodeRefactorCategory)}; |
| 38 | +static cl::list<std::string> ClassNamesToReplace{ |
| 39 | + "replace-names", |
| 40 | + cl::desc("Names of the classes/structs which usages should be renamed"), |
| 41 | + cl::OneOrMore, cl::CommaSeparated, cl::cat(CodeRefactorCategory)}; |
| 42 | +static cl::list<std::string> ClassNamesToInsert{ |
| 43 | + "insert-names", |
| 44 | + cl::desc("Names of the classes/structs which should be used instead"), |
| 45 | + cl::OneOrMore, cl::CommaSeparated, cl::cat(CodeRefactorCategory)}; |
| 46 | + |
| 47 | +//===----------------------------------------------------------------------===// |
| 48 | +// PluginASTAction |
| 49 | +//===----------------------------------------------------------------------===// |
| 50 | +class CodeRefactorPluginAction : public PluginASTAction { |
| 51 | + public: |
| 52 | + explicit CodeRefactorPluginAction() {} |
| 53 | + // Not used |
| 54 | + bool ParseArgs(const CompilerInstance &compiler, |
| 55 | + const std::vector<std::string> &args) override { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &compiler, |
| 60 | + StringRef file) override { |
| 61 | + rewriter.setSourceMgr(compiler.getSourceManager(), compiler.getLangOpts()); |
| 62 | + |
| 63 | + std::vector<ReplacePair> pairs; |
| 64 | + pairs.reserve(ClassNamesToReplace.size()); |
| 65 | + for (int i = 0; i < ClassNamesToReplace.size(); ++i) { |
| 66 | + pairs.emplace_back(ClassNamesToReplace[i], ClassNamesToInsert[i]); |
| 67 | + } |
| 68 | + |
| 69 | + return std::make_unique<CodeRefactorASTConsumer>( |
| 70 | + compiler.getASTContext(), rewriter, pairs, TemporaryPrefix); |
| 71 | + } |
| 72 | + |
| 73 | + private: |
| 74 | + Rewriter rewriter; |
| 75 | +}; |
| 76 | + |
| 77 | +//===----------------------------------------------------------------------===// |
| 78 | +// Main driver code. |
| 79 | +//===----------------------------------------------------------------------===// |
| 80 | +int main(int argc, const char **argv) { |
| 81 | + Expected<tooling::CommonOptionsParser> options = |
| 82 | + clang::tooling::CommonOptionsParser::create(argc, argv, |
| 83 | + CodeRefactorCategory); |
| 84 | + if (auto E = options.takeError()) { |
| 85 | + errs() << "Problem constructing CommonOptionsParser " |
| 86 | + << toString(std::move(E)) << '\n'; |
| 87 | + return EXIT_FAILURE; |
| 88 | + } |
| 89 | + |
| 90 | + // auto files = eOptParser->getSourcePathList(); |
| 91 | + // std::vector<const char*> Args = {"clang++", "-E" }; |
| 92 | + // Args.insert(Args.end(), files.begin(), files.end()); |
| 93 | + |
| 94 | + // auto OptionsParser = clang::tooling::CommonOptionsParser::create( |
| 95 | + // Args.size(), Args.data(), |
| 96 | + // ); |
| 97 | + |
| 98 | + // clang::tooling::ClangTool Tool(OptionsParser->getCompilations(), |
| 99 | + // OptionsParser->getSourcePathList()); |
| 100 | + |
| 101 | + clang::tooling::RefactoringTool tool(options->getCompilations(), |
| 102 | + options->getSourcePathList()); |
| 103 | + |
| 104 | + return tool.run( |
| 105 | + clang::tooling::newFrontendActionFactory<CodeRefactorPluginAction>() |
| 106 | + .get()); |
| 107 | +} |
0 commit comments