clang  14.0.0git
EditedSource.h
Go to the documentation of this file.
1 //===- EditedSource.h - Collection of source edits --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_EDIT_EDITEDSOURCE_H
10 #define LLVM_CLANG_EDIT_EDITEDSOURCE_H
11 
13 #include "clang/Basic/LLVM.h"
15 #include "clang/Edit/FileOffset.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Allocator.h"
20 #include <map>
21 #include <tuple>
22 #include <utility>
23 
24 namespace clang {
25 
26 class LangOptions;
27 class PPConditionalDirectiveRecord;
28 class SourceManager;
29 
30 namespace edit {
31 
32 class Commit;
33 class EditsReceiver;
34 
35 class EditedSource {
36  const SourceManager &SourceMgr;
37  const LangOptions &LangOpts;
38  const PPConditionalDirectiveRecord *PPRec;
39 
40  struct FileEdit {
41  StringRef Text;
42  unsigned RemoveLen = 0;
43 
44  FileEdit() = default;
45  };
46 
47  using FileEditsTy = std::map<FileOffset, FileEdit>;
48 
49  FileEditsTy FileEdits;
50 
51  struct MacroArgUse {
53  SourceLocation ImmediateExpansionLoc;
54 
55  // Location of argument use inside the top-level macro
56  SourceLocation UseLoc;
57 
58  bool operator==(const MacroArgUse &Other) const {
59  return std::tie(Identifier, ImmediateExpansionLoc, UseLoc) ==
60  std::tie(Other.Identifier, Other.ImmediateExpansionLoc,
61  Other.UseLoc);
62  }
63  };
64 
65  llvm::DenseMap<SourceLocation, SmallVector<MacroArgUse, 2>> ExpansionToArgMap;
67  CurrCommitMacroArgExps;
68 
69  IdentifierTable IdentTable;
70  llvm::BumpPtrAllocator StrAlloc;
71 
72 public:
73  EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
74  const PPConditionalDirectiveRecord *PPRec = nullptr)
75  : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), IdentTable(LangOpts) {}
76 
77  const SourceManager &getSourceManager() const { return SourceMgr; }
78  const LangOptions &getLangOpts() const { return LangOpts; }
79 
81  return PPRec;
82  }
83 
84  bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
85 
86  bool commit(const Commit &commit);
87 
88  void applyRewrites(EditsReceiver &receiver, bool adjustRemovals = true);
89  void clearRewrites();
90 
91  StringRef copyString(StringRef str) { return str.copy(StrAlloc); }
92  StringRef copyString(const Twine &twine);
93 
94 private:
95  bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
96  bool beforePreviousInsertions);
97  bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
98  FileOffset InsertFromRangeOffs, unsigned Len,
99  bool beforePreviousInsertions);
100  void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
101 
102  StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
103  bool &Invalid);
104  FileEditsTy::iterator getActionForOffset(FileOffset Offs);
105  void deconstructMacroArgLoc(SourceLocation Loc,
106  SourceLocation &ExpansionLoc,
107  MacroArgUse &ArgUse);
108 
109  void startingCommit();
110  void finishedCommit();
111 };
112 
113 } // namespace edit
114 
115 } // namespace clang
116 
117 #endif // LLVM_CLANG_EDIT_EDITEDSOURCE_H
clang::comments::tok::text
@ text
Definition: CommentLexer.h:35
clang::edit::EditedSource::canInsertInOffset
bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs)
Definition: EditedSource.cpp:74
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::edit::EditedSource::getLangOpts
const LangOptions & getLangOpts() const
Definition: EditedSource.h:78
FileOffset.h
Identifier
StringRef Identifier
Definition: Format.cpp:2341
clang::edit::EditedSource::applyRewrites
void applyRewrites(EditsReceiver &receiver, bool adjustRemovals=true)
Definition: EditedSource.cpp:409
clang::edit::EditsReceiver
Definition: EditsReceiver.h:19
clang::transformer::edit
EditGenerator edit(ASTEdit E)
Generates a single (specified) edit.
Definition: RewriteRule.cpp:75
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::edit::EditedSource
Definition: EditedSource.h:35
IdentifierTable.h
clang::edit::EditedSource::copyString
StringRef copyString(StringRef str)
Definition: EditedSource.h:91
SourceLocation.h
clang::PPConditionalDirectiveRecord
Records preprocessor conditional directive regions and allows querying in which region source locatio...
Definition: PPConditionalDirectiveRecord.h:25
clang::edit::EditedSource::clearRewrites
void clearRewrites()
Definition: EditedSource.cpp:449
clang::edit::Commit
Definition: Commit.h:29
clang::edit::EditedSource::getPPCondDirectiveRecord
const PPConditionalDirectiveRecord * getPPCondDirectiveRecord() const
Definition: EditedSource.h:80
LLVM.h
clang::edit::EditedSource::commit
bool commit(const Commit &commit)
Definition: EditedSource.cpp:276
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:84
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:58
clang
Definition: CalledOnceCheck.h:17
Text
StringRef Text
Definition: Format.cpp:2334
clang::IdentifierTable
Implements an efficient mapping from strings to IdentifierInfo nodes.
Definition: IdentifierTable.h:559
clang::edit::FileOffset
Definition: FileOffset.h:18
SM
#define SM(sm)
Definition: Cuda.cpp:78
clang::edit::EditedSource::getSourceManager
const SourceManager & getSourceManager() const
Definition: EditedSource.h:77
clang::edit::EditedSource::EditedSource
EditedSource(const SourceManager &SM, const LangOptions &LangOpts, const PPConditionalDirectiveRecord *PPRec=nullptr)
Definition: EditedSource.h:73
clang::operator==
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition: CallGraph.h:207