1 //===----- EditedSource.h - Collection of source edits ----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_CLANG_EDIT_EDITEDSOURCE_H 11 #define LLVM_CLANG_EDIT_EDITEDSOURCE_H 12 13 #include "clang/Basic/IdentifierTable.h" 14 #include "clang/Edit/FileOffset.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/StringRef.h" 17 #include "llvm/ADT/TinyPtrVector.h" 18 #include "llvm/Support/Allocator.h" 19 #include <map> 20 21 namespace clang { 22 class LangOptions; 23 class PPConditionalDirectiveRecord; 24 25 namespace edit { 26 class Commit; 27 class EditsReceiver; 28 29 class EditedSource { 30 const SourceManager &SourceMgr; 31 const LangOptions &LangOpts; 32 const PPConditionalDirectiveRecord *PPRec; 33 34 struct FileEdit { 35 StringRef Text; 36 unsigned RemoveLen; 37 FileEditFileEdit38 FileEdit() : RemoveLen(0) {} 39 }; 40 41 typedef std::map<FileOffset, FileEdit> FileEditsTy; 42 FileEditsTy FileEdits; 43 44 llvm::DenseMap<unsigned, llvm::TinyPtrVector<IdentifierInfo*>> 45 ExpansionToArgMap; 46 SmallVector<std::pair<SourceLocation, IdentifierInfo*>, 2> 47 CurrCommitMacroArgExps; 48 49 IdentifierTable IdentTable; 50 llvm::BumpPtrAllocator StrAlloc; 51 52 public: 53 EditedSource(const SourceManager &SM, const LangOptions &LangOpts, 54 const PPConditionalDirectiveRecord *PPRec = nullptr) SourceMgr(SM)55 : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), IdentTable(LangOpts), 56 StrAlloc() { } 57 getSourceManager()58 const SourceManager &getSourceManager() const { return SourceMgr; } getLangOpts()59 const LangOptions &getLangOpts() const { return LangOpts; } getPPCondDirectiveRecord()60 const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const { 61 return PPRec; 62 } 63 64 bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs); 65 66 bool commit(const Commit &commit); 67 68 void applyRewrites(EditsReceiver &receiver); 69 void clearRewrites(); 70 copyString(StringRef str)71 StringRef copyString(StringRef str) { return str.copy(StrAlloc); } 72 StringRef copyString(const Twine &twine); 73 74 private: 75 bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text, 76 bool beforePreviousInsertions); 77 bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs, 78 FileOffset InsertFromRangeOffs, unsigned Len, 79 bool beforePreviousInsertions); 80 void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len); 81 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 IdentifierInfo *&II); 88 89 void startingCommit(); 90 void finishedCommit(); 91 }; 92 93 } 94 95 } // end namespace clang 96 97 #endif 98