EditedSource.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- EditedSource.h - Collection of source 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_EDITEDSOURCE_H
  14. #define LLVM_CLANG_EDIT_EDITEDSOURCE_H
  15. #include "clang/Basic/IdentifierTable.h"
  16. #include "clang/Basic/LLVM.h"
  17. #include "clang/Basic/SourceLocation.h"
  18. #include "clang/Edit/FileOffset.h"
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/Support/Allocator.h"
  23. #include <map>
  24. #include <tuple>
  25. #include <utility>
  26. namespace clang {
  27. class LangOptions;
  28. class PPConditionalDirectiveRecord;
  29. class SourceManager;
  30. namespace edit {
  31. class Commit;
  32. class EditsReceiver;
  33. class EditedSource {
  34. const SourceManager &SourceMgr;
  35. const LangOptions &LangOpts;
  36. const PPConditionalDirectiveRecord *PPRec;
  37. struct FileEdit {
  38. StringRef Text;
  39. unsigned RemoveLen = 0;
  40. FileEdit() = default;
  41. };
  42. using FileEditsTy = std::map<FileOffset, FileEdit>;
  43. FileEditsTy FileEdits;
  44. struct MacroArgUse {
  45. IdentifierInfo *Identifier;
  46. SourceLocation ImmediateExpansionLoc;
  47. // Location of argument use inside the top-level macro
  48. SourceLocation UseLoc;
  49. bool operator==(const MacroArgUse &Other) const {
  50. return std::tie(Identifier, ImmediateExpansionLoc, UseLoc) ==
  51. std::tie(Other.Identifier, Other.ImmediateExpansionLoc,
  52. Other.UseLoc);
  53. }
  54. };
  55. llvm::DenseMap<SourceLocation, SmallVector<MacroArgUse, 2>> ExpansionToArgMap;
  56. SmallVector<std::pair<SourceLocation, MacroArgUse>, 2>
  57. CurrCommitMacroArgExps;
  58. IdentifierTable IdentTable;
  59. llvm::BumpPtrAllocator StrAlloc;
  60. public:
  61. EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
  62. const PPConditionalDirectiveRecord *PPRec = nullptr)
  63. : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), IdentTable(LangOpts) {}
  64. const SourceManager &getSourceManager() const { return SourceMgr; }
  65. const LangOptions &getLangOpts() const { return LangOpts; }
  66. const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const {
  67. return PPRec;
  68. }
  69. bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
  70. bool commit(const Commit &commit);
  71. void applyRewrites(EditsReceiver &receiver, bool adjustRemovals = true);
  72. void clearRewrites();
  73. StringRef copyString(StringRef str) { return str.copy(StrAlloc); }
  74. StringRef copyString(const Twine &twine);
  75. private:
  76. bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
  77. bool beforePreviousInsertions);
  78. bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
  79. FileOffset InsertFromRangeOffs, unsigned Len,
  80. bool beforePreviousInsertions);
  81. void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
  82. StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
  83. bool &Invalid);
  84. FileEditsTy::iterator getActionForOffset(FileOffset Offs);
  85. void deconstructMacroArgLoc(SourceLocation Loc,
  86. SourceLocation &ExpansionLoc,
  87. MacroArgUse &ArgUse);
  88. void startingCommit();
  89. void finishedCommit();
  90. };
  91. } // namespace edit
  92. } // namespace clang
  93. #endif // LLVM_CLANG_EDIT_EDITEDSOURCE_H
  94. #ifdef __GNUC__
  95. #pragma GCC diagnostic pop
  96. #endif