1 | //===--- InsertionPoint.h - Where should we add new code? --------*- 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_TOOLS_EXTRA_CLANGD_REFACTOR_INSERTIONPOINT_H |
10 | #define |
11 | |
12 | #include "clang/AST/DeclCXX.h" |
13 | #include "clang/Basic/Specifiers.h" |
14 | #include "clang/Tooling/Core/Replacement.h" |
15 | |
16 | namespace clang { |
17 | namespace clangd { |
18 | |
19 | // An anchor describes where to insert code into a decl sequence. |
20 | // |
21 | // It allows inserting above or below a block of decls matching some criterion. |
22 | // For example, "insert after existing constructors". |
23 | struct Anchor { |
24 | // A predicate describing which decls are considered part of a block. |
25 | // Match need not handle TemplateDecls, which are unwrapped before matching. |
26 | std::function<bool(const Decl *)> Match; |
27 | // Whether the insertion point should be before or after the matching block. |
28 | enum Dir { Above, Below } Direction = Below; |
29 | }; |
30 | |
31 | // Returns the point to insert a declaration according to Anchors. |
32 | // Anchors are tried in order. For each, the first matching location is chosen. |
33 | SourceLocation insertionPoint(const DeclContext &Ctx, |
34 | llvm::ArrayRef<Anchor> Anchors); |
35 | |
36 | // Returns an edit inserting Code inside Ctx. |
37 | // Location is chosen according to Anchors, falling back to the end of Ctx. |
38 | // Fails if the chosen insertion point is in a different file than Ctx itself. |
39 | llvm::Expected<tooling::Replacement> insertDecl(llvm::StringRef Code, |
40 | const DeclContext &Ctx, |
41 | llvm::ArrayRef<Anchor> Anchors); |
42 | |
43 | // Variant for C++ classes that ensures the right access control. |
44 | SourceLocation insertionPoint(const CXXRecordDecl &InClass, |
45 | std::vector<Anchor> Anchors, |
46 | AccessSpecifier Protection); |
47 | |
48 | // Variant for C++ classes that ensures the right access control. |
49 | // May insert a new access specifier if needed. |
50 | llvm::Expected<tooling::Replacement> insertDecl(llvm::StringRef Code, |
51 | const CXXRecordDecl &InClass, |
52 | std::vector<Anchor> Anchors, |
53 | AccessSpecifier Protection); |
54 | |
55 | } // namespace clangd |
56 | } // namespace clang |
57 | |
58 | #endif |
59 | |