clang  7.0.0svn
Commit.h
Go to the documentation of this file.
1 //===- Commit.h - A unit of 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_COMMIT_H
11 #define LLVM_CLANG_EDIT_COMMIT_H
12 
13 #include "clang/Basic/LLVM.h"
15 #include "clang/Edit/FileOffset.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Allocator.h"
19 
20 namespace clang {
21 
22 class LangOptions;
23 class PPConditionalDirectiveRecord;
24 class SourceManager;
25 
26 namespace edit {
27 
28 class EditedSource;
29 
30 class Commit {
31 public:
32  enum EditKind {
36  };
37 
38  struct Edit {
40  StringRef Text;
44  unsigned Length;
45  bool BeforePrev;
46 
50  };
51 
52 private:
53  const SourceManager &SourceMgr;
54  const LangOptions &LangOpts;
55  const PPConditionalDirectiveRecord *PPRec;
56  EditedSource *Editor = nullptr;
57 
58  bool IsCommitable = true;
59  SmallVector<Edit, 8> CachedEdits;
60 
61  llvm::BumpPtrAllocator StrAlloc;
62 
63 public:
64  explicit Commit(EditedSource &Editor);
65  Commit(const SourceManager &SM, const LangOptions &LangOpts,
66  const PPConditionalDirectiveRecord *PPRec = nullptr)
67  : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec) {}
68 
69  bool isCommitable() const { return IsCommitable; }
70 
71  bool insert(SourceLocation loc, StringRef text, bool afterToken = false,
72  bool beforePreviousInsertions = false);
73 
74  bool insertAfterToken(SourceLocation loc, StringRef text,
75  bool beforePreviousInsertions = false) {
76  return insert(loc, text, /*afterToken=*/true, beforePreviousInsertions);
77  }
78 
79  bool insertBefore(SourceLocation loc, StringRef text) {
80  return insert(loc, text, /*afterToken=*/false,
81  /*beforePreviousInsertions=*/true);
82  }
83 
85  bool afterToken = false,
86  bool beforePreviousInsertions = false);
87  bool insertWrap(StringRef before, CharSourceRange range, StringRef after);
88 
89  bool remove(CharSourceRange range);
90 
91  bool replace(CharSourceRange range, StringRef text);
92  bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange);
93  bool replaceText(SourceLocation loc, StringRef text,
94  StringRef replacementText);
95 
97  bool afterToken = false,
98  bool beforePreviousInsertions = false) {
99  return insertFromRange(loc, CharSourceRange::getTokenRange(TokenRange),
100  afterToken, beforePreviousInsertions);
101  }
102 
103  bool insertWrap(StringRef before, SourceRange TokenRange, StringRef after) {
104  return insertWrap(before, CharSourceRange::getTokenRange(TokenRange), after);
105  }
106 
107  bool remove(SourceRange TokenRange) {
108  return remove(CharSourceRange::getTokenRange(TokenRange));
109  }
110 
111  bool replace(SourceRange TokenRange, StringRef text) {
112  return replace(CharSourceRange::getTokenRange(TokenRange), text);
113  }
114 
115  bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange) {
117  CharSourceRange::getTokenRange(TokenInnerRange));
118  }
119 
121 
122  edit_iterator edit_begin() const { return CachedEdits.begin(); }
123  edit_iterator edit_end() const { return CachedEdits.end(); }
124 
125 private:
126  void addInsert(SourceLocation OrigLoc,
127  FileOffset Offs, StringRef text, bool beforePreviousInsertions);
128  void addInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
129  FileOffset RangeOffs, unsigned RangeLen,
130  bool beforePreviousInsertions);
131  void addRemove(SourceLocation OrigLoc, FileOffset Offs, unsigned Len);
132 
133  bool canInsert(SourceLocation loc, FileOffset &Offset);
134  bool canInsertAfterToken(SourceLocation loc, FileOffset &Offset,
135  SourceLocation &AfterLoc);
136  bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
137  bool canRemoveRange(CharSourceRange range, FileOffset &Offs, unsigned &Len);
138  bool canReplaceText(SourceLocation loc, StringRef text,
139  FileOffset &Offs, unsigned &Len);
140 
141  void commitInsert(FileOffset offset, StringRef text,
142  bool beforePreviousInsertions);
143  void commitRemove(FileOffset offset, unsigned length);
144 
145  bool isAtStartOfMacroExpansion(SourceLocation loc,
146  SourceLocation *MacroBegin = nullptr) const;
147  bool isAtEndOfMacroExpansion(SourceLocation loc,
148  SourceLocation *MacroEnd = nullptr) const;
149 };
150 
151 } // namespace edit
152 
153 } // namespace clang
154 
155 #endif // LLVM_CLANG_EDIT_COMMIT_H
bool insertWrap(StringRef before, CharSourceRange range, StringRef after)
Definition: Commit.cpp:104
static CharSourceRange getTokenRange(SourceRange R)
SmallVectorImpl< Edit >::const_iterator edit_iterator
Definition: Commit.h:120
bool insertAfterToken(SourceLocation loc, StringRef text, bool beforePreviousInsertions=false)
Definition: Commit.h:74
bool replace(SourceRange TokenRange, StringRef text)
Definition: Commit.h:111
bool insertFromRange(SourceLocation loc, CharSourceRange range, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.cpp:65
bool insert(SourceLocation loc, StringRef text, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.cpp:49
edit_iterator edit_end() const
Definition: Commit.h:123
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
Commit(const SourceManager &SM, const LangOptions &LangOpts, const PPConditionalDirectiveRecord *PPRec=nullptr)
Definition: Commit.h:65
bool replace(CharSourceRange range, StringRef text)
Definition: Commit.cpp:117
uint32_t Offset
Definition: CacheTokens.cpp:43
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
SourceLocation OrigLoc
Definition: Commit.h:41
CharSourceRange getFileRange(SourceManager &SM) const
Definition: Commit.cpp:32
Represents a character-granular source range.
edit_iterator edit_begin() const
Definition: Commit.h:122
FileOffset InsertFromRangeOffs
Definition: Commit.h:43
float __ovld __cnfn length(float p)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)
const SourceManager & SM
Definition: Format.cpp:1468
Encodes a location in the source.
Records preprocessor conditional directive regions and allows querying in which region source locatio...
bool insertWrap(StringRef before, SourceRange TokenRange, StringRef after)
Definition: Commit.h:103
Dataflow Directional Tag Classes.
bool isCommitable() const
Definition: Commit.h:69
CharSourceRange getInsertFromRange(SourceManager &SM) const
Definition: Commit.cpp:37
bool replaceText(SourceLocation loc, StringRef text, StringRef replacementText)
Definition: Commit.cpp:166
bool insertFromRange(SourceLocation loc, SourceRange TokenRange, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.h:96
Commit(EditedSource &Editor)
Definition: Commit.cpp:44
Defines the clang::SourceLocation class and associated facilities.
bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange)
Definition: Commit.h:115
bool insertBefore(SourceLocation loc, StringRef text)
Definition: Commit.h:79
SourceLocation getFileLocation(SourceManager &SM) const
Definition: Commit.cpp:25
bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange)
Definition: Commit.cpp:133
A trivial tuple used to represent a source range.
This class handles loading and caching of source files into memory.