Devon Loehr | 3b59be9 | 2025-03-10 14:16:16 | [diff] [blame] | 1 | // Copyright 2025 The Chromium Authors |
| 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 "OutputHelper.h" |
| 6 | |
| 7 | void OutputHelper::Delete(const clang::CharSourceRange& replacement_range, |
| 8 | const clang::SourceManager& source_manager, |
| 9 | const clang::LangOptions& lang_opts) { |
| 10 | Replace(replacement_range, "", source_manager, lang_opts); |
| 11 | } |
| 12 | |
| 13 | // Replaces `replacement_range` with `replacement_text`. |
| 14 | void OutputHelper::Replace(const clang::CharSourceRange& replacement_range, |
| 15 | std::string replacement_text, |
| 16 | const clang::SourceManager& source_manager, |
| 17 | const clang::LangOptions& lang_opts) { |
| 18 | clang::tooling::Replacement replacement(source_manager, replacement_range, |
| 19 | std::move(replacement_text), |
| 20 | lang_opts); |
| 21 | |
| 22 | llvm::StringRef file_path = replacement.getFilePath(); |
| 23 | if (file_path.empty()) { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | PrintReplacement(file_path, replacement.getOffset(), replacement.getLength(), |
| 28 | replacement.getReplacementText()); |
| 29 | } |
| 30 | // Inserts `lhs` and `rhs` to the left and right of `replacement_range`. |
| 31 | void OutputHelper::Wrap(const clang::CharSourceRange& replacement_range, |
| 32 | std::string_view lhs, |
| 33 | std::string_view rhs, |
| 34 | const clang::SourceManager& source_manager, |
| 35 | const clang::LangOptions& lang_opts) { |
| 36 | clang::tooling::Replacement replacement(source_manager, replacement_range, "", |
| 37 | lang_opts); |
| 38 | |
| 39 | llvm::StringRef file_path = replacement.getFilePath(); |
| 40 | if (file_path.empty()) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | PrintReplacement(file_path, replacement.getOffset(), 0, lhs); |
| 45 | PrintReplacement(file_path, replacement.getOffset() + replacement.getLength(), |
| 46 | 0, rhs); |
| 47 | } |
| 48 | |
| 49 | // This is run automatically when the tool is first invoked |
| 50 | bool OutputHelper::handleBeginSource(clang::CompilerInstance& compiler) { |
| 51 | const clang::FrontendOptions& frontend_options = compiler.getFrontendOpts(); |
| 52 | |
| 53 | // Validate our expectations about how this tool should be used |
| 54 | assert((frontend_options.Inputs.size() == 1) && |
| 55 | "run_tool.py should invoke the rewriter one file at a time"); |
| 56 | const clang::FrontendInputFile& input_file = frontend_options.Inputs[0]; |
| 57 | assert(input_file.isFile() && |
| 58 | "run_tool.py should invoke the rewriter on actual files"); |
| 59 | |
| 60 | current_language_ = input_file.getKind().getLanguage(); |
| 61 | |
| 62 | // Report that we succeeded |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | // This is run automatically at the end of the file. |
| 67 | void OutputHelper::handleEndSource() { |
| 68 | for (auto& file : files_replaced_in_) { |
| 69 | for (auto& header : headers_to_add_) { |
| 70 | llvm::outs() << "include-user-header:::" << file.getKey() |
| 71 | << ":::-1:::-1:::" << header.getKey() << "\n"; |
| 72 | } |
| 73 | } |
| 74 | } |