1 //===- Rewrite.cpp --------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang-c/Rewrite.h"
10 #include "CXSourceLocation.h"
11 #include "CXTranslationUnit.h"
12 #include "clang/Basic/SourceManager.h"
13 #include "clang/Frontend/ASTUnit.h"
14 #include "clang/Rewrite/Core/Rewriter.h"
16 CXRewriter
clang_CXRewriter_create(CXTranslationUnit TU
) {
17 if (clang::cxtu::isNotUsableTU(TU
)) {
21 clang::ASTUnit
*AU
= clang::cxtu::getASTUnit(TU
);
23 return reinterpret_cast<CXRewriter
>(
24 new clang::Rewriter(AU
->getSourceManager(), AU
->getLangOpts()));
27 void clang_CXRewriter_insertTextBefore(CXRewriter Rew
, CXSourceLocation Loc
,
30 clang::Rewriter
&R
= *reinterpret_cast<clang::Rewriter
*>(Rew
);
31 R
.InsertTextBefore(clang::cxloc::translateSourceLocation(Loc
), Insert
);
34 void clang_CXRewriter_replaceText(CXRewriter Rew
, CXSourceRange ToBeReplaced
,
35 const char *Replacement
) {
37 clang::Rewriter
&R
= *reinterpret_cast<clang::Rewriter
*>(Rew
);
38 R
.ReplaceText(clang::cxloc::translateCXRangeToCharRange(ToBeReplaced
),
42 void clang_CXRewriter_removeText(CXRewriter Rew
, CXSourceRange ToBeRemoved
) {
44 clang::Rewriter
&R
= *reinterpret_cast<clang::Rewriter
*>(Rew
);
45 R
.RemoveText(clang::cxloc::translateCXRangeToCharRange(ToBeRemoved
));
48 int clang_CXRewriter_overwriteChangedFiles(CXRewriter Rew
) {
50 clang::Rewriter
&R
= *reinterpret_cast<clang::Rewriter
*>(Rew
);
51 return R
.overwriteChangedFiles();
54 void clang_CXRewriter_writeMainFileToStdOut(CXRewriter Rew
) {
56 clang::Rewriter
&R
= *reinterpret_cast<clang::Rewriter
*>(Rew
);
57 R
.getEditBuffer(R
.getSourceMgr().getMainFileID()).write(llvm::outs());
60 void clang_CXRewriter_dispose(CXRewriter Rew
) {
62 delete reinterpret_cast<clang::Rewriter
*>(Rew
);