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