Rewrite.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //===- Rewrite.cpp --------------------------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang-c/Rewrite.h"
  9. #include "CXSourceLocation.h"
  10. #include "CXTranslationUnit.h"
  11. #include "clang/Basic/SourceManager.h"
  12. #include "clang/Frontend/ASTUnit.h"
  13. #include "clang/Rewrite/Core/Rewriter.h"
  14. CXRewriter clang_CXRewriter_create(CXTranslationUnit TU) {
  15. if (clang::cxtu::isNotUsableTU(TU)) {
  16. LOG_BAD_TU(TU);
  17. return {};
  18. }
  19. clang::ASTUnit *AU = clang::cxtu::getASTUnit(TU);
  20. assert(AU);
  21. return reinterpret_cast<CXRewriter>(
  22. new clang::Rewriter(AU->getSourceManager(), AU->getLangOpts()));
  23. }
  24. void clang_CXRewriter_insertTextBefore(CXRewriter Rew, CXSourceLocation Loc,
  25. const char *Insert) {
  26. assert(Rew);
  27. clang::Rewriter &R = *reinterpret_cast<clang::Rewriter *>(Rew);
  28. R.InsertTextBefore(clang::cxloc::translateSourceLocation(Loc), Insert);
  29. }
  30. void clang_CXRewriter_replaceText(CXRewriter Rew, CXSourceRange ToBeReplaced,
  31. const char *Replacement) {
  32. assert(Rew);
  33. clang::Rewriter &R = *reinterpret_cast<clang::Rewriter *>(Rew);
  34. R.ReplaceText(clang::cxloc::translateCXRangeToCharRange(ToBeReplaced),
  35. Replacement);
  36. }
  37. void clang_CXRewriter_removeText(CXRewriter Rew, CXSourceRange ToBeRemoved) {
  38. assert(Rew);
  39. clang::Rewriter &R = *reinterpret_cast<clang::Rewriter *>(Rew);
  40. R.RemoveText(clang::cxloc::translateCXRangeToCharRange(ToBeRemoved));
  41. }
  42. int clang_CXRewriter_overwriteChangedFiles(CXRewriter Rew) {
  43. assert(Rew);
  44. clang::Rewriter &R = *reinterpret_cast<clang::Rewriter *>(Rew);
  45. return R.overwriteChangedFiles();
  46. }
  47. void clang_CXRewriter_writeMainFileToStdOut(CXRewriter Rew) {
  48. assert(Rew);
  49. clang::Rewriter &R = *reinterpret_cast<clang::Rewriter *>(Rew);
  50. R.getEditBuffer(R.getSourceMgr().getMainFileID()).write(llvm::outs());
  51. }
  52. void clang_CXRewriter_dispose(CXRewriter Rew) {
  53. if (Rew)
  54. delete reinterpret_cast<clang::Rewriter *>(Rew);
  55. }