1 //===- EditedSource.h - Collection of source edits --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_EDIT_EDITEDSOURCE_H
11 #define LLVM_CLANG_EDIT_EDITEDSOURCE_H
13 #include "clang/Basic/IdentifierTable.h"
14 #include "clang/Basic/LLVM.h"
15 #include "clang/Basic/SourceLocation.h"
16 #include "clang/Edit/FileOffset.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/Allocator.h"
28 class PPConditionalDirectiveRecord;
37 const SourceManager &SourceMgr;
38 const LangOptions &LangOpts;
39 const PPConditionalDirectiveRecord *PPRec;
43 unsigned RemoveLen = 0;
48 using FileEditsTy = std::map<FileOffset, FileEdit>;
50 FileEditsTy FileEdits;
53 IdentifierInfo *Identifier;
54 SourceLocation ImmediateExpansionLoc;
56 // Location of argument use inside the top-level macro
57 SourceLocation UseLoc;
59 bool operator==(const MacroArgUse &Other) const {
60 return std::tie(Identifier, ImmediateExpansionLoc, UseLoc) ==
61 std::tie(Other.Identifier, Other.ImmediateExpansionLoc,
66 llvm::DenseMap<unsigned, SmallVector<MacroArgUse, 2>> ExpansionToArgMap;
67 SmallVector<std::pair<SourceLocation, MacroArgUse>, 2>
68 CurrCommitMacroArgExps;
70 IdentifierTable IdentTable;
71 llvm::BumpPtrAllocator StrAlloc;
74 EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
75 const PPConditionalDirectiveRecord *PPRec = nullptr)
76 : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), IdentTable(LangOpts) {}
78 const SourceManager &getSourceManager() const { return SourceMgr; }
79 const LangOptions &getLangOpts() const { return LangOpts; }
81 const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const {
85 bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
87 bool commit(const Commit &commit);
89 void applyRewrites(EditsReceiver &receiver, bool adjustRemovals = true);
92 StringRef copyString(StringRef str) { return str.copy(StrAlloc); }
93 StringRef copyString(const Twine &twine);
96 bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
97 bool beforePreviousInsertions);
98 bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
99 FileOffset InsertFromRangeOffs, unsigned Len,
100 bool beforePreviousInsertions);
101 void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
103 StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
105 FileEditsTy::iterator getActionForOffset(FileOffset Offs);
106 void deconstructMacroArgLoc(SourceLocation Loc,
107 SourceLocation &ExpansionLoc,
108 MacroArgUse &ArgUse);
110 void startingCommit();
111 void finishedCommit();
118 #endif // LLVM_CLANG_EDIT_EDITEDSOURCE_H