Path: blob/main/contrib/llvm-project/clang/lib/Tooling/Refactoring.cpp
35233 views
//===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Implements tools to support refactorings.9//10//===----------------------------------------------------------------------===//1112#include "clang/Tooling/Refactoring.h"13#include "clang/Basic/DiagnosticOptions.h"14#include "clang/Basic/FileManager.h"15#include "clang/Basic/SourceManager.h"16#include "clang/Format/Format.h"17#include "clang/Frontend/TextDiagnosticPrinter.h"18#include "clang/Lex/Lexer.h"19#include "clang/Rewrite/Core/Rewriter.h"20#include "llvm/Support/Path.h"21#include "llvm/Support/raw_os_ostream.h"2223namespace clang {24namespace tooling {2526RefactoringTool::RefactoringTool(27const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,28std::shared_ptr<PCHContainerOperations> PCHContainerOps)29: ClangTool(Compilations, SourcePaths, std::move(PCHContainerOps)) {}3031std::map<std::string, Replacements> &RefactoringTool::getReplacements() {32return FileToReplaces;33}3435int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {36if (int Result = run(ActionFactory)) {37return Result;38}3940LangOptions DefaultLangOptions;41IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();42TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);43DiagnosticsEngine Diagnostics(44IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),45&*DiagOpts, &DiagnosticPrinter, false);46SourceManager Sources(Diagnostics, getFiles());47Rewriter Rewrite(Sources, DefaultLangOptions);4849if (!applyAllReplacements(Rewrite)) {50llvm::errs() << "Skipped some replacements.\n";51}5253return saveRewrittenFiles(Rewrite);54}5556bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {57bool Result = true;58for (const auto &Entry : groupReplacementsByFile(59Rewrite.getSourceMgr().getFileManager(), FileToReplaces))60Result = tooling::applyAllReplacements(Entry.second, Rewrite) && Result;61return Result;62}6364int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {65return Rewrite.overwriteChangedFiles() ? 1 : 0;66}6768bool formatAndApplyAllReplacements(69const std::map<std::string, Replacements> &FileToReplaces,70Rewriter &Rewrite, StringRef Style) {71SourceManager &SM = Rewrite.getSourceMgr();72FileManager &Files = SM.getFileManager();7374bool Result = true;75for (const auto &FileAndReplaces : groupReplacementsByFile(76Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) {77const std::string &FilePath = FileAndReplaces.first;78auto &CurReplaces = FileAndReplaces.second;7980FileEntryRef Entry = llvm::cantFail(Files.getFileRef(FilePath));81FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);82StringRef Code = SM.getBufferData(ID);8384auto CurStyle = format::getStyle(Style, FilePath, "LLVM");85if (!CurStyle) {86llvm::errs() << llvm::toString(CurStyle.takeError()) << "\n";87return false;88}8990auto NewReplacements =91format::formatReplacements(Code, CurReplaces, *CurStyle);92if (!NewReplacements) {93llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n";94return false;95}96Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;97}98return Result;99}100101} // end namespace tooling102} // end namespace clang103104105