clang 24.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"
16#include "llvm/ADT/ArrayRef.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"
21#include <map>
22#include <tuple>
23#include <utility>
24
25namespace clang {
26
27class LangOptions;
29class SourceManager;
30class FixItHint;
31
32namespace edit {
33
34class Commit;
35class EditsReceiver;
36
38 const SourceManager &SourceMgr;
39 const LangOptions &LangOpts;
41
42 struct FileEdit {
43 StringRef Text;
44 unsigned RemoveLen = 0;
45
46 FileEdit() = default;
47 };
48
49 using FileEditsTy = std::map<FileOffset, FileEdit>;
50
51 FileEditsTy FileEdits;
52
53 struct MacroArgUse {
54 IdentifierInfo *Identifier;
55 SourceLocation ImmediateExpansionLoc;
56
57 // Location of argument use inside the top-level macro
58 SourceLocation UseLoc;
59
60 bool operator==(const MacroArgUse &Other) const {
61 return std::tie(Identifier, ImmediateExpansionLoc, UseLoc) ==
62 std::tie(Other.Identifier, Other.ImmediateExpansionLoc,
63 Other.UseLoc);
64 }
65 };
66
67 llvm::DenseMap<SourceLocation, SmallVector<MacroArgUse, 2>> ExpansionToArgMap;
69 CurrCommitMacroArgExps;
70
71 IdentifierTable IdentTable;
72 llvm::BumpPtrAllocator StrAlloc;
73
74public:
75 EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
76 const PPConditionalDirectiveRecord *PPRec = nullptr)
77 : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), IdentTable(LangOpts) {}
78
79 const SourceManager &getSourceManager() const { return SourceMgr; }
80 const LangOptions &getLangOpts() const { return LangOpts; }
81
83 return PPRec;
84 }
85
87
88 bool commit(const Commit &commit);
89
90 void applyRewrites(EditsReceiver &receiver, bool adjustRemovals = true);
91 void clearRewrites();
92
93 StringRef copyString(StringRef str) { return str.copy(StrAlloc); }
94 StringRef copyString(const Twine &twine);
95
96private:
97 bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
98 bool beforePreviousInsertions);
99 bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
100 FileOffset InsertFromRangeOffs, unsigned Len,
101 bool beforePreviousInsertions);
102 void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
103
104 StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
105 bool &Invalid);
106 FileEditsTy::iterator getActionForOffset(FileOffset Offs);
107 void deconstructMacroArgLoc(SourceLocation Loc,
108 SourceLocation &ExpansionLoc,
109 MacroArgUse &ArgUse);
110
111 void startingCommit();
112 void finishedCommit();
113};
114
115/// Merges \p FixItHints into a normalized set of file edits.
116///
117/// \p MergedFixits is cleared before use. Removals may be adjusted to avoid
118/// changing token boundaries.
119void mergeFixits(ArrayRef<FixItHint> FixItHints, const SourceManager &SM,
120 const LangOptions &LangOpts,
121 SmallVectorImpl<FixItHint> &MergedFixits);
122} // namespace edit
123
124} // namespace clang
125
126#endif // LLVM_CLANG_EDIT_EDITEDSOURCE_H
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::SourceLocation class and associated facilities.
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition Diagnostic.h:81
One of these records is kept for each identifier that is lexed.
Implements an efficient mapping from strings to IdentifierInfo nodes.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Records preprocessor conditional directive regions and allows querying in which region source locatio...
Encodes a location in the source.
This class handles loading and caching of source files into memory.
const LangOptions & getLangOpts() const
StringRef copyString(StringRef str)
const PPConditionalDirectiveRecord * getPPCondDirectiveRecord() const
bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs)
void applyRewrites(EditsReceiver &receiver, bool adjustRemovals=true)
bool commit(const Commit &commit)
EditedSource(const SourceManager &SM, const LangOptions &LangOpts, const PPConditionalDirectiveRecord *PPRec=nullptr)
const SourceManager & getSourceManager() const
void mergeFixits(ArrayRef< FixItHint > FixItHints, const SourceManager &SM, const LangOptions &LangOpts, SmallVectorImpl< FixItHint > &MergedFixits)
Merges FixItHints into a normalized set of file edits.
Top level wrappers for InstallAPI frontend operations.
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:218
@ Other
Other implicit parameter.
Definition Decl.h:1774