Index: clang/include/clang-c/Rewrite.h =================================================================== --- /dev/null +++ clang/include/clang-c/Rewrite.h @@ -0,0 +1,63 @@ +/*===-- clang-c/Rewrite.h - C Rewriter --------------------------*- C -*-===*\ +|* *| +|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| +|* Exceptions. *| +|* See https://llvm.org/LICENSE.txt for license information. *| +|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| +|* *| +|*===----------------------------------------------------------------------===*/ + +#ifndef LLVM_CLANG_C_REWRITE_H +#define LLVM_CLANG_C_REWRITE_H + +#include "clang-c/CXString.h" +#include "clang-c/ExternC.h" +#include "clang-c/Index.h" +#include "clang-c/Platform.h" + +LLVM_CLANG_C_EXTERN_C_BEGIN + +typedef void *Rewriter; + +/** + * Create Rewriter. + */ +CINDEX_LINKAGE Rewriter clang_createRewriter(CXTranslationUnit TU); + +/** + * Insert the specified string at the specified location in the original buffer. + */ +CINDEX_LINKAGE void clang_insertTextBefore(Rewriter Rew, CXSourceLocation Loc, + const char *Insert); + +/** + * Replace the specified range of characters in the input with the specified + * replacement. + */ +CINDEX_LINKAGE void clang_replaceText(Rewriter Rew, CXSourceRange ToBeReplaced, + const char *Replacement); + +/** + * Remove the specified range. + */ +CINDEX_LINKAGE void clang_removeText(Rewriter Rew, CXSourceRange ToBeRemoved); + +/** + * Save all changed files to disk. + * Returns 1 if any files were not saved successfully, returns 0 otherwise. + */ +CINDEX_LINKAGE int clang_overwriteChangedFiles(Rewriter Rew); + +/** + * Write out rewritten version of the main file to stdout. + */ +CINDEX_LINKAGE void clang_writeMainFileToStdOut(Rewriter Rew); + +/** + * Free the given Rewriter. + */ +CINDEX_LINKAGE void clang_disposeRewriter(Rewriter Rew); + +LLVM_CLANG_C_EXTERN_C_END + +#endif Index: clang/tools/libclang/CMakeLists.txt =================================================================== --- clang/tools/libclang/CMakeLists.txt +++ clang/tools/libclang/CMakeLists.txt @@ -20,6 +20,7 @@ CXType.cpp Indexing.cpp FatalErrorHandler.cpp + Rewrite.cpp ADDITIONAL_HEADERS CIndexDiagnostic.h Index: clang/tools/libclang/Rewrite.cpp =================================================================== --- /dev/null +++ clang/tools/libclang/Rewrite.cpp @@ -0,0 +1,63 @@ +//===- CXString.cpp - Routines for manipulating CXStrings -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "clang-c/Rewrite.h" +#include "CXSourceLocation.h" +#include "CXTranslationUnit.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Frontend/ASTUnit.h" +#include "clang/Rewrite/Core/Rewriter.h" + +Rewriter clang_createRewriter(CXTranslationUnit TU) { + if (clang::cxtu::isNotUsableTU(TU)) { + LOG_BAD_TU(TU); + return {}; + } + clang::ASTUnit *AU = clang::cxtu::getASTUnit(TU); + assert(AU); + return reinterpret_cast( + new clang::Rewriter(AU->getSourceManager(), AU->getLangOpts())); +} + +void clang_insertTextBefore(Rewriter Rew, CXSourceLocation Loc, + const char *Insert) { + assert(Rew); + clang::Rewriter &R = *reinterpret_cast(Rew); + R.InsertTextBefore(clang::cxloc::translateSourceLocation(Loc), Insert); +} + +void clang_replaceText(Rewriter Rew, CXSourceRange ToBeReplaced, + const char *Replacement) { + assert(Rew); + clang::Rewriter &R = *reinterpret_cast(Rew); + R.ReplaceText(clang::cxloc::translateCharRangeToCharRange(ToBeReplaced), + Replacement); +} + +void clang_removeText(Rewriter Rew, CXSourceRange ToBeRemoved) { + assert(Rew); + clang::Rewriter &R = *reinterpret_cast(Rew); + R.RemoveText(clang::cxloc::translateCharRangeToCharRange(ToBeRemoved)); +} + +int clang_overwriteChangedFiles(Rewriter Rew) { + assert(Rew); + clang::Rewriter &R = *reinterpret_cast(Rew); + return R.overwriteChangedFiles(); +} + +void clang_writeMainFileToStdOut(Rewriter Rew) { + assert(Rew); + clang::Rewriter &R = *reinterpret_cast(Rew); + R.getEditBuffer(R.getSourceMgr().getMainFileID()).write(llvm::outs()); +} + +void clang_disposeRewriter(Rewriter Rew) { + if (Rew) + delete reinterpret_cast(Rew); +} Index: clang/tools/libclang/libclang.exports =================================================================== --- clang/tools/libclang/libclang.exports +++ clang/tools/libclang/libclang.exports @@ -384,4 +384,11 @@ clang_uninstall_llvm_fatal_error_handler clang_getVarDeclInitializer clang_hasVarDeclGlobalStorage -clang_hasVarDeclExternalStorage \ No newline at end of file +clang_hasVarDeclExternalStorage +clang_createRewriter +clang_insertTextBefore +clang_replaceText +clang_removeText +clang_overwriteChangedFiles +clang_writeMainFileToStdOut +clang_disposeRewriter \ No newline at end of file