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