Commit.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Commit.h - A unit of edits -------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_EDIT_COMMIT_H
  14. #define LLVM_CLANG_EDIT_COMMIT_H
  15. #include "clang/Basic/LLVM.h"
  16. #include "clang/Basic/SourceLocation.h"
  17. #include "clang/Edit/FileOffset.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/Support/Allocator.h"
  21. namespace clang {
  22. class LangOptions;
  23. class PPConditionalDirectiveRecord;
  24. class SourceManager;
  25. namespace edit {
  26. class EditedSource;
  27. class Commit {
  28. public:
  29. enum EditKind {
  30. Act_Insert,
  31. Act_InsertFromRange,
  32. Act_Remove
  33. };
  34. struct Edit {
  35. EditKind Kind;
  36. StringRef Text;
  37. SourceLocation OrigLoc;
  38. FileOffset Offset;
  39. FileOffset InsertFromRangeOffs;
  40. unsigned Length;
  41. bool BeforePrev;
  42. SourceLocation getFileLocation(SourceManager &SM) const;
  43. CharSourceRange getFileRange(SourceManager &SM) const;
  44. CharSourceRange getInsertFromRange(SourceManager &SM) const;
  45. };
  46. private:
  47. const SourceManager &SourceMgr;
  48. const LangOptions &LangOpts;
  49. const PPConditionalDirectiveRecord *PPRec;
  50. EditedSource *Editor = nullptr;
  51. bool IsCommitable = true;
  52. SmallVector<Edit, 8> CachedEdits;
  53. llvm::BumpPtrAllocator StrAlloc;
  54. public:
  55. explicit Commit(EditedSource &Editor);
  56. Commit(const SourceManager &SM, const LangOptions &LangOpts,
  57. const PPConditionalDirectiveRecord *PPRec = nullptr)
  58. : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec) {}
  59. bool isCommitable() const { return IsCommitable; }
  60. bool insert(SourceLocation loc, StringRef text, bool afterToken = false,
  61. bool beforePreviousInsertions = false);
  62. bool insertAfterToken(SourceLocation loc, StringRef text,
  63. bool beforePreviousInsertions = false) {
  64. return insert(loc, text, /*afterToken=*/true, beforePreviousInsertions);
  65. }
  66. bool insertBefore(SourceLocation loc, StringRef text) {
  67. return insert(loc, text, /*afterToken=*/false,
  68. /*beforePreviousInsertions=*/true);
  69. }
  70. bool insertFromRange(SourceLocation loc, CharSourceRange range,
  71. bool afterToken = false,
  72. bool beforePreviousInsertions = false);
  73. bool insertWrap(StringRef before, CharSourceRange range, StringRef after);
  74. bool remove(CharSourceRange range);
  75. bool replace(CharSourceRange range, StringRef text);
  76. bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange);
  77. bool replaceText(SourceLocation loc, StringRef text,
  78. StringRef replacementText);
  79. bool insertFromRange(SourceLocation loc, SourceRange TokenRange,
  80. bool afterToken = false,
  81. bool beforePreviousInsertions = false) {
  82. return insertFromRange(loc, CharSourceRange::getTokenRange(TokenRange),
  83. afterToken, beforePreviousInsertions);
  84. }
  85. bool insertWrap(StringRef before, SourceRange TokenRange, StringRef after) {
  86. return insertWrap(before, CharSourceRange::getTokenRange(TokenRange), after);
  87. }
  88. bool remove(SourceRange TokenRange) {
  89. return remove(CharSourceRange::getTokenRange(TokenRange));
  90. }
  91. bool replace(SourceRange TokenRange, StringRef text) {
  92. return replace(CharSourceRange::getTokenRange(TokenRange), text);
  93. }
  94. bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange) {
  95. return replaceWithInner(CharSourceRange::getTokenRange(TokenRange),
  96. CharSourceRange::getTokenRange(TokenInnerRange));
  97. }
  98. using edit_iterator = SmallVectorImpl<Edit>::const_iterator;
  99. edit_iterator edit_begin() const { return CachedEdits.begin(); }
  100. edit_iterator edit_end() const { return CachedEdits.end(); }
  101. private:
  102. void addInsert(SourceLocation OrigLoc,
  103. FileOffset Offs, StringRef text, bool beforePreviousInsertions);
  104. void addInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
  105. FileOffset RangeOffs, unsigned RangeLen,
  106. bool beforePreviousInsertions);
  107. void addRemove(SourceLocation OrigLoc, FileOffset Offs, unsigned Len);
  108. bool canInsert(SourceLocation loc, FileOffset &Offset);
  109. bool canInsertAfterToken(SourceLocation loc, FileOffset &Offset,
  110. SourceLocation &AfterLoc);
  111. bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
  112. bool canRemoveRange(CharSourceRange range, FileOffset &Offs, unsigned &Len);
  113. bool canReplaceText(SourceLocation loc, StringRef text,
  114. FileOffset &Offs, unsigned &Len);
  115. void commitInsert(FileOffset offset, StringRef text,
  116. bool beforePreviousInsertions);
  117. void commitRemove(FileOffset offset, unsigned length);
  118. bool isAtStartOfMacroExpansion(SourceLocation loc,
  119. SourceLocation *MacroBegin = nullptr) const;
  120. bool isAtEndOfMacroExpansion(SourceLocation loc,
  121. SourceLocation *MacroEnd = nullptr) const;
  122. };
  123. } // namespace edit
  124. } // namespace clang
  125. #endif // LLVM_CLANG_EDIT_COMMIT_H
  126. #ifdef __GNUC__
  127. #pragma GCC diagnostic pop
  128. #endif